1 |
|
-module(mongoose_graphql_account_admin_query). |
2 |
|
-behaviour(mongoose_graphql). |
3 |
|
|
4 |
|
-export([execute/4]). |
5 |
|
|
6 |
|
-ignore_xref([execute/4]). |
7 |
|
|
8 |
|
-include("../mongoose_graphql_types.hrl"). |
9 |
|
|
10 |
|
-import(mongoose_graphql_helper, [format_result/2, make_error/3]). |
11 |
|
|
12 |
|
execute(_Ctx, _Obj, <<"listUsers">>, Args) -> |
13 |
8 |
list_users(Args); |
14 |
|
execute(_Ctx, _Obj, <<"countUsers">>, Args) -> |
15 |
9 |
count_users(Args); |
16 |
|
execute(_Ctx, _Obj, <<"checkPassword">>, Args) -> |
17 |
10 |
check_password(Args); |
18 |
|
execute(_Ctx, _Obj, <<"checkPasswordHash">>, Args) -> |
19 |
19 |
check_password_hash(Args); |
20 |
|
execute(_Ctx, _Obj, <<"checkUser">>, Args) -> |
21 |
10 |
check_user(Args). |
22 |
|
|
23 |
|
% Internal |
24 |
|
|
25 |
|
-spec list_users(map()) -> {ok, [{ok, binary()}]} | {error, resolver_error()}. |
26 |
|
list_users(#{<<"domain">> := Domain}) -> |
27 |
8 |
case mongoose_account_api:list_users(jid:nameprep(Domain)) of |
28 |
|
{domain_not_found, Msg} -> |
29 |
2 |
make_error(domain_not_found, Msg, #{domain => Domain}); |
30 |
|
{ok, Users} -> |
31 |
6 |
Users2 = lists:map(fun(U) -> {ok, U} end, Users), |
32 |
6 |
{ok, Users2} |
33 |
|
end. |
34 |
|
|
35 |
|
-spec count_users(map()) -> {ok, non_neg_integer()}. |
36 |
|
count_users(#{<<"domain">> := Domain}) -> |
37 |
9 |
case mongoose_account_api:count_users(Domain) of |
38 |
|
{domain_not_found, Msg} -> |
39 |
2 |
make_error(domain_not_found, Msg, #{domain => Domain}); |
40 |
|
{ok, UserCount} -> |
41 |
7 |
{ok, UserCount} |
42 |
|
end. |
43 |
|
|
44 |
|
-spec check_password(map()) -> {ok, map()} | {error, resolver_error()}. |
45 |
|
check_password(#{<<"user">> := JID, <<"password">> := Password}) -> |
46 |
10 |
case mongoose_account_api:check_password(JID, Password) of |
47 |
|
{user_does_not_exist, Msg} -> |
48 |
4 |
make_error(user_does_not_exist, Msg, #{jid => jid:to_binary(JID)}); |
49 |
|
{incorrect, Msg} -> |
50 |
3 |
{ok, #{<<"correct">> => false, <<"message">> => Msg}}; |
51 |
|
{ok, Msg} -> |
52 |
3 |
{ok, #{<<"correct">> => true, <<"message">> => Msg}} |
53 |
|
end. |
54 |
|
|
55 |
|
-spec check_password_hash(map()) -> {ok, map()} | {error, resolver_error()}. |
56 |
|
check_password_hash(#{<<"user">> := JID, <<"passwordHash">> := Hash, |
57 |
|
<<"hashMethod">> := HashMethod}) -> |
58 |
19 |
Val = binary_to_list(Hash), |
59 |
19 |
Method = binary_to_list(HashMethod), |
60 |
19 |
case mongoose_account_api:check_password_hash(JID, Val, Method) of |
61 |
|
{incorrect, Msg} -> |
62 |
4 |
{ok, #{<<"correct">> => false, <<"message">> => Msg}}; |
63 |
|
{ok, Msg} -> |
64 |
4 |
{ok, #{<<"correct">> => true, <<"message">> => Msg}}; |
65 |
|
{ErrCode, Msg} -> |
66 |
11 |
make_error(ErrCode, Msg, #{jid => jid:to_binary(JID)}) |
67 |
|
end. |
68 |
|
|
69 |
|
-spec check_user(map()) -> {ok, map()}. |
70 |
|
check_user(#{<<"user">> := JID}) -> |
71 |
10 |
case mongoose_account_api:check_account(JID) of |
72 |
|
{ok, Msg} -> |
73 |
3 |
{ok, #{<<"exist">> => true, <<"message">> => Msg}}; |
74 |
|
{user_does_not_exist, Msg} -> |
75 |
7 |
{ok, #{<<"exist">> => false, <<"message">> => Msg}} |
76 |
|
end. |