RBAC: AuthManager doesn't create any records

Hi All

I’m trying to setup RBAC. First I added the AuthManager component to main.php:


'authManager'=>array(

                        'class'=>'CDbAuthManager',

                        'connectionID'=>'db',

                ),



Next, I added a command to yiic (all this stuff comes from the book: Agile Webdevelopment with Yii) Anyway, I created the file RbacCommand.php in command/shell




<?php 


class RbacCommand extends CConsoleCommand {

	private $_authManager;

        ..... 

       public function run($args) {

                         ......


			//first we need to remove all operations, roles, child relationship and assignments

			$this->_authManager->clearAll();


			//create the lowest level operations for users 

			$this->_authManager->createOperation("createUser","create a new user"); 

			$this->_authManager->createOperation("readUser","read user profile information"); 

			$this->_authManager->createOperation("updateUser","update a users information"); 

			$this->_authManager->createOperation("deleteUser","remove a user from a project");


			//create the lowest level operations for projects 

			$this->_authManager->createOperation("createProject","create a new project"); 

			$this->_authManager->createOperation("readProject","read project information"); 

			$this->_authManager->createOperation("updateProject","update project information"); 

			$this->_authManager->createOperation("deleteProject","delete a project"); 


			//create the lowest level operations for issues

			$this->_authManager->createOperation("createIssue","create a new issue");

			$this->_authManager->createOperation("readIssue","read issue information");

			$this->_authManager->createOperation("updateIssue","update issue information");

			$this->_authManager->createOperation("deleteIssue","delete an issue from a project");


			//create the reader role and add the appropriate permissions as children to this role

			$role=$this->_authManager->createRole("reader"); 

			$role->addChild("readUser"); 

			$role->addChild("readProject"); 

			$role->addChild("readIssue");


			//create the member role, and add the appropriate permissions, as well as the reader role itself, as children

			$role=$this->_authManager->createRole("member"); 

			$role->addChild("reader"); 

			$role->addChild("createIssue"); 

			$role->addChild("updateIssue"); 

			$role->addChild("deleteIssue");


			//create the owner role, and add the appropriate permissions, as well as both the reader and member roles as children 

			$role=$this->_authManager->createRole("owner"); 

			$role->addChild("reader"); 

			$role->addChild("member"); 

			$role->addChild("createUser"); 

			$role->addChild("updateUser"); 

			$role->addChild("deleteUser"); 

			$role->addChild("createProject"); 

			$role->addChild("updateProject"); 

			$role->addChild("deleteProject");

                        ...

           }

}



When I run yiic:


./yiic shell config/main.php 

or


./protected/yiic shell

(I guess there’s no difference between the two)

Anyway, it runs ok, and completes. But when I check the database tables AuthAssignment, AuthItem and AuthItemChild (because all my tables are prefixed with ‘tbl_’ I’ve also created these 3 tables with the prefix, but they’re also empty)

Any suggestions why there’s no DB action here (I know the DB stuff is setup correctly because it works for the web-application)?

thanks a lot

cheers

Luca

I found out that the class of the authManager is CPhpAuthManager and not CDbAuthManager as configured in main.php

I get the feeling that it ignores the setup of main.php

Here is the content of the components configuration (main.php):


        // application components

        'components'=>array(

                'user'=>array(

                        // enable cookie-based authentication

                        'allowAutoLogin'=>true,

                ),

                // uncomment the following to enable URLs in path-format

                /*

                'urlManager'=>array(

                        'urlFormat'=>'path',

                        'rules'=>array(

                                '<controller:\w+>/<id:\d+>'=>'<controller>/view',

                                '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',

                                '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

                        ),

                ),

                */

                //'db'=>array(

                        //'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',

                //),

                // uncomment the following to use a MySQL database


                'db'=>array(

                        'connectionString' => 'mysql:host=localhost;dbname=trackstar',

                        'emulatePrepare' => true,

                        'username' => 'root',

                        'password' => '***********',

                        'charset' => 'utf8',

                ),

                'errorHandler'=>array(

                        // use 'site/error' action to display errors

                'errorAction'=>'site/error',

                'authManager'=>array(

                        'class'=>'CDbAuthManager',

                        'connectionID'=>'db',

                ),

        ),



Any suggestions ?

To use different tables use this parameters: assignmentTable itemChildTable itemTable, check the doc for more info.

At this point the tables are not my biggest problem!

For some reason the configuration is ignored, because I get an instance of CPhpAuthManager.

Here is a test I did:




[me@tux protected]$ ./yiic shell config/main.php 

Yii Interactive Tool v1.1 (based on Yii v1.1.6)

Please type 'help' for help. Type 'exit' to quit.

>> echo get_class(Yii::app()->authManager) ;

CPhpAuthManager

>> 



Any suggestions how to debug this ?

UPDATE: I noticed in the file framework/web/CWebApplication.php (function registerCoreComponents), which contains some default component configuration. If I modify this code like


 ......

'authManager'=>array(

                                //'class'=>'CPhpAuthManager',

                                'class'=>'CDbAuthManager',

                                'connectionID'=>'db',

),



it all works. Anyway, I get the impression my authManager code in main.php is ignored. Any help would be appreciated!

cheers

I think that the configuration for console commands must be done in protected\config\console.php instead of main.php

You’re absolutely right, it ignores the main.php config by default.

To solve the problem, just add the config file when you call "yiic shell":

e.g. PathToYourWebapp>PathToYii/framework/yiic shell PathToYourWebapp/protected/config/main.php

… then proceed as usual.

At least this works for Yii 1.1.6

I didn’t had any such issues with older releases.

typing that in managed to get the yiic shell help to come up for me and it told me to enter an entry script so I just typed in




yiic shell index.php



since I was inside my trackstar root and that works too. I think it’s because the config files are included that way.

Is it just me or is your authManager declaration within the errorHandler declaration. It should be:




	'components'=>array(

                'user'=>array(

                        // enable cookie-based authentication

                        'allowAutoLogin'=>true,

                ),


   			...


                'errorHandler'=>array(

                        // use 'site/error' action to display errors

                	'errorAction'=>'site/error',

		);

                'authManager'=>array(

                        'class'=>'CDbAuthManager',

                        'connectionID'=>'db',

                ),


        ),