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