Since: 2.0
Author: Qiang Xue (qiang.xue@gmail.com)
Example #1
0
 /**
  * @return bool
  */
 public function updatePermission()
 {
     if ($this->validate()) {
         $permission = $this->authManager->getPermission($this->name);
         $permission->description = $this->description;
         return $this->authManager->update($this->name, $permission);
     }
     return false;
 }
Example #2
0
 /**
  * @param ManagerInterface $auth
  * @param string $name
  * @param array $children
  */
 private function removePermission($auth, $name, $children = array())
 {
     $permission = $auth->getPermission($name);
     $auth->removeChildren($permission);
     if ($children) {
         foreach ($children as $childName => $childOptions) {
             $this->removePermission($auth, $childName, isset($childOptions['children']) ? $childOptions['children'] : array());
         }
     }
     $auth->remove($permission);
 }
Example #3
0
 /**
  * Get available and assigned routes
  *
  * @return array
  */
 public function getRoutes()
 {
     $routes = $this->getAppRoutes();
     $exists = [];
     foreach (array_keys($this->manager->getPermissions()) as $name) {
         if ($name[0] !== '/') {
             continue;
         }
         $exists[] = $name;
         unset($routes[$name]);
     }
     return ['available' => array_keys($routes), 'assigned' => $exists];
 }
 /**
  * @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);
 }
Example #5
0
 public function testGetRolesByUser()
 {
     $this->prepareData();
     $roles = $this->auth->getRolesByUser('reader A');
     $this->assertTrue(reset($roles) instanceof Role);
     $this->assertEquals($roles['reader']->name, 'reader');
 }
Example #6
0
 protected function findRole($name)
 {
     $role = $this->auth->getRole($name);
     if ($role) {
         return $role;
     }
     throw new HttpException(404);
 }
 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'));
 }
Example #8
0
 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)));
 }
Example #9
0
 /**
  * @param Role $role
  */
 public function setRole($role)
 {
     $this->_role = $role;
     $this->name = $role->name;
     $this->oldName = $role->name;
     $this->description = $role->description;
     if (!$this->isNewRecord) {
         $permissions = $this->auth->getPermissionsByRole($role->name);
         foreach ($permissions as $permission) {
             $this->actions[] = $permission->name;
         }
     }
 }
Example #10
0
File: Role.php Project: apurey/cmf
 /**
  * @return array
  */
 public function loadChildren()
 {
     if ($this->permissions + $this->roles == []) {
         foreach ($this->authManager->getChildren($this->name) as $item) {
             switch ($item->type) {
                 case Permission::TYPE:
                     $this->permissions = ArrayHelper::merge($this->permissions, [$item->name]);
                     break;
                 case Role::TYPE:
                     $this->roles = ArrayHelper::merge($this->roles, [$item->name]);
                     break;
             }
         }
     }
 }
Example #11
0
 /**
  * Get all available and assigned roles and permissions
  *
  * @return array
  */
 public function getItems()
 {
     $available = [];
     $assigned = [];
     foreach (array_keys($this->manager->getRoles()) as $name) {
         $available[$name] = 'role';
     }
     foreach (array_keys($this->manager->getPermissions()) as $name) {
         if ($name[0] != '/') {
             $available[$name] = 'permission';
         }
     }
     foreach ($this->manager->getAssignments($this->userId) as $item) {
         $assigned[$item->roleName] = $available[$item->roleName];
         unset($available[$item->roleName]);
     }
     return ['available' => $available, 'assigned' => $assigned];
 }
Example #12
0
 /**
  * Get all available and assigned roles, permission and routes
  *
  * @return array
  */
 public function getItems()
 {
     $available = [];
     $assigned = [];
     if ($this->type == Item::TYPE_ROLE) {
         foreach (array_keys($this->manager->getRoles()) as $name) {
             $available[$name] = 'role';
         }
     }
     foreach (array_keys($this->manager->getPermissions()) as $name) {
         $available[$name] = $name[0] == '/' ? 'route' : 'permission';
     }
     foreach ($this->manager->getChildren($this->_item->name) as $item) {
         $assigned[$item->name] = $item->type == 1 ? 'role' : ($item->name[0] == '/' ? 'route' : 'permission');
         unset($available[$item->name]);
     }
     unset($available[$this->name]);
     return ['available' => $available, 'assigned' => $assigned];
 }
 /**
  * Remove permissions.
  */
 protected function removePermissions()
 {
     if (!($permissions = ArrayHelper::getValue($this->rbac, 'permissions'))) {
         return;
     }
     $this->_permissions = [];
     foreach ($permissions as $name => $data) {
         if (!($permission = $this->_auth->getPermission($name))) {
             continue;
         }
         if (($childName = ArrayHelper::getValue($data, 'child')) && ($child = $this->_auth->getPermission($childName))) {
             $this->_auth->removeChild($permission, $child);
         }
         if ($ruleClass = ArrayHelper::getValue($data, 'rule')) {
             $rule = Yii::createObject($ruleClass);
             $this->_auth->remove($rule);
         }
         $this->_auth->remove($permission);
         $this->_permissions[$name] = $permission;
     }
 }
Example #14
0
 /**
  * Save rule
  *
  * @return bool
  */
 public function save()
 {
     if ($this->validate()) {
         $class = $this->className;
         if ($this->_item === null) {
             $this->_item = new $class();
             $isNew = true;
             $oldName = false;
         } else {
             $isNew = false;
             $oldName = $this->_item->name;
         }
         $this->_item->name = $this->name;
         if ($isNew) {
             $this->manager->add($this->_item);
         } else {
             $this->manager->update($oldName, $this->_item);
         }
         return true;
     }
     return false;
 }
Example #15
0
 public static function cleanAll()
 {
     self::enSureAuthManager();
     self::$_authManager->removeAll();
 }