示例#1
0
 public function actionView($id, $term = '')
 {
     $model = new RoleAuthForm();
     $model->roles = [$id => $id];
     $model->setScenario('auth');
     if ($model->load(Yii::$app->request->post()) && $model->validate()) {
         foreach ($model->getAttributes() as $key => $value) {
             if (empty($value)) {
                 $model->{$key} = [];
             }
         }
         $roles = ArrayHelper::merge($model->roles, $model->routes, $model->permissions);
         $manager = Yii::$app->getAuthManager();
         $parent = $manager->getRole($id);
         $manager->removeChildren($parent);
         foreach ($roles as $role) {
             if ($role == $id) {
                 continue;
             }
             $child = $manager->getRole($role);
             $child = $child ?: $manager->getPermission($role);
             $manager->addChild($parent, $child);
         }
         Yii::$app->session->setFlash('success', "修改 {$id} 成功");
         $this->redirect(['index']);
     }
     $result = ['Roles' => [], 'Permissions' => [], 'Routes' => []];
     $authManager = Yii::$app->authManager;
     $children = array_keys($authManager->getChildren($id));
     $children[] = $id;
     foreach ($authManager->getRoles() as $name => $role) {
         if (empty($term) or strpos($name, $term) !== false) {
             $result['Roles'][$name] = $name;
         }
     }
     foreach ($authManager->getPermissions() as $name => $role) {
         if (empty($term) or strpos($name, $term) !== false) {
             $result[$name[0] === '/' ? 'Routes' : 'Permissions'][$name] = $role->description;
         }
     }
     foreach ($authManager->getChildren($id) as $name => $child) {
         if (empty($term) or strpos($name, $term) !== false) {
             if ($child->type == Item::TYPE_ROLE) {
                 $model->roles[$name] = $name;
             } else {
                 if ($name[0] === '/') {
                     $model->routes[$name] = $name;
                 } else {
                     $model->permissions[$name] = $name;
                 }
             }
         }
     }
     return $this->render('view', ['result' => $result, 'model' => $model]);
 }
 public function actionView($id, $term = '')
 {
     $model = new RoleAuthForm();
     $model->roles = [$id => $id];
     $model->setScenario('auth');
     if ($model->load(Yii::$app->request->post()) && $model->validate()) {
         foreach ($model->getAttributes() as $key => $value) {
             if (empty($value)) {
                 $model->{$key} = [];
             }
         }
         $roles = ArrayHelper::merge($model->roles, $model->routes, $model->permissions, $model->app, $model->platforms);
         $manager = Yii::$app->getAuthManager();
         $parent = $manager->getRole($id);
         $manager->removeChildren($parent);
         foreach ($roles as $role) {
             if ($role == $id) {
                 continue;
             }
             $child = $manager->getRole($role);
             $child = $child ?: $manager->getPermission($role);
             $manager->addChild($parent, $child);
         }
         Yii::$app->session->setFlash('success', "修改 {$id} 权限成功");
         $this->redirect(['index']);
     }
     $result = ['Roles' => [], 'Permissions' => [], 'Routes' => [], 'App' => [], 'Platforms' => []];
     $authManager = Yii::$app->authManager;
     $children = array_keys($authManager->getChildren($id));
     if (empty($children)) {
         throw new ErrorException('cannot find id ' . $id);
     }
     $children[] = $id;
     foreach ($authManager->getRoles() as $name => $role) {
         if (empty($term) or strpos($name, $term) !== false) {
             $result['Roles'][$name] = $name;
             //角色权限
         }
     }
     foreach ($authManager->getPermissions() as $name => $role) {
         if (empty($term) or strpos($name, $term) !== false) {
             if (substr($name, 0, 3) === 'app') {
                 //游戏权限
                 $result['App'][$name] = $role->description;
             } elseif ($name[0] === '/') {
                 //路由权限
                 $result['Routes'][$name] = $role->description;
             } elseif (substr($name, 0, 8) === 'platform') {
                 //地区权限
                 $result['Platforms'][$name] = $role->description;
             } else {
                 //权限组
                 $result['Permissions'][$name] = $role->description;
             }
         }
     }
     foreach ($authManager->getChildren($id) as $name => $child) {
         if (empty($term) or strpos($name, $term) !== false) {
             if ($child->type == Item::TYPE_ROLE) {
                 $model->roles[$name] = $name;
             } else {
                 if (substr($name, 0, 3) === 'app') {
                     $model->app[$name] = $name;
                 } elseif ($name[0] === '/') {
                     $model->routes[$name] = $name;
                 } elseif (substr($name, 0, 8) === 'platform') {
                     $model->platforms[$name] = $name;
                 } else {
                     $model->permissions = $name;
                 }
             }
         }
     }
     $routes = Tools::serializeRoutes($result['Routes']);
     return $this->render('view', ['result' => $result, 'model' => $model, 'routes' => $routes]);
 }