Use of auth.php
#1
Posted 20 May 2009 - 08:37 AM
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
#2
Posted 20 May 2009 - 10:43 AM
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".
#4
Posted 10 June 2009 - 05:19 PM
I`m triying to develop a console command to setup CPhpAuthManager.
I have created a console command class:
Quote
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
// 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
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
#6
Posted 13 October 2009 - 04:39 AM
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
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.
#8
Posted 14 October 2009 - 02:49 AM
#9
Posted 14 October 2009 - 05:19 AM
#11
Posted 15 October 2009 - 01:51 AM
qiang, on 20 May 2009 - 10:43 AM, said:
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.
#12
Posted 15 October 2009 - 03:17 AM
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.
#13
Posted 15 October 2009 - 04:13 AM
Quote
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.
#14
Posted 22 October 2010 - 05:28 AM
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.
#15
Posted 22 October 2010 - 05:52 AM
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.
#16
Posted 13 January 2011 - 10:06 AM
moisty70, on 22 October 2010 - 05:28 AM, said:
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',
),
)
);

Help
This topic is locked











