./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 131 mongoose_user_cache:start_new_cache(HostType, ?MODULE, Opts),
25 131 ejabberd_hooks:add(hooks(HostType)),
26 131 ok.
27
28 -spec stop(mongooseim:host_type()) -> ok.
29 stop(HostType) ->
30 129 ejabberd_hooks:delete(hooks(HostType)),
31 129 mongoose_user_cache:stop_cache(HostType, ?MODULE),
32 129 ok.
33
34 -spec config_spec() -> mongoose_config_spec:config_section().
35 config_spec() ->
36 164 #section{items = Items,
37 defaults = Defaults} = Sec = mongoose_user_cache:config_spec(),
38 164 Sec#section{items = maps:remove(<<"module">>, Items),
39 defaults = maps:remove(<<"module">>, Defaults)}.
40
41 -spec supported_features() -> [atom()].
42 supported_features() ->
43 64 [dynamic_domains].
44
45 hooks(HostType) ->
46 260 [
47 %% These hooks must run before and after the ejabberd_auth does_user_exist hook
48 {does_user_exist, HostType, ?MODULE, does_cached_user_exist, 30},
49 {does_user_exist, HostType, ?MODULE, maybe_put_user_into_cache, 70},
50 %% It is important that these two handlers happen _after_ ejabberd_auth
51 %% but _before_ all other modules
52 {remove_user, HostType, ?MODULE, remove_user, 10},
53 {remove_domain, HostType, ?MODULE, remove_domain, 20}
54 ].
55
56 %%====================================================================
57 %% Hooks
58 %%====================================================================
59
60 -spec remove_user(Acc :: mongoose_acc:t(),
61 LUser :: jid:luser(),
62 LServer :: jid:lserver() | string()) -> mongoose_acc:t().
63 remove_user(Acc, LUser, LServer) ->
64 2291 HostType = mongoose_acc:host_type(Acc),
65 2291 Jid = jid:make_noprep(LUser, LServer, <<>>),
66 2291 mongoose_user_cache:delete_user(HostType, ?MODULE, Jid),
67 2291 Acc.
68
69 -spec remove_domain(mongoose_hooks:simple_acc(),
70 mongooseim:host_type(), jid:lserver()) ->
71 mongoose_hooks:simple_acc().
72 remove_domain(Acc, HostType, Domain) ->
73
:-(
mongoose_user_cache:delete_domain(HostType, ?MODULE, Domain),
74
:-(
Acc.
75
76 %%====================================================================
77 %% Helpers
78 %%====================================================================
79
80 -spec does_cached_user_exist(Status :: boolean(),
81 HostType :: mongooseim:host_type(),
82 Jid :: jid:jid(),
83 RequestType :: ejabberd_auth:exist_type()) ->
84 boolean() | {stop, true}.
85 does_cached_user_exist(false, HostType, Jid, stored) ->
86 410 case mongoose_user_cache:is_member(HostType, ?MODULE, Jid) of
87 160 true -> {stop, true};
88 250 false -> false
89 end;
90 does_cached_user_exist(Status, _, _, _) ->
91
:-(
Status.
92
93 -spec maybe_put_user_into_cache(Status :: boolean(),
94 HostType :: mongooseim:host_type(),
95 Jid :: jid:jid(),
96 RequestType :: ejabberd_auth:exist_type()) ->
97 boolean().
98 maybe_put_user_into_cache(true, HostType, Jid, stored) ->
99 209 mongoose_user_cache:merge_entry(HostType, ?MODULE, Jid, #{}),
100 209 true;
101 maybe_put_user_into_cache(Status, _, _, _) ->
102 41 Status.
Line Hits Source