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