Comparison

plugins/mod_authz_internal.lua @ 11745:3a2d58a39872

usermanager, mod_authz_internal: Add methods to fetch users/JIDs of given role
author Matthew Wild <mwild1@gmail.com>
date Thu, 26 Aug 2021 16:35:43 +0100
parent 11474:8fba807e5256
child 12642:9061f9621330
comparison
equal deleted inserted replaced
11744:5f99aa6bb76d 11745:3a2d58a39872
1 local array = require "util.array";
2 local it = require "util.iterators";
3 local set = require "util.set";
4 local jid_split = require "util.jid".split;
1 local normalize = require "util.jid".prep; 5 local normalize = require "util.jid".prep;
2 local admin_jids = module:get_option_inherited_set("admins", {}) / normalize; 6 local config_admin_jids = module:get_option_inherited_set("admins", {}) / normalize;
3 local host = module.host; 7 local host = module.host;
4 local role_store = module:open_store("roles"); 8 local role_store = module:open_store("roles");
9 local role_map_store = module:open_store("roles", "map");
5 10
6 local admin_role = { ["prosody:admin"] = true }; 11 local admin_role = { ["prosody:admin"] = true };
7 12
8 function get_user_roles(user) 13 function get_user_roles(user)
9 if admin_jids:contains(user.."@"..host) then 14 if config_admin_jids:contains(user.."@"..host) then
10 return admin_role; 15 return admin_role;
11 end 16 end
12 return role_store:get(user); 17 return role_store:get(user);
13 end 18 end
14 19
15 function set_user_roles(user, roles) 20 function set_user_roles(user, roles)
16 role_store:set(user, roles) 21 role_store:set(user, roles)
17 return true; 22 return true;
18 end 23 end
19 24
25 function get_users_with_role(role)
26 local storage_role_users = it.to_array(it.keys(role_map_store:get_all(role) or {}));
27 if role == "prosody:admin" then
28 local config_admin_users = config_admin_jids / function (admin_jid)
29 local j_node, j_host = jid_split(admin_jid);
30 if j_host == host then
31 return j_node;
32 end
33 end;
34 return it.to_array(config_admin_users + set.new(storage_role_users));
35 end
36 return storage_role_users;
37 end
38
20 function get_jid_roles(jid) 39 function get_jid_roles(jid)
21 if admin_jids:contains(jid) then 40 if config_admin_jids:contains(jid) then
22 return admin_role; 41 return admin_role;
23 end 42 end
24 return nil; 43 return nil;
25 end 44 end
26 45
27 function set_jid_roles(jid) -- luacheck: ignore 212 46 function set_jid_roles(jid) -- luacheck: ignore 212
28 return false; 47 return false;
29 end 48 end
49
50 function get_jids_with_role(role)
51 -- Fetch role users from storage
52 local storage_role_jids = array.map(get_users_with_role(role), function (username)
53 return username.."@"..host;
54 end);
55 if role == "prosody:admin" then
56 return it.to_array(config_admin_jids + set.new(storage_role_jids));
57 end
58 return storage_role_jids;
59 end