YII2 如何在访问控制中允许 debug/default/toolbar
YII2 How to allow debug/default/toolbar in accesscontrol
我在 web.php 文件中进行了以下配置,以强制用户在使用该应用程序之前先登录。
'as access' => [
'class' => \yii\filters\AccessControl::className(), //AccessControl::className(),
'rules' => [
[
'actions' => ['login', 'error'],
'allow' => true,
],
[
'actions' => ['logout', 'index'], // add all actions to take guest to login page
'allow' => true,
'roles' => ['@'],
],
],
],
但是我在 http://localhost/yii2/debug/default/toolbar?tag=58759099581f2
中收到 Forbidden (#403)
错误
如何在规则中允许这样做?
首先,这个配置是不正确的。这部分:
[
'actions' => ['logout', 'index'], // add all actions to take guest to login page
'allow' => true,
'roles' => ['@'],
],
将另外仅允许 logout
和 index
对经过身份验证的用户执行操作。需要改为:
[
'allow' => true,
'roles' => ['@'],
],
允许访问整个网站。然后,您可以在 AccessControl
或特定控制器的操作中进一步自定义访问权限。因此,在您的案例中,调试并不是唯一被禁止的页面。
我认为它是从 复制粘贴到 SO 上的相关问题。
顺便说一下,调试已经在基本应用程序的应用程序配置中启用:
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
// uncomment the following to add your IP if you are not connecting from localhost.
//'allowedIPs' => ['127.0.0.1', '::1'],
];
// Below Gii is enabled too, code is omitted for brevity
}
因此,当用户通过身份验证后,您将可以毫无问题地访问调试模块。
Note: With this configuration login
and error
actions of every controller are allowed to non-authenticated users. Be careful with that. There is a chance of actions with similar names exist in other controllers.
更新: 其实你可以更进一步,使这个解决方案更灵活 $matchCallback:
'as access' => [
'class' => \yii\filters\AccessControl::className(),
'rules' => [
[
'matchCallback' => function ($rule, $action) {
$allowedControllers = [
'debug/default',
];
$allowedActions = [
'site/login',
'site/error',
];
$isAllowedController = in_array($action->controller->uniqueId, $allowedControllers);
$isAllowedAction = in_array($action->uniqueId, $allowedActions);
return $isAllowedController || $isAllowedAction;
},
'allow' => true,
],
[
'allow' => true,
'roles' => ['@'],
],
],
],
- 将完全允许的控制器放在
$allowedControllers
列表中(如果它在模块内,请在其前面加上模块名称前缀)以完全允许它们(允许所有操作)。
- 在
$allowedActions
列表中放置允许的操作(如果它属于一个模块,则使用控制器名称和模块名称作为前缀)。
这样您就可以在每个页面(包括 login
和 error
)上完全访问本地服务器上的调试模块,这很有用。
这也可以防止来自不同模块/控制器的动作名称重合。
您必须启用工具栏操作 web.php 配置文件:
'rules' => [
[
'actions' => ['login', 'error', 'toolbar'],
'allow' => true,
],
我在 web.php 文件中进行了以下配置,以强制用户在使用该应用程序之前先登录。
'as access' => [
'class' => \yii\filters\AccessControl::className(), //AccessControl::className(),
'rules' => [
[
'actions' => ['login', 'error'],
'allow' => true,
],
[
'actions' => ['logout', 'index'], // add all actions to take guest to login page
'allow' => true,
'roles' => ['@'],
],
],
],
但是我在 http://localhost/yii2/debug/default/toolbar?tag=58759099581f2
Forbidden (#403)
错误
如何在规则中允许这样做?
首先,这个配置是不正确的。这部分:
[
'actions' => ['logout', 'index'], // add all actions to take guest to login page
'allow' => true,
'roles' => ['@'],
],
将另外仅允许 logout
和 index
对经过身份验证的用户执行操作。需要改为:
[
'allow' => true,
'roles' => ['@'],
],
允许访问整个网站。然后,您可以在 AccessControl
或特定控制器的操作中进一步自定义访问权限。因此,在您的案例中,调试并不是唯一被禁止的页面。
我认为它是从
顺便说一下,调试已经在基本应用程序的应用程序配置中启用:
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
// uncomment the following to add your IP if you are not connecting from localhost.
//'allowedIPs' => ['127.0.0.1', '::1'],
];
// Below Gii is enabled too, code is omitted for brevity
}
因此,当用户通过身份验证后,您将可以毫无问题地访问调试模块。
Note: With this configuration
login
anderror
actions of every controller are allowed to non-authenticated users. Be careful with that. There is a chance of actions with similar names exist in other controllers.
更新: 其实你可以更进一步,使这个解决方案更灵活 $matchCallback:
'as access' => [
'class' => \yii\filters\AccessControl::className(),
'rules' => [
[
'matchCallback' => function ($rule, $action) {
$allowedControllers = [
'debug/default',
];
$allowedActions = [
'site/login',
'site/error',
];
$isAllowedController = in_array($action->controller->uniqueId, $allowedControllers);
$isAllowedAction = in_array($action->uniqueId, $allowedActions);
return $isAllowedController || $isAllowedAction;
},
'allow' => true,
],
[
'allow' => true,
'roles' => ['@'],
],
],
],
- 将完全允许的控制器放在
$allowedControllers
列表中(如果它在模块内,请在其前面加上模块名称前缀)以完全允许它们(允许所有操作)。 - 在
$allowedActions
列表中放置允许的操作(如果它属于一个模块,则使用控制器名称和模块名称作为前缀)。
这样您就可以在每个页面(包括 login
和 error
)上完全访问本地服务器上的调试模块,这很有用。
这也可以防止来自不同模块/控制器的动作名称重合。
您必须启用工具栏操作 web.php 配置文件:
'rules' => [
[
'actions' => ['login', 'error', 'toolbar'],
'allow' => true,
],