Getting Auth role name

Hi,

I am using CDbAuthManager, and I was wondering if there was a quick way to find out the "role name" the user is assigned to.

I know I could query the database manually etc, but is there a way built in way in Yii? Something like.

Yii::app()->user->assignedRole // for current logged in user

AND

User::model->findByPK($id)->assignedRole // for not logged in user.

Ideally I need it for both logged in and not logged in (or ANY user).

You may want to try




Yii::app()->user->checkAccess()



For more details, please see the documentation on Authentication and Authorization.

Thanks.

Anyone know how do this for different users? Not nessacarily the logged in user without having to query the DB manually to get the role name.

Mind you I could always just write the query once in the user model and wrap it in a method, that would not be too difficult. However still open to ideas everyone.

If you use the yii right extension, you just need create a function called getUserRoles() in the class RWebUser that extends from CWebUser, this class is under the path:

YouYiiRootPathprotected/modules/rights/components/RWebUser.php

this is the code of the function:




         /**

	 * Retunr an array with the roles

	 */

	function getUserRoles()

	{

	

		$roles = array();

		$criteria=new CDbCriteria;

		$criteria->condition='userid=:id';

		$criteria->params=array(':id'=>Yii::app()->user->id);

		$Rol = Authassignment::model()->findAll($criteria);

	

		$indexArray = 0;

		foreach ($Rol as $item)

		{

			$roles[$indexArray] = $item->itemname;

		}

	

		return $roles;

	}



this will return an array with the roles of the current user.