Doubt about charpter 8 rbac

Hello friends

I’m using Yii 1.1.4

I created the file RbacCommand.php:




<?php

class RbacCommand extends CConsoleCommand

{

    private $authManager;


    public function getHelp()

    {

        return <<<EOD

        USAGE

         rbac


        DESCRIPTION

         This command generates an initial RBAC authorization hierarchy.


        EOD;

    }


    /*

      Execute the action

      @param array command line paramenters specific for this command

    */

    public function run($args)

    {

        //ensure that an authManager is defined as this is mandatory for creating an auth heirarchy

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

        {

            echo "Error: an authorization manager, named, 'authManager' must be configured to use this command.\n";

            echo "If you already added 'authManager' component in application configuration, \n";

            echo "please quit an re-enter the yiic shell.\n";

            return;

        }


        //provide the oportunity for the use to abort the request

        echo "create, read, update and delete user,\n";

        echo "create, read, update and delete project,\n";

        echo "create, read, update and delete issue,\n";

        echo "Would you like to continue?[Yes][No] ";


        //check the input from the user and continue if they indicate yes ti the above question

        if(!strncasecmp(trim(fgets(STDIN)), 'y', 1))

        {

            //first we need to remove all operations, roles, child relationships andassignments

            $this->_authManager->clearAll();


            //create the lowest leveloperations 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 lwest level operations for issues

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

            $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 both 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");


            //provide a messege indicating success


            echo "Authorization hierarchy successfully generated";

        }

    }

}

        

?>



But when I execute on the shell there is a error:

[b]

alexandre@alexandre-desktop:/var/www/trackstar$ /var/www/yii/framework/yiic shell

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

Please type ‘help’ for help. Type ‘exit’ to quit.

>> rbac

PHP Parse error: syntax error, unexpected $end in /var/www/trackstar/protected/commands/shell/RbacCommand.php on line 96

[/b]

This the line 96:

?>

After this line:

return <<<EOD

whole code becomes comment

Is Where the error?

If a Remove the code:




public function getHelp()

    {

        return <<<EOD

        USAGE

         rbac


        DESCRIPTION

         This command generates an initial RBAC authorization hierarchy.


        EOD;

    }



I have this error:

[b]

alexandre@alexandre-desktop:/var/www/trackstar$ /var/www/yii/framework/yiic shell

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

Please type ‘help’ for help. Type ‘exit’ to quit.

>> rbac

exception ‘CException’ with message ‘Property “RbacCommand._authManager” is not defined.’ in /var/www/yii/framework/base/CComponent.php:173

Stack trace:

#0 /var/www/trackstar/protected/commands/shell/RbacCommand.php(25): CComponent->__set(’_authManager’, Object(CDbAuthManager))

#1 /var/www/yii/framework/cli/commands/ShellCommand.php(147): RbacCommand->run(Array)

#2 /var/www/yii/framework/cli/commands/ShellCommand.php(99): ShellCommand->runShell()

#3 /var/www/yii/framework/console/CConsoleCommandRunner.php(62): ShellCommand->run(Array)

#4 /var/www/yii/framework/console/CConsoleApplication.php(88): CConsoleCommandRunner->run(Array)

#5 /var/www/yii/framework/base/CApplication.php(135): CConsoleApplication->processRequest()

#6 /var/www/yii/framework/yiic.php(33): CApplication->run()

#7 /var/www/yii/framework/yiic(15): require_once(’/var/www/yii/fr…’)

#8 {main}

>>

[/b]

sorry if my English is bad I’m from Brazil

Problem solved

I wrote it wrong: private $authManager;

The correct: private $_authManager;

But I have a doubt:

But when I execute on the shell there is a error:

[b]

alexandre@alexandre-desktop:/var/www/trackstar$ /var/www/yii/framework/yiic shell

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

Please type ‘help’ for help. Type ‘exit’ to quit.

>> rbac

PHP Parse error: syntax error, unexpected $end in /var/www/trackstar/protected/commands/shell/RbacCommand.php on line 96

[/b]

This the line 96:

[b]

?>

[/b]

After this line:




return <<<EOD




whole code becomes comment. Why?

The EOD; statement may not have any spaces in front of it. In other words, it must be at the far left of the line. Also, the stuff between return <<<EOD and EOD; will be printed as-is. So if you put spaces in front of those lines, it will show up in the console output as spaces too. Here is the getHelp() function with proper indentation.




    public function getHelp()

    {

        return <<<EOD

USAGE

rbac


DESCRIPTION

This command generates an initial RBAC authorization hierarchy.


EOD;


    }

Thanks for help. I get it. There was space. I removed the spaces

Thanks, I had the same problem, it worked for me too.

same problem but solved…damm spaces :))

yea, tnx it worked for me to =)

I hope its okay that I capture this thread?

I have a similar problem, I guess.

Code in auth.php




class RbacCommand extends CConsoleCommand

{

   public function run($args)

   {

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

       

   

        $auth->createOperation('createPost','create a post');

        $auth->createOperation('readPost','read a post');

[...]



When I use:

I dont know what I shall do now. I thought I start the shell, use this php file and than my DBs are filled with my roles. ;(

Hi Louis,

to run the RBAC script you have to add the path to your application config file as a commandline parameter.

The RBAC script needs access to your application config settings, in this case to the ‘authManager’ properties.




C:\xampp\htdocs\trackstar\protected>yiic shell C:\xampp\htdocs\trackstar\protected\config\main.php



Looking here for more information

posted by hnam

Here I attached my "RbacCommand.php" class which works fine with yii 1.1.9




<?php


class RbacCommand extends CConsoleCommand

{

    private $_authManager;


    public function getHelp()

    {

        return <<<EOD

USAGE

  rbac


DESCRIPTION

  This command generates an initial RBAC authorization hierarchy.


EOD;

    }


    /*

      Execute the action

      @param array command line paramenters specific for this command

    */

    public function run($args)

    {

        //ensure that an authManager is defined as this is mandatory for creating an auth heirarchy

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

        {

            echo "Error: an authorization manager, named, 'authManager' must be configured to use this command.\n";

            echo "If you already added 'authManager' component in application configuration, \n";

            echo "please quit an re-enter the yiic shell.\n";

            return;

        }


        //provide the oportunity for the use to abort the request

        echo "create, read, update and delete user,\n";

        echo "create, read, update and delete project,\n";

        echo "create, read, update and delete issue,\n";

        echo "Would you like to continue?[Yes][No] ";


        //check the input from the user and continue if they indicate yes ti the above question

        if(!strncasecmp(trim(fgets(STDIN)), 'y', 1))

        {

            //first we need to remove all operations, roles, child relationships andassignments

            $this->_authManager->clearAll();


            //create the lowest leveloperations 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 lwest level operations for issues

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

            $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 both 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");


            //provide a messege indicating success

            echo "Authorization hierarchy successfully generated";

        }

    }

}

        

?>



Thanks jodev.

Grate hint about return <<<EOD.

For more information google [color=#666666][font=Verdana, Tahoma, Arial, sans-serif]

PHP heredoc

[/font][/color]