Code snippets for symfony 1.x

Navigation

Refine Tags

Snippets tagged "group"

Check if a user is in one or more groups

Extend the myUser.class.php in any /lib folder with the following function:

public function isInGroups(array $groups, $strict = false)
{
    if ($strict)
    {
        return (array_intersect($groups, $this->getGroupNames()) == $groups);
    }
    else
    {
        foreach ($groups as $g)
        {
            if (in_array($g, $this->getGroupNames())) return true;
        }
    }
    return false;
}
 

Now you can check the groups of the user as following:

if ($sf_user->isInGroups(array('admin', 'editor')))
{
    // do stuff here...
}
 

You can also check if the user is in all the groups specified. Just set the second parameter to true:

if ($sf_user->isInGroups(array('admin', 'editor'), true))
{
    // this is executed only if the user is in the groups admin AND editor
    // please note: he might still be a member of other groups
}
 
by Volker on 2009-09-18, tagged check  group  groups  user 
(2 comments)