simpman 2011-10-11 16:55:00 8284次浏览 18条回复 0 0 0

今天在写权限的时候遇到问题。我给admin赋与了"cpUserList"但是访问actionList的始终没有权限。 以下是代码:

public function actionList(){
//$this->check('cpUserList');
//print_r(Yii::app()->user->getState());
if(!Yii::app()->user->checkAccess('cpUserList')){
    die('无权限');
}
$criteria = new CDbCriteria();
$count = User::model()->count($criteria);
$pages=new CPagination($count);
$pages->pageSize = 10;
$pages->applyLimit($criteria);
$allUser = User::model()->findAll($criteria);
$role = Authitem::model()->find('type=:type',array(':type'=>2));
$this->render('list',array('allUser'=>$allUser,
    'role'=>$role,
    'pages'=>$pages,
));
}

在数据库 authassignment表中:

itemname	userid	bizrule	data
			system	adsss	NULL	N;
			jin	demo	NULL	N;
			cpUserList	admin	NULL	N;

请教下怎么不行呢? 然后我查源代码,看不懂啊。伤不起!! 在Yii/web/auth/CWebUser.php中:

public function checkAccess($operation,$params=array(),$allowCaching=true)
{
if($allowCaching && $params===array() && isset($this->_access[$operation]))
    return $this->_access[$operation];
else
    return $this->_access[$operation]=Yii::app()->getAuthManager()->checkAccess($operation,$this->getId(),$params);
}

注意红色部分,但是$this->_access[$operation];始终是个空数组。这样能检测权限吗?

  • 回复于 2011-10-11 18:01 举报

    没人帮忙,我自己顶

  • 回复于 2011-10-11 19:08 举报

    这个问题不会弄啊。找舰长过来看看

  • 回复于 2011-10-12 07:33 举报

    accessrules 里面添加list action的权限了吗?

  • 回复于 2011-10-12 08:50 举报

    帮你顶下~等BOSs过来

  • 回复于 2011-10-12 10:04 举报

    accessrules 在哪里?

  • 回复于 2011-10-12 10:15 举报

    Controller 中

    /**
    * @return array action filters
    */
    public function filters()
    {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }
    
    /**
    * Specifies the access control rules.
    * This method is used by the 'accessControl' filter.
    * @return array access control rules
    */
    public function accessRules()
    {
        return array(
            array('allow',  // allow all users to perform 'index' and 'view' actions
                'actions'=>array('list'),
                'users'=>array('*')
            ),
            array('deny',  // deny all users
                'users'=>array('*'),
            ),
        );
    }
    
  • 回复于 2011-10-12 11:37 举报

    rbac 和filer应该属于两种独立验证方式。仍然没有搞定。期待指教

  • 回复于 2011-10-12 11:40 举报

    张迪说的对!

  • 回复于 2011-10-12 11:42 举报
    <?php
    /*
     * Created on 2011-10-12
     *
     * To change the template for this generated file go to
     * Window - Preferences - PHPeclipse - PHP - Code Templates
     */
    class AuthController extends CController{
    /**
    * @return array action filters
    */
    public function filters()
    {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }
    
    /**
    * Specifies the access control rules.
    * This method is used by the 'accessControl' filter.
    * @return array access control rules
    */
    public function accessRules()
    {
        return array(
            array('allow',  // allow all users to perform 'index' and 'view' actions
                'actions'=>array('show'),
                'users'=>array('*')
            ),
        );
    }
    
    public function actionTest(){
        $auth=Yii::app()->authManager;
        $auth->createOperation("postop",'postpost');
        $auth->createTask("postta","posts");
        $auth->createRole("postro","post");
        $auth->addItemChild("postro","postop");
        $auth->assign("postro",'demo');
        $auth->save();
    }
    public function actionShow(){
        echo Yii::app()->user->name;
        if(Yii::app()->user->checkAccess("postop")){
            echo 'Yes';
        }else{
            echo 'No';
        }
    }
    }
    ?>
    
  • 回复于 2011-10-12 12:10 举报

    你给当前用户赋权限了吗?

  • 回复于 2011-10-12 12:16 举报

    赋权限了啊。是不是还要配置哪个地方啊?
    再补充下。照10#的代码。我先执行了 test,然后又执行show方法。用的demo登录了。可是还打印没有权限

  • 回复于 2011-10-12 12:33 举报

    在你的 test 里面最后加上
    $auth->save();

  • 回复于 2011-10-12 13:07 举报

    还是不行啊。真怀疑我人品问题

  • 回复于 2011-10-12 15:08 举报

    终于搞定了。不蛋疼了。谢谢大家。

    public function filters()
    public function accessRules()
    

    和rbac应该是独立的两种验证机制。
    我刚才是由于 创建item的时候写了“bizrule”。而验证的时候没有写这个参数。
    反面教材,大家注意点吧

  • 回复于 2011-10-12 17:15 举报

    checkAccess 验证的源码;有不懂的看源码吧。
    framework/web/auth/CPhpAuthManager.php#64

    public function checkAccess($itemName,$userId,$params=array())
    {
        if(!isset($this->_items[$itemName]))
            return false;
            $item=$this->_items[$itemName];
            Yii::trace('Checking permission "'.$item->getName().'"','system.web.auth.CPhpAuthManager');
            if($this->executeBizRule($item->getBizRule(),$params,$item->getData()))
            {
                if(in_array($itemName,$this->defaultRoles))
                    return true;
                    if(isset($this->_assignments[$userId][$itemName]))
                    {
                        $assignment=$this->_assignments[$userId[$itemName];	
                        if($this->executeBizRule($assignment->getBizRule(),$params,$assignment->getData()))
                            return true;
                    }
                    foreach($this->_children as $parentName=>$children)
                    {
                        if(isset($children[$itemName]) && $this->checkAccess($parentName,$userId,$params))
                            return true;
                    }
            }
        return false;
    }
    

    貌似两种可以通过,一种是defaultRole,一种按照rbac体系检验,中间还用到了递归

  • 回复于 2012-03-01 10:54 举报

    还是看不懂啊

  • 回复于 2012-07-17 11:12 举报

    学习了!

  • 回复于 2012-07-17 11:13 举报

    楼主能把你修改过的代码提出来吗?

您需要登录后才可以回复。登录 | 立即注册