Question on Rights and Authorization

Two questions:

  1. Have a relationship stat(count) that I use to see if a user already performed an action (ie voting). The problem is that when using the app as a guest, the relation returns an error since there is no userID.

  2. Is there an easy way to make certain features (parts of the html, ie everything with a given div) visible/invisible to different types of users (guest vs user vs admin)?

Was thinking of possible solutions, one of which was to set the userID to 0 for any guest and use that as the condition for both problems. But I couldn’t figure out how to do it.

just use


Yii::app()->getUser()->isGuest

or in the UserIdentity file do something like:


/**

     * @return integer the ID of the user record

     */

	public function getId()

	{

		return Yii::app()->getUser()->isGuest ? false : $this->id;

	}

and then check if ($id !==false )

again in UserIdentity, on the login set its role as a state after the sucessful authentication




$this->setState('role',$user->getRole());



so you can later check against like:




Yii::app()->getUser()->role===USER::ADMIN;

//or

Yii::app()->getUser()->role===USER::USER;



to facilitate create shortcut functions to check it, like




function isAdmin(){

  	return Yii::app()->getUser()->role===USER::ADMIN;

}



and then in the view use




if(isAdmin()){

	echo '<div id="myDiv">';

}



One question where would I put the isAdmin function? In UserIdentity?

its just a shortcut, so you should put under a separated file and include it

check out this

but you could also put in User model as static and use like




User::isAdmin();



Cool. thx