【问题标题】:Yii2 controller not allowing another action to guest userYii2 控制器不允许对访客用户执行其他操作
【发布时间】:2016-05-10 10:45:02
【问题描述】:

我正在使用 Yii2 Advance 应用程序。我有SiteController.php,我有loginlogoutindex 之类的操作。这里login 用于访客用户,indexlogout 用于登录用户。现在我又创建了一个名为reset 的操作来提供忘记密码功能。但每当我尝试调用 reset 操作时,它会将我重定向回登录页面。

以下是我的控制器:

namespace backend\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use common\models\LoginForm;
use yii\filters\VerbFilter;

/**
 * Site controller
 */
class SiteController extends Controller {

    /**
     * @inheritdoc
     */
    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['login', 'error'],
                        'allow' => true
                    ],
                    [
                        'actions' => ['logout', 'index'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                    [
                        'allow' => true,
                        'actions' => ['reset'],
                        'roles' => ['?'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post', 'get'],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions() {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    public function actionIndex() {
        return $this->render('index');
    }

    public function actionLogin() {
        $this->layout = 'guest';

        if (!\Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();

        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        } else {
            return $this->render('login', [
                        'model' => $model,
            ]);
        }
    }

    public function actionLogout() {
        Yii::$app->user->logout();
        return $this->goHome();
    }

    public function actionReset() {        
        return $this->render('reset');
    }

}

我已经为它添加了正确的roles & behaviours,但它仍然无法正常工作。我也尝试添加更多操作,但实际上它不允许我呈现除登录之外的任何其他操作。

任何帮助将不胜感激。

【问题讨论】:

  • 删除'roles' => ['?'],
  • 'actions' => ['login', 'error', 'reset'],
  • @MuhammadShahzad:都试过了。但不工作。

标签: php yii2 yii2-advanced-app yii2-rbac


【解决方案1】:

自己解决了:)

vendor/yiisoft/yii2/web/Controller.php 中有条件,即

if($this->action->id != 'login') { .. }

我把它改成了

if($this->action->id != 'login' && $this->action->id != 'reset') { .. }

public function beforeAction($action)
{ 
    // If user is guest then redirect to login page
    if($this->action->id != 'login' && $this->action->id != 'reset') {
        if(!isset($_SESSION['__id'])) {
           $this->redirect(array('site/login'));   
       }
    }
    if (parent::beforeAction($action)) {
        if ($this->enableCsrfValidation && Yii::$app->getErrorHandler()->exception === null && !Yii::$app->getRequest()->validateCsrfToken()) {
            throw new BadRequestHttpException(Yii::t('yii', 'Unable to verify your data submission.'));
        }
        return true;
    } else {
        return false;
    }
}

它的工作原理。

【讨论】:

  • 这就是问题
  • 您可以在站点控制器beforeAction()中执行此操作
【解决方案2】:

试试这个:

<?php
namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;

    class SiteController extends Controller
    {
        /**
         * @inheritdoc
         */
        public function behaviors()
        {
            return [
                'access' => [
                    'class' => AccessControl::className(),
                    'rules' => [
                        [
                            'actions' => ['login', 'error', 'reset'],
                            'allow' => true,
                        ],
                        [
                            'actions' => ['logout', 'index'],
                            'allow' => true,
                            'roles' => ['@'],
                        ],

                    ],
                ],
                'verbs' => [
                    'class' => VerbFilter::className(),
                    'actions' => [
                        'logout' => ['post'],
                    ],
                ],
            ];
        }

 /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }
       public function actionReset(){
            //die('You are here');
            return $this->render('reset');
        }

    ...
    }

我的重置视图代码:

<?php

/* @var $this yii\web\View */
/* @var $name string */
/* @var $message string */
/* @var $exception Exception */

use yii\helpers\Html;

$this->title = "Reset";
?>
<div class="site-index">

    <h1><?= Html::encode($this->title) ?></h1>

    <div class="alert alert-success">
        Hi here you are without login
    </div>


</div>

【讨论】:

  • 它与dieexit 一起工作,但是当我通过return $this-&gt;render('reset'); 渲染时,它会重定向到登录页面
  • site/reset 视图只有简单的&lt;h1&gt;here&lt;/h1&gt; 标签。
  • 请检查我的答案,让我知道它是否正确?
  • 它在我身边工作,我已经更新了我的答案。请告诉我您使用的是哪个 yii2 版本?我的是 2.0.8
  • 我的 Yii.2 版本是 2.0.3
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-16
  • 2012-07-01
  • 2020-01-11
相关资源
最近更新 更多