/** * Creating items object using given config data collection * * @param array $collection * @throws InvalidConfigException */ protected function generateItems(array $collection) { $this->_items = []; $this->_children = []; foreach ($collection as $c) { /** @var ConfigBase $c */ if (!$c instanceof ConfigBase) { throw new \RuntimeException('Invalid configuration source given.'); } foreach ($c->getData() as $key => $value) { if (is_array($value)) { if (count($value) == 1 && isset($value['children']) && is_array($value['children'])) { if (!isset($this->_children[$key])) { $this->_children[$key] = []; } foreach ($value['children'] as $kid) { $this->_children[$key][] = $kid; } continue; } if (!isset($value['type'])) { throw new InvalidConfigException('Please specify type for ' . $key . ' auth item!'); } if ($value['type'] == Item::TYPE_ROLE) { $item = $this->_auth->createRole($key); } elseif ($value['type'] == Item::TYPE_PERMISSION) { $item = $this->_auth->createPermission($key); } else { throw new InvalidConfigException('Wrong type for ' . $key . ' auth item! Type can be ' . Item::TYPE_ROLE . ' (role) or ' . Item::TYPE_PERMISSION . ' (permission).'); } $item->description = isset($value['description']) ? $value['description'] : null; $item->data = isset($value['data']) ? $value['data'] : null; if (isset($value['rule']) && $value['rule'] instanceof Rule) { /** @var Rule $rule */ $rule = $value['rule']; $rule->updatedAt = time(); $item->ruleName = !empty($rule->name) ? $rule->name : $rule::className(); $this->_items[$item->ruleName] = $rule; } if (isset($value['children']) && is_array($value['children'])) { if (!isset($this->_children[$key])) { $this->_children[$key] = []; } foreach ($value['children'] as $kid) { $this->_children[$key][] = $kid; } } } else { $item = $this->_auth->createPermission($key); $item->description = $value; } $this->_items[$key] = $item; } } }
/** * Fill roles from config */ protected function fillRoles() { foreach ($this->getConfig('roles') as $roleConfig) { $existingRole = $this->authManager->getRole($roleConfig['name']); if ($existingRole) { if ($roleConfig['description'] == $existingRole->description) { continue; } $existingRole->description = $roleConfig['description']; $this->authManager->update($roleConfig['name'], $existingRole); } else { $role = $this->authManager->createRole($roleConfig['name']); $role->description = $roleConfig['description']; $this->authManager->add($role); } } }
/** * @param BaseManager $authManger * @return \yii\rbac\Role[] */ public function getRoles(BaseManager $authManger) { return [self::ADMIN => $authManger->createRole(self::ADMIN)]; }