Changeset

12748:7b9de8109a90

util.roles: Add a :policies() method to iterate through available policies We don't expose the policies directly, to force people to go through :may(). However, there are times when we really just need to know what policies a role has inside it (e.g. for reporting or debugging purposes).
author Matthew Wild <mwild1@gmail.com>
date Fri, 07 Oct 2022 17:01:35 +0100
parents 12747:9d6d64fb7641
children 12749:eb9814372c54
files util/roles.lua
diffstat 1 files changed, 10 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/util/roles.lua	Fri Oct 07 16:58:52 2022 +0100
+++ b/util/roles.lua	Fri Oct 07 17:01:35 2022 +0100
@@ -86,6 +86,16 @@
 	return true;
 end
 
+function role_methods:policies()
+	local policy_iterator, s, v = it.join(pairs(self[permissions_key]));
+	if self.inherits then
+		for _, inherited_role in ipairs(self.inherits) do
+			policy_iterator:append(inherited_role:policies());
+		end
+	end
+	return policy_iterator, s, v;
+end
+
 function role_mt.__tostring(self)
 	return ("role<[%s] %s>"):format(self.id or "nil", self.name or "[no name]");
 end