/** * Fill permissions from config */ protected function fillPermissions() { foreach ($this->getConfig('permissions') as $permissionConfig) { $existingPermission = $this->authManager->getPermission($permissionConfig['name']); if ($existingPermission && $permissionConfig['name'] == $existingPermission->name) { $permission = $existingPermission; $permission->description = $permissionConfig['description']; $this->authManager->update($permissionConfig['name'], $permission); } else { $permission = $this->authManager->createPermission($permissionConfig['name']); $permission->description = $permissionConfig['description']; $this->authManager->add($permission); } if (isset($permissionConfig['rule'])) { /* @var $rule \yii\rbac\Rule */ $rule = new $permissionConfig['rule'](); $existingRule = $this->authManager->getRule($rule->name); if (!$permission->ruleName && !$existingRule) { $this->authManager->add($rule); } $permission->ruleName = $rule->name; $this->authManager->update($permissionConfig['name'], $permission); } } }
/** * 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; } } }