fix(authn): fix enhance_authn fuzzy searching not working

This commit is contained in:
JianBo He 2022-06-13 13:38:06 +08:00
parent 4702e5314c
commit 29baf0f68a
2 changed files with 26 additions and 16 deletions

View File

@ -54,8 +54,9 @@
-define(TAB, ?MODULE). -define(TAB, ?MODULE).
-define(AUTHN_QSCHEMA, [ -define(AUTHN_QSCHEMA, [
{<<"like_username">>, binary}, {<<"like_user_id">>, binary},
{<<"user_group">>, binary} {<<"user_group">>, binary},
{<<"is_superuser">>, atom}
]). ]).
-define(QUERY_FUN, {?MODULE, query}). -define(QUERY_FUN, {?MODULE, query}).
@ -298,9 +299,9 @@ run_fuzzy_filter(_, []) ->
true; true;
run_fuzzy_filter( run_fuzzy_filter(
E = #user_info{user_id = {_, UserID}}, E = #user_info{user_id = {_, UserID}},
[{username, like, UsernameSubStr} | Fuzzy] [{user_id, like, UserIDSubStr} | Fuzzy]
) -> ) ->
binary:match(UserID, UsernameSubStr) /= nomatch andalso run_fuzzy_filter(E, Fuzzy). binary:match(UserID, UserIDSubStr) /= nomatch andalso run_fuzzy_filter(E, Fuzzy).
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% Internal functions %% Internal functions
@ -392,17 +393,12 @@ format_user_info(#user_info{user_id = {_, UserID}, is_superuser = IsSuperuser})
#{user_id => UserID, is_superuser => IsSuperuser}. #{user_id => UserID, is_superuser => IsSuperuser}.
ms_from_qstring(QString) -> ms_from_qstring(QString) ->
[Ms] = lists:foldl( case lists:keytake(user_group, 1, QString) of
fun {value, {user_group, '=:=', UserGroup}, QString2} ->
({user_group, '=:=', UserGroup}, AccIn) -> group_match_spec(UserGroup, QString2);
[group_match_spec(UserGroup) | AccIn]; _ ->
(_, AccIn) -> []
AccIn end.
end,
[],
QString
),
Ms.
group_match_spec(UserGroup) -> group_match_spec(UserGroup) ->
ets:fun2ms( ets:fun2ms(
@ -410,3 +406,17 @@ group_match_spec(UserGroup) ->
User User
end end
). ).
group_match_spec(UserGroup, QString) ->
case lists:keyfind(is_superuser, 1, QString) of
false ->
ets:fun2ms(fun(#user_info{user_id = {Group, _}} = User) when Group =:= UserGroup ->
User
end);
{is_superuser, '=:=', Value} ->
ets:fun2ms(fun(#user_info{user_id = {Group, _}, is_superuser = IsSuper} = User) when
Group =:= UserGroup, IsSuper =:= Value
->
User
end)
end.

View File

@ -324,7 +324,7 @@ t_list_users(_) ->
#{ #{
<<"page">> => 1, <<"page">> => 1,
<<"limit">> => 3, <<"limit">> => 3,
<<"like_username">> => <<"1">> <<"like_user_id">> => <<"1">>
}, },
State State
). ).