Source for file BitPermUser.php
Documentation is available at BitPermUser.php
* $Header: /cvsroot/bitweaver/_bit_users/BitPermUser.php,v 1.51 2007/01/14 23:26:01 hiranchaudhuri Exp $
* Lib for user administration, groups and permissions
* This lib uses pear so the constructor requieres
* Copyright (c) 2004 bitweaver.org
* Copyright (c) 2003 tikwiki.org
* Copyright (c) 2002-2003, Luis Argerich, Garland Foster, Eduardo Polidor, et. al.
* All Rights Reserved. See copyright.txt for details and a complete list of authors.
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details
* $Id: BitPermUser.php,v 1.51 2007/01/14 23:26:01 hiranchaudhuri Exp $
require_once( dirname( __FILE__ ). '/BitUser.php' );
* Class that holds all information for a given user
* @author spider <spider@steelsun.com>
* @version $Revision: 1.51 $
* @subpackage BitPermUser
# var $db; // The PEAR db object used to access the database
// change this to an email address to receive debug emails from the LDAP code
function BitPermUser( $pUserId= NULL, $pContentId= NULL ) {
global $gBitUser, $user_cookie_site;
// make double sure the current logged in user has permission
if( $gBitUser->hasPermission( 'p_users_admin' ) ) {
$assumeUser->loadPermissions();
if( $assumeUser->hasPermission( 'p_users_admin' ) ) {
$this->mErrors['assume_user'] = tra( "User administrators cannot be assumed." );
$this->mDb->query( "UPDATE `". BIT_DB_PREFIX. "users_cnxn` SET `user_id`=?, `assume_user_id`=? WHERE `cookie`=?", array( $pUserId, $gBitUser->mUserId, $_COOKIE[$user_cookie_site] ) );
function load( $pFull= FALSE, $pUserName= NULL ) {
// Used to hide sensitive information when it is unneccessary (i.e. $gQueryUser)
if (!empty($this->mInfo)) {
if (!empty($this->mInfo['provpass'])) {
unset ($this->mInfo['provpass']);
if (!empty($this->mInfo['hash'])) {
unset ($this->mInfo['hash']);
if (!empty($this->mInfo['challenge'])) {
unset ($this->mInfo['challenge']);
if (!empty($this->mInfo['user_password'])) {
unset ($this->mInfo['user_password']);
function store( &$pParamHash ) {
// keep track of newUser before calling base class
$this->mDb->StartTrans();
if( $gBitSystem->isFeatureActive( 'users_eponymous_groups' ) ) {
// Create a group just for this user, for permissions assignment.
'name' => $pParamHash['user_store']['login'],
'desc' => "Personal group for ". (!empty( $pParamHash['user_store']['real_name'] ) ? $pParamHash['user_store']['real_name'] : $pParamHash['user_store']['login'])
$this->mDb->CompleteTrans();
function groupExists( $pGroupName, $pUserId= ROOT_USER_ID ) {
if( !isset ( $rv[$pUserId][$pGroupName] ) ) {
$bindVars = array( $pGroupName );
$whereSql = 'AND `user_id`=?';
$query = "SELECT ug.`group_name`, ug.`group_id`, ug.`user_id`
FROM `". BIT_DB_PREFIX. "users_groups` ug
WHERE `group_name`=? $whereSql";
if( $result = $this->mDb->getAssoc( $query, $bindVars ) ) {
if( empty( $rv[$pUserId] ) ) {
$rv[$pUserId][$pGroupName] = $result[$pGroupName];
$rv[$pUserId][$pGroupName]['group_id'] = NULL;
return( $rv[$pUserId][$pGroupName]['group_id'] );
* removes user and associated private data
* @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
global $gBitSystem, $gBitUser;
$this->mDb->StartTrans();
if( $this->mUserId == $gBitUser->mUserId ) {
$this->mDb->RollbackTrans();
$gBitSystem->fatalError( tra( 'You cannot delete yourself' ) );
foreach( $userTables as $table ) {
$query = "DELETE FROM `". BIT_DB_PREFIX. $table. "` WHERE `user_id` = ?";
$result = $this->mDb->query( $query, array( $this->mUserId ) );
$this->mDb->CompleteTrans();
$this->mDb->RollbackTrans();
$this->mDb->RollbackTrans();
$gBitSystem->fatalError( tra( 'The anonymous user cannot be deleted' ) );
// =-=-=-=-=-=-=-=-=-=-=-= GROUP FUNCTIONS =-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Old style group name passed in
$ret = isset ( $this->mGroups[$pGroupMixed] );
if( empty( $pListHash['sort_mode'] ) || $pListHash['sort_mode'] == 'name_asc' ) {
$pListHash['sort_mode'] = 'group_name_asc';
$sortMode = $this->mDb->convertSortmode( $pListHash['sort_mode'] );
if( !empty( $pListHash['find_groups'] ) ) {
$mid = " WHERE UPPER(`group_name`) like ?";
$bindvars[] = "%". strtoupper( $pListHash['find_groups'] ). "%";
} elseif( !empty( $pListHash['find'] ) ) {
$mid = " WHERE UPPER(`group_name`) like ?";
$bindvars[] = "%". strtoupper( $pListHash['find'] ). "%";
if (!empty($pListHash['hide_root_groups'])) {
if ( !empty( $pListHash['is_public'] ) ) {
$mid .= '`is_public`= ?';
$bindvars[] = $pListHash['is_public'];
$query = "SELECT `user_id`, `group_id`, `group_name` , `group_desc`, `group_home`, `is_default`, `is_public`
if( $rs = $this->mDb->query( $query, $bindvars ) ) {
while( $row = $rs->fetchRow() ) {
$groupId = $row['group_id'];
$ret[$groupId]['included'] = $inc;
$query_cant = "select count(*) from `". BIT_DB_PREFIX. "users_groups` $mid";
$cant = $this->mDb->getOne($query_cant, $bindvars);
if( empty( $pUserId ) ) {
$sql = "SELECT ug.`group_id`, ug.* FROM `". BIT_DB_PREFIX. "users_groups` ug
ORDER BY ug.`group_name` ASC";
return $this->mDb->getAssoc($sql, array( $pUserId ) );
if( !empty( $pUserName ) ) {
$id = $this->mDb->getOne("select `user_id` from `". BIT_DB_PREFIX. "users_users` where `login`=?", array($pUserName));
$id = ($id === NULL) ? - 1 : $id;
function get_included_groups($pGroupId) {
$query = "SELECT `include_group_id`, ug.`group_name`
FROM `".BIT_DB_PREFIX."users_groups_inclusion` ugi INNER JOIN `".BIT_DB_PREFIX."users_groups` ug ON (ug.`group_id`=ugi.`group_id`)
return( $this->mDb->getAssoc( $query, array($pGroupId) ) );
function get_user_groups( $pUserId ) {
if (!@$this->verifyId($pUserId)) {
// For legacy calls still using $user as the parameter
$pUserId = $this->get_user_id($pUserId);
if (!isset($this->usergroups_cache[$pUserId])) {
//$userid = $this->get_user_id($user);
$query = "SELECT ug.`group_id`, ug.`group_name`
FROM `".BIT_DB_PREFIX."users_groups_map` ugm INNER JOIN `".BIT_DB_PREFIX."users_groups` ug ON (ug.`group_id`=ugm.`group_id`)
WHERE ugm.`user_id`=? OR ug.`group_name`='Anonymous'";
$ret = $this->mDb->getAssoc($query, array((int)$pUserId));
$this->usergroups_cache[$pUserId] = $ret;
return $this->usergroups_cache[$pUserId];
// we cannot remove the anonymous group
WHERE `group_id` = ? OR `include_group_id` = ?";
$result = $this->mDb->query($query, array($pGroupId, $pGroupId));
$query = "delete from `". BIT_DB_PREFIX. "users_group_permissions` where `group_id` = ?";
$result = $this->mDb->query($query, array($pGroupId));
$query = "delete from `". BIT_DB_PREFIX. "users_groups` where `group_id` = ?";
$result = $this->mDb->query($query, array($pGroupId));
function getGroups( $pUserId= NULL, $pForceRefresh = FALSE ) {
$pUserId = !empty( $pUserId ) ? $pUserId : $this->mUserId;
$query = "SELECT ug.`group_id`, ug.`group_name`, ug.`user_id` as group_owner_user_id
$ret = $this->mDb->getAssoc($query, array((int) $pUserId));
foreach( $res as $key=> $val) {
$ret[$key] = array('group_name' => $val);
$query = "SELECT ugi.`include_group_id`, ug.`group_name`
INNER JOIN `". BIT_DB_PREFIX. "users_groups` ug ON ( ugi.`include_group_id`=ug.`group_id` )
$ret = $this->mDb->getAssoc($query, array($pGroupId));
if( empty( $pIncludes[$groupId] ) ) {
$pIncludes[$groupId] = $ret[$groupId];
$query = "INSERT INTO `". BIT_DB_PREFIX. "users_groups_inclusion` (`group_id`,`include_group_id`)
$this->mDb->query($query, array($pGroupId, $pIncludeId));
$query = "DELETE FROM `". BIT_DB_PREFIX. "users_groups_inclusion` where `group_id` = ?";
$result = $this->mDb->query($query, array($pGroupId));
// pass in pGroupId to make conditional function
if( !empty( $pGroupId ) ) {
$whereSql = "AND `group_id`=? ";
$bindvars = array( $pGroupId );
return( $this->mDb->getAssoc("select `group_id`, `group_name` from `". BIT_DB_PREFIX. "users_groups` where `is_default` = 'y' $whereSql ", $bindvars ) );
$query = "SELECT uu.`user_id` AS hash_key, uu.`login`, uu.`real_name`, uu.`user_id` FROM `". BIT_DB_PREFIX. "users_users` uu INNER JOIN `". BIT_DB_PREFIX. "users_groups_map` ug ON (uu.`user_id`=ug.`user_id`) WHERE `group_id`=?";
return( $this->mDb->getAssoc( $query, array( $pGroupId ) ) );
$query = "SELECT `group_home` FROM `". BIT_DB_PREFIX. "users_groups` WHERE `group_id`=?";
$result = $this->mDb->query( $query,array( $pGroupId ) );
while($res = $result->fetchRow()) {
$ret = $res['group_home'];
$query = "UPDATE `". BIT_DB_PREFIX. "users_users` SET `default_group_id` = ?
$this->mDb->query($query, array( $ |