users
[ class tree: users ] [ index: users ] [ all elements ]

Source for file BitPermUser.php

Documentation is available at BitPermUser.php

  1. <?php
  2. /**
  3.  * $Header: /cvsroot/bitweaver/_bit_users/BitPermUser.php,v 1.51 2007/01/14 23:26:01 hiranchaudhuri Exp $
  4.  *
  5.  * Lib for user administration, groups and permissions
  6.  * This lib uses pear so the constructor requieres
  7.  * a pear DB object
  8.  
  9.  * Copyright (c) 2004 bitweaver.org
  10.  * Copyright (c) 2003 tikwiki.org
  11.  * Copyright (c) 2002-2003, Luis Argerich, Garland Foster, Eduardo Polidor, et. al.
  12.  * All Rights Reserved. See copyright.txt for details and a complete list of authors.
  13.  * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details
  14.  *
  15.  * $Id: BitPermUser.php,v 1.51 2007/01/14 23:26:01 hiranchaudhuri Exp $
  16.  * @package users
  17.  */
  18.  
  19. /**
  20.  * required setup
  21.  */
  22. require_oncedirname__FILE__ ).'/BitUser.php' );
  23.  
  24. /**
  25.  * Class that holds all information for a given user
  26.  *
  27.  * @author   spider <spider@steelsun.com>
  28.  * @version  $Revision: 1.51 $
  29.  * @package  users
  30.  * @subpackage  BitPermUser
  31.  */
  32. class BitPermUser extends BitUser {
  33. # var $db;  // The PEAR db object used to access the database
  34.     // change this to an email address to receive debug emails from the LDAP code
  35.     
  36.     var $debug = false;
  37.     var $usergroups_cache;
  38.     var $groupperm_cache;
  39.  
  40.     function BitPermUser$pUserId=NULL$pContentId=NULL {
  41.         BitUser::BitUser$pUserId$pContentId );
  42.         // Initialize caches
  43.         $this->usergroups_cache = array();
  44.         $this->groupperm_cache = array(array());
  45.     }
  46.  
  47.     function assumeUser$pUserId {
  48.         global $gBitUser$user_cookie_site;
  49.         $ret FALSE;
  50.         // make double sure the current logged in user has permission
  51.         if$gBitUser->hasPermission'p_users_admin' ) ) {
  52.             $assumeUser new BitPermUser$pUserId );
  53.             $assumeUser->loadPermissions();
  54.  
  55.             if$assumeUser->hasPermission'p_users_admin' ) ) {
  56.                 $this->mErrors['assume_user'tra"User administrators cannot be assumed." );
  57.             else {
  58.                 $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) );
  59.                 $ret TRUE;
  60.             }
  61.         }
  62.         return $ret;
  63.     }
  64.  
  65.     function load$pFull=FALSE$pUserName=NULL {
  66.         ifBitUser::load$pFull$pUserName ) ) {
  67.             if$pFull {
  68.                 unset$this->mPerms );
  69.                 $this->loadGroups();
  70.                 $this->loadPermissions();
  71.             }
  72.         }
  73.         return$this->mUserId != NULL );
  74.     }
  75.  
  76.     // Used to hide sensitive information when it is unneccessary (i.e. $gQueryUser)
  77.     
  78.     function sanitizeUserInfo({
  79.         if (!empty($this->mInfo)) {
  80.             if (!empty($this->mInfo['provpass'])) {
  81.                 unset($this->mInfo['provpass']);
  82.             }
  83.             if (!empty($this->mInfo['hash'])) {
  84.                 unset($this->mInfo['hash']);
  85.             }
  86.             if (!empty($this->mInfo['challenge'])) {
  87.                 unset($this->mInfo['challenge']);
  88.             }
  89.             if (!empty($this->mInfo['user_password'])) {
  90.                 unset($this->mInfo['user_password']);
  91.             }
  92.         }
  93.     }
  94.  
  95.     function store&$pParamHash {
  96.         global $gBitSystem;
  97.         // keep track of newUser before calling base class
  98.         $newUser !$this->isRegistered();
  99.         $this->mDb->StartTrans();
  100.         ifBitUser::store$pParamHash && $newUser {
  101.             $defaultGroups $this->getDefaultGroup();
  102.             $this->addUserToGroup$this->mUserId$defaultGroups );
  103.             if$gBitSystem->isFeatureActive'users_eponymous_groups' ) ) {
  104.                 // Create a group just for this user, for permissions assignment.
  105.                 $groupParams array(
  106.                     'user_id' => $this->mUserId,
  107.                     'name' => $pParamHash['user_store']['login'],
  108.                     'desc' => "Personal group for ".(!empty$pParamHash['user_store']['real_name'$pParamHash['user_store']['real_name'$pParamHash['user_store']['login'])
  109.                 );
  110.                 if$this->storeGroup$groupParams ) ) {
  111.                     $this->addUserToGroup$this->mUserId$groupParams['group_id');
  112.                 }
  113.             }
  114.             $this->loadTRUE );
  115.         }
  116.         $this->mDb->CompleteTrans();
  117.         returncount$this->mErrors == );
  118.     }
  119.  
  120.     function groupExists$pGroupName$pUserId=ROOT_USER_ID {
  121.         static $rv array();
  122.         if!isset$rv[$pUserId][$pGroupName) ) {
  123.             $bindVars array$pGroupName );
  124.             $whereSql '';
  125.             if$pUserId != '*' {
  126.                 $whereSql 'AND `user_id`=?';
  127.                 $bindVars[$pUserId;
  128.             }
  129.             $query "SELECT ug.`group_name`, ug.`group_id`,  ug.`user_id`
  130.                       FROM `".BIT_DB_PREFIX."users_groupsug
  131.                       WHERE `group_name`=? $whereSql";
  132.             if$result $this->mDb->getAssoc$query$bindVars ) ) {
  133.                 ifempty$rv[$pUserId) ) {
  134.                     $rv[$pUserIdarray();
  135.                 }
  136.                 $rv[$pUserId][$pGroupName$result[$pGroupName];
  137.             else {
  138.                 $rv[$pUserId][$pGroupName]['group_id'NULL;
  139.             }
  140.         }
  141.         return$rv[$pUserId][$pGroupName]['group_id');
  142.     }
  143.  
  144.     /**
  145.      * removes user and associated private data
  146.      *
  147.      * @access public
  148.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  149.      */
  150.     function expunge({
  151.         global $gBitSystem$gBitUser;
  152.         if$this->isValid() ) {
  153.             $this->mDb->StartTrans();
  154.             if$this->mUserId == $gBitUser->mUserId {
  155.                 $this->mDb->RollbackTrans();
  156.                 $gBitSystem->fatalErrortra'You cannot delete yourself' ) );
  157.             elseif$this->mUserId != ANONYMOUS_USER_ID {
  158.                 $userTables array(
  159.                     'users_groups_map',
  160.                 );
  161.  
  162.                 foreach$userTables as $table {
  163.                     $query "DELETE FROM `".BIT_DB_PREFIX.$table."` WHERE `user_id` = ?";
  164.                     $result $this->mDb->query$queryarray$this->mUserId ) );
  165.                 }
  166.  
  167.                 ifBitUser::expunge$this->mUserId ) ) {
  168.                     $this->mDb->CompleteTrans();
  169.                     return TRUE;
  170.                 else {
  171.                     $this->mDb->RollbackTrans();
  172.                 }
  173.             else {
  174.                 $this->mDb->RollbackTrans();
  175.                 $gBitSystem->fatalErrortra'The anonymous user cannot be deleted' ) );
  176.             }
  177.         }
  178.         return FALSE;
  179.     }
  180.     // =-=-=-=-=-=-=-=-=-=-=-= GROUP FUNCTIONS =-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  181.  
  182.     
  183.  
  184.     function loadGroups$pForceRefresh FALSE {
  185.         if$this->isValid() ) {
  186.             $this->mGroups = $this->getGroupsNULL$pForceRefresh );
  187.         }
  188.     }
  189.  
  190.     function isInGroup$pGroupMixed {
  191.         $ret FALSE;
  192.         if$this->isAdmin() ) {
  193.             $ret TRUE;
  194.         if$this->isValid() ) {
  195.             ifempty$this->mGroups ) ) {
  196.                 $this->loadGroups();
  197.             }
  198.             ifpreg_match'/A-Za-z/'$pGroupMixed ) ) {
  199.                 // Old style group name passed in
  200.                 $ret in_array$pGroupMixed$this->mGroups );
  201.             else {
  202.                 $ret = isset$this->mGroups[$pGroupMixed);
  203.             }
  204.         }
  205.         return $ret;
  206.     }
  207.  
  208.     function getAllGroups&$pListHash {
  209.         ifempty(  $pListHash['sort_mode'|| $pListHash['sort_mode'== 'name_asc' {
  210.              $pListHash['sort_mode''group_name_asc';
  211.         }
  212.         $this->prepGetList$pListHash );
  213.  
  214.         $sortMode $this->mDb->convertSortmode$pListHash['sort_mode');
  215.         if!empty$pListHash['find_groups') ) {
  216.             $mid " WHERE UPPER(`group_name`) like ?";
  217.             $bindvars["%".strtoupper$pListHash['find_groups')."%";
  218.         elseif!empty$pListHash['find') ) {
  219.             $mid " WHERE UPPER(`group_name`) like ?";
  220.             $bindvars["%".strtoupper$pListHash['find')."%";
  221.         else {
  222.             $mid '';
  223.             $bindvars array();
  224.         }
  225.  
  226.         if (!empty($pListHash['hide_root_groups'])) {
  227.             if (strlen($mid0{
  228.                 $mid .= ' AND `user_id` <> '.ROOT_USER_ID;
  229.             else {
  230.                 $mid " WHERE `user_id` <> ".ROOT_USER_ID;
  231.             }
  232.         }
  233.         if !empty$pListHash['is_public') ) {
  234.             if (strlen($mid0{
  235.                 $mid .= ' AND ';
  236.             else {
  237.                 $mid 'WHERE ';
  238.             }
  239.             $mid .= '`is_public`= ?';
  240.             $bindvars[$pListHash['is_public'];
  241.         }
  242.  
  243.         $query "SELECT `user_id`, `group_id`, `group_name` , `group_desc`, `group_home`, `is_default`, `is_public`
  244.                   FROM `".BIT_DB_PREFIX."users_groups$mid
  245.                   ORDER BY $sortMode";
  246.         $ret array();
  247.         if$rs $this->mDb->query$query$bindvars ) ) {
  248.             while$row $rs->fetchRow() ) {
  249.                 $groupId $row['group_id'];
  250.                 $ret[$groupId$row;
  251.                 $ret[$groupId]['perms'$this->getGroupPermissions$groupId );
  252.                 $inc array();
  253.                 $this->getIncludedGroups$groupId$inc );
  254.                 $ret[$groupId]['included'$inc;
  255.             }
  256.         }
  257.         $query_cant "select count(*) from `".BIT_DB_PREFIX."users_groups$mid";
  258.         $cant $this->mDb->getOne($query_cant$bindvars);
  259.         $retval array();
  260.         $retval["data"$ret;
  261.         $retval["cant"$cant;
  262.         return $retval;
  263.     }
  264.  
  265.     function getAllUserGroups$pUserId=NULL {
  266.         ifempty$pUserId ) ) {
  267.             $pUserId $this->mUserId;
  268.         }
  269.  
  270.         $sql "SELECT ug.`group_id`, ug.* FROM `".BIT_DB_PREFIX."users_groups` ug
  271.                 WHERE `user_id`=?
  272.                 ORDER BY ug.`group_name` ASC";
  273.         return $this->mDb->getAssoc($sqlarray$pUserId ) );
  274.     }
  275.  
  276.     function get_user_id$pUserName {
  277.         if!empty$pUserName ) ) {
  278.             $id $this->mDb->getOne("select `user_id` from `".BIT_DB_PREFIX."users_users` where `login`=?"array($pUserName));
  279.             $id ($id === NULL? -$id;
  280.             return $id;
  281.         }
  282.     }
  283. /*
  284.     function get_included_groups($pGroupId) {
  285.         $query = "SELECT `include_group_id`, ug.`group_name`
  286.                   FROM `".BIT_DB_PREFIX."users_groups_inclusion` ugi INNER JOIN `".BIT_DB_PREFIX."users_groups` ug ON (ug.`group_id`=ugi.`group_id`)
  287.                   WHERE ugi.`group_id`=?";
  288.         return( $this->mDb->getAssoc( $query, array($pGroupId) ) );
  289.     }
  290.  
  291.     function get_user_groups( $pUserId ) {
  292.         if (!@$this->verifyId($pUserId)) {
  293.             // For legacy calls still using $user as the parameter
  294.             $pUserId = $this->get_user_id($pUserId);
  295.         }
  296.         if (!isset($this->usergroups_cache[$pUserId])) {
  297.             //$userid = $this->get_user_id($user);
  298.             $query = "SELECT ug.`group_id`, ug.`group_name`
  299.                       FROM `".BIT_DB_PREFIX."users_groups_map` ugm INNER JOIN `".BIT_DB_PREFIX."users_groups` ug ON (ug.`group_id`=ugm.`group_id`)
  300.                       WHERE ugm.`user_id`=? OR ug.`group_name`='Anonymous'";
  301.             $ret = $this->mDb->getAssoc($query, array((int)$pUserId));
  302.             // cache it
  303.             $this->usergroups_cache[$pUserId] = $ret;
  304.             return $ret;
  305.         } else {
  306.             return $this->usergroups_cache[$pUserId];
  307.         }
  308.     }
  309. */
  310.  
  311.     // we cannot remove the anonymous group
  312.     
  313.     function remove_group($pGroupId{
  314.         if$pGroupId != ANONYMOUS_GROUP_ID {
  315.             $query "DELETE FROM `".BIT_DB_PREFIX."users_groups_inclusion`
  316.                       WHERE `group_id` = ? OR `include_group_id` = ?";
  317.             $result $this->mDb->query($queryarray($pGroupId$pGroupId));
  318.             $query "delete from `".BIT_DB_PREFIX."users_group_permissions` where `group_id` = ?";
  319.             $result $this->mDb->query($queryarray($pGroupId));
  320.             $query "delete from `".BIT_DB_PREFIX."users_groups` where `group_id` = ?";
  321.             $result $this->mDb->query($queryarray($pGroupId));
  322.             return true;
  323.         }
  324.     }
  325.  
  326.     function getGroups$pUserId=NULL$pForceRefresh FALSE {
  327.         $pUserId !empty$pUserId $pUserId $this->mUserId;
  328.         if (!isset($this->usergroups_cache[$pUserId]|| $pForceRefresh{
  329.             $query "SELECT ug.`group_id`, ug.`group_name`, ug.`user_id` as group_owner_user_id
  330.                       FROM `".BIT_DB_PREFIX."users_groups_map` ugm INNER JOIN `".BIT_DB_PREFIX."users_groups` ug ON (ug.`group_id`=ugm.`group_id`)
  331.                       WHERE ugm.`user_id`=? OR ugm.`group_id`=".ANONYMOUS_GROUP_ID;
  332.             $ret $this->mDb->getAssoc($queryarray((int)$pUserId));
  333.             if$ret {
  334.                 foreacharray_keys$ret as $groupId {
  335.                     $res array();
  336.                     $this->getIncludedGroups$groupId$res );
  337.                     foreach$res as $key=>$val{
  338.                         $ret[$keyarray('group_name' => $val);
  339.                     }
  340.                 }
  341.             }
  342.             // cache it
  343.             $this->usergroups_cache[$pUserId$ret;
  344.             return $ret;
  345.         else {
  346.             return $this->usergroups_cache[$pUserId];
  347.         }
  348.     }
  349.  
  350.     function getIncludedGroups$pGroupId&$pIncludes {
  351.         $query "SELECT ugi.`include_group_id`, ug.`group_name`
  352.                   FROM `".BIT_DB_PREFIX."users_groups_inclusion` ugi
  353.                       INNER JOIN `".BIT_DB_PREFIX."users_groups` ug ON ( ugi.`include_group_id`=ug.`group_id` )
  354.                   WHERE ugi.`group_id`=?";
  355.         $ret $this->mDb->getAssoc($queryarray($pGroupId));
  356.         if$ret {
  357.             foreacharray_keys$ret as $groupId {
  358.                 ifempty$pIncludes[$groupId) ) {
  359.                     $pIncludes[$groupId$ret[$groupId];
  360.                     $this->getIncludedGroups$groupId$pIncludes );
  361.                 }
  362.             }
  363.         }
  364.     }
  365.  
  366.     function addGroupInclusion$pGroupId$pIncludeId {
  367.         if@BitBase::verifyId$pGroupId && @BitBase::verifyId$pIncludeId )  ) {
  368.             $query "INSERT INTO `".BIT_DB_PREFIX."users_groups_inclusion` (`group_id`,`include_group_id`)
  369.                       VALUES(?,?)";
  370.             $this->mDb->query($queryarray($pGroupId$pIncludeId));
  371.         }
  372.     }
  373.  
  374.     function removeGroupInclusions$pGroupId {
  375.         if@BitBase::verifyId$pGroupId ) ) {
  376.             $query "DELETE FROM `".BIT_DB_PREFIX."users_groups_inclusion` where `group_id` = ?";
  377.             $result $this->mDb->query($queryarray($pGroupId));
  378.         }
  379.         return true;
  380.     }
  381.  
  382.  
  383.     // pass in pGroupId to make conditional function
  384.     
  385.     function getDefaultGroup$pGroupId=NULL {
  386.         $bindvars NULL;
  387.         $whereSql '';
  388.         if!empty$pGroupId ) ) {
  389.             $whereSql "AND `group_id`=? ";
  390.             $bindvars array$pGroupId );
  391.         }
  392.         return$this->mDb->getAssoc("select `group_id`, `group_name` from `".BIT_DB_PREFIX."users_groupswhere `is_default` = 'y$whereSql "$bindvars ) );
  393.     }
  394.  
  395.     function get_group_users$pGroupId {
  396.         $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`=?";
  397.         return$this->mDb->getAssoc$queryarray$pGroupId ) ) );
  398.     }
  399.  
  400.     function getGroupHome$pGroupId {
  401.         $ret FALSE;
  402.         $query "SELECT `group_home` FROM `".BIT_DB_PREFIX."users_groups` WHERE `group_id`=?";
  403.         $result $this->mDb->query$query,array$pGroupId ) );
  404.         while($res $result->fetchRow()) {
  405.             $ret $res['group_home'];
  406.         }
  407.         return $ret;
  408.     }
  409.  
  410.     function storeUserDefaultGroup$pUserId$pGroupId {
  411.         $query "UPDATE `".BIT_DB_PREFIX."users_users` SET `default_group_id` = ?
  412.                   WHERE `user_id` = ?";
  413.         $this->mDb->query($queryarray$