コード例 #1
0
 /**
  * Assign or revoke assignment to user
  * @param  integer $id
  * @param  string  $action
  * @return mixed
  */
 public function actionAssign($id, $action)
 {
     $post = Yii::$app->request->post();
     $roles = $post['roles'];
     $manager = Yii::$app->authManager;
     $error = [];
     if ($action == 'assign') {
         foreach ($roles as $name) {
             try {
                 $item = $manager->getRole($name);
                 $item = $item ?: $manager->getPermission($name);
                 $manager->assign($item, $id);
             } catch (\Exception $exc) {
                 $error[] = $exc->getMessage();
             }
         }
     } else {
         foreach ($roles as $name) {
             try {
                 $item = $manager->getRole($name);
                 $item = $item ?: $manager->getPermission($name);
                 $manager->revoke($item, $id);
             } catch (\Exception $exc) {
                 $error[] = $exc->getMessage();
             }
         }
     }
     MenuHelper::invalidate();
     Yii::$app->response->format = Response::FORMAT_JSON;
     return [$this->actionRoleSearch($id, 'avaliable', $post['search_av']), $this->actionRoleSearch($id, 'assigned', $post['search_asgn']), $error];
 }
コード例 #2
0
 /**
  * Assign or remove items
  * @param string $action
  * @return array
  */
 public function actionAssign($action)
 {
     $post = Yii::$app->getRequest()->post();
     $routes = $post['routes'];
     $manager = Yii::$app->getAuthManager();
     $error = [];
     if ($action == 'assign') {
         $this->saveNew($routes);
     } else {
         foreach ($routes as $route) {
             $child = $manager->getPermission($route);
             try {
                 $manager->remove($child);
             } catch (Exception $exc) {
                 $error[] = $exc->getMessage();
             }
         }
     }
     MenuHelper::invalidate();
     Yii::$app->getResponse()->format = Response::FORMAT_JSON;
     return [$this->actionRouteSearch('new', $post['search_av']), $this->actionRouteSearch('exists', $post['search_asgn']), $error];
 }
コード例 #3
0
 /**
  * Deletes an existing AuthItem model.
  * If deletion is successful, the browser will be redirected to the 'index' page.
  * @param  string $id
  * @return mixed
  */
 public function actionDelete($id)
 {
     $model = $this->findModel($id);
     Yii::$app->authManager->remove($model->item);
     MenuHelper::invalidate();
     return $this->redirect(['index']);
 }
コード例 #4
0
 /**
  * Assign or remove items
  * @param string $id
  * @param string $action
  * @return array
  */
 public function actionAssign($id, $action)
 {
     $post = Yii::$app->getRequest()->post();
     $roles = $post['roles'];
     $manager = Yii::$app->getAuthManager();
     $parent = $manager->getRole($id);
     $error = [];
     if ($action == 'assign') {
         foreach ($roles as $role) {
             $child = $manager->getRole($role);
             $child = $child ?: $manager->getPermission($role);
             try {
                 $manager->addChild($parent, $child);
             } catch (\Exception $e) {
                 $error[] = $e->getMessage();
             }
         }
     } else {
         foreach ($roles as $role) {
             $child = $manager->getRole($role);
             $child = $child ?: $manager->getPermission($role);
             try {
                 $manager->removeChild($parent, $child);
             } catch (\Exception $e) {
                 $error[] = $e->getMessage();
             }
         }
     }
     MenuHelper::invalidate();
     Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
     return [$this->actionRoleSearch($id, 'avaliable', $post['search_av']), $this->actionRoleSearch($id, 'assigned', $post['search_asgn']), $error];
 }