duanlisha2335 2014-09-29 10:42
浏览 20
已采纳

yii中的访问控制列表或基于角色的访问控制

hy i wanna understand what it the best methode to use and how if there is a simple exemple because i can't specify whitch rol to give to witch personne

public function accessRules()
{
    return array(
        array('allow',  // allow authenticated  to perform 'index' and 'view' actions
            'actions'=>array('index','view'),
            'users'=>array('@'),
        ),
        array('allow', // allow authenticated user to perform 'create' and 'update' actions
            'actions'=>array('create','update'),
            'users'=>array('@'),
        ),
        array('allow', // allow admin user to perform 'admin' and 'delete' actions
            'actions'=>array('admin','delete'),
            'users'=>array('admin'),
        ),
        array('deny',  // deny all users
            'users'=>array('*'),
        ),
    );
}
  • 写回答

2条回答 默认 最新

  • duankekan9269 2014-09-29 11:46
    关注

    You can specify based on user roles like for editor you want show only edit user features and for publisher you can provide only access to block and unblock the user and so on. For eg: We have used the below code in our project

    public function accessRules()
        {
            if(isset(Yii::app()->user->role) && Yii::app()->user->role == "superadmin"){
                $arr = array('create','update','admin','delete','block','help','download','forgot');
            } elseif(isset(Yii::app()->user->role) && Yii::app()->user->role == "admin") {
                $arr = array('help','download','forgot');
            } elseif(isset(Yii::app()->user->role) && Yii::app()->user->role == "editor") {
                $arr = array('update');
            } elseif(isset(Yii::app()->user->role) && Yii::app()->user->role == "publisher") {
                $arr = array('block');
            } else {
                $arr = array('');
            }
            return array(
                array('allow', // allow admin user to perform 'admin' and 'delete' actions
                    'actions'=>$arr,
                    'users'=>array('@'),
                ),
                array('deny',  // deny all users
                    'users'=>array('*'),
                ),
            );
        }
    

    Like wise you want to show a page only to registered user and some of the pages for both registered and guest users. We can do the same in the accessRules() function

    To allow all users

    array('allow', 
                'actions'=>array('create','update'),
                'users'=>array('*'),
            ),
    

    To allow only registered users

    array('allow', 
                'actions'=>array('create','update'),
                'users'=>array('@'),
            ),
    

    To create restriction based on usernames

    array('allow',
                'actions'=>array('create','update'),
                'users'=>array('username1','username2'),
            ),
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳