From 0bd5cd93363b1162aaed0de177ff1aeb239482f6 Mon Sep 17 00:00:00 2001 From: JimMoen Date: Tue, 19 Oct 2021 16:50:30 +0800 Subject: [PATCH] feat(authn): authn users page query --- apps/emqx/src/emqx_authentication.erl | 13 ++++---- apps/emqx_authn/src/emqx_authn_api.erl | 31 +++++++++++++++---- .../src/simple_authn/emqx_authn_mnesia.erl | 12 +++---- 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/apps/emqx/src/emqx_authentication.erl b/apps/emqx/src/emqx_authentication.erl index 793a96cf2..226d697d8 100644 --- a/apps/emqx/src/emqx_authentication.erl +++ b/apps/emqx/src/emqx_authentication.erl @@ -59,7 +59,7 @@ , delete_user/3 , update_user/4 , lookup_user/3 - , list_users/2 + , list_users/3 ]). %% gen_server callbacks @@ -378,10 +378,9 @@ update_user(ChainName, AuthenticatorID, UserID, NewUserInfo) -> lookup_user(ChainName, AuthenticatorID, UserID) -> call({lookup_user, ChainName, AuthenticatorID, UserID}). -%% TODO: Support pagination --spec list_users(chain_name(), authenticator_id()) -> {ok, [user_info()]} | {error, term()}. -list_users(ChainName, AuthenticatorID) -> - call({list_users, ChainName, AuthenticatorID}). +-spec list_users(chain_name(), authenticator_id(), map()) -> {ok, [user_info()]} | {error, term()}. +list_users(ChainName, AuthenticatorID, Params) -> + call({list_users, ChainName, AuthenticatorID, Params}). %%-------------------------------------------------------------------- %% 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(Reply, State); -handle_call({list_users, ChainName, AuthenticatorID}, _From, State) -> - Reply = call_authenticator(ChainName, AuthenticatorID, list_users, []), +handle_call({list_users, ChainName, AuthenticatorID, PageParams}, _From, State) -> + Reply = call_authenticator(ChainName, AuthenticatorID, list_users, [PageParams]), reply(Reply, State); handle_call(Req, _From, State) -> diff --git a/apps/emqx_authn/src/emqx_authn_api.erl b/apps/emqx_authn/src/emqx_authn_api.erl index 49d00e976..faad2fcd8 100644 --- a/apps/emqx_authn/src/emqx_authn_api.erl +++ b/apps/emqx_authn/src/emqx_authn_api.erl @@ -836,6 +836,24 @@ list_users_api_spec() -> type => string }, 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 => #{ @@ -1796,8 +1814,8 @@ import_users2(post, #{bindings := #{listener_id := _, id := _}, body := _}) -> users(post, #{bindings := #{id := AuthenticatorID}, body := UserInfo}) -> add_user(?GLOBAL, AuthenticatorID, UserInfo); -users(get, #{bindings := #{id := AuthenticatorID}}) -> - list_users(?GLOBAL, AuthenticatorID). +users(get, #{bindings := #{id := AuthenticatorID}, query_string := PageParams}) -> + list_users(?GLOBAL, AuthenticatorID, PageParams). users2(put, #{bindings := #{id := AuthenticatorID, user_id := UserID}, body := UserInfo}) -> @@ -1811,8 +1829,9 @@ users3(post, #{bindings := #{listener_id := ListenerID, id := AuthenticatorID}, body := UserInfo}) -> add_user(ListenerID, AuthenticatorID, UserInfo); users3(get, #{bindings := #{listener_id := ListenerID, - id := AuthenticatorID}}) -> - list_users(ListenerID, AuthenticatorID). + id := AuthenticatorID}, + query_string := PageParams}) -> + list_users(ListenerID, AuthenticatorID, PageParams). users4(put, #{bindings := #{listener_id := ListenerID, id := AuthenticatorID, @@ -1946,9 +1965,9 @@ delete_user(ChainName0, AuthenticatorID, UserID) -> serialize_error(Reason) end. -list_users(ChainName0, AuthenticatorID) -> +list_users(ChainName0, AuthenticatorID, PageParams) -> ChainName = to_atom(ChainName0), - case ?AUTHN:list_users(ChainName, AuthenticatorID) of + case ?AUTHN:list_users(ChainName, AuthenticatorID, PageParams) of {ok, Users} -> {200, Users}; {error, Reason} -> diff --git a/apps/emqx_authn/src/simple_authn/emqx_authn_mnesia.erl b/apps/emqx_authn/src/simple_authn/emqx_authn_mnesia.erl index d1fc121ca..05f109b4a 100644 --- a/apps/emqx_authn/src/simple_authn/emqx_authn_mnesia.erl +++ b/apps/emqx_authn/src/simple_authn/emqx_authn_mnesia.erl @@ -39,7 +39,7 @@ , delete_user/2 , update_user/3 , lookup_user/2 - , list_users/1 + , list_users/2 ]). -type user_id_type() :: clientid | username. @@ -237,14 +237,14 @@ update_user(UserID, UserInfo, lookup_user(UserID, #{user_group := UserGroup}) -> case mnesia:dirty_read(?TAB, {UserGroup, UserID}) of [UserInfo] -> - {ok, serialize_user_info(UserInfo)}; + {ok, format_user_info(UserInfo)}; [] -> {error, not_found} end. -list_users(#{user_group := UserGroup}) -> - Users = [serialize_user_info(UserInfo) || #user_info{user_id = {UserGroup0, _}} = UserInfo <- ets:tab2list(?TAB), UserGroup0 =:= UserGroup], - {ok, Users}. +list_users(PageParams, #{user_group := UserGroup}) -> + MatchSpec = [{{user_info, {UserGroup, '_'}, '_', '_', '_'}, [], ['$_']}], + {ok, emqx_mgmt_api:paginate(?TAB, MatchSpec, PageParams, fun format_user_info/1)}. %%------------------------------------------------------------------------------ %% Internal functions @@ -396,5 +396,5 @@ to_binary(B) when is_binary(B) -> to_binary(L) when is_list(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}.