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 |
:-( |
list_users(Args); |
14 |
|
execute(_Ctx, _Obj, <<"countUsers">>, Args) -> |
15 |
:-( |
count_users(Args); |
16 |
|
execute(_Ctx, _Obj, <<"checkPassword">>, Args) -> |
17 |
:-( |
check_password(Args); |
18 |
|
execute(_Ctx, _Obj, <<"checkPasswordHash">>, Args) -> |
19 |
:-( |
check_password_hash(Args); |
20 |
|
execute(_Ctx, _Obj, <<"checkUser">>, Args) -> |
21 |
:-( |
check_user(Args). |
22 |
|
|
23 |
|
% Internal |
24 |
|
|
25 |
|
-spec list_users(map()) -> {ok, [{ok, binary()}]} | {error, resolver_error()}. |
26 |
|
list_users(#{<<"domain">> := Domain}) -> |
27 |
:-( |
case mongoose_account_api:list_users(jid:nameprep(Domain)) of |
28 |
|
{domain_not_found, Msg} -> |
29 |
:-( |
make_error(domain_not_found, Msg, #{domain => Domain}); |
30 |
|
{ok, Users} -> |
31 |
:-( |
Users2 = lists:map(fun(U) -> {ok, U} end, Users), |
32 |
:-( |
{ok, Users2} |
33 |
|
end. |
34 |
|
|
35 |
|
-spec count_users(map()) -> {ok, non_neg_integer()}. |
36 |
|
count_users(#{<<"domain">> := Domain}) -> |
37 |
:-( |
case mongoose_account_api:count_users(Domain) of |
38 |
|
{domain_not_found, Msg} -> |
39 |
:-( |
make_error(domain_not_found, Msg, #{domain => Domain}); |
40 |
|
{ok, UserCount} -> |
41 |
:-( |
{ok, UserCount} |
42 |
|
end. |
43 |
|
|
44 |
|
-spec check_password(map()) -> {ok, map()} | {error, resolver_error()}. |
45 |
|
check_password(#{<<"user">> := JID, <<"password">> := Password}) -> |
46 |
:-( |
case mongoose_account_api:check_password(JID, Password) of |
47 |
|
{user_does_not_exist, Msg} -> |
48 |
:-( |
make_error(user_does_not_exist, Msg, #{jid => jid:to_binary(JID)}); |
49 |
|
{incorrect, Msg} -> |
50 |
:-( |
{ok, #{<<"correct">> => false, <<"message">> => Msg}}; |
51 |
|
{ok, Msg} -> |
52 |
:-( |
{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 |
:-( |
Val = binary_to_list(Hash), |
59 |
:-( |
Method = binary_to_list(HashMethod), |
60 |
:-( |
case mongoose_account_api:check_password_hash(JID, Val, Method) of |
61 |
|
{incorrect, Msg} -> |
62 |
:-( |
{ok, #{<<"correct">> => false, <<"message">> => Msg}}; |
63 |
|
{ok, Msg} -> |
64 |
:-( |
{ok, #{<<"correct">> => true, <<"message">> => Msg}}; |
65 |
|
{ErrCode, Msg} -> |
66 |
:-( |
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 |
:-( |
case mongoose_account_api:check_account(JID) of |
72 |
|
{ok, Msg} -> |
73 |
:-( |
{ok, #{<<"exist">> => true, <<"message">> => Msg}}; |
74 |
|
{user_does_not_exist, Msg} -> |
75 |
:-( |
{ok, #{<<"exist">> => false, <<"message">> => Msg}} |
76 |
|
end. |