Search code examples
phppermissionssymfony1sfguard

advanced / dynamic permissions on sfguard - symfony


I have a symfony project and would like to add communities feature.

Everyone can open a community as an admin and invites people to join the community.

Admin has more permissions than a regular community user.

The thing is, I want to user Syfony's sfguarduser, sfguardgroup, sfguardpermission

  1. Does it make any sense to use the sfguard architecture for that purpose?
  2. how do I check if a specific user has a spcecific permission on a specific group?

Solution

  • Hey, this will help you a little ;)

    Inside the action :

      class myAccountActions extends sfActions
    {
      public function executeDoThingsWithCredentials()
      {
        $user = $this->getUser();
    
        // Check if the user has a credential
        echo $user->hasCredential('foo');                      =>   true
    
        // Check if the user has both credentials
        echo $user->hasCredential(array('foo', 'bar'));        =>   true
    
        // Check if the user has one of the credentials
        echo $user->hasCredential(array('foo', 'bar'), false); =>   true
    
        // Remove a credential
        $user->removeCredential('foo');
        echo $user->hasCredential('foo');                      =>   false
    
        // Remove all credentials (useful in the logout process)
        $user->clearCredentials();
        echo $user->hasCredential('bar');                      =>   false
      }
    }
    

    Inside the layer :

         <?php if ($sf_user->hasCredential('section3')): ?>
      ....
      <?php endif; ?>
    

    You might consider using in addition :

    if($user->hasGroup('SOME_GROUP')) 
    

    Source : Symfony inside the layer