-
Notifications
You must be signed in to change notification settings - Fork 112
Dynamic role support #318
Description
I have a project where I customized Rbac. The thing was that I needed dynamic roles; a person can be an Owner
/Admin
for one resource instance and for another resource instance of the same type (so a different resource identifier) he has for example no role at all (Guest
role). To be able to implement that I introduced a RoleResolver
.
In my config I tie resource classes to certain role resolvers to be able to resolve the roles for the provided $resource
instance for the current AuthenticatedIdentity
at run time (or optional another identity passed to the resolver).
something like:
'role_resolvers' => [
'Application\Resource\Object' => 'Application\Role\Resolver\ObjectRoleResolver'
//... more role resolver configs
)
My RoleResolverInterface
looks like this:
<?php
namespace Rbac\Role;
use Rbac\Identity\IdentityInterface;
/**
* A role resolver is a class that collects role names (strings)
*/
interface RoleResolverInterface
{
/**
* Get the roles for the resource
*
* @param IdentityInterface $identity
* @param object $resource
* @return array
*/
public function getRoles(IdentityInterface $identity, $resource);
}
I created a RoleResolverPluginManager
where I load the config. This allows easy access to the correct role resolver.
In my authorization event I get the correct resource (or a resource reference) and resolve the roles for the current user for the resource so that the isAuthorized
method in my AuthorizationService
can be executed with the correct roles for that particular resource.
Would it be interesting to add such dynamic role support to this RBAC
module? Or would it be interesting to make another module called DynamicRBAC
? Or do you see other ways of achieving the same results with the current module?