1 |
|
-module(mongoose_graphql_account_admin_mutation). |
2 |
|
-behaviour(mongoose_graphql). |
3 |
|
|
4 |
|
-export([execute/4]). |
5 |
|
-export([await_execution/4]). |
6 |
|
|
7 |
|
-ignore_xref([execute/4]). |
8 |
|
|
9 |
|
-include("../mongoose_graphql_types.hrl"). |
10 |
|
|
11 |
|
-import(mongoose_graphql_helper, [format_result/2, make_error/2]). |
12 |
|
|
13 |
|
execute(_Ctx, account, <<"registerUser">>, Args) -> |
14 |
16 |
register_user(Args); |
15 |
|
execute(_Ctx, account, <<"removeUser">>, Args) -> |
16 |
18 |
remove_user(Args); |
17 |
|
execute(_Ctx, account, <<"banUser">>, Args) -> |
18 |
6 |
ban_user(Args); |
19 |
|
execute(_Ctx, account, <<"changeUserPassword">>, Args) -> |
20 |
8 |
change_user_password(Args); |
21 |
|
execute(#{method := cli}, account, <<"importUsers">>, Args) -> |
22 |
2 |
import_users(Args); |
23 |
|
execute(#{method := http}, account, <<"importUsers">>, #{<<"filename">> := Filename}) -> |
24 |
1 |
spawn(?MODULE, await_execution, [1000, mongoose_account_api, import_users, [Filename]]), |
25 |
1 |
{ok, #{<<"status">> => <<"ImportUsers scheduled">>}}. |
26 |
|
|
27 |
|
% Internal |
28 |
|
|
29 |
|
-spec register_user(map()) -> {ok, map()} | {error, resolver_error()}. |
30 |
|
register_user(#{<<"domain">> := Domain, <<"username">> := null, |
31 |
|
<<"password">> := Password}) -> |
32 |
2 |
case mongoose_account_api:register_generated_user(Domain, Password) of |
33 |
|
{{ok, Msg}, JID} -> |
34 |
2 |
{ok, make_user_payload(Msg, JID)}; |
35 |
|
{Result, JID} -> |
36 |
:-( |
format_result(Result, #{jid => JID}) |
37 |
|
end; |
38 |
|
register_user(#{<<"domain">> := Domain, <<"username">> := Username, |
39 |
|
<<"password">> := Password}) -> |
40 |
14 |
Result = mongoose_account_api:register_user(Username, Domain, Password), |
41 |
14 |
format_user_payload(Result, jid:make_bare(Username, Domain)). |
42 |
|
|
43 |
|
-spec remove_user(map()) -> {ok, map()} | {error, resolver_error()}. |
44 |
|
remove_user(#{<<"user">> := JID}) -> |
45 |
18 |
Result = mongoose_account_api:unregister_user(JID), |
46 |
18 |
format_user_payload(Result, JID). |
47 |
|
|
48 |
|
-spec ban_user(map()) -> {ok, map()} | {error, resolver_error()}. |
49 |
|
ban_user(#{<<"user">> := JID, <<"reason">> := Reason}) -> |
50 |
6 |
Result = mongoose_account_api:ban_account(JID, Reason), |
51 |
6 |
format_user_payload(Result, JID). |
52 |
|
|
53 |
|
-spec change_user_password(map()) -> {ok, map()} | {error, resolver_error()}. |
54 |
|
change_user_password(#{<<"user">> := JID, <<"newPassword">> := Password}) -> |
55 |
8 |
Result = mongoose_account_api:change_password(JID, Password), |
56 |
8 |
format_user_payload(Result, JID). |
57 |
|
|
58 |
|
-spec import_users(map()) -> {ok, map()} | {error, resolver_error()}. |
59 |
|
import_users(#{<<"filename">> := Filename}) -> |
60 |
2 |
case mongoose_account_api:import_users(Filename) of |
61 |
1 |
{ok, _} = Result -> Result; |
62 |
1 |
Error -> make_error(Error, #{filename => Filename}) |
63 |
|
end. |
64 |
|
|
65 |
|
-spec format_user_payload({atom(), string()}, jid:jid()) -> {ok, map()} | {error, resolver_error()}. |
66 |
|
format_user_payload(InResult, JID) -> |
67 |
46 |
case InResult of |
68 |
|
{ok, Msg} -> |
69 |
24 |
{ok, make_user_payload(Msg, jid:to_binary(JID))}; |
70 |
|
Result -> |
71 |
22 |
make_error(Result, #{jid => jid:to_binary(JID)}) |
72 |
|
end. |
73 |
|
|
74 |
|
-spec make_user_payload(string(), jid:literal_jid()) -> map(). |
75 |
|
make_user_payload(Msg, JID) -> |
76 |
26 |
#{<<"message">> => iolist_to_binary(Msg), <<"jid">> => JID}. |
77 |
|
|
78 |
|
await_execution(Timeout, Module, Fun, Args) -> |
79 |
1 |
timer:sleep(Timeout), |
80 |
1 |
apply(Module, Fun, Args). |