Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 - Validate AccessControl over IP

Tags:

yii2

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

What is the right way to configure? (Example, I would like only IP 172.19.37.175 to have 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'],
            ],
        ],
    ];
}
like image 307
gugoan Avatar asked Jul 06 '17 12:07

gugoan


1 Answers

change

'roles' => ['?'] 

to

'roles' => ['@'] 
like image 156
gugoan Avatar answered Oct 01 '22 23:10

gugoan