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).
--- 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