<?php
 
// this file must be stored in:
// protected/components/WebUser.php
 
class WebUser extends CWebUser {
 
  // Store model to not repeat query .
  private $_roles;
  private $_nim;
  private $_nama;
  private $_roles_name;
  
   
  // Return role name.
  // access it by Yii::app()->user->role
  function getRole(){
	$userId =  Yii::App()->user->id;
	if($this->_roles===null){
		$this->_roles = UserRole::model()->findAll(array("condition"=>"user_id = '$userId'"));
	}
	if($this->_roles != NULL){	
		foreach ($this->_roles as $role) {
				if($this->_roles_name===null){
					$this->_roles_name = Role::model()->findByPk($role->sys_m_role_id);
				}
				return $this->_roles_name->name;
		}			
	}
  }
	
  // This is a function that checks the field 'role'
  // in the User model to be equal to 1, that means it's admin
  // access it by Yii::app()->user->isAdmin()
  function isMahasiswa(){
    $role = $this->getRole();
    return $role == "mahasiswa";
  }
  function isStaff(){
    $role = $this->getRole();
    return $role == "staff";
  }
  function isAdmin(){
    $role = $this->getRole();
    return $role == "admin";
  }
  function isDosen(){
    $role = $this->getRole();
    return $role == "dosen";
  }
}
?>