Yii2 Errors/No view using AccessControl "as beforeRequest" with custom roles

时间:2017-06-12 16:48:55

标签: yii2

I use following code:

'as beforeRequest' => [
    'class' => 'yii\filters\AccessControl',
    'rules' => [
        [
            'actions' => ['login', 'forgot', 'error'], // guests can just login and nothing else
            'allow' => true,
            'roles' => ['?'],
        ],
        [
            'allow' => true,
            'roles' => ['admin', 'access'],
        ],
    ],
],

Using this code causes following errors (if a user is logged in, but have not the role admin or access):

An Error occurred while handling another error:
yii\web\ForbiddenHttpException: Sie dürfen diese Aktion nicht durchführen. in /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/filters/AccessControl.php:154
Stack trace:
0 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/filters/AccessControl.php(137): yii\filters\AccessControl->denyAccess(Object(yii\web\User))
1 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/base/ActionFilter.php(75): yii\filters\AccessControl->beforeAction(Object(yii\web\ErrorAction))
2 [internal function]: yii\base\ActionFilter->beforeFilter(Object(yii\base\ActionEvent))
3 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/base/Component.php(545): call_user_func(Array, Object(yii\base\ActionEvent))
4 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/base/Module.php(676): yii\base\Component->trigger('beforeAction', Object(yii\base\ActionEvent))
5 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/base/Controller.php(144): yii\base\Module->beforeAction(Object(yii\web\ErrorAction))
6 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/base/Module.php(523): yii\base\Controller->runAction('error', Array)
7 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/web/ErrorHandler.php(97): yii\base\Module->runAction('site/error')
8 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/base/ErrorHandler.php(111): yii\web\ErrorHandler->renderException(Object(yii\web\ForbiddenHttpException))
9 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\web\ForbiddenHttpException))
10 {main}
Previous exception:
yii\web\ForbiddenHttpException: Sie dürfen diese Aktion nicht durchführen. in /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/filters/AccessControl.php:154
Stack trace:
0 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/filters/AccessControl.php(137): yii\filters\AccessControl->denyAccess(Object(yii\web\User))
1 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/base/ActionFilter.php(75): yii\filters\AccessControl->beforeAction(Object(yii\base\InlineAction))
2 [internal function]: yii\base\ActionFilter->beforeFilter(Object(yii\base\ActionEvent))
3 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/base/Component.php(545): call_user_func(Array, Object(yii\base\ActionEvent))
4 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/base/Module.php(676): yii\base\Component->trigger('beforeAction', Object(yii\base\ActionEvent))
5 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/base/Controller.php(144): yii\base\Module->beforeAction(Object(yii\base\InlineAction))
6 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/base/Module.php(523): yii\base\Controller->runAction('', Array)
7 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/web/Application.php(102): yii\base\Module->runAction('', Array)
8 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/base/Application.php(380): yii\web\Application->handleRequest(Object(yii\web\Request))
9 /Applications/XAMPP/xamppfiles/htdocs/cms/backend/web/index.php(17): yii\base\Application->run()
10 {main}

If I use ['@'] instead of ['admin', 'access'] it works fine. That means that site/error is rendering probably.
But using ['admin, 'access'] causes errors (in text form) and site/error is NOT rendering probably. Why?

admin is a defined rule and access is a permission. Everything works great using in controllers.

Does anyone have an idea what's wrong here?
Thank you!

1 个答案:

答案 0 :(得分:0)

您似乎遇到错误,应用程序尝试显示错误页面。但是用户也无法访问错误页面。