如何设置Yii中不同控制器的操作访问权限?

时间:2021-04-30 23:59:08

This is my access rule :

这是我的访问规则:

public function accessRules() {
           return array(
                array('allow',
                    'controllers'=> array('Cont1', 'Cont2', 'Contt3'),
                    'actions'    =>  ??// I don't know what should I write here
                ),
                array('deny', // deny all users
                    'users' => array('*'),
                ),
            );
}

Lets suppose that:

让我们假设:

  • in Cont1 I have function cont1func1, cont1func2
  • 在Cont1中我有函数cont1func1,cont1func2

  • in Cont2 I have function cont2func1, cont2func2, cont2func3
  • 在Cont2我有函数cont2func1,cont2func2,cont2func3

  • List in Cont3 I have function cont3func1, cont3func2
  • Cont3中的列表我有函数cont3func1,cont3func2

I want to give access for user:

我想为用户提供访问权限:

  • UserX to cont1 and function cont1func1

    UserX到cont1和函数cont1func1

  • UserY to cont1 having function cont1funct1, cont1func2 and to cont2 having function cont2func1, cont2func2, cont2func3

    UserY到cont1具有函数cont1funct1,cont1func2和cont2具有函数cont2func1,cont2func2,cont2func3

  • UserZ to cont1 all function and cont3 all function
  • UserZ到cont1所有功能和cont3所有功能

How can I do that ? How do I define my actions ? Thx

我怎样才能做到这一点 ?我如何定义我的行为?谢谢

UPDATE:

public function accessRules() {



        $controllers = array(' '); $actions = array('index');

        if (Yii::app()->user->getState("State1") == true){ 
            array_push($controllers, 'cont1','cont2');
            array_push($actions, 'cont1funct1', 'cont1funct2');
        }
        if (Yii::app()->user->getState("State2") == true){ 
            array_push($controllers, 'cont1');
        }
        if (Yii::app()->user->getState("State3") == true){ 
            array_push($controllers, 'cont1');
            array_push($actions, 'cont1funct3','cont1funct4','cont1funct5','cont1funct6');
        }
        if (Yii::app()->user->getState("State4") == true){
            array_push($controllers, 'cont1');
        } 
        if (Yii::app()->user->getState("State5") == true){ D
            array_push($controllers, 'cont1');
        }
        if (Yii::app()->user->getState("State1") == false  && Yii::app()->user->getState("State2") == false && Yii::app()->user->getState("State3") == false  && Yii::app()->user->getState("State4") == false  && Yii::app()->user->getState("State5") == false){
            return array(
                array('deny', // deny all users
                    'users' => array('*'),
                ),
            );
        }else{
            $controllers = array_unique($controllers); //remove duplicates
            $actions = array_unique($actions);//remove duplicates
            return array(
                array('allow',
                    'controllers'=> $controllers,
                    'actions'    => $actions 
                ),
                array('deny', // deny all users
                    'users' => array('*'),
                ),
            );
        }
    }

This is what I achieved yet. But I don't know what will happens when I am starting adding function from another controller . Can u help me with this ?

这就是我所取得的成就。但我不知道当我开始从另一个控制器添加功能时会发生什么。你可以帮帮我吗?

1 个解决方案

#1


Function accessRules() should be inside every controller. So something like this will be helpful:

函数accessRules()应该在每个控制器中。所以这样的事情会有所帮助:

public function accessRules() {
       return array(
            array('allow',
                'actions'=>  array("create", "update", ...),
                'users'=>array("@")
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
}

#1


Function accessRules() should be inside every controller. So something like this will be helpful:

函数accessRules()应该在每个控制器中。所以这样的事情会有所帮助:

public function accessRules() {
       return array(
            array('allow',
                'actions'=>  array("create", "update", ...),
                'users'=>array("@")
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
}