Yii2 - access check via IP address

I would like to restrict access to the controller to only one IP (or IP list).

What is the correct way to configure? (For example, I would like only IP to 172.19.37.175have access to index.php?r=painel/restrict).

I tried this way:

public function behaviors()
{
    return [
        'access' => [
            'class' => AccessControl::classname(),
            'only'  => ['index'],
            'rules' => [
                [
                    'allow' => true,
                    'roles' => ['?'],
                    'ips' => ['172.19.37.175'],
                ],
            ],
            'denyCallback' => function ($rule, $action) {
            throw new \Exception('You are not allowed to access this page');
                    }                
        ],
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'delete' => ['post'],
            ],
        ],
    ];
}
+4
source share
1 answer

change

'roles' => ['?'] 

to

'roles' => ['@'] 
+3
source

Source: https://habr.com/ru/post/1680886/


All Articles