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