Exemple #1
0
 /**
  * Logs in a user using the provided username and password.
  *
  * @return boolean whether the user is logged in successfully
  */
 public function login()
 {
     if ($this->validate() && Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0)) {
         $user = Yii::$app->user;
         if ($user->identity->className() == Tech::className() && $user->identity->reload_roles) {
             MenuHelper::invalidate();
             $user->identity->reload_roles = false;
             $user->identity->save(false);
         }
         return true;
     } else {
         return false;
     }
 }
Exemple #2
0
 /**
  * @inheritdoc
  */
 public function afterSave()
 {
     $authManager = Yii::$app->authManager;
     // Get a list of the user's current roles (if any)
     $oldRoles = array_keys($authManager->getRolesByUser($this->contact_id));
     // Remove entries for old roles that were unchecked
     foreach (array_diff($oldRoles, $this->roles) as $roleName) {
         $authManager->revoke($authManager->getRole($roleName), $this->contact_id);
     }
     // Insert entries for newly selected roles
     foreach (array_diff($this->roles, $oldRoles) as $roleName) {
         $authManager->assign($authManager->getRole($roleName), $this->contact_id);
     }
     MenuHelper::invalidate();
 }
 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];
 }
 /**
  * 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];
 }
 /**
  * Assign or revoke assignment to user
  * @param  integer $id
  * @param  string  $action
  * @return type
  */
 public function actionAssign($id, $action)
 {
     $post = Yii::$app->request->post();
     $roles = $post['roles'];
     $manager = Yii::$app->authManager;
     $error = [];
     if ($action == 'assign') {
         foreach ($roles as $role) {
             try {
                 $manager->assign($manager->getRole($role), $id);
             } catch (\Exception $exc) {
                 $error[] = $exc->getMessage();
             }
         }
     } else {
         foreach ($roles as $role) {
             try {
                 $manager->revoke($manager->getRole($role), $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];
 }
 /**
  * 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 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];
 }
 /**
  * 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];
 }
 /**
  * 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];
 }
Exemple #10
0
 /**
  * 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']);
 }