Yii RBAC make Users update profile by himself
我正在尝试使用mongodbauthmanager进行此操作。我在
请告诉我怎么了?
1 //配置
1 2 3 4 | 'authManager'=>array( 'class' =>'CMongoDbAuthManager', 'showErrors' => true, ), |
2 //在db
中创建身份验证项
1 2 3 4 5 6 7 8 9 | $auth = new CMongoDbAuthManager(); $bizRule = 'return Yii::app()->user->id==$params["User"]->_id;'; $auth->createTask('updateSelf', 'update own information', $bizRule); //I had tried with $auth->createOperation() but they has the same error $role = $auth->createRole('user'); $role->addChild('updateSelf'); $auth->save(); |
这是db中的结果
结果进入数据库http://i.minus.com/iIpXoBlDxaEfo.png
** 3 //正在检查控制器中的访问权限**-更新代码和错误
1 2 3 4 5 6 7 8 9 10 | public function actionUpdate($id) { $model=$this->loadModel($id); $params = array('User'=>$model); if (!Yii::app()->user->checkAccess('updateSelf', Yii::app()->user->id,$params) ) { throw new CHttpException(403, 'You are not authorized to perform this action'); } //another statement ... } |
4 //出现错误:
致命错误:无法将MongoId类型的对象用作F:\\\\ Data \\\\ 03中的数组。 Lab \\\\ www \\\\ yii \\\\ framework \\\\ web \\\\ auth \\\\ CAuthManager.php(150):第1行上的eval()代码
解决的问题
根据
0 //首先,使用defaultRoles
配置authManager
1 2 3 4 5 | 'authManager'=>array( 'class'=>'CMongoDbAuthManager', 'showErrors' => true, 'defaultRoles'=> array('user'),//important, this line help we don't need assign role for every user manually ), |
1 //修复UserIdentity类中的保存ID
1 2 3 4 5 6 7 8 9 10 11 12 | class UserIdentity extends CUserIdentity { private $_id; //... public function authenticate() { //... $this->_id = (string)$user->_id;//force $this save _id by string, not MongoId object //... } //... } |
2 //修复$ bizrule的其他物品
($ bizrule将由
1 2 | //use _id as string, not MongoId object $bizRule = 'return Yii::app()->user->id==(string)$params["User"]->_id;'; |
3 //和用户检查访问授权
1 2 3 4 5 6 7 8 9 10 11 12 | public function actionUpdate($id){ /** * @var User $model */ $model=$this->loadModel($id); $params = array('User'=>$model); if (!Yii::app()->user->checkAccess('updateSelf', $params) ) { throw new CHttpException(403, 'You are not authorized to perform this action'); } //... } |
4 //完成,现在我们可以使用checkAccess:D
首先,您对
http://www.yiiframework.com/doc/api/1.1/CWebUser#checkAccess-detail
现在,
http://www.yiiframework.com/doc/api/1.1/CPhpAuthManager#checkAccess-detail
您在扩展页面上发布的堆栈跟踪显示以下行给出了问题:
1 | if(isset($this->_assignments[$userId][$itemName])) |
因此,对于非法偏移量,我们有两种可能性:
由于
(作为一个附带说明,您的堆栈跟踪揭示了此错误的周围代码这一事实还表明,至少对于
在这一点上,我可能已经猜到问题是指定的用户未登录,因此
因为第二个参数实际上是
我不熟悉这种类型的对象,所以抬起头来:
http://php.net/manual/zh/class.mongoid.php
长话短说,您需要强制
示例:
1 | $this->_id = (string)$User->_id; |
根据您的
然后,将您的
但是请注意,我没有使用此扩展名,并且在执行以下操作时应该可以解决此问题,但是如果扩展名依赖于