public function search($params) { $query = static::ITEM_TYPE == static::TYPE_ROLE ? Role::find() : Permission::find(); $query->joinWith(['group']); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => \Yii::$app->request->cookies->getValue('_grid_page_size', 20)], 'sort' => ['defaultOrder' => ['created_at' => SORT_DESC]]]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['like', Yii::$app->getModule('user')->auth_item_table . '.name', $this->name])->andFilterWhere(['like', Yii::$app->getModule('user')->auth_item_table . '.description', $this->description])->andFilterWhere([Yii::$app->getModule('user')->auth_item_table . '.group_code' => $this->group_code]); return $dataProvider; }
/** * @param int $id User ID * * @throws \yii\web\NotFoundHttpException * @return string */ public function actionSet($id) { $user = User::findOne($id); if (!$user) { throw new NotFoundHttpException('User not found'); } $permissionsByGroup = []; $permissions = Permission::find()->andWhere([Yii::$app->getModule('user')->auth_item_table . '.name' => array_keys(Permission::getUserPermissions($user->id))])->joinWith('group')->all(); foreach ($permissions as $permission) { $permissionsByGroup[@$permission->group->name][] = $permission; } return $this->renderIsAjax('set', compact('user', 'permissionsByGroup')); }
/** * @param string $id * * @return string */ public function actionView($id) { $item = $this->findModel($id); $routes = Route::find()->asArray()->all(); $permissions = Permission::find()->andWhere(['not in', Yii::$app->getModule('user')->auth_item_table . '.name', [Yii::$app->getModule('user')->commonPermissionName, $id]])->joinWith('group')->all(); $permissionsByGroup = []; foreach ($permissions as $permission) { $permissionsByGroup[@$permission->group->name][] = $permission; } $childRoutes = AuthHelper::getChildrenByType($item->name, AbstractItem::TYPE_ROUTE); $childPermissions = AuthHelper::getChildrenByType($item->name, AbstractItem::TYPE_PERMISSION); return $this->renderIsAjax('view', compact('item', 'childPermissions', 'routes', 'permissionsByGroup', 'childRoutes')); }
/** * @param string $id * * @return string */ public function actionView($id) { $role = $this->findModel($id); $authManager = new DbManager(); $allRoles = Role::find()->asArray()->andWhere('name != :current_name', [':current_name' => $id])->all(); $permissions = Permission::find()->andWhere(Yii::$app->getModule('user')->auth_item_table . '.name != :commonPermissionName', [':commonPermissionName' => Yii::$app->getModule('user')->commonPermissionName])->joinWith('group')->all(); $permissionsByGroup = []; foreach ($permissions as $permission) { $permissionsByGroup[@$permission->group->name][] = $permission; } $childRoles = $authManager->getChildren($role->name); $currentRoutesAndPermissions = AuthHelper::separateRoutesAndPermissions($authManager->getPermissionsByRole($role->name)); $currentPermissions = $currentRoutesAndPermissions->permissions; return $this->renderIsAjax('view', compact('role', 'allRoles', 'childRoles', 'currentPermissions', 'permissionsByGroup')); }
/** * Get permissions assigned to this role or its children * * @param string $roleName * @param bool $asArray * * @return array|Permission[] */ public static function getPermissionsByRole($roleName, $asArray = true) { $rbacPermissions = (new DbManager())->getPermissionsByRole($roleName); $permissionNames = ArrayHelper::map($rbacPermissions, 'name', 'description'); return $asArray ? $permissionNames : Permission::find()->andWhere(['name' => array_keys($permissionNames)])->all(); }