./ct_report/coverage/mod_cache_users.COVER.html

1 %% @doc Caches info about non-anonymous users using a queue of ets tables
2 -module(mod_cache_users).
3
4 -include("mongoose_config_spec.hrl").
5
6 -behaviour(gen_mod).
7
8 %% gen_mod API
9 -export([start/2, stop/1, config_spec/0, supported_features/0]).
10
11 %% Hooks.
12 -export([does_cached_user_exist/4, maybe_put_user_into_cache/4,
13 remove_user/3, remove_domain/3]).
14
15 -ignore_xref([does_cached_user_exist/4, maybe_put_user_into_cache/4,
16 remove_domain/3, remove_user/3]).
17
18 %%====================================================================
19 %% gen_mod callbacks
20 %%====================================================================
21
22 -spec start(mongooseim:host_type(), gen_mod:module_opts()) -> ok.
23 start(HostType, Opts) ->
24 126 mongoose_user_cache:start_new_cache(HostType, ?MODULE, Opts),
25 126 ejabberd_hooks:add(hooks(HostType)),
26 126 ok.
27
28 -spec stop(mongooseim:host_type()) -> ok.
29 stop(HostType) ->
30 126 ejabberd_hooks:delete(hooks(HostType)),
31 126 mongoose_user_cache:stop_cache(HostType, ?MODULE),
32 126 ok.
33
34 -spec config_spec() -> mongoose_config_spec:config_section().
35 config_spec() ->
36 146 Sec = #section{items = Items} = mongoose_user_cache:config_spec(),
37 146 Sec#section{items = maps:remove(<<"module">>, Items)}.
38
39 -spec supported_features() -> [atom()].
40 supported_features() ->
41 64 [dynamic_domains].
42
43 hooks(HostType) ->
44 252 [
45 %% These hooks must run before and after the ejabberd_auth does_user_exist hook
46 {does_user_exist, HostType, ?MODULE, does_cached_user_exist, 30},
47 {does_user_exist, HostType, ?MODULE, maybe_put_user_into_cache, 70},
48 %% It is important that these two handlers happen _after_ ejabberd_auth
49 %% but _before_ all other modules
50 {remove_user, HostType, ?MODULE, remove_user, 10},
51 {remove_domain, HostType, ?MODULE, remove_domain, 20}
52 ].
53
54 %%====================================================================
55 %% Hooks
56 %%====================================================================
57
58 -spec remove_user(Acc :: mongoose_acc:t(),
59 LUser :: jid:luser(),
60 LServer :: jid:lserver() | string()) -> mongoose_acc:t().
61 remove_user(Acc, LUser, LServer) ->
62 3547 HostType = mongoose_acc:host_type(Acc),
63 3547 Jid = jid:make_noprep(LUser, LServer, <<>>),
64 3547 mongoose_user_cache:delete_user(HostType, ?MODULE, Jid),
65 3547 Acc.
66
67 -spec remove_domain(mongoose_hooks:simple_acc(),
68 mongooseim:host_type(), jid:lserver()) ->
69 mongoose_hooks:simple_acc().
70 remove_domain(Acc, HostType, Domain) ->
71 15 mongoose_user_cache:delete_domain(HostType, ?MODULE, Domain),
72 15 Acc.
73
74 %%====================================================================
75 %% Helpers
76 %%====================================================================
77
78 -spec does_cached_user_exist(Status :: boolean(),
79 HostType :: mongooseim:host_type(),
80 Jid :: jid:jid(),
81 RequestType :: ejabberd_auth:exist_type()) ->
82 boolean() | {stop, true}.
83 does_cached_user_exist(false, HostType, Jid, stored) ->
84 9379 case mongoose_user_cache:is_member(HostType, ?MODULE, Jid) of
85 7698 true -> {stop, true};
86 1681 false -> false
87 end;
88 does_cached_user_exist(Status, _, _, _) ->
89
:-(
Status.
90
91 -spec maybe_put_user_into_cache(Status :: boolean(),
92 HostType :: mongooseim:host_type(),
93 Jid :: jid:jid(),
94 RequestType :: ejabberd_auth:exist_type()) ->
95 boolean().
96 maybe_put_user_into_cache(true, HostType, Jid, stored) ->
97 1247 mongoose_user_cache:merge_entry(HostType, ?MODULE, Jid, #{}),
98 1247 true;
99 maybe_put_user_into_cache(Status, _, _, _) ->
100 433 Status.
Line Hits Source