Beispiel #1
0
 /**
  * Assign or remove items
  * @param string $action
  * @return array
  */
 public function actionAssign()
 {
     $post = Yii::$app->getRequest()->post();
     $action = $post['action'];
     $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();
             }
         }
     }
     Helper::invalidate();
     Yii::$app->getResponse()->format = 'json';
     return array_merge($this->getRoutes(), ['errors' => $error]);
 }
 /**
  * Assign or revoke assignment to user
  * @param  integer $id
  * @param  string  $action
  * @return mixed
  */
 public function actionAssign($id)
 {
     $post = Yii::$app->getRequest()->post();
     $action = $post['action'];
     $roles = $post['roles'];
     $manager = Yii::$app->getAuthManager();
     $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();
             }
         }
     }
     Helper::invalidate();
     Yii::$app->response->format = 'json';
     return array_merge($this->getItems($id), ['errors' => $error]);
 }
Beispiel #3
0
 /**
  * Assign or remove items
  * @param string $id
  * @param string $action
  * @return array
  */
 public function actionAssign($id)
 {
     $post = Yii::$app->getRequest()->post();
     $action = $post['action'];
     $items = $post['items'];
     $manager = Yii::$app->getAuthManager();
     $parent = $this->type === Item::TYPE_ROLE ? $manager->getRole($id) : $manager->getPermission($id);
     $error = [];
     if ($action == 'assign') {
         foreach ($items as $name) {
             $child = $manager->getPermission($name);
             if ($this->type === Item::TYPE_ROLE && $child === null) {
                 $child = $manager->getRole($name);
             }
             try {
                 $manager->addChild($parent, $child);
             } catch (\Exception $e) {
                 $error[] = $e->getMessage();
             }
         }
     } else {
         foreach ($items as $name) {
             $child = $manager->getPermission($name);
             if ($this->type === Item::TYPE_ROLE && $child === null) {
                 $child = $manager->getRole($name);
             }
             try {
                 $manager->removeChild($parent, $child);
             } catch (\Exception $e) {
                 $error[] = $e->getMessage();
             }
         }
     }
     Helper::invalidate();
     Yii::$app->getResponse()->format = 'json';
     return $this->getItems($id);
 }