Returns a value indicating whether the user is a guest (not authenticated).
public getIsGuest ( ) : boolean | ||
return | boolean | whether the current user is a guest. |
/** * check the permission, if we rewrite and controller, the controller id and module id is not changed * @param \yii\base\Action $action * @param \yii\web\User $user * @param \yii\web\Request $request * @return bool */ public function matchActionAccess($action, $user, $request) { if ($user->getIsGuest()) { return false; } /** @var \core\auth\Module $authModule */ $authModule = \Yii::$app->getModule('core_auth'); foreach ($authModule->getAdmins() as $key => $admin) { if ($user->getIdentity()->username == $admin['username']) { return true; } } if ($action->controller->module instanceof Application) { $key = 'default' . '_' . $action->controller->id . '_' . $action->id; } else { $key = $action->getUniqueId(); $key = explode('/', $key); array_shift($key); $key = implode('_', $key); } $key = lcfirst(implode('', array_map(function ($k) { return ucfirst($k); }, explode('-', $key)))); return $user->can($key, $this->params); }
/** * Denies the access of the user. * The default implementation will redirect the user to the login page if he is a guest; * if the user is already logged, a 403 HTTP exception will be thrown. * @param User $user the current user * @throws ForbiddenHttpException if the user is already logged in. */ protected function denyAccess($user) { if ($user->getIsGuest()) { $user->loginRequired(); } else { throw new ForbiddenHttpException(Yii::t('yii', 'You are not allowed to perform this action.')); } }
/** * @param string $default * * @return string */ protected function getIdentifier($default) { $id = $default; if ($this->user instanceof User && !$this->user->getIsGuest()) { $id = $this->user->getId(); } return $id; }
/** * Denies the access of the user. * The default implementation will redirect the user to the login page if he is a guest; * if the user is already logged, a 403 HTTP exception will be thrown. * @param Yii\web\User $user the current user * @throws Yii\web\ForbiddenHttpException if the user is already logged in. */ protected function denyAccess($user) { if ($user->getIsGuest()) { $user->loginRequired(); } else { $this->ajaxOnly(); } }
/** * @param \yii\web\User $user * @return bool */ protected function matchRole($user) { if (empty($this->roles)) { return true; } foreach ($this->roles as $role) { if ($role === '?') { if ($user->getIsGuest()) { return true; } } elseif ($role === '@') { if (!$user->getIsGuest()) { return true; } } elseif (!$user->getIsGuest() && $role === $user->identity->role) { return true; } } return false; }
/** * 拒绝访问 * @param \yii\web\User $user * @throws ForbiddenHttpException */ protected function denyAccess($user) { if ($user->getIsGuest()) { Yii::$app->getSession()->setFlash('danger', Yii::t('common', 'You have not login, please login first.')); $user->loginRequired(); } else { //检查权限是否有配置 // $this->checkAuthItem(); throw new ForbiddenHttpException(Yii::t('yii', 'You are not allowed to perform this action.')); } }
/** * Denies the access of the user. * The default implementation will redirect the user to the login page if he is a guest; * if the user is already logged, a 403 HTTP exception will be thrown. * @param User $user the current user * @throws ForbiddenHttpException if the user is already logged in. */ protected function denyAccess($user) { $rr = new RequestResponse(); if ($user->getIsGuest()) { $authUrl = UrlHelper::construct("admin/auth")->setCurrentRef()->enableAdmin()->createUrl(); if (\Yii::$app->request->isAjax && !\Yii::$app->request->isPjax) { $rr->redirect = $authUrl; return (array) $rr; } else { \Yii::$app->getResponse()->redirect($authUrl); } } else { throw new ForbiddenHttpException(\Yii::t('yii', \Yii::t('app', 'You are not allowed to perform this action.'))); } }
/** * 覆写方法 * @param \yii\web\User $user * @return bool|void */ protected function matchRole($user) { //如果没有给点roles,那么是所有的角色都可以用 if (count($this->roles) === 0) { return true; } //分析所有配资了得roles,在controller的behaviors里面配置 foreach ($this->roles as $role) { //?代表游客 if ($role === "?") { return true; } elseif (!$user->getIsGuest() && $role === $user->identity->role) { //判断其他的权限 return true; } } return false; }
/** * Denies the access of the user. * The default implementation will redirect the user to the login page if he is a guest; * if the user is already logged, a 403 HTTP exception will be thrown. * @param User $user the current user * @throws ForbiddenHttpException if the user is already logged in. */ protected function denyAccess($user) { if ($user->getIsGuest()) { $user->loginRequired(); } else { throw new ForbiddenHttpException(Yii::t('yii', 'No esás autorizado para realizar eta acción.')); } }
/** * @param User $user the user object * @return boolean whether the rule applies to the role */ protected function matchRole($user) { if (empty($this->roles)) { return true; } foreach ($this->roles as $role) { if ($role === '?' && $user->getIsGuest()) { return true; } elseif ($role === '@' && !$user->getIsGuest()) { return true; } elseif ($user->checkAccess($role)) { return true; } } return false; }