/** * 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(); } } } MenuHelper::invalidate(); Yii::$app->getResponse()->format = Response::FORMAT_JSON; return ['type' => 'S', 'errors' => $error]; }
/** * Deletes an existing Menu model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionDelete($id) { $this->findModel($id)->delete(); MenuHelper::invalidate(); return $this->redirect(['index']); }
/** * 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']); }
/** * Assign or remove items * @param string $id * @param string $action * @return array */ public function actionAssign() { $post = Yii::$app->getRequest()->post(); $id = $post['id']; $action = $post['action']; $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 = 'json'; return ['type' => 'S', 'errors' => $error]; }
/** * Assign or revoke assignment to user * @param integer $id * @param string $action * @return mixed */ public function actionAssign() { $post = Yii::$app->request->post(); $id = $post['id']; $action = $post['action']; $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 ['type' => 'S', 'errors' => $error]; }