G
G
good_beginer2018-05-27 23:27:32
Yii
good_beginer, 2018-05-27 23:27:32

How to properly configure Access Control for guest?

I want to open access to the main (index.php) but at the same time block all other addresses.
Can it be done in yii2??
Thinking about if(Yii::app()->user->isGuest) $this->redirect(Yii::app()->getModule('user')->loginUrl); but this is just a last resort.

<?php
return [
    'aliases' => [
        '@bower' => '@vendor/bower-asset',
        '@npm'   => '@vendor/npm-asset',
    ],
    'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
    'name' =>  "Hello_World",
    'modules' => [
        'admin' => [
            'class' => 'mdm\admin\Module',
        ],
     ],

    'as access' => [
        'class' => 'mdm\admin\components\AccessControl',
        'allowActions' => [
            'site/*',
        ],
    ],

    'components' => [
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'user' => [
            'identityClass' => 'mdm\admin\models\User',
            'loginUrl' => ['admin/user/login'],
            'class' => 'yii\web\User'
        ],

        'authManager' => [
            'class' => 'yii\rbac\PhpManager',
        ],
        'authManager' => [
            'class' => 'yii\rbac\PhpManager',
            'defaultRoles' => ['user', 'admin'],
            'itemFile' => '@common/config/rbac/items.php',
            'assignmentFile' => '@common/config/rbac/assignments.php',
            'ruleFile' => '@common/config/rbac/rules.php'
        ],
        'formatter' => [
            'class' => 'yii\i18n\Formatter',
            'dateFormat' => 'dd-MM-yyyy',
            'decimalSeparator' => ',',
            'thousandSeparator' => ' ',
       ],
       
    ],

];

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2018-05-29
@webinar

config in your hands and events to help:

'on beforeAction' => function($event){
      Yii::$app->controller->attachBehavior('',[
          'class' => \yii\filters\AccessControl::className(),
          'except' => ['login','error','index'],
          'rules' => [
              [
                  'allow' => true,
                  'roles' => ['@'],
              ],
          ],
      ]);
    },

Please note that there are 3 actions. If you do not open login and error for everyone, then there will be sadness.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question