NOTE: This extension is now part of the directmongosuite ¶
A quick hack with a few lines of code. CMongoDbAuthManager extends CPhpAuthManager: 'loadFromFile' and 'saveToFile' will load/save to a mongodb collection
New: Added support for multiple configurations in the collection. It is like you would work with different authfiles in CPhpAuthmanager.
Requirements ¶
Yii 1.1.5
Extension yiimongodbsuite
Usage ¶
- Install and configure yiimongodbsuite.
- Copy CMongoDbAuthManager.php to /protected/components
- Install and use CMongoDbAuthManager the same way as you would use CPhpAuthManager.
'components'=>array(
'mongodb'=>array(
... configure yiimongodbsuite ....
),
'authManager'=>array(
'class'=>'CMongoDbAuthManager',
//'mongoConnectionId'=>'mongodb', (default)
//'authFile' => 'mongodb_authmanager' (default, is now the collection name)
),
),
Usage ¶
See documentation role-based-access-control
$auth = new CMongoDbAuthManager();
$auth->createOperation('createPost','create a post');
$auth->createOperation('readPost','read a post');
...
$role=$auth->createRole('admin');
$role->addChild('createPost');
...
$auth->save();
//switch to a TestEnviroment
$auth->switchConfig('TestEnviroment');
$role=$auth->createRole('reader');
$role->addChild('readPost');
...
$auth->save();
//switch back to 'default' and load data
$auth->switchConfig(null,true);
//or $auth->switchConfig(); $auth->init();
var_dump($auth);
//switch to 'TestEnviroment' and load data
$auth->switchConfig('TestEnviroment',true);
var_dump($auth);
Note: Run the above code to save authdata one time, and NOT for every request.
Use checkAccess, assign users... the same way as you would do with CPhpAuthManager Don't forget to load the data from mongodb by calling init();
$auth = new CMongoDbAuthManager('TestEnviroment');
$auth->init(); //load the data
....
Changelog ¶
- Bugfixes (see comments)
- Support for multiple configurations
A few errors, and causes errors
$this->_id = $result->_id;
should be
$this->_id = $result['_id'];
and
$this->_id = $data->_id;
should be
$this->_id = $data['_id'];
When I get past those, I get other errors in framework/base/CComponent.php(128)
User does not have a method named "tableName".
EMongoDocument models use getCollectionName(), not tableName()
If I create one, then I CComponent wants "getDbConnection"
There must be more than what you've posted.
Updated
Thanks for your remarks, you are right regarding to _id and ['_id'].
But the other problem with your 'User' is not a problem of the authmanager:
An authmanager works independent of user models by managing authitems (roles, operations ...).
You can assign users to a role ... but the authmanger only works with the user id, not with the model.
You can take a look at my upcoming first release of my extension mongocms (hope in a few days) to see how users/roles/permissions can be managed in the mongodb.
yeah, 'User' model
Turns out, when you try to validate - 'exists' is an inherited method that assumes a standard DB connection. Override in place and all is well.
Thanks for your hints!
I'm getting "PHP warning: Illegal offset type"
1// Config
'authManager'=>array( 'class' =>'CMongoDbAuthManager', ),
2// Create auth items in db
$auth = new CMongoDbAuthManager(); $bizRule = 'return Yii::app()->user->id==$params["User"]->_id;'; $auth->createTask('updateSelf', 'update own information', $bizRule); $role = $auth->createRole('user'); $role->addChild('updateSelf'); $auth->save();
and here is result in db
3// Checking access in controller
public function actionUpdate($id) { $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'); } //another statement ... }
4// Getting error:
Illegal offset type
Is the CMongoDbAuthManager initialized?
Better use:
$auth=Yii::app()->authManager; //or //$auth = new CMongoDbAuthManager(); //$auth->init(); //load the data
Besides: Where did you assign a userId to the role 'user'?
$auth->assign('user',Yii::app()->user->id);
Re: #12720 and #12717
Thank @Joblo, I realize I'm not assign users. Instead assign user-role for every
user
, one by one manually, I setdefaultRoles
in main config:'authManager'=>array( 'class'=>'CMongoDbAuthManager', 'defaultRoles'=> array('user'), ),
And and some small problem, like as checkAccess need $userId is a string, we have force Yii::app()->user->id, $params['User']->_id to string.
See here for detail (StackOverflow.com)
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.