util.roles: Add a :policies() method to iterate through available policies
authorMatthew Wild <mwild1@gmail.com>
Fri, 07 Oct 2022 17:01:35 +0100
changeset 12752 7b9de8109a90
parent 12751 9d6d64fb7641
child 12753 eb9814372c54
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).
util/roles.lua
--- 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