<?php
/**
* @file includes/user.inc
* @brief User Library
* @author Kenneth Smith <hide@address.com>
*
* Modularized Information Environment (MIE)
* Copyright (C) 2005-2006 by Kenneth Smith. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place - Suite 330, Boston, MA 02111-1307, USA.
*/
defined('VALIE_MIE') || die(_('Direct access not allowed'));
defined('USER_INC') && exit;
define('USER_INC', true);
/**
* @brief User Object
*
* Class for USER objects.
*/
class USER {
/// User's Data
private $data;
/// Construct Object.
function __construct() {
global $session;
$this->data = isset($session) ? $this->by_id($session->user_id) : $this->by_id(0);
}
function __get($_id) {
return isset($this->data[$_id]) ? $this->data[$_id] : null;
}
/**
* @brief User By Id
*
* Returns basic information about a system user
* @param $_id
* The user's id
* @return
* Array of user data
*/
function by_id($_id) {
global $db;
$sql = $db->rewrite('SELECT * FROM t{users} WHERE k{id} = %i', $_id);
if($user = $db->result($sql)) {
// Unserialize configuration
$user['conf'] = unserialize((string)$user['conf']);
// Determine alias
$user['alias'] = isset($user['conf']['alias']) ? $user['conf']['alias'] : $user['name'];
// Retrieve role data
$user['roles'] = $this->get_roles($user['id']);
// Retrieve sanction data
$user['sanctions'] = $this->get_sanctions(mie_extract_key($user['roles'], 'id'));
}
return $user;
}
function get_roles($_userid) {
global $db;
$sql = $db->rewrite('SELECT r.k{id}, r.k{name} FROM t{members} AS m LEFT JOIN t{roles} AS r ON m.k{role_id} = r.k{id} WHERE m.k{user_id} = %i ORDER BY r.k{name}', $_userid);
return $db->table($sql);
}
function get_sanctions($_roles) {
global $db;
if(empty($_roles)) {
return array();
}
else {
$_roles = implode(',', $_roles);
$sql = $db->rewrite("SELECT a.k{id}, a.k{name} FROM t{sanctions} AS s LEFT JOIN t{actions} AS a ON s.k{action_id} = a.k{id} WHERE s.k{role_id} IN ($_roles)");
return $db->table($sql);
}
}
/**
* @brief Sanction Check
*
* Returns true if this user is sanctioned to perform this action.
* @return
* True if user has this sanction (or user is root)
*/
function may($_sanction){
return ($this->data['id'] == 1 || in_array($_sanction, mie_extract_key($this->data['sanctions'], 'name')));
}
}
?>