# HG changeset patch
# User Kim Alvefur <zash@zash.se>
# Date 1677951643 -3600
# Node ID cdb996637b084f5f675ee6f4329af2edc47db10b
# Parent  7c0e5c7eff7cf49fd7152000ff0fc36d44eef7b9
authz: Add method for retrieving all roles

Some of the OAuth stuff highlights a small need to retrieve a list of
roles somehow. Handy if you ever need a role selector in adhoc or
something.

Unless there's some O(n) thing we were avoiding?

diff -r 7c0e5c7eff7c -r cdb996637b08 core/usermanager.lua
--- a/core/usermanager.lua	Thu Mar 02 22:34:29 2023 +0100
+++ b/core/usermanager.lua	Sat Mar 04 18:40:43 2023 +0100
@@ -53,6 +53,7 @@
 	get_users_with_role = function (role_name) end;
 	add_default_permission = function (role_name, action, policy) end;
 	get_role_by_name = function (role_name) end;
+	get_all_roles = function () end;
 };
 
 local provider_mt = { __index = new_null_provider() };
@@ -293,6 +294,11 @@
 	return hosts[host].authz.get_role_by_name(role_name);
 end
 
+local function get_all_roles(host)
+	if host and not hosts[host] then return false; end
+	return hosts[host].authz.get_all_roles();
+end
+
 return {
 	new_null_provider = new_null_provider;
 	initialize_host = initialize_host;
@@ -320,6 +326,7 @@
 	set_jid_role = set_jid_role;
 	get_jids_with_role = get_jids_with_role;
 	get_role_by_name = get_role_by_name;
+	get_all_roles = get_all_roles;
 
 	-- Deprecated
 	is_admin = is_admin;
diff -r 7c0e5c7eff7c -r cdb996637b08 plugins/mod_authz_internal.lua
--- a/plugins/mod_authz_internal.lua	Thu Mar 02 22:34:29 2023 +0100
+++ b/plugins/mod_authz_internal.lua	Sat Mar 04 18:40:43 2023 +0100
@@ -280,6 +280,10 @@
 	return assert(role_registry[role_name], role_name);
 end
 
+function get_all_roles()
+	return role_registry;
+end
+
 -- COMPAT: Migrate from 0.12 role storage
 local function do_migration(migrate_host)
 	local old_role_store = assert(module:context(migrate_host):open_store("roles"));