Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

yii2

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?

like image 381
Arta09 Avatar asked Sep 08 '15 02:09

Arta09


People also ask

What is word meaning of forbidden?

Definition of forbidden 1 : not permitted or allowed.


2 Answers

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.

like image 118
arogachev Avatar answered Sep 28 '22 15:09

arogachev


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'],
            ],
        ],
    ];
}
like image 25
Faez Ahmed Avatar answered Sep 28 '22 15:09

Faez Ahmed