Yii Framework Forum: Use of auth.php - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Use of auth.php Rate Topic: -----

#1 User is offline   Lollinger.de 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 79
  • Joined: 19-May 09
  • Location:Ulm, Germany

Posted 20 May 2009 - 08:37 AM

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
0

#2 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,879
  • Joined: 04-October 08
  • Location:DC, USA

Posted 20 May 2009 - 10:43 AM

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

0

#3 User is offline   Lollinger.de 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 79
  • Joined: 19-May 09
  • Location:Ulm, Germany

Posted 21 May 2009 - 08:42 AM

Thank you for your help the auth manager is running
0

#4 User is offline   jarb 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 10-June 09

Posted 10 June 2009 - 05:19 PM

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->createOperation('createPost','create a post');

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

$auth->createOperation('updatePost','update a post');

$auth->createOperation('deletePost','delete a post');



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

$task=$auth->createTask('updateOwnPost','update a post by author himself',$bizRule);

$task->addChild('updatePost');



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

$role->addChild('readPost');



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

$role->addChild('reader');

$role->addChild('createPost');

$role->addChild('updateOwnPost');



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

$role->addChild('reader');

$role->addChild('updatePost');



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

$role->addChild('editor');

$role->addChild('author');

$role->addChild('deletePost');



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



$auth->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
0

#5 User is offline   will 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 179
  • Joined: 21-March 09

Posted 10 June 2009 - 06:20 PM

@jarb: You should run the script under yiic shell
0

#6 User is offline   pangjanne 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 118
  • Joined: 01-July 09

Posted 13 October 2009 - 04:39 AM

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

Quote

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

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

#7 User is offline   pangjanne 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 118
  • Joined: 01-July 09

Posted 13 October 2009 - 11:19 PM

please help to create auth.php?
0

#8 User is offline   omut 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 13-October 09

Posted 14 October 2009 - 02:49 AM

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

#9 User is offline   omut 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 13-October 09

Posted 14 October 2009 - 05:19 AM

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

#10 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 14 October 2009 - 06:22 AM

View Postomut, on 14 October 2009 - 05:19 AM, said:

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

#11 User is offline   Rashid A. 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 03-July 09
  • Location:Islamabad, PK

Posted 15 October 2009 - 01:51 AM

View Postqiang, on 20 May 2009 - 10:43 AM, said:

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


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

#12 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 15 October 2009 - 03:17 AM

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

#13 User is offline   Rashid A. 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 03-July 09
  • Location:Islamabad, PK

Posted 15 October 2009 - 04:13 AM

Quote

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

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

#14 User is offline   moisty70 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 58
  • Joined: 23-December 09

Posted 22 October 2010 - 05:28 AM

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

#15 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 22 October 2010 - 05:52 AM

@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.
0

#16 User is offline   mongoose76 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 13-January 11

Posted 13 January 2011 - 10:06 AM

View Postmoisty70, on 22 October 2010 - 05:28 AM, said:

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.



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

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users