2018-04-22 18:55:29 2709次浏览 0条回答 0 悬赏 10 金钱

min.php配置行为

main.php

'as myBehavior' => [
    'class' => 'backend\components\MyBehavior',
    'allowActions' => [
        'site/error','site/login',
    ]
],

backend\components\MyBehavior.php

namespace backend\components;

use Yii;
use yii\base\ActionFilter;

class MyBehavior extends ActionFilter
{
    public $allowActions = [];
    public function beforeAction($action)
    {
        $currentRequestRoute = $action->getUniqueId();
        var_dump(Yii::$app->user->can('/' . $currentRequestRoute));
        var_dump('/' . $currentRequestRoute);
        if (!Yii::$app->user->can('/' . $currentRequestRoute)) {
            throw new \yii\web\ForbiddenHttpException('没有权限,请与管理员联系ss');
        }
        return true;
    }

}

错误页面

bool(false) string(11) "/site/error"
An Error occurred while handling another error:
yii\web\ForbiddenHttpException: 没有权限,请与管理员联系ss in E:\gitClone\yii-adv-rbac\backend\components\MyBehavior.php:22
Stack trace:
#0 E:\gitClone\yii-adv-rbac\vendor\yiisoft\yii2\base\ActionFilter.php(75): backend\components\MyBehavior->beforeAction(Object(yii\web\ErrorAction))
#1 [internal function]: yii\base\ActionFilter->beforeFilter(Object(yii\base\ActionEvent))
#2 E:\gitClone\yii-adv-rbac\vendor\yiisoft\yii2\base\Component.php(557): call_user_func(Array, Object(yii\base\ActionEvent))
#3 E:\gitClone\yii-adv-rbac\vendor\yiisoft\yii2\base\Module.php(682): yii\base\Component->trigger('beforeAction', Object(yii\base\ActionEvent))
#4 E:\gitClone\yii-adv-rbac\vendor\yiisoft\yii2\base\Controller.php(145): yii\base\Module->beforeAction(Object(yii\web\ErrorAction))
#5 E:\gitClone\yii-adv-rbac\vendor\yiisoft\yii2\base\Module.php(528): yii\base\Controller->runAction('error', Array)
#6 E:\gitClone\yii-adv-rbac\vendor\yiisoft\yii2\web\ErrorHandler.php(97): yii\base\Module->runAction('site/error')
#7 E:\gitClone\yii-adv-rbac\vendor\yiisoft\yii2\base\ErrorHandler.php(111): yii\web\ErrorHandler->renderException(Object(yii\web\ForbiddenHttpException))
#8 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\web\ForbiddenHttpException))
#9 {main}
Previous exception:
yii\web\ForbiddenHttpException: 没有权限,请与管理员联系ss in E:\gitClone\yii-adv-rbac\backend\components\MyBehavior.php:22
Stack trace:
#0 E:\gitClone\yii-adv-rbac\vendor\yiisoft\yii2\base\ActionFilter.php(75): backend\components\MyBehavior->beforeAction(Object(yii\base\InlineAction))
#1 [internal function]: yii\base\ActionFilter->beforeFilter(Object(yii\base\ActionEvent))
#2 E:\gitClone\yii-adv-rbac\vendor\yiisoft\yii2\base\Component.php(557): call_user_func(Array, Object(yii\base\ActionEvent))
#3 E:\gitClone\yii-adv-rbac\vendor\yiisoft\yii2\base\Module.php(682): yii\base\Component->trigger('beforeAction', Object(yii\base\ActionEvent))
#4 E:\gitClone\yii-adv-rbac\vendor\yiisoft\yii2\base\Controller.php(145): yii\base\Module->beforeAction(Object(yii\base\InlineAction))
#5 E:\gitClone\yii-adv-rbac\vendor\yiisoft\yii2\base\Module.php(528): yii\base\Controller->runAction('create', Array)
#6 E:\gitClone\yii-adv-rbac\vendor\yiisoft\yii2\web\Application.php(103): yii\base\Module->runAction('blog/create', Array)
#7 E:\gitClone\yii-adv-rbac\vendor\yiisoft\yii2\base\Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))
#8 E:\gitClone\yii-adv-rbac\backend\web\index.php(17): yii\base\Application->run()
#9 {main}

控制器中配置行为(出现正确的权限提示页面)

 public function behaviors()
{
    return [
        //附加行为
        'myBehavior' => \backend\components\MyBehavior::className(),

        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'delete' => ['POST','GET'],
            ],
        ],
    ];
}

正确提示页面

X3}HSUZMT_3RB~UL5355`QD.png

补充于 2018-04-24 10:33

在配置文件里是全局的行为,当第一次访问blog/ndex页面没有权限,抛出错误然后访问site/error,,site/error又走一遍行为,但是没有去过滤site/error所以报错了

    没有找到数据。
您需要登录后才可以回答。登录 | 立即注册
wayneluo
助理

wayneluo

注册时间:2017-11-22
最后登录:2018-04-24
在线时长:10小时44分
  • 粉丝0
  • 金钱15
  • 威望0
  • 积分115

热门问题