Forbidden (#403) - You are not allowed to perform this action [Yii2]

本小妞迷上赌 提交于 2019-12-07 07:59:04

问题


I've tried to add menu map in backend-side. I use yii2-advanced. This is my “controller” code:

public function actionMap()
{
    return $this->render('map');
}

But, when I try to access it with this url http://localhost/yii2advanced/backend/web/index.php?r=site/map, I've got error message Forbidden (#403) - You are not allowed to perform this action. I don't understand why I got this error message, can anybody help me to fix this problem?


回答1:


It's caused by AccessControl. Most likely the action map is blocked according to access rules. Example of allowing it for all authenticated users:

/**
 * @inheritdoc
 */ 
public function behaviors()
{
    return [
        'access' => [
            'class' => \yii\filters\AccessControl::className(),
            'only' => ['create', 'update'],
            'rules' => [                
                // allow authenticated users
                [
                    'allow' => true,
                    'roles' => ['@'],
                ],
                // everything else is denied
            ],
        ],
    ];
}

Alternatively you can adjust access according to some RBAC roles.




回答2:


In addition to the arogachev's answer: Paste it in your site controller:

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


来源:https://stackoverflow.com/questions/32448409/forbidden-403-you-are-not-allowed-to-perform-this-action-yii2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!