[Resolved] RBAC not saving to the database

I’m following an RBAC tutorial for adding roles/operations/etc… to a Yii project using the console and all seems fine but nothing is being saved to the database as expected. I have copied the schema for the three required tables from framework/web/auth/schema-mysql.sql

Sample code I’m trying:




    $auth=Yii::app()->authManager;

    $role=$auth->createRole('owner');

    $auth->createOperation('createProject', 'Create a new project');

    $role->addChild('createProject');



I have exited and re-entered the console to ensure it has any changes made. I have also ensured that protected/config/console.php contains the correct db info.

Is there a final method that should be called to save to the database that I’m missing?

Edit: Also I’m confused at why in the AuthAssignment table in the example database schema that the userid field is set to a string. Is it significant that I changed this to an integer?

Where are you calling the code from e.g path? What message are you getting or log? Post your code

The shell command is being run from the webroot and I know it is processing run() as it prints my successful message at the end. I am not getting any error messages in the console. I don’t believe any logs are being written to in this case.

A trial piece of code I’m trying is:




 public function run($args)

 {

    if(($this->_authManager=Yii::app()->authManager)!==null)

    {

        echo "This command will create new roles\n";

        echo 'Would you like to continue? [y|n]: ';

 

        if(!strcasecmp(trim(fgets(STDIN)), 'y'))

        {

             $this->_authManager->clearAll();

 

             $auth=Yii::app()->authManager;

             $role=$auth->createRole('owner');

             $auth->createOperation('createProject', 'Create a new project');

             $role->addChild('createProject');


             echo 'Authorisation hierarchy successfully generated.';

         }

    }

}




this is rather odd.

Did you set-up separate dbs within main, test & console? Or are you using the same db name?

On a trivial side note, you want to use the same auth rather than call yii::app() twice.

If you are not getting any error messages and your console is reporting success. Based on what you are telling me, I really cannot think of how to help. If I can think of anything else I will post back.

Main and console are using the same database. Not using test while trying to import the roles.

No, I was trying to supply a cut-down version. How’s this:




<?php


/*

 *  User roles go like this:

 *

 *  - superUser

 *  - businessManager

 *  - officeManager

 *  - regularUser

 *  - muteUser // Can only read quotes

 *

 */


    class RbacCommand extends CConsoleCommand

    {

        private $_authManager;


        public function getHelp()

        {

            print( "

            USAGE

                rbac


            DESCRPITION

                This command generates an initial RBAC authorisation hierachy.

            ");

        }


        public function run($args)

        {

            if(($this->_authManager=Yii::app()->authManager)!==null)

            {

                echo "This command will create four roles: superUser, businessManager, officeManager, regularUser & muteUser\n";

                echo 'Would you like to continue? [y|n]: ';


                if(!strcasecmp(trim(fgets(STDIN)), 'y'))

                {

                    $this->_authManager->clearAll();


                    /****************************  CREATE OPERATIONS  ********************************/


                    $auth=Yii::app()->authManager;

                    $role=$auth->createRole('owner');

                    $auth->createOperation('createProject', 'Create a new project');

                    $role->addChild('createProject');


                    $this->_authManager->createOperation('createUser', 'Create a new user');

                    $this->_authManager->createOperation('readUser', 'Read user profileInformation');

                    $this->_authManager->createOperation('updateUser', 'Update a users information');

                    $this->_authManager->createOperation('deleteUser', 'Delete a new user');


                    $this->_authManager->createOperation('createOffice', 'Create a new office');

                    $this->_authManager->createOperation('readOffice', 'Read office profileInformation');

                    $this->_authManager->createOperation('updateOffice', 'Update a office\'s information');

                    $this->_authManager->createOperation('deleteOffice', 'Delete a new office');


                    $this->_authManager->createOperation('createJob', 'Create a new job');

                    $this->_authManager->createOperation('readJob', 'Read job profileInformation');

                    $this->_authManager->createOperation('updateJob', 'Update a job\'s information');

                    $this->_authManager->createOperation('deleteJob', 'Delete a new job');


                    $this->_authManager->createOperation('createArtwork', 'Create a new artwork');

                    $this->_authManager->createOperation('readArtwork', 'Read artwork profileInformation');

                    $this->_authManager->createOperation('updateArtwork', 'Update a artwork\'s information');

                    $this->_authManager->createOperation('deleteArtwork', 'Delete a new artwork');


                    $this->_authManager->createOperation('createQuote', 'Create a new quote');

                    $this->_authManager->createOperation('readQuote', 'Read quote profileInformation');

                    $this->_authManager->createOperation('updateQuote', 'Update a artwork\'s quote');

                    $this->_authManager->createOperation('deleteQuote', 'Delete a new quote');


                    /****************************  CREATE ROLES  ********************************/


                    $role=$this->_authManager->createRole('regularUser');

                    $role->addchild('muteUser');

                    $role->addChild('createJob');

                    $role->addChild('updateJob');

                    $role->addChild('deleteJob');

                    $role->addChild('createArtwork');

                    $role->addChild('updateArtwork');

                    $role->addChild('deleteArtwork');

                    $role->addChild('createQuote');

                    $role->addChild('updateQuote');

                    $role->addChild('deleteQuote');


                    $role=$this->_authManager->createRole('officeManager');

                    $role->addChild('regularUser');

                    $role->addChild('updateOffice');

                    $role->addChild('createUser');

                    $role->addChild('updateUser');

                    $role->addChild('deleteUser');


                    $role=$this->_authManager->createRole('businessManager');

                    $role->addChild('officeManager');

                    $role->addChild('createOffice');

                    $role->addChild('deleteOffice');


                    echo 'Authorisation hierarchy successfully generated.';


                }

            }

        }

    }




Argghh,

I discovered that I left out the CDbAuthManager from the components section in the main config file. I could have sworn I put it in there. Unsure why the if statement didn’t catch this.

Anyway, fixed now.

Thanks for helping.

Good on you friend