Search code examples
phpsymfonyregistrationaclroles

Symfony complex acl based on dynamic roles and permissions


I want to build a system which has multiple users, each user can be assigned 0 to many roles to projects or sections of projects (Objects). Each role can have 1 to many permissions. The roles can be created dynamically, so assigning them to users. However, permissions can be hard-coded.

I know I should use ACL, however I am not sure of how to add dynamic roles into it in Symfony2. Additionally, should I use voters?


Solution

  • Hopefully the below code help you

    // creating the ACL
    $aclProvider = $this->get('security.acl.provider');
    $objectIdentity = ObjectIdentity::fromDomainObject($the_object_to_be_granted);
    $acl = $aclProvider->createAcl($objectIdentity);
    
    $securityIdentity = new RoleSecurityIdentity("CUSTOM_ROLE_YOU_HAVE");
    
    // grant owner access
    $acl->insertObjectAce($securityIdentity, MaskBuilder::MASK_OWNER);// This is sample you can use any other masks you need
    $aclProvider->updateAcl($acl);
    

    You can obtain more info on the following link (Symfony ACL)