AccessControl.php 1.19 KB
<?php

namespace backend\components\filters;

use Yii;
use yii\helpers\ArrayHelper;
use yii\web\ForbiddenHttpException;

/**
 *
 * @author wuxiao
 * @date 2016-8-19
 */
class AccessControl extends \yii\filters\AccessControl
{
    
    /**
     * @var array a list of access rule objects or configuration arrays for creating the rule objects.
     * If a rule is specified via a configuration array, it will be merged with [[ruleConfig]] first
     * before it is used for creating the rule object.
     * @see ruleConfig
     */
    public $rules = [
        [
            'allow' => true,
            'roles' => ['@'],
        ],
    ];
    
    public function init()
    {
        $rules = [];
        $this->rules = ArrayHelper::merge($rules, $this->rules);
        
        $this->denyCallback = function ($rule, $action) {
            if (Yii::$app->user->getIsGuest()) {
                Yii::$app->user->loginRequired();
            } else {
                throw new ForbiddenHttpException(Yii::t('yii', 'You are not allowed to perform this action.'));
            }
        };
        parent::init();
    }
    
    public function beforeAction($action)
    {
        return parent::beforeAction($action);
    }
}