Yii2 Dektrium用户,所有操作和所有控制器都需要登录

nukf8bse  于 2022-11-09  发布在  其他
关注(0)|答案(2)|浏览(211)

为了使所有控制器和操作都需要登录,我按照Yii2 require all Controller and Action to login中的说明进行了操作,并将下面的代码添加到web.php中

'as beforeRequest' => [
        'class' => 'yii\filters\AccessControl',
        'rules' => [
                [
                        'allow' => true,
                        'actions' => ['login', 'forgot'],
                ],
                [
                        'allow' => true,
                        'roles' => ['@'],
                ],

        ],
        'denyCallback' => function () {
                return Yii::$app->response->redirect(['user/login']);
        },
],

但问题是,所有其他的操作,如忘记密码被重定向到登录页面,我想从登录所需的条件中排除用户/忘记路线.请帮助!
谢谢

bjg7j2ky

bjg7j2ky1#

我知道我晚了3年,但它可能是有用的其他人搜索这个答案:)
在config/web.php文件中

$config => [
/* ... */
    'as AccessBehavior' => [
    'class' => 'app\components\AccessBehavior',
    'allowedRoutes' => [
        '/auth/register',
        '/auth/forgot',
        '/auth/resend',
    ],
    'redirectUri' => '/auth/login',
],
/* ... */

然后在根项目中创建一个“components”文件夹,并使用以下代码创建一个“components\AccessBehavior.php”文件:

<?php

namespace app\components;

use Yii;
use yii\base\Behavior;
use yii\console\Controller;
use yii\helpers\Url;

class AccessBehavior extends Behavior
{
    protected $redirectUri;
    protected $allowedRoutes = [];
    protected $allowedUrls = [];

    public function setRedirectUri($uri)
    {
        $this->redirectUri = $uri;
    }    
    public function setAllowedRoutes(array $routes)
    {
        if (count($routes)) {
            foreach ($routes as $route) {
                $this->allowedUrls[] = Url::to($route);
            }
        }
        $this->allowedRoutes = $routes;
    }
    public function init()
    {
        if (empty($this->redirectUri)) {
            $this->redirectUri = Yii::$app->getUser()->loginUrl;
        }
    }   
    private function removeParams()
    {
        //enabled pretty url
        if (strpos(Yii::$app->getRequest()->url, "?") === false) 
        {
            $requestUrl = explode('/', Yii::$app->getRequest()->url);
            $params = array_values(Yii::$app->getRequest()->queryParams);
            $result = implode('/', array_diff($requestUrl, $params));
        } 
        else 
        {//not enabled pretty url
            $result = explode("?", \Yii::$app->getRequest()->url);
        }
        return $result;
    }    
    public function events()
    {
        return [Controller::EVENT_BEFORE_ACTION => 'beforeAction'];
    }

    public function beforeAction()
    {
        $requestUrl = $this->removeParams();
        if (Yii::$app->user->isGuest)
        {
            if ($requestUrl !== Url::to($this->redirectUri) && !in_array($requestUrl, $this->allowedUrls))
            {
                Yii::$app->getResponse()->redirect($this->redirectUri)->send();  
                exit(0);
            }
        }
    }
}

这段代码只是检查用户是否登录并检查请求的路由。如果guest用户正在访问允许的路由(您可以在config中添加允许的路由),则什么也不做,否则将用户重定向到登录页面:)在上面的代码中,我将dektrium前缀路由设置为“auth”。当然,在允许的路由中,您必须设置您实际使用的路由,以使用户注册,确认,更改密码。

06odsfpq

06odsfpq2#

还没有测试过,但应该能用。

'denyCallback'=>function() {
          if($this->action->id == 'forgot')
                return Yii::$app->response->redirect(['whatever/whatever']);
          else
                return Yii::$app->response->redirect(['user/login']);
},...

相关问题