For more details and usage information on DbManager, see the guide article on security authorization.
Since: 2.0
Author: Qiang Xue (qiang.xue@gmail.com)
Inheritance: extends yii\base\Component, implements yii\rbac\ManagerInterface
 /**
  * Initializes the application component.
  * This method overrides the parent implementation by establishing the database connection.
  */
 public function init()
 {
     parent::init();
     $this->db = Instance::ensure($this->db, Connection::className());
     $this->db->getCollection($this->itemTable)->createIndex(['name' => 1], ['unique' => true]);
     $this->db->getCollection($this->ruleTable)->createIndex(['name' => 1], ['unique' => true]);
 }
Example #2
0
 /**
  * @param array $assignments
  * $assignments = [
  *  'user_id' => ['role_1', 'role_2', 'role_3'],
  *  '1' => ['admin', 'user'],
  *  '2' => ['client', 'user'],
  *  '3' => ['manager', 'seller', 'support', 'user'],
  * ];
  * @throws \yii\base\InvalidConfigException
  */
 protected function restoreAssignments($assignments)
 {
     $useCache = $this->useCache === true;
     foreach ($assignments as $user_id => $items) {
         if (!empty($this->forceAssign)) {
             if (!is_array($this->forceAssign)) {
                 $this->forceAssign = (array) $this->forceAssign;
             }
             foreach ($this->forceAssign as $role) {
                 $this->authManager->assign(RbacFactory::Role($role), $user_id);
                 echo sprintf('    > role `%s` force assigned to user id: %s.', $role, $user_id) . PHP_EOL;
             }
         }
         if (!empty($items)) {
             foreach ($items as $item) {
                 $item = isset($this->assignmentsMap[$item]) ? $this->assignmentsMap[$item] : $item;
                 if (empty($item) || in_array($item, (array) $this->forceAssign, true)) {
                     continue;
                 }
                 $this->authManager->assign(RbacFactory::Role($item), $user_id);
                 echo sprintf('    > role `%s` assigned to user id: %s.', $item, $user_id) . PHP_EOL;
             }
         }
     }
     if ($useCache) {
         if ($this->cache->exists('assignments-0')) {
             $this->cacheIterator(function ($key) {
                 $this->cache->delete($key);
             });
         }
     }
 }
Example #3
0
 /**
  * Updates RBAC configuration using current RBAC manager component.
  *
  * @param array $collection
  * @return bool
  * @throws InvalidConfigException
  */
 public function update(array $collection)
 {
     $this->_auth = Yii::$app->authManager;
     $this->generateItems($collection);
     $this->manageItems();
     $this->manageRelations();
     if ($this->_auth instanceof \yii\rbac\DbManager) {
         $this->_auth->invalidateCache();
     }
 }
Example #4
0
 /**
  * Fill relations between roles and permissions
  */
 protected function fillChildren()
 {
     foreach ($this->getConfig('children') as $roleName => $permissionsNames) {
         $role = $this->authManager->getRole($roleName);
         foreach ($permissionsNames as $permissionName) {
             $permission = $this->authManager->getPermission($permissionName);
             if (!$this->authManager->hasChild($role, $permission)) {
                 $this->authManager->addChild($role, $permission);
             }
         }
     }
 }
 /**
  * Initializes the application component.
  * This method overrides the parent implementation by establishing the database connection.
  */
 public function init()
 {
     parent::init();
     $this->db = Instance::ensure($this->db, Connection::className());
 }
Example #6
0
 /**
  * Initializes the application component.
  * This method overrides parent implementation by loading the authorization data
  * from PHP script.
  */
 public function init()
 {
     parent::init();
     $this->itemFile = Yii::getAlias($this->itemFile);
     $this->assignmentFile = Yii::getAlias($this->assignmentFile);
     $this->ruleFile = Yii::getAlias($this->ruleFile);
     $this->load();
 }
Example #7
0
 /**
  * Initializes the application component.
  * This method overrides the parent implementation by establishing the database connection.
  */
 public function init()
 {
     parent::init();
     $this->db = Instance::ensure($this->db, Connection::className());
     if ($this->cache !== null) {
         $this->cache = Instance::ensure($this->cache, Cache::className());
     }
 }
Example #8
0
 /**
  * Initializes the application component.
  * This method overrides parent implementation by loading the authorization data
  * from PHP script.
  */
 public function init()
 {
     parent::init();
     $this->db = Instance::ensure($this->db, Connection::className());
     if ($this->enableCaching) {
         $this->cache = Instance::ensure($this->cache, Cache::className());
     } else {
         $this->cache = null;
     }
 }
 /**
  * @param BaseManager $authManger
  * @return \yii\rbac\Role[]
  */
 public function getRoles(BaseManager $authManger)
 {
     return [self::ADMIN => $authManger->createRole(self::ADMIN)];
 }
Example #10
0
 /**
  * Initialise component.
  */
 public function init()
 {
     parent::init();
     try {
         $this->db = Instance::ensure($this->db, Connection::className());
         if ($this->db->driverName === 'mysql') {
             $this->setTableOptions('CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB');
         }
         $this->authManager = Instance::ensure($this->authManager, BaseManager::className());
     } catch (Exception $e) {
         Yii::error([$e->getName(), $e->getMessage()], __METHOD__);
     }
 }
Example #11
0
 /**
  * Initializes the application component.
  * This method overrides parent implementation by loading the authorization data
  * from PHP script.
  */
 public function init()
 {
     parent::init();
     $this->authFile = Yii::getAlias($this->authFile);
     $this->load();
 }