Use of auth.php

Hello Everyone,

ich have a questing belonging to the CPhpAuthManager. I want to set up an Test Software with three different roles and want to implement that in the auth.php file in the data folder. But i don't know what i have to put into this auth.php file to definie my roles. In which file i have to save for example this?

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

$auth->createOperation('erstelleBeitrag','Einen Beitrag erstellen');

$auth->createOperation('leseBeitrag','Einen Beitrag lesen');

$auth->createOperation('aktualisiereBeitrag','Einen Beitrag aktualisieren');

$auth->createOperation('löscheBeitrag','Einen Beitrag löschen');

$bizRule='return Yii::app()->user->id==$params["post"]->authID;';

$task=$auth->createTask('aktualisiereEigenenBeitrag','Einen eigenen Beitrag aktualisieren',$bizRule);

$task->addChild('aktualisiereBeitrag');

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

$role->addChild('leseBeitrag');

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

$role->addChild('leser');

$role->addChild('erstelleBeitrag');

$role->addChild('aktualisiereEigenenBeitrag');

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

$role->addChild('leser');

$role->addChild('aktualisiereBeitrag');

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

$role->addChild('redakteur');

$role->addChild('autor');

$role->addChild('löscheBeitrag');

$auth->assign('leser','leserA');

$auth->assign('autor','autorB');

$auth->assign('redakteur','redakteurC');

$auth->assign('admin','adminD');

I hope everyone understands my poor english :-/

Thanking you in anticipation

The code you gave can be used to generate the auth.php file. You need to call $auth->save(); at the end to save the result into auth.php.

You can put this piece of code in a console command class (e.g. RbacCommand in protected/commands/RbacCommand.php) so that you can execute this script more easily using "yiic rbac".

Thank you for your help the auth manager is running

Hello everyone,

I`m triying to develop a console command to setup CPhpAuthManager.

I have created a console command class:

Quote

<?php

class RbacCommand extends CConsoleCommand

{

    public function run($args)

    {

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

	$auth-&gt;createOperation(&#039;createPost&#039;,&#039;create a post&#039;);





	$auth-&gt;createOperation(&#039;readPost&#039;,&#039;read a post&#039;);





	$auth-&gt;createOperation(&#039;updatePost&#039;,&#039;update a post&#039;);





	$auth-&gt;createOperation(&#039;deletePost&#039;,&#039;delete a post&#039;);





	 





	$bizRule=&#039;return Yii::app()-&gt;user-&gt;id==$params[&quot;post&quot;]-&gt;authID;&#039;;





	$task=$auth-&gt;createTask(&#039;updateOwnPost&#039;,&#039;update a post by author himself&#039;,$bizRule);





	$task-&gt;addChild(&#039;updatePost&#039;);





	 





	$role=$auth-&gt;createRole(&#039;reader&#039;);





	$role-&gt;addChild(&#039;readPost&#039;);





	 





	$role=$auth-&gt;createRole(&#039;author&#039;);





	$role-&gt;addChild(&#039;reader&#039;);





	$role-&gt;addChild(&#039;createPost&#039;);





	$role-&gt;addChild(&#039;updateOwnPost&#039;);





	 





	$role=$auth-&gt;createRole(&#039;editor&#039;);





	$role-&gt;addChild(&#039;reader&#039;);





	$role-&gt;addChild(&#039;updatePost&#039;);





	 





	$role=$auth-&gt;createRole(&#039;admin&#039;);





	$role-&gt;addChild(&#039;editor&#039;);





	$role-&gt;addChild(&#039;author&#039;);





	$role-&gt;addChild(&#039;deletePost&#039;);





	 





	$auth-&gt;assign(&#039;admin&#039;,&#039;demo&#039;);





	





	$auth-&gt;save(); 

    }

}

?>

and an entry script

Quote

<?php

// change the following paths if necessary

$yii=dirname(FILE).'/…/yii/framework/yii.php';

$config=dirname(FILE).'/protected/commands/RbacCommand.php';

// remove the following line when in production mode

defined('YII_DEBUG') or define('YII_DEBUG',true);

require_once($yii);

?>

But when I try to execute it I get

Quote

# /usr/local/bin/php  entry.php rbac

exception 'CException' with message 'Property "CConsoleApplication.authManager" is not defined.' in /home/tropical/public_html/yii/framework/base/CComponent.php:120

Stack trace:

#0 /home/tropical/public_html/yii/framework/base/CModule.php(93): CComponent->__get('authManager')

#1 /home/tropical/public_html/blog/protected/commands/RbacCommand.php(7): CModule->__get('authManager')

#2 /home/tropical/public_html/yii/framework/console/CConsoleCommandRunner.php(62): RbacCommand->run(Array)

#3 /home/tropical/public_html/yii/framework/console/CConsoleApplication.php(88): CConsoleCommandRunner->run(Array)

#4 /home/tropical/public_html/yii/framework/base/CApplication.php(133): CConsoleApplication->processRequest()

#5 /home/tropical/public_html/blog/entry.php(11): CApplication->run()

Yii::createConsoleApplication($config)->run();

Sorry for my english and thanks in advance

@jarb: You should run the script under yiic shell

help me, please!

how can I generate the auth.php file?

first, I don’t konw how to write the RbacCommand.php file(is a class)? can you show me some code snippets you writed.

i writed the RbacCommand.php like this:


class RbacCommand extends CCConsoleCommand

{

	public function run()

	{

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

		$auth->createOperation('contact','contact a person');

		$auth->createOperation('index','present index page');

		

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

		$role->addChild('contact');

		

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

		$role->addChild('index');

		

		$auth->assign($role, $id);

		$auth->save();

	}

}

second, Is it necessary adding code into console.php file ? as following:


return array(

	'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',

	'name'=>'My Console Application',

	'components'=>array(

		'authManager'=>array(

					'class'=>'CPhpAuthManager',

					'defaultRoles'=>array('guest'),

		),	

	),

); 

if i don’t add componts code into the file , it will occur some error(as following):

third, How run this command?(i run the command like this: protected/yiic rbac).

hope you can understand my pool english!

please give me some advice;

thanks.

please help to create auth.php?

qiang, why have you introduced rbac in yii, if you do not want to give proper information on it?

Why all that concerns rbac a secret? Where to see examples? The official guide does not offer

Please check the guide or search the forum. This topic has been discussed already a lot.

Why do I need to create a console command for it? That makes it usable via command line but on production server it has to be executed like normal PHP script.

Thanks.

You don’t have to. But setting up the RBAC hierarchy is something you usually only do once (just like creating a model or CRUD). That’s why some guys like to have a console command for it.

If you want to do it from script - go ahead. Just make sure you don’t re-create the hierarchy on every request.

After the hierarchy is created you can assign users to roles.

Maybe the srbac extension is also of use for you.

OK, using the console seems to be the better way but if I want to use PHP then how can I know when this code is executing again? To be precise, when it is re-generating the hierarchy.

I stated my exact problem here.

Thanks Mike for helping out.

regards,

PS: I have checked srbac extension and its database driven whereas I have simple roles to implement. Please see the link to other post.

This is getting more than complicated.

I get this error:


exception 'CException' with message 'Property "CConsoleApplication.authManager" is not defined.' in /.../CComponent.php:131

with


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

@Mike

The topic could have been discussed but is not clear at all.

@moisty70:

You have not configured an authManager component for your console application. This is mentioned in the guide here:

http://www.yiiframew…ization-manager

Did you also try Spiros’ excellent srbac extension? It seems to help many people to work with RBAC.

RBAC is a complex topic, so maybe you should not expect an easy description. Yes, there’s a lack of examples, but please be mild: Lack of documentation is IMO the biggest problem of many OSS projects. Most developers are good at writing code but not at writing docs. So it’s postponed until it’s finally forgotten. From that perspective Yii’s documentation is already outstanding. It really explains a lot and only requires some good will to help yourself in the rare cases the documentation is vague.

I think you have the same issue I did. The authManager needs to be declared inside the console configuration file, not the web application configuration file. In the config folder there’s a main.php and a console.php. You need to declare the authManager in the console.php as well, not just in the main.php because the CConsoleCommand has a different config file.

My console.php looks like this:




<?php


// This is the configuration for yiic console application.

// Any writable CConsoleApplication properties can be configured here.

return array(

	'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',

	'name'=>'My Console Application',

    'components'=>array(

         'authManager'=>array(

            'class'=>'CPhpAuthManager',

        ),

    )

);