D
D
des1roer2016-03-25 07:45:06
Yii
des1roer, 2016-03-25 07:45:06

Yii 2 ban to module?

how to restrict access to a module? so I inherited all the controllers from the base one and wrote in it

<?php

namespace app\modules\video\controllers;

use yii\web\Controller;

class DefaultController extends Controller {

    public function actionIndex()
    {
        return $this->render('index');
    }

    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['*'],
                'rules' => [
                    [
                        'actions' => ['*'],
                        'allow' => true,
                        'roles' => ['@'],
                    //  'roles' => ['admin'],
                    ],
                ],
            ],
        ];
    }

}

but access to everything is open

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
des1roer, 2016-03-25
@des1roer


you can add to \modules\video\Module.php

public function beforeAction($action)
    {

        if (!parent::beforeAction($action))
        {
            return false;
        }

        if (!Yii::$app->user->isGuest)
        {
            return true;
        }
        else
        {
            Yii::$app->getResponse()->redirect(Yii::$app->getHomeUrl());
            //для перестраховки вернем false
            return false;
        }
    }

Option 2
\config\web.php
$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'language' => 'ru',
    'timezone' => 'Asia/Yekaterinburg',
    'modules' => [
        'user' => [
            'class' => 'amnah\yii2\user\Module',
        ],
        'video' => [
            'class' => 'app\modules\video\Module',
            'as access' => [ // if you need to set access
                'class' => 'yii\filters\AccessControl',
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@'] // all auth users
                    ],
                ]
            ],
        ],
    ],

A
AlikDex, 2016-03-25
@AlikDex

I don't remember exactly right now. But something like:

'rules' => [
                    [
                        'actions' => ['login', 'error'], // тут список разрешенных экшенов
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                    [
                        'actions' => ['*'],
                        'allow' => true,
                        'roles' => ['admin'],
                    ],
                ],

You specify the role and what it is allowed to do by enumerating the list of methods.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question