feat(authn): authn users page query

This commit is contained in:
JimMoen 2021-10-19 16:50:30 +08:00
parent a399ac20e9
commit 0bd5cd9336
3 changed files with 37 additions and 19 deletions

View File

@ -59,7 +59,7 @@
, delete_user/3 , delete_user/3
, update_user/4 , update_user/4
, lookup_user/3 , lookup_user/3
, list_users/2 , list_users/3
]). ]).
%% gen_server callbacks %% gen_server callbacks
@ -378,10 +378,9 @@ update_user(ChainName, AuthenticatorID, UserID, NewUserInfo) ->
lookup_user(ChainName, AuthenticatorID, UserID) -> lookup_user(ChainName, AuthenticatorID, UserID) ->
call({lookup_user, ChainName, AuthenticatorID, UserID}). call({lookup_user, ChainName, AuthenticatorID, UserID}).
%% TODO: Support pagination -spec list_users(chain_name(), authenticator_id(), map()) -> {ok, [user_info()]} | {error, term()}.
-spec list_users(chain_name(), authenticator_id()) -> {ok, [user_info()]} | {error, term()}. list_users(ChainName, AuthenticatorID, Params) ->
list_users(ChainName, AuthenticatorID) -> call({list_users, ChainName, AuthenticatorID, Params}).
call({list_users, ChainName, AuthenticatorID}).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% gen_server callbacks %% gen_server callbacks
@ -540,8 +539,8 @@ handle_call({lookup_user, ChainName, AuthenticatorID, UserID}, _From, State) ->
Reply = call_authenticator(ChainName, AuthenticatorID, lookup_user, [UserID]), Reply = call_authenticator(ChainName, AuthenticatorID, lookup_user, [UserID]),
reply(Reply, State); reply(Reply, State);
handle_call({list_users, ChainName, AuthenticatorID}, _From, State) -> handle_call({list_users, ChainName, AuthenticatorID, PageParams}, _From, State) ->
Reply = call_authenticator(ChainName, AuthenticatorID, list_users, []), Reply = call_authenticator(ChainName, AuthenticatorID, list_users, [PageParams]),
reply(Reply, State); reply(Reply, State);
handle_call(Req, _From, State) -> handle_call(Req, _From, State) ->

View File

@ -836,6 +836,24 @@ list_users_api_spec() ->
type => string type => string
}, },
required => true required => true
},
#{
name => page,
in => query,
description => <<"Page Index">>,
schema => #{
type => integer
},
required => false
},
#{
name => limit,
in => query,
description => <<"Page limit">>,
schema => #{
type => integer
},
required => false
} }
], ],
responses => #{ responses => #{
@ -1796,8 +1814,8 @@ import_users2(post, #{bindings := #{listener_id := _, id := _}, body := _}) ->
users(post, #{bindings := #{id := AuthenticatorID}, body := UserInfo}) -> users(post, #{bindings := #{id := AuthenticatorID}, body := UserInfo}) ->
add_user(?GLOBAL, AuthenticatorID, UserInfo); add_user(?GLOBAL, AuthenticatorID, UserInfo);
users(get, #{bindings := #{id := AuthenticatorID}}) -> users(get, #{bindings := #{id := AuthenticatorID}, query_string := PageParams}) ->
list_users(?GLOBAL, AuthenticatorID). list_users(?GLOBAL, AuthenticatorID, PageParams).
users2(put, #{bindings := #{id := AuthenticatorID, users2(put, #{bindings := #{id := AuthenticatorID,
user_id := UserID}, body := UserInfo}) -> user_id := UserID}, body := UserInfo}) ->
@ -1811,8 +1829,9 @@ users3(post, #{bindings := #{listener_id := ListenerID,
id := AuthenticatorID}, body := UserInfo}) -> id := AuthenticatorID}, body := UserInfo}) ->
add_user(ListenerID, AuthenticatorID, UserInfo); add_user(ListenerID, AuthenticatorID, UserInfo);
users3(get, #{bindings := #{listener_id := ListenerID, users3(get, #{bindings := #{listener_id := ListenerID,
id := AuthenticatorID}}) -> id := AuthenticatorID},
list_users(ListenerID, AuthenticatorID). query_string := PageParams}) ->
list_users(ListenerID, AuthenticatorID, PageParams).
users4(put, #{bindings := #{listener_id := ListenerID, users4(put, #{bindings := #{listener_id := ListenerID,
id := AuthenticatorID, id := AuthenticatorID,
@ -1946,9 +1965,9 @@ delete_user(ChainName0, AuthenticatorID, UserID) ->
serialize_error(Reason) serialize_error(Reason)
end. end.
list_users(ChainName0, AuthenticatorID) -> list_users(ChainName0, AuthenticatorID, PageParams) ->
ChainName = to_atom(ChainName0), ChainName = to_atom(ChainName0),
case ?AUTHN:list_users(ChainName, AuthenticatorID) of case ?AUTHN:list_users(ChainName, AuthenticatorID, PageParams) of
{ok, Users} -> {ok, Users} ->
{200, Users}; {200, Users};
{error, Reason} -> {error, Reason} ->

View File

@ -39,7 +39,7 @@
, delete_user/2 , delete_user/2
, update_user/3 , update_user/3
, lookup_user/2 , lookup_user/2
, list_users/1 , list_users/2
]). ]).
-type user_id_type() :: clientid | username. -type user_id_type() :: clientid | username.
@ -237,14 +237,14 @@ update_user(UserID, UserInfo,
lookup_user(UserID, #{user_group := UserGroup}) -> lookup_user(UserID, #{user_group := UserGroup}) ->
case mnesia:dirty_read(?TAB, {UserGroup, UserID}) of case mnesia:dirty_read(?TAB, {UserGroup, UserID}) of
[UserInfo] -> [UserInfo] ->
{ok, serialize_user_info(UserInfo)}; {ok, format_user_info(UserInfo)};
[] -> [] ->
{error, not_found} {error, not_found}
end. end.
list_users(#{user_group := UserGroup}) -> list_users(PageParams, #{user_group := UserGroup}) ->
Users = [serialize_user_info(UserInfo) || #user_info{user_id = {UserGroup0, _}} = UserInfo <- ets:tab2list(?TAB), UserGroup0 =:= UserGroup], MatchSpec = [{{user_info, {UserGroup, '_'}, '_', '_', '_'}, [], ['$_']}],
{ok, Users}. {ok, emqx_mgmt_api:paginate(?TAB, MatchSpec, PageParams, fun format_user_info/1)}.
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% Internal functions %% Internal functions
@ -396,5 +396,5 @@ to_binary(B) when is_binary(B) ->
to_binary(L) when is_list(L) -> to_binary(L) when is_list(L) ->
iolist_to_binary(L). iolist_to_binary(L).
serialize_user_info(#user_info{user_id = {_, UserID}, is_superuser = IsSuperuser}) -> format_user_info(#user_info{user_id = {_, UserID}, is_superuser = IsSuperuser}) ->
#{user_id => UserID, is_superuser => IsSuperuser}. #{user_id => UserID, is_superuser => IsSuperuser}.