Performing authentication with or without RBAC

I've got two questions.

  1. How to allow all the users with the is_admin flag in my db.users_table perform administrative tasks with support from CController::accessRules()?

I'd be tempted to use 'exp​ression' => 'return Yii::app()->user->is_admin', but that won't work, since app()->user is a CWebUser instance.

PS: without RBAC

  1. Where to do all the Yii::app()->authManager-> createOperation/addChild/etc, as described in The Guide? (with RBAC)

I don't require baby steps, only concrete names for classes/methods and how they're going to work together in terms of the execution flow.

Thanks.

  1. You can override CWebUser and provide a method to retrieve the current user's is_admin value. Then you can use 'exp​ression' in the access rule.

  2. These methods are supposed to be called by user permission management code. It can be either offline or online, depending on your system requirement.

Check CDbAuthManager for the createOperation, addChild, etc.

Quote

1. You can override CWebUser and provide a method to retrieve the current user's is_admin value. Then you can use 'exp​ression' in the access rule.
  1. These methods are supposed to be called by user permission management code. It can be either offline or online, depending on your system requirement.

  • If I'm going that way, how to make my new class known to the system (i.e. "register" it) ?

  • How to do it both online and offline, with concrete class names / methods involved, at least the key ones?

    1. You can configure the 'user' application component in app config (like you do with other app component such as 'db', 'log').

    2. The guide already shows the example code to create new roles and assignment. It is your responsibility to develop a GUI to call these code. We do plan to add a GUI in future releases (1.1 or 1.2).

    Quote

    1. You can configure the 'user' application component in app config (like you do with other app component such as 'db', 'log').
    1. The guide already shows the example code to create new roles and assignment. It is your responsibility to develop a GUI to call these code. We do plan to add a GUI in future releases (1.1 or 1.2).

  • Yes, but how/where to hook my code into? By using main.php's 'preload' ?

  • By the way, great job you’re doing here, developing and helping out the community. My utmost respect :)

    PS: I hope I'll understand yii's internals soon enough to be able to write some patches.

    Quote

    2. Yes, but how/where to hook my code into? By using main.php's 'preload' ?

    I have tried to figure out that myself without sucess, so i guess the question is of more general interest. Were is the best place to put the code?

    I't would be nice with a simple demo application that shows the basic principles i a little bit more complicated authentication sceme than the on en i e the blog demo. I't might be interesting to do a blog v2 with more advanced authentication.

    I also share OriginalCopys respect for qiang, great work!

    I kinda done something, at least now there's the flag is_admin pulled from the db into the app()->user at authentication time via CUserIdentity. I don't know if this is "the right way" in Yii terms, anyway the exp​ression still does not evaluate. So here's the code:

    class PostController extends CController
    
    
    {
    
    
    	const PAGE_SIZE=10;
    
    
    
    
    
    	/**
    
    
    	 * @var string specifies the default action to be 'list'.
    
    
    	 */
    
    
    	public $defaultAction='list';
    
    
    
    
    
    	/**
    
    
    	 * @var CActiveRecord the currently loaded data model instance.
    
    
    	 */
    
    
    	private $_post;
    
    
    	public function init() {
    
    
    		throw new Exception(print_r('is admin:'.Yii::app()->user->is_admin,TRUE));
    
    
    	}
    
    
    
    
    
    	/**
    
    
    	 * @return array action filters
    
    
    	 */
    
    
    	public function filters()
    
    
    	{
    
    
    		return array(
    
    
    			'accessControl', // perform access control for CRUD operations
    
    
    		);
    
    
    	}
    
    
    
    
    
    	/**
    
    
    	 * Specifies the access control rules.
    
    
    	 * This method is used by the 'accessControl' filter.
    
    
    	 * @return array access control rules
    
    
    	 */
    
    
    	public function accessRules()
    
    
    	{
    
    
    		return array(
    
    
    			array('allow',  // allow all users to perform 'list' and 'show' actions
    
    
    				'actions'=>array('list','show'),
    
    
    				'users'=>array('*')
    
    
    			),
    
    
    			array('allow', // allow admin user to perform 'admin' and 'delete' actions
    
    
    				'actions'=>array('admin','delete','create','update','remove'),
    
    
    				'exp​ression' => array('!$user->isGuest && $user->is_admin')
    
    
    			),
    
    
    			array('deny',  // deny all users
    
    
    				'users'=>array('*')
    
    
    			),
    
    
    		);
    
    
    	}
    
    
    //....
    
    
    

    However 'exp​ression' is either not evaluated, nor it works as expected (I get the "Unauthorized" error page). Yet init() shows correctly "is admin: 1", so at least I managed to pull the info from the db, by using setState():

    
    
    class UserIdentity extends CUserIdentity
    
    
    {
    
    
    	private $_id;
    
    
    	public function authenticate()
    
    
    	{
    
    
    		$username = strtolower($this->username);
    
    
    		$user = User::model()->find('LOWER(username)=?',array($username));
    
    
    		if(NULL === $user) {
    
    
    			$this->errorCode = self::ERROR_USERNAME_INVALID;
    
    
    		}
    
    
    		elseif(md5($this->password) !== $user->password) {
    
    
    			$this->errorCode = self::ERROR_PASSWORD_INVALID;
    
    
    		}
    
    
    		else {
    
    
    			$this->errorCode = self::ERROR_NONE;
    
    
    			$this->_id = $user->id;
    
    
    			$this->username = $user->username;
    
    
    			$this->setState('is_admin',$user->is_admin,0);
    
    
    		}
    
    
    		return !$this->errorCode;
    
    
    	}
    
    
    	public function getId() {
    
    
    		return $this->_id;
    
    
    	}
    
    
    }
    
    
    

    What am I missing?

    ‘exp​ression’ => array(’!$user->isGuest && $user->is_admin’)

    http://www.yiiframew…Rule#exp​ression:

    public string $exp​ression;

    Quote

    'exp​ression' => array('!$user->isGuest && $user->is_admin')

    http://www.yiiframew…Rule#exp​ression:

    public string $exp​ression;

    Yeah, but practically it won’t array_map() if you use that, it must be an array ;)

    'exp​ression' => array('1===1') doesn't even work.

    It should be a string. There's a bug in 1.0.3 which has been fixed in SVN.

    Quote

    It should be a string. There's a bug in 1.0.3 which has been fixed in SVN.

    Hi Qiang,

    When will the next release be?

    Thanks,

    _da.

    April 5. You can use SVN snapshot for now.

    Quote

    April 5. You can use SVN snapshot for now.

    OK, thanks.

    Quote

    It should be a string. There's a bug in 1.0.3 which has been fixed in SVN.

    Great. Could you tell me which revision that is? And please, please write log messages when commiting, there are many eye balls looking what you’re doing :P

    Quote

    Quote

    It should be a string. There's a bug in 1.0.3 which has been fixed in SVN.

    Great. Could you tell me which revision that is? And please, please write log messages when commiting, there are many eye balls looking what you’re doing :P

    http://code.google.c…ce/detail?r=781

    Quote

    2. Yes, but how/where to hook my code into? By using main.php's 'preload' ?

    hey mate… you need to run it just once… create an action in any controller with RBAC settings and in the end type $auth->save(), run it and then delete your action from the controller - RBAC settings should be saved once for all.

    What does this execution overwrites? I mean, where do I view changes in the application/system?