Author: Fabien Potencier (fabien@symfony.com)
Inheritance: implements Symfony\Component\Security\Core\Role\RoleInterface
示例#1
0
 /**
  * Check to see if the user has the specified role.
  *
  * @param Role|string $role
  * @return boolean
  */
 public function hasRole($role)
 {
     if (!$role instanceof Role) {
         $role = new Role($role);
     }
     return $this->role->getRole() == $role->getRole();
 }
示例#2
0
 /**
  * Utility function to find role in token
  *
  * @param TokenInterface $token
  * @param $role
  * @return bool
  */
 public static function hasRole(TokenInterface $token, $role)
 {
     if (is_string($role)) {
         $role = new Role($role);
     }
     foreach ($token->getRoles() as $tokenRole) {
         if ($role->getRole() == $tokenRole->getRole()) {
             return true;
         }
     }
 }
示例#3
0
 /**
  * Role constructor.
  *
  * @param string      $role
  * @param null|string $title
  * @param null|string $description
  * @param boolean     $hidden
  * @param array       $tags
  */
 public function __construct($role, $title, $description, $hidden, array $tags)
 {
     parent::__construct($role);
     $this->title = $title;
     $this->description = $description;
     $this->hidden = $hidden;
     $this->tags = $tags;
 }
示例#4
0
 public function testGetRole()
 {
     $role = new Role('FOO');
     $this->assertEquals('FOO', $role->getRole());
 }
示例#5
0
 /**
  * Constructor.
  *
  * @param string         $role   The role as a string
  * @param TokenInterface $source The original token
  */
 public function __construct($role, TokenInterface $source)
 {
     parent::__construct($role);
     $this->source = $source;
 }
示例#6
0
 /**
  * @param Connection    $connection
  * @param UserInterface $user
  *
  * @return int[]
  */
 private function findSidIds(Connection $connection, UserInterface $user = null)
 {
     $userSid = $this->aclIdentifier->getUserSecurityIdentity($user);
     $queryBuilder = $connection->createQueryBuilder();
     $queryBuilder->select('acl_s.id')->from($this->aclTables['sid'], 'acl_s')->where('acl_s.username = :username_true AND acl_s.identifier = :identifier')->setParameter('identifier', $userSid->getClass() . '-' . $userSid->getUsername())->setParameter('username_true', true, \PDO::PARAM_BOOL);
     if (null === $user && null !== $this->tokenStorage->getToken()) {
         $user = $this->tokenStorage->getToken()->getUser();
     }
     if ($user instanceof UserInterface) {
         $roles = $this->roleHierarchy->getReachableRoles(array_map(function ($role) {
             if (is_string($role)) {
                 $role = new Role($role);
             }
             return $role;
         }, $user->getRoles()));
         $roles = array_map(function (RoleInterface $role) {
             return $role->getRole();
         }, $roles);
         if (!empty($roles)) {
             $queryBuilder->orWhere('acl_s.username = :username_false AND acl_s.identifier IN (:roles)')->setParameter('roles', $roles, Connection::PARAM_STR_ARRAY)->setParameter('username_false', false, \PDO::PARAM_BOOL);
         }
     }
     return array_map(function (array $row) {
         return (int) $row['id'];
     }, $queryBuilder->execute()->fetchAll());
 }