Select Git revision
messenger.class.php
user.class.php 3.84 KiB
<?php
/**
* user class contains username and its roles
* This class is used in the base class
*
* @author hahn
*/
class user {
private $_sUsername=false;
private $_aUserGroups=array();
private $_aUserRoles=array();
private $_sLastCheckedRole=false;
/**
* init user with optional given user
* @param type $sUser
*/
public function __construct($sUser=false){
$this->setUser($sUser);
}
/**
* detect a user
* @return type
*/
private function _autoDetectUser(){
$sUser=false;
if (is_array($_SERVER) && array_key_exists("PHP_AUTH_USER", $_SERVER)){
$sUser=$_SERVER["PHP_AUTH_USER"];
}
return $sUser;
}
/**
* TODO: reimplement
* get the user groups of the current user from an internal source.
* The function returns a flat aray with names of the groups
* @return array
*/
private function _getUserGroups(){
$aGroups=array();
if ($this->_sUsername){
$aGroups[]="authenticated";
$aGroups[]=$this->_sUsername;
}
$this->_aUserGroups=$aGroups;
return $this->_aUserGroups;
}
/**
* TODO: reimplement
* get the user roles of the current user from an internal source.
* The function returns a flat aray with names of the roles
* @return array
*/
private function _getUserRoles(){
$aRoles=array();
// anonymous roles:
// $aRoles[]="view";
$aRoles[]="viewProjectOverview";
if ($this->hasGroup("authenticated")){
if ($this->hasGroup("developer")){
$aRoles[]="build";
/*
$aRoles[]="deploy";
$aRoles[]="accept";
$aRoles[]="setup-project";
*
*/
}
if ($this->hasGroup("admin")){
// $aRoles[]="setup-all";
}
}
$this->_aUserRoles=$aRoles;
return $this->_aUserRoles;
}
/**
* TODO: implement authentication somewhere
* set a new authenticated user
* @param string $sUser username
*/
public function setUser($sUser=false){
if (!$sUser){
$sUser=$this->_autoDetectUser();
}
$this->_sUsername=$sUser;
$this->_getUserGroups();
$this->_getUserRoles();
}
/**
* get the current username
* @return string
*/
public function getUsername(){
return $this->_sUsername;
}
/**
* get a flat array with roles of the current user
* @return string
*/
public function getUserGroups(){
return $this->_aUserGroups;
}
/**
* get a flat array with roles of the current user
* @return string
*/
public function getUserRoles(){
return $this->_aUserRoles;
}
/**
* check if the current user has a given role name
* @param string $sGroupname name of the role to check
* @return type
*/
public function hasGroup($sGroupname){
return (array_search($sGroupname, $this->_aUserGroups)!==false);
}
/**
* check if the current user has a given role name
* @param string $sRolename name of the role to check
* @return type
*/
public function hasRole($sRolename){
$this->_sLastCheckedRole=$sRolename;
return (array_search($sRolename, $this->_aUserRoles)!==false);
}
/**
* return html code to display a denied message
* @return type
*/
public function showDenied(){
return '<div class="error">'.t("class-user-error-deny-no-role").' ('.$this->_sLastCheckedRole.')</div>';
}
}