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.
Yii 1.1.5
Extension yiimongodbsuite
'components'=>array( 'mongodb'=>array( ... configure yiimongodbsuite .... ), 'authManager'=>array( 'class'=>'CMongoDbAuthManager', //'mongoConnectionId'=>'mongodb', (default) //'authFile' => 'mongodb_authmanager' (default, is now the collection name) ), ),
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 ....
Total 6 comments
Thank @Joblo, I realize I'm not assign users. Instead assign user-role for every
user, one by one manually, I setdefaultRolesin main config: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)
Is the CMongoDbAuthManager initialized?
Better use:
Besides: Where did you assign a userId to the role 'user'?
1// Config
2// Create auth items in db
and here is result in db
3// Checking access in controller
4// Getting error:
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!
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.
should be
and
should be
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.
Leave a comment
Please login to leave your comment.