【发布时间】:2013-04-07 09:13:21
【问题描述】:
我正在尝试使用mongodbauthmanager 来执行此操作。我在Usage 部分一步一步地进行操作,但最后我收到PHP 警告:非法偏移类型。在克隆到 SO 之前,我已经在 Yii Extension 发布了这个问题:
请告诉我有什么问题?
1// 配置
'authManager'=>array(
'class' =>'CMongoDbAuthManager',
'showErrors' => true,
),
2//在db中创建认证项
$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 中的结果 result in db http://i.minus.com/iIpXoBlDxaEfo.png
**3// 检查控制器中的访问权限** - 更新代码和错误
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) : eval()'d 代码在第 1 行
已解决的问题
根据@Willem Renzema 的回答,我解决了我的问题。现在,我在这里更新,希望对遇到此错误的人有用。
0//首先,用defaultRoles配置authManager
'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
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 将由eval() 在checkAccess 中运行)
//use _id as string, not MongoId object
$bizRule = 'return Yii::app()->user->id==(string)$params["User"]->_id;';
3//和用户检查访问权限
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
【问题讨论】:
-
看
checkAccess的签名,特别是第二个参数。 -
是的,我输入 Yii::app()->user->id 并获取
Fatal error: Cannot use object of type MongoId as array in \www\yii\framework\web\auth\CAuthManager.php(150) : eval()'d code on line 1。
-
那么您更新后的代码是什么样的?从错误消息看来,您似乎也更改了
$params... -
我改一行:Yii::app()->user->checkAccess('updateSelf', Yii::app()->user->id, $params)
-
抱歉,我不明白为什么会发生这个问题。它在您的
bizRule的eval期间失败了,所以也许您可以在其中执行var_dump($params)或类似的操作。 (使用最新的代码和错误更新您的帖子,以便其他人可以看到状态。)
标签: mongodb yii authorization yii-extensions rbac