<?php
/**
* user class
*
* this is intended to be a base user class that others extend
* could possibly be an 'abstract' class, except I think I
* want to have some implementation stuff in here to share
*
*/
class base_user {
public $name;
public $level;
public $permissions;
private $__authenticated = false;
public $info = array();
public $session = array();
public function base_user() {
}
/**
* determine type of user
*
* @param string Type to check against (staff/org/etc)
* @return boolean
*/
public function isType($type){
if(@$this->info['type']==$type) {
return true;
} else {
if(@$this->type==$type) {
return true;
} else {
return false;
}
}
}
/**
* return boolean true/false if user is authenticated or not
*
* @return boolean
*/
public function isAuthenticated() {
return $this->__authenticated;
}
/**
* get info about the user
* this is the set of key/value pairs from the db row with the user info in it
* may evolve in to more info
*
* @param String $key
* @return mixed
*/
public function getInfo($key) {
if(@$this->info[$key]!='') {
return @$this->info[$key];
} else {
return @$this->$key;
}
}
/**
* authenticate the user and return a user object
* we'll either return a basic base_user object
* or a user_site,user_org,user_staff or user_registrar
* object based on the user's type
*
* @static
*
* @param string username
* @param string password
* @return base_user user object
*/
public function authenticate($user, $password) {
$db = new xdb();
$rows = $db->queryGetAll("select * from ".config::get("tbl_site")." where
username=':?:' and password=SHA1(':?:')", array($user,$password));
if(count($rows)!=1) {
$_SESSION['user'] = new base_user();
} else {
$user = base_user::loadById($rows[0]['ID']);
$_SESSION['user'] = $user;
}
return $_SESSION['user'];
}
/**
* load by id
*
* @param string ID
* @return base_user Appropriate subclass of base_user
*
*/
public function loadById($id) {
$db = new xdb();
$rows = $db->queryGetAll("select * from ".config::get("tbl_site")." where id=':?:' ", array($id));
if(count($rows)!=1) {
$user = new site();
} else {
$row = $rows[0];
$userClassName = "user_".$row['type'];
// need to call this to fix typo with addEnrollments - there might be others too
$row['userCapabilities'] = base_user::fixCapabilities($row['userCapabilities']);
$user = new $userClassName($row);
$user->__authenticated = TRUE;
$user->setPermissions($row['userCapabilities']);
$user->name = $user->getInfo('name');
$user->info['currentUserID'] = $row['username'];
$user->info['currentUser_access'] = $row['type'];
$user->info['siteId'] = $row['ID'];
}
return $user;
}
/**
* load by any key
*
* @param string Key to search for - what column name to use
* @param string ID - value to search for
* @return base_user Appropriate subclass of base_user
*
*/
public function loadByKey($key,$value) {
$db = new xdb();
$rows = $db->queryGetAll("select * from ".config::get("tbl_site")." where
$key=':?:' ", array($value));
if(count($rows)!=1) {
$user = new base_user();
} else {
$row = $rows[0];
$userClassName = "user_".$row['type'];
// need to call this to fix typo with addEnrollments - there might be others too
$row['userCapabilities'] = base_user::fixCapabilities($row['userCapabilities']);
$user = new $userClassName($row);
$user->setPermissions($row['userCapabilities']);
$user->name = $user->getInfo('name');
$user->info['currentUserID'] = $row['username'];
$user->info['currentUser_access'] = $row['type'];
$user->info['siteId'] = $row['ID'];
}
return $user;
}
/**
* fix the userCapabilities string
* @param string original userCapabilities string
* @return string fixed string
*
*/
function fixCapabilities($string) {
$newString = str_replace("addEnrollemnts","addEnrollments", $string);
return $newString;
}
/**
* Set the permission strings for this user
*
* @param array $perms
*/
public function setPermissions($perms) {
$this->permissions = explode(",",$perms);
}
/**
* return boolean if user has permission string or not
*
* @param string $permission
* @return boolean
*/
public function hasPerm($permission) {
if(in_array($permission, $this->permissions)) {
return true;
} else {
return false;
}
}
static public function get() {
return $_SESSION['user'];
}
}
?>