权限检查错误,checkaccess [ 新手入门 ]
今天在写权限的时候遇到问题。我给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];始终是个空数组。这样能检测权限吗?
共 18 条回复
-
-
-
-
-
-
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('*'), ), ); }
-
-
-
<?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'; } } } ?>
-
-
-
-
-
-
checkAccess 验证的源码;有不懂的看源码吧。
framework/web/auth/CPhpAuthManager.php#64public 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体系检验,中间还用到了递归
-
-
jungsagacity 回复于 2012-07-17 11:12 举报
学习了!
-
jungsagacity 回复于 2012-07-17 11:13 举报
楼主能把你修改过的代码提出来吗?
simpman
注册时间:2011-09-16
最后登录:2021-11-04
在线时长:6小时32分
最后登录:2021-11-04
在线时长:6小时32分
- 粉丝1
- 金钱225
- 威望0
- 积分285