Esempio n. 1
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);
             });
         }
     }
 }