Yii 1.1: mongodbauthmanager

Extends CPhpAuthmanager by saving/loading the data in a mongodb collection

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.


Yii 1.1.5

Extension yiimongodbsuite


  • Install and configure yiimongodbsuite.
  • Copy CMongoDbAuthManager.php to /protected/components
  • Install and use CMongoDbAuthManager the same way as you would use CPhpAuthManager.
            ... configure yiimongodbsuite ....
            //'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');
//switch to a TestEnviroment
//switch back to 'default' and load data
//or $auth->switchConfig(); $auth->init();
//switch to 'TestEnviroment' and load data

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


  • Bugfixes (see comments)
  • Support for multiple configurations

Total 6 comments

#12725 report it
Davuz at 2013/04/08 11:39am
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 set defaultRoles in main config:

        '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)

#12720 report it
Joblo at 2013/04/08 04:59am
Illegal offset type

Is the CMongoDbAuthManager initialized?

Better use:

//$auth = new CMongoDbAuthManager();
//$auth->init(); //load the data

Besides: Where did you assign a userId to the role 'user'?

#12707 report it
Davuz at 2013/04/07 04:57am
I'm getting "PHP warning: Illegal offset type"

1// Config

    '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');

and here is result in db result in db

3// Checking access in controller

public function actionUpdate($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: Ilegal Type Error

#2940 report it
stutteringp0et at 2011/02/28 12:25am
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!

#2939 report it
Joblo at 2011/02/27 08:01pm

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.

#2922 report it
stutteringp0et at 2011/02/26 12:59am
A few errors, and causes errors
$this->_id = $result->_id;

should be

$this->_id = $result['_id'];


$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.

Leave a comment

Please to leave your comment.

Create extension