get user role based on user id

I’m currently using SRBAC that created by spyros. In my application I need to get user role based on user id and I’m stuck with it. Does somebody know how to solve this?

Thanks in advance,

Yuda

The problem SOLVED refer to this link :D

the code is:




$arrayAuthRoleItems = Yii::app()->authManager->getAuthItems(2, Yii::app()->user->getId());

$arrayKeys = array_keys($arrayAuthRoleItems);

$role = strtolower ($arrayKeys[0]);



Your solution is not correct, because:

  • it can be that user belongs to more than one role
  • role names are case sensitive (for example, when specifying in controlers’ accessRules() method)

public function getRoles () {


	$roles = Yii::app()->authManager->defaultRoles ; // default roles

	$user = $this->loadUser( Yii::app()->user->id ) ; // example method which loads user model


	return $user === null ? $roles : array_merge( $roles, array_keys( Yii::app()->authManager->getAuthItems(2, $user->id) ) ) ;


}

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.