aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2022-08-18 10:37:59 +0100
committerMatthew Wild <mwild1@gmail.com>2022-08-18 10:37:59 +0100
commitf75ac951b518b04fb6b5f425950cfb2a8c8bb67b (patch)
treee145d2b2fd4f51c7e3cc6c887ea279c8f2a615ab
parentf5768f63c993cee9f7f8e3c89db7e4e3080beab5 (diff)
downloadprosody-f75ac951b518b04fb6b5f425950cfb2a8c8bb67b.tar.gz
prosody-f75ac951b518b04fb6b5f425950cfb2a8c8bb67b.zip
mod_authz_internal: Expose convenience method to test if user can assume role
-rw-r--r--core/usermanager.lua8
-rw-r--r--plugins/mod_authz_internal.lua12
2 files changed, 20 insertions, 0 deletions
diff --git a/core/usermanager.lua b/core/usermanager.lua
index cf54fc31..4f15c302 100644
--- a/core/usermanager.lua
+++ b/core/usermanager.lua
@@ -164,6 +164,13 @@ local function set_user_role(user, host, role_name)
return role, err;
end
+local function user_can_assume_role(user, host, role_name)
+ if host and not hosts[host] then return false; end
+ if type(user) ~= "string" then return false; end
+
+ return hosts[host].authz.user_can_assume_role(user, role_name);
+end
+
local function add_user_secondary_role(user, host, role_name)
if host and not hosts[host] then return false; end
if type(user) ~= "string" then return false; end
@@ -260,6 +267,7 @@ return {
get_provider = get_provider;
get_user_role = get_user_role;
set_user_role = set_user_role;
+ user_can_assume_role = user_can_assume_role;
add_user_secondary_role = add_user_secondary_role;
remove_user_secondary_role = remove_user_secondary_role;
get_user_secondary_roles = get_user_secondary_roles;
diff --git a/plugins/mod_authz_internal.lua b/plugins/mod_authz_internal.lua
index af402d3e..4f88b176 100644
--- a/plugins/mod_authz_internal.lua
+++ b/plugins/mod_authz_internal.lua
@@ -181,6 +181,18 @@ function get_user_secondary_roles(user)
return stored_roles;
end
+function user_can_assume_role(user, role_name)
+ local primary_role = get_user_role(user);
+ if primary_role and primary_role.role_name == role_name then
+ return true;
+ end
+ local secondary_roles = get_user_secondary_roles(user);
+ if secondary_roles and secondary_roles[role_name] then
+ return true;
+ end
+ return false;
+end
+
-- This function is *expensive*
function get_users_with_role(role_name)
local function role_filter(username, default_role) --luacheck: ignore 212/username