getRole() public method

Returns the named role.
public getRole ( string $name ) : null | Role
$name string the role name.
return null | Role the role corresponding to the specified name. Null is returned if no such role.
コード例 #1
0
ファイル: AssignmentModel.php プロジェクト: yii2mod/yii2-rbac
 /**
  * Revokes a roles and permissions from the user.
  *
  * @param array $items
  *
  * @return int number of successful revoke
  */
 public function revoke($items)
 {
     foreach ($items as $name) {
         $item = $this->manager->getRole($name);
         $item = $item ?: $this->manager->getPermission($name);
         $this->manager->revoke($item, $this->userId);
     }
     return true;
 }
コード例 #2
0
 /**
  * @param string|Role $role role item object or it's name
  * @return Role
  */
 protected function findRole($role)
 {
     if ($role instanceof Role) {
         return $role;
     }
     return $this->authManager->getRole($role);
 }
コード例 #3
0
ファイル: RoleController.php プロジェクト: wsdslm/yii2-rbac
 protected function findRole($name)
 {
     $role = $this->auth->getRole($name);
     if ($role) {
         return $role;
     }
     throw new HttpException(404);
 }
コード例 #4
0
 public function testGetAssignmentsByRole()
 {
     $this->prepareData();
     $reader = $this->auth->getRole('reader');
     $this->auth->assign($reader, 123);
     $this->auth = $this->createManager();
     $this->assertEquals([], $this->auth->getUserIdsByRole('nonexisting'));
     $this->assertEquals(['reader A', '123'], $this->auth->getUserIdsByRole('reader'));
     $this->assertEquals(['author B'], $this->auth->getUserIdsByRole('author'));
     $this->assertEquals(['admin C'], $this->auth->getUserIdsByRole('admin'));
 }
コード例 #5
0
ファイル: AuthItemModel.php プロジェクト: yii2mod/yii2-rbac
 /**
  * Remove child from an item
  *
  * @param array $items
  *
  * @return int
  */
 public function removeChildren($items)
 {
     if ($this->_item !== null) {
         foreach ($items as $name) {
             $child = $this->manager->getPermission($name);
             if (empty($child) && $this->type == Item::TYPE_ROLE) {
                 $child = $this->manager->getRole($name);
             }
             $this->manager->removeChild($this->_item, $child);
         }
     }
     return true;
 }
コード例 #6
0
ファイル: ManagerTestCase.php プロジェクト: howq/yii2
 public function testAssignmentsToIntegerId()
 {
     $this->prepareData();
     $reader = $this->auth->getRole('reader');
     $author = $this->auth->getRole('author');
     $this->auth->assign($reader, 42);
     $this->auth->assign($author, 1337);
     $this->auth->assign($reader, 1337);
     $this->auth = $this->createManager();
     $this->assertEquals(0, count($this->auth->getAssignments(0)));
     $this->assertEquals(1, count($this->auth->getAssignments(42)));
     $this->assertEquals(2, count($this->auth->getAssignments(1337)));
 }
コード例 #7
0
ファイル: Role.php プロジェクト: apurey/cmf
 /**
  * @param string $name
  * @param array $permissions
  * @param array $roles
  * @return bool
  */
 public function updateRole($name, array $permissions, array $roles)
 {
     if ($this->validate()) {
         $object = $this->authManager->getRole($name);
         $object->description = $this->description;
         if ($this->authManager->update($name, $object)) {
             $this->authManager->removeChildren($object);
             foreach ($permissions as $permission) {
                 $this->authManager->addChild($object, $this->authManager->getPermission($permission));
             }
             foreach ($roles as $role) {
                 $this->authManager->addChild($object, $this->authManager->getRole($role));
             }
             return true;
         }
     }
     return false;
 }
コード例 #8
0
 /**
  * Remove roles.
  */
 protected function removeRoles()
 {
     if (!($roles = ArrayHelper::getValue($this->rbac, 'roles'))) {
         return;
     }
     foreach ($roles as $name => $children) {
         if (!($role = $this->_auth->getRole($name))) {
             continue;
         }
         foreach ($children as $childName) {
             if ($permission = ArrayHelper::getValue($this->_permissions, $childName)) {
                 $this->_auth->removeChild($role, $permission);
             } elseif ($childRole = $this->_auth->getRole($childName)) {
                 $this->_auth->removeChild($role, $childRole);
             }
         }
         $this->_auth->remove($role);
         $userIds = $this->_auth->getUserIdsByRole($name);
         foreach ($userIds as $userId) {
             $this->_auth->revoke($role, $userId);
         }
     }
 }