./ct_report/coverage/mongoose_api_users.COVER.html

1 %%==============================================================================
2 %% Copyright 2014 Erlang Solutions Ltd.
3 %%
4 %% Licensed under the Apache License, Version 2.0 (the "License");
5 %% you may not use this file except in compliance with the License.
6 %% You may obtain a copy of the License at
7 %%
8 %% http://www.apache.org/licenses/LICENSE-2.0
9 %%
10 %% Unless required by applicable law or agreed to in writing, software
11 %% distributed under the License is distributed on an "AS IS" BASIS,
12 %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 %% See the License for the specific language governing permissions and
14 %% limitations under the License.
15 %%==============================================================================
16 -module(mongoose_api_users).
17
18 %% mongoose_api callbacks
19 -export([prefix/0,
20 routes/0,
21 handle_options/2,
22 handle_get/2,
23 handle_put/3,
24 handle_delete/2]).
25
26 -ignore_xref([handle_delete/2, handle_get/2, handle_options/2, handle_put/3,
27 prefix/0, routes/0]).
28
29 -define(ERROR, {error, unprocessable}).
30
31 %%--------------------------------------------------------------------
32 %% mongoose_api callbacks
33 %%--------------------------------------------------------------------
34 -spec prefix() -> mongoose_api:prefix().
35 prefix() ->
36 236 "/users".
37
38 -spec routes() -> mongoose_api:routes().
39 routes() ->
40 118 [{"/host/:host", [host_users]},
41 {"/host/:host/username/:username", [host_user]}].
42
43 -spec handle_options(mongoose_api:bindings(), mongoose_api:options()) ->
44 mongoose_api:methods().
45 handle_options(_Bindings, [host_users]) ->
46 50 [get];
47 handle_options(_Bindings, [host_user]) ->
48 53 [put, delete].
49
50 -spec handle_get(mongoose_api:bindings(), mongoose_api:options()) ->
51 mongoose_api:response().
52 handle_get(Bindings, [host_users]) ->
53 50 get_users(Bindings).
54
55 -spec handle_put(term(), mongoose_api:bindings(), mongoose_api:options()) ->
56 mongoose_api:response().
57 handle_put(Data, Bindings, [host_user]) ->
58 31 put_user(Data, Bindings).
59
60 -spec handle_delete(mongoose_api:bindings(), mongoose_api:options()) ->
61 mongoose_api:response().
62 handle_delete(Bindings, [host_user]) ->
63 21 delete_user(Bindings).
64
65 %%--------------------------------------------------------------------
66 %% mongoose_api commands actual handlers
67 %%--------------------------------------------------------------------
68 get_users(Bindings) ->
69 50 Host = proplists:get_value(host, Bindings),
70 50 Users = ejabberd_auth:get_vh_registered_users(Host),
71 50 Response = [{count, length(Users)},
72 {users, users_to_proplist(Users)}],
73 50 {ok, Response}.
74
75 put_user(Data, Bindings) ->
76 31 Host = gen_mod:get_opt(host, Bindings),
77 31 Username = gen_mod:get_opt(username, Bindings),
78 31 case proplist_to_user(Data) of
79 {ok, Password} ->
80 30 maybe_register_user(Username, Host, Password);
81 {error, _} ->
82 1 ?ERROR
83 end.
84
85 delete_user(Bindings) ->
86 21 Host = gen_mod:get_opt(host, Bindings),
87 21 Username = gen_mod:get_opt(username, Bindings),
88 21 JID = jid:make(Username, Host, <<>>),
89 21 case ejabberd_auth:does_user_exist(JID) of
90 true ->
91 20 maybe_delete_user(JID);
92 false ->
93 1 {error, not_found}
94 end.
95
96 %%--------------------------------------------------------------------
97 %% internal functions
98 %%--------------------------------------------------------------------
99 maybe_register_user(Username, Host, Password) ->
100 30 JID = jid:make(Username, Host, <<>>),
101 30 case ejabberd_auth:try_register(JID, Password) of
102 {error, not_allowed} ->
103
:-(
?ERROR;
104 {error, exists} ->
105 10 maybe_change_password(JID, Password);
106 _ ->
107 20 ok
108 end.
109
110 maybe_change_password(JID, Password) ->
111 10 case ejabberd_auth:set_password(JID, Password) of
112 {error, _} ->
113
:-(
?ERROR;
114 ok ->
115 10 ok
116 end.
117
118 maybe_delete_user(JID) ->
119 20 case ejabberd_auth:remove_user(JID) of
120 ok ->
121 20 ok;
122 _ ->
123
:-(
error
124 end.
125
126 users_to_proplist(Users) ->
127 50 [user_to_proplist(User) || User <- Users].
128
129 user_to_proplist({Username, Host}) ->
130 290 {user, [{username, Username}, {host, Host}]}.
131
132 proplist_to_user([{<<"user">>, User}]) ->
133 31 case gen_mod:get_opt(<<"password">>, User, undefined) of
134 undefined ->
135 1 ?ERROR;
136 Password ->
137 30 {ok, Password}
138 end;
139 proplist_to_user(_Other) ->
140
:-(
?ERROR.
Line Hits Source