assign() public method

Assigns a role to a user.
public assign ( Role $role, string | integer $userId ) : Assignment
$role Role
$userId string | integer the user ID (see [[\yii\web\User::id]])
return Assignment the role assignment information.
Esempio n. 1
0
 /**
  * @param $userId
  */
 public static function initAdminAuth($userId)
 {
     self::enSureAuthManager();
     /**
      * @param                            $data
      * @param \yii\rbac\ManagerInterface $authManager
      * @param null                       $parent
      */
     function addItem($data, $authManager, $parent = null)
     {
         foreach ($data as $d) {
             $item = $authManager->createPermission($d['action']);
             $item->description = $d['name'];
             $authManager->add($item);
             $authManager->addChild($parent, $item);
             if (isset($d['children'])) {
                 addItem($d['children'], $authManager, $item);
             }
         }
     }
     \App::me()->db->transaction(function () use($userId) {
         self::cleanAll();
         $role = self::$_authManager->createRole('admin');
         $role->description = '超级管理员';
         self::$_authManager->add($role);
         addItem(self::all(), self::$_authManager, $role);
         self::$_authManager->assign($role, $userId);
     });
 }
Esempio n. 2
0
 /**
  * Assign a roles and permissions to the user.
  *
  * @param array $items
  *
  * @return int number of successful grand
  */
 public function assign($items)
 {
     foreach ($items as $name) {
         $item = $this->manager->getRole($name);
         $item = $item ?: $this->manager->getPermission($name);
         $this->manager->assign($item, $this->userId);
     }
     return true;
 }
Esempio n. 3
0
 protected function prepareData()
 {
     $rule = new AuthorRule();
     $this->auth->add($rule);
     $createPost = $this->auth->createPermission('createPost');
     $createPost->description = 'create a post';
     $this->auth->add($createPost);
     $readPost = $this->auth->createPermission('readPost');
     $readPost->description = 'read a post';
     $this->auth->add($readPost);
     $updatePost = $this->auth->createPermission('updatePost');
     $updatePost->description = 'update a post';
     $updatePost->ruleName = $rule->name;
     $this->auth->add($updatePost);
     $updateAnyPost = $this->auth->createPermission('updateAnyPost');
     $updateAnyPost->description = 'update any post';
     $this->auth->add($updateAnyPost);
     $reader = $this->auth->createRole('reader');
     $this->auth->add($reader);
     $this->auth->addChild($reader, $readPost);
     $author = $this->auth->createRole('author');
     $this->auth->add($author);
     $this->auth->addChild($author, $createPost);
     $this->auth->addChild($author, $updatePost);
     $this->auth->addChild($author, $reader);
     $admin = $this->auth->createRole('admin');
     $this->auth->add($admin);
     $this->auth->addChild($admin, $author);
     $this->auth->addChild($admin, $updateAnyPost);
     $this->auth->assign($reader, 'reader A');
     $this->auth->assign($author, 'author B');
     $this->auth->assign($admin, 'admin C');
 }
 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'));
 }
Esempio n. 5
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)));
 }