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

Source for file BitPermUser.php

Documentation is available at BitPermUser.php

  1. <?php
  2. /**
  3.  * $Header$
  4.  *
  5.  * Lib for user administration, groups and permissions
  6.  * This lib uses pear so the constructor requieres
  7.  
  8.  * Copyright (c) 2004 bitweaver.org
  9.  * Copyright (c) 2003 tikwiki.org
  10.  * Copyright (c) 2002-2003, Luis Argerich, Garland Foster, Eduardo Polidor, et. al.
  11.  * All Rights Reserved. See below for details and a complete list of authors.
  12.  * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details
  13.  *
  14.  * $Id$
  15.  * @package users
  16.  */
  17.  
  18. /**
  19.  * required setup
  20.  */
  21. require_once( USERS_PKG_PATH.'/BitUser.php' );
  22.  
  23. /**
  24.  * Class that holds all information for a given user
  25.  *
  26.  * @author   spider <spider@steelsun.com>
  27.  * @version  $Revision$
  28.  * @package  users
  29.  * @subpackage  BitPermUser
  30.  */
  31. class BitPermUser extends BitUser {
  32.  
  33.     var $mPerms;
  34.  
  35.     /**
  36.      * BitPermUser Initialise class
  37.      * 
  38.      * @param numeric $pUserId User ID of the user we wish to load
  39.      * @param numeric $pContentId Content ID of the user we wish to load
  40.      * @access public
  41.      * @return void 
  42.      */
  43.     function BitPermUser( $pUserId=NULL, $pContentId=NULL ) {
  44.         BitUser::BitUser( $pUserId, $pContentId );
  45.  
  46.         // Permission setup
  47.         $this->mAdminContentPerm = 'p_users_admin';
  48.     }
  49.  
  50.     public function __wakeup() {
  51.         parent::__wakeup();
  52.         if( empty( $this->mPerms ) ) {
  53.             $this->loadPermissions();
  54.         }
  55.     }
  56.  
  57.     /**
  58.      * assumeUser Assume the identity of anothre user - Only admins may do this
  59.      * 
  60.      * @param numeric $pUserId User ID of the user you want to hijack
  61.      * @access public
  62.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  63.      */
  64.     function assumeUser( $pUserId ) {
  65.         global $gBitUser;
  66.         $ret = FALSE;
  67.         // make double sure the current logged in user has permission, check for p_users_admin, not admin, as that is all you need for assuming another user.
  68.         // this enables creating of a non technical site adminstrators group, eg customer support representatives.
  69.         if( $gBitUser->hasPermission( 'p_users_admin' ) ) {
  70.             $assumeUser = new BitPermUser( $pUserId );
  71.             $assumeUser->loadPermissions();
  72.             if( $assumeUser->isAdmin() ) {
  73.                 $this->mErrors['assume_user'] = tra( "User administrators cannot be assumed." );
  74.             } else {
  75.                 $this->mDb->query( "UPDATE `".BIT_DB_PREFIX."users_cnxn` SET `user_id`=?, `assume_user_id`=? WHERE `cookie`=?", array( $pUserId, $gBitUser->mUserId, $_COOKIE[$this->getSiteCookieName()] ) );
  76.                 $ret = TRUE;
  77.             }
  78.         }
  79.  
  80.         return $ret;
  81.     }
  82.  
  83.     /**
  84.      * load
  85.      *
  86.      * @param boolean $pFull Load all permissions
  87.      * @param string $pUserName User login name
  88.      * @access public
  89.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  90.      */
  91.     function load( $pFull=FALSE, $pUserName=NULL ) {
  92.         if( BitUser::load( $pFull, $pUserName ) ) {
  93.             if( $pFull ) {
  94.                 unset( $this->mPerms );
  95.                 $this->loadGroups();
  96.                 $this->loadPermissions();
  97.             }
  98.         }
  99.         return( $this->mUserId != NULL );
  100.     }
  101.  
  102.     /**
  103.      * sanitizeUserInfo Used to remove sensitive information from $this->mInfo when it is unneccessary (i.e. $gQueryUser)
  104.      * 
  105.      * @access public
  106.      * @return void 
  107.      */
  108.     function sanitizeUserInfo() {
  109.         if( !empty( $this->mInfo )) {
  110.             $unsanitary = array( 'provpass', 'hash', 'challenge', 'user_password' );
  111.             foreach( array_keys( $this->mInfo ) as $key ) {
  112.                 if( in_array( $key, $unsanitary )) {
  113.                     unset( $this->mInfo[$key] );
  114.                 }
  115.             }
  116.         }
  117.     }
  118.  
  119.     /**
  120.      * store
  121.      * 
  122.      * @param array $pParamHash 
  123.      * @access public
  124.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  125.      */
  126.     function store( &$pParamHash ) {
  127.         global $gBitSystem;
  128.         // keep track of newUser before calling base class
  129.         $newUser = !$this->isRegistered();
  130.         $this->mDb->StartTrans();
  131.         if( BitUser::store( $pParamHash ) && $newUser ) {
  132.             $defaultGroups = $this->getDefaultGroup();
  133.             $this->addUserToGroup( $this->mUserId, $defaultGroups );
  134.             if( $gBitSystem->isFeatureActive( 'users_eponymous_groups' ) ) {
  135.                 // Create a group just for this user, for permissions assignment.
  136.                 $groupParams = array(
  137.                     'user_id' => $this->mUserId,
  138.                     'name'    => $pParamHash['user_store']['login'],
  139.                     'desc'    => "Personal group for ".( !empty( $pParamHash['user_store']['real_name'] ) ? $pParamHash['user_store']['real_name'] : $pParamHash['user_store']['login'] )
  140.                 );
  141.                 if( $this->storeGroup( $groupParams ) ) {
  142.                     $this->addUserToGroup( $this->mUserId, $groupParams['group_id'] );
  143.                 }
  144.             }
  145.             $this->load( TRUE );
  146.  
  147.             // store any uploaded images, this can stuff mErrors, so we want to do this as the very last thing.
  148.             $pParamHash['upload']['thumbnail'] = FALSE;   // i don't think this does anything - perhaps replace it by setting thumbnail_sizes
  149.             $this->storeImages( $pParamHash );
  150.         }
  151.         $this->mDb->CompleteTrans();
  152.         return( count( $this->mErrors ) == 0 );
  153.     }
  154.  
  155.     /**
  156.      * groupExists work out if a given group exists
  157.      * 
  158.      * @param string $pGroupName 
  159.      * @param numeric $pUserId 
  160.      * @access public
  161.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  162.      */
  163.     function groupExists( $pGroupName, $pUserId = ROOT_USER_ID ) {
  164.         static $sGroups = array();
  165.         if( !isset( $sGroups[$pUserId][$pGroupName] ) ) {
  166.             $bindVars = array( $pGroupName );
  167.             $whereSql = '';
  168.             if( $pUserId != '*' ) {
  169.                 $whereSql = 'AND `user_id`=?';
  170.                 $bindVars[] = $pUserId;
  171.             }
  172.             $query = "
  173.                 SELECT ug.`group_name`, ug.`group_id`,  ug.`user_id`
  174.                 FROM `".BIT_DB_PREFIX."users_groups` ug
  175.                 WHERE `group_name`=? $whereSql";
  176.             if( $result = $this->mDb->getAssoc( $query, $bindVars ) ) {
  177.                 if( empty( $sGroups[$pUserId] ) ) {
  178.                     $sGroups[$pUserId] = array();
  179.                 }
  180.                 $sGroups[$pUserId][$pGroupName] = $result[$pGroupName];
  181.             } else {
  182.                 $sGroups[$pUserId][$pGroupName]['group_id'] = NULL;
  183.             }
  184.         }
  185.         return( $sGroups[$pUserId][$pGroupName]['group_id'] );
  186.     }
  187.  
  188.     /**
  189.      * removes user and associated private data
  190.      *
  191.      * @access public
  192.      * @return always FALSE???
  193.      *  TODO: fix return
  194.      */
  195.     function expunge( $pExpungeContent=NULL) {
  196.         global $gBitSystem, $gBitUser;
  197.         if( $this->isValid() ) {
  198.             $this->mDb->StartTrans();
  199.             if( $this->mUserId == $gBitUser->mUserId ) {
  200.                 $this->mDb->RollbackTrans();
  201.                 $gBitSystem->fatalError( tra( 'You cannot delete yourself' ) );
  202.             } elseif( $this->mUserId != ANONYMOUS_USER_ID ) {
  203.                 $userTables = array(
  204.                     'users_groups_map',
  205.                 );
  206.  
  207.                 foreach( $userTables as $table ) {
  208.                     $query = "DELETE FROM `".BIT_DB_PREFIX.$table."` WHERE `user_id` = ?";
  209.                     $result = $this->mDb->query( $query, array( $this->mUserId ) );
  210.                 }
  211.  
  212.                 if( parent::expunge( $pExpungeContent ) ) {
  213.                     $this->mDb->CompleteTrans();
  214.                     return TRUE;
  215.                 } else {
  216.                     $this->mDb->RollbackTrans();
  217.                 }
  218.             } else {
  219.                 $this->mDb->RollbackTrans();
  220.                 $gBitSystem->fatalError( tra( 'The anonymous user cannot be deleted' ) );
  221.             }
  222.         }
  223.         return FALSE;
  224.     }
  225.  
  226.  
  227.  
  228.     // =-=-=-=-=-=-=-=-=-=-=-= Group Functions =-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  229.     /**
  230.      * loadGroups load groups into $this->mGroups
  231.      * 
  232.      * @param boolean $pForceRefresh 
  233.      * @access public
  234.      * @return void 
  235.      */
  236.     function loadGroups( $pForceRefresh = FALSE ) {
  237.         if( $this->isValid() ) {
  238.             $this->mGroups = $this->getGroups( NULL, $pForceRefresh );
  239.         }
  240.     }
  241.  
  242.     /**
  243.      * isInGroup work out if a given user is in a group
  244.      * 
  245.      * @param mixed $pGroupMixed Group ID or Group Name (deprecated)
  246.      * @access public
  247.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  248.      */
  249.     function isInGroup( $pGroupMixed ) {
  250.         $ret = FALSE;
  251.         if( $this->isAdmin() ) {
  252.             $ret = TRUE;
  253.         } if( $this->isValid() ) {
  254.             if( empty( $this->mGroups ) ) {
  255.                 $this->loadGroups();
  256.             }
  257.             if( preg_match( '/A-Za-z/', $pGroupMixed ) ) {
  258.                 // Old style group name passed in
  259.                 deprecated( "Please use the Group ID instead of the Group name." );
  260.                 $ret = in_array( $pGroupMixed, $this->mGroups );
  261.             } else {
  262.                 $ret = isset( $this->mGroups[$pGroupMixed] );
  263.             }
  264.         }
  265.         return $ret;
  266.     }
  267.  
  268.     /**
  269.      * getAllGroups Get a list of all Groups
  270.      * 
  271.      * @param array $pListHash List Hash
  272.      * @access public
  273.      * @return array of groups
  274.      */
  275.     function getAllGroups( &$pListHash ) {
  276.         if( empty(  $pListHash['sort_mode'] ) || $pListHash['sort_mode'] == 'name_asc' ) {
  277.             $pListHash['sort_mode'] = 'group_name_asc';
  278.         }
  279.         LibertyContent::prepGetList( $pListHash );
  280.         $sortMode = $this->mDb->convertSortmode( $pListHash['sort_mode'] );
  281.         if( !empty( $pListHash['find_groups'] ) ) {
  282.             $mid = " AND UPPER(`group_name`) like ?";
  283.             $bindvars[] = "%".strtoupper( $pListHash['find_groups'] )."%";
  284.         } elseif( !empty( $pListHash['find'] ) ) {
  285.             $mid = " AND  UPPER(`group_name`) like ?";
  286.             $bindvars[] = "%".strtoupper( $pListHash['find'] )."%";
  287.         } else {
  288.             $mid = '';
  289.             $bindvars = array();
  290.         }
  291.  
  292.         if( !empty( $pListHash['hide_root_groups'] )) {
  293.             $mid .= ' AND `user_id` <> '.ROOT_USER_ID;
  294.         } elseif( !empty( $pListHash['only_root_groups'] )) {
  295.             $mid .= ' AND `user_id` = '.ROOT_USER_ID;
  296.         }
  297.  
  298.         if( !empty( $pListHash['user_id'] ) ){
  299.             $mid .= ' AND `user_id` = ? ';
  300.             $bindvars[] = $pListHash['user_id'];
  301.         }
  302.         if( !empty( $pListHash['is_public'] ) ) {
  303.             $mid .= ' AND `is_public` = ?';
  304.             $bindvars[] = $pListHash['is_public'];
  305.         }
  306.         if( !empty( $pListHash['visible'] ) && !$this->isAdmin() ){
  307.             global $gBitUser;
  308.             $mid .= ' AND `user_id` = ? OR `is_public` = ? ';
  309.             $bindvars[] = $gBitUser->mUserId;
  310.             $bindvars[] = 'y';
  311.  
  312.         }
  313.  
  314.         $mid =  preg_replace('/^ AND */',' WHERE ', $mid);
  315.  
  316.         $query = "
  317.             SELECT `user_id`, `group_id`, `group_name` , `group_desc`, `group_home`, `is_default`, `is_public`
  318.             FROM `".BIT_DB_PREFIX."users_groups` $mid
  319.             ORDER BY $sortMode";
  320.         $ret = array();
  321.         if( $rs = $this->mDb->query( $query, $bindvars ) ) {
  322.             while( $row = $rs->fetchRow() ) {
  323.                 $groupId = $row['group_id'];
  324.                 $ret[$groupId] = $row;
  325.                 $ret[$groupId]['perms'] = $this->getGroupPermissions( array( 'group_id' => $groupId ));
  326.             }
  327.         }
  328.  
  329.         $pListHash['cant'] = $this->mDb->getOne( "SELECT COUNT(*) FROM `".BIT_DB_PREFIX."users_groups` $mid", $bindvars );
  330.  
  331.         return $ret;
  332.     }
  333.  
  334.     /**
  335.      * getAllUserGroups
  336.      * 
  337.      * @param numeric $pUserId 
  338.      * @access public
  339.      * @return array of groups a user belongs to
  340.      */
  341.     function getAllUserGroups( $pUserId = NULL ) {
  342.         if( empty( $pUserId ) ) {
  343.             $pUserId = $this->mUserId;
  344.         }
  345.  
  346.         $sql = "
  347.             SELECT ug.`group_id` AS `hash_key`, ug.* FROM `".BIT_DB_PREFIX."users_groups` ug
  348.             WHERE `user_id`=?
  349.             ORDER BY ug.`group_name` ASC";
  350.         return $this->mDb->getAssoc( $sql, array( $pUserId ));
  351.     }
  352.  
  353.     /**
  354.      * expungeGroup remove a group
  355.      * 
  356.      * @param numeric $pGroupId 
  357.      * @access public
  358.      * @return TRUE on success, FALSE on failure
  359.      */
  360.     function expungeGroup( $pGroupId ) {
  361.         // we cannot remove the anonymous group
  362.         if( $pGroupId != ANONYMOUS_GROUP_ID ) {
  363.             $query = "DELETE FROM `".BIT_DB_PREFIX."users_groups_map` WHERE `group_id` = ?";
  364.             $result = $this->mDb->query( $query, array( $pGroupId ));
  365.             $query = "DELETE FROM `".BIT_DB_PREFIX."users_group_permissions` WHERE `group_id` = ?";
  366.             $result = $this->mDb->query( $query, array( $pGroupId ));
  367.             $query = "DELETE FROM `".BIT_DB_PREFIX."users_groups` WHERE `group_id` = ?";
  368.             $result = $this->mDb->query( $query, array( $pGroupId ));
  369.             return TRUE;
  370.         }
  371.     }
  372.  
  373.     /**
  374.      * getDefaultGroup get the default group of a given user
  375.      * 
  376.      * @param array $pGroupId pass in a Group ID to make conditional function
  377.      * @access public
  378.      * @return Default Group ID if one is set
  379.      */
  380.     function getDefaultGroup( $pGroupId = NULL ) {
  381.         $bindvars = NULL;
  382.         $whereSql = '';
  383.         if( @BitBase::verifyId( $pGroupId )) {
  384.             $whereSql = "AND `group_id`=? ";
  385.             $bindvars = array( $pGroupId );
  386.         }
  387.         return( $this->mDb->getAssoc( "SELECT `group_id`, `group_name` FROM `".BIT_DB_PREFIX."users_groups` WHERE `is_default` = 'y' $whereSql ", $bindvars ) );
  388.     }
  389.  
  390.     /**
  391.      * getGroupUsers Get a list of users who share a given group id
  392.      * 
  393.      * @param array $pGroupId 
  394.      * @access public
  395.      * @return list of users who are in the group id
  396.      */
  397.     function getGroupUsers( $pGroupId ) {
  398.         $ret = array();
  399.         if( @BitBase::verifyId( $pGroupId )) {
  400.             $query = "
  401.                 SELECT uu.`user_id` AS hash_key, uu.`login`, uu.`real_name`, uu.`user_id`
  402.                 FROM `".BIT_DB_PREFIX."users_users` uu
  403.                 INNER JOIN `".BIT_DB_PREFIX."users_groups_map` ug ON (uu.`user_id`=ug.`user_id`)
  404.                 WHERE `group_id`=?";
  405.             $ret = $this->mDb->getAssoc( $query, array( $pGroupId ));
  406.         }
  407.         return $ret;
  408.     }
  409.  
  410.     /**
  411.      * getGroupHome get the URL where a user of that group should be sent
  412.      * 
  413.      * @param array $pGroupId 
  414.      * @access public
  415.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  416.      */
  417.     function getGroupHome( $pGroupId ) {
  418.         $ret = FALSE;
  419.         if( @BitBase::verifyId( $pGroupId )) {
  420.             $query = "SELECT `group_home` FROM `".BIT_DB_PREFIX."users_groups` WHERE `group_id`=?";
  421.             $ret = $this->mDb->getOne( $query,array( $pGroupId ) );
  422.         }
  423.         return $ret;
  424.     }
  425.  
  426.     /**
  427.      * storeUserDefaultGroup
  428.      * 
  429.      * @param array $pUserId 
  430.      * @param array $pGroupId 
  431.      * @access public
  432.      * @return TRUE on success, FALSE on failure
  433.      */
  434.     function storeUserDefaultGroup( $pUserId, $pGroupId ) {
  435.         if( @BitBase::verifyId( $pUserId ) && @BitBase::verifyId( $pGroupId )) {
  436.             $query = "UPDATE `".BIT_DB_PREFIX."users_users` SET `default_group_id` = ? WHERE `user_id` = ?";
  437.             return $this->mDb->query( $query, array( $pGroupId, $pUserId ));
  438.         }
  439.     }
  440.  
  441.     /**
  442.      * batchAssignUsersToGroup assign all users to a given group
  443.      * 
  444.      * @param array $pGroupId 
  445.      * @access public
  446.      * @return void 
  447.      */
  448.     function batchAssignUsersToGroup( $pGroupId ) {
  449.         if( @BitBase::verifyId( $pGroupId )) {
  450.             $users = $this->getGroupUsers( $pGroupId );
  451.             $result = $this->mDb->getCol( "SELECT uu.`user_id` FROM `".BIT_DB_PREFIX."users_users` uu" );
  452.             foreach( $result as $userId ) {
  453.                 if( empty( $users[$userId] ) && $userId != ANONYMOUS_USER_ID ) {
  454.                     $this->addUserToGroup( $userId, $pGroupId );
  455.                 }
  456.             }
  457.         }
  458.     }
  459.  
  460.     /**
  461.      * batchSetUserDefaultGroup
  462.      * 
  463.      * @param array $pGroupId 
  464.      * @access public
  465.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  466.      */
  467.     function batchSetUserDefaultGroup( $pGroupId ) {
  468.         if( @BitBase::verifyId( $pGroupId )) {
  469.             $users = $this->getGroupUsers($pGroupId);
  470.             foreach( array_keys( $users ) as $userId ) {
  471.                 $this->storeUserDefaultGroup( $userId, $pGroupId );
  472.             }
  473.         }
  474.     }
  475.  
  476.     /**
  477.      * getGroupInfo
  478.      * 
  479.      * @param array $pGroupId 
  480.      * @access public
  481.      * @return group information
  482.      */
  483.     function getGroupInfo( $pGroupId ) {
  484.         if( @BitBase::verifyId( $pGroupId )) {
  485.             $sql = "SELECT * FROM `".BIT_DB_PREFIX."users_groups` WHERE `group_id` = ?";
  486.             $ret = $this->mDb->getRow( $sql, array( $pGroupId ));
  487.  
  488.             $listHash = array(
  489.                 'group_id' => $pGroupId,
  490.                 'sort_mode' => 'up.perm_name_asc',
  491.             );
  492.             $ret["perms"] = $this->getGroupPermissions( $listHash );
  493.  
  494.             $sql = "SELECT COUNT(*) FROM `".BIT_DB_PREFIX."users_groups_map` WHERE `group_id` = ?";
  495.             $ret['num_members'] = $this->mDb->getOne( $sql, array( $pGroupId ));
  496.  
  497.             return $ret;
  498.         }
  499.     }
  500.  
  501.     /**
  502.      * addUserToGroup Adds user pUserId to group(s) pGroupMixed.
  503.      * 
  504.      * @param numeric $pUserId User ID
  505.      * @param mixed $pGroupMixed A single group ID or an array of group IDs
  506.      * @access public
  507.      * @return Either an ADO RecordSet (success) or FALSE (failure).
  508.      */
  509.     function addUserToGroup( $pUserId, $pGroupMixed ) {
  510.         $result = FALSE;
  511.         if( @BitBase::verifyId( $pUserId ) && !empty( $pGroupMixed )) {
  512.             $result = TRUE;
  513.             $addGroups = array();
  514.             if( is_array( $pGroupMixed ) ) {
  515.                 $addGroups = array_keys( $pGroupMixed );
  516.             } elseif( @BitBase::verifyId($pGroupMixed) ) {
  517.                 $addGroups = array( $pGroupMixed );
  518.             }
  519.             $currentUserGroups = $this->getGroups( $pUserId );
  520.             foreach( $addGroups AS $groupId ) {
  521.                 if( !$this->mDb->getOne( "SELECT group_id FROM `".BIT_DB_PREFIX."users_groups_map` WHERE `user_id` = ? AND `group_id` = ?", array( $pUserId, $groupId ) ) ) {
  522.                     $query = "INSERT INTO `".BIT_DB_PREFIX."users_groups_map` (`user_id`,`group_id`) VALUES(?,?)";
  523.                     $result = $this->mDb->query( $query, array( $pUserId, $groupId ));
  524.                 }
  525.             }
  526.         }
  527.         return $result;
  528.     }
  529.  
  530.     /**
  531.      * removeUserFromGroup
  532.      * 
  533.      * @param array $pUserId 
  534.      * @param array $pGroupId 
  535.      * @access public
  536.      * @return void 
  537.      */
  538.     function removeUserFromGroup( $pUserId, $pGroupId ) {
  539.         if( @BitBase::verifyId( $pUserId ) && @BitBase::verifyId( $pGroupId )) {
  540.             $query = "DELETE FROM `".BIT_DB_PREFIX."users_groups_map` WHERE `user_id` = ? AND `group_id` = ?";
  541.             $result = $this->mDb->query( $query, array( $pUserId, $pGroupId ));
  542.             $default = $this->getDefaultGroup();
  543.             if( $pGroupId == key( $default )) {
  544.                 $query = "UPDATE `".BIT_DB_PREFIX."users_users` SET `default_group_id` = NULL WHERE `user_id` = ?";
  545.                 $this->mDb->query( $query, array( $pUserId ));
  546.             }
  547.         }
  548.     }
  549.  
  550.     /**
  551.      * verifyGroup
  552.      * 
  553.      * @param array $pParamHash 
  554.      * @access public
  555.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  556.      */
  557.     function verifyGroup( &$pParamHash ) {
  558.         if( !empty($pParamHash['group_id'] )) {
  559.             if( @$this->verifyId( $pParamHash['group_id'] )) {
  560.                 $pParamHash['group_store']['group_id'] = $pParamHash['group_id'];
  561.             } else {
  562.                 $this->mErrors['groups'] = 'Unknown Group';
  563.             }
  564.         }
  565.  
  566.         if( !empty( $pParamHash["name"] )) {
  567.             $pParamHash['group_store']['group_name'] = substr( $pParamHash["name"], 0, 30 );
  568.         }
  569.         if( !empty( $pParamHash["desc"] )) {
  570.             $pParamHash['group_store']['group_desc'] = substr( $pParamHash["desc"], 0, 255 );;
  571.         }
  572.         $pParamHash['group_store']['group_home']              = !empty( $pParamHash["home"] )                    ? $pParamHash["home"]                    : '';
  573.         $pParamHash['group_store']['is_default']              = !empty( $pParamHash["is_default"] )              ? $pParamHash["is_default"]              : NULL;
  574.         $pParamHash['group_store']['user_id']                 = @$this->verifyId( $pParamHash["user_id"] )       ? $pParamHash["user_id"]                 : $this->mUserId;
  575.         $pParamHash['group_store']['is_public']               = !empty( $pParamHash['is_public'] )               ? $pParamHash['is_public']               : NULL;
  576.         $pParamHash['group_store']['after_registration_page'] = !empty( $pParamHash['after_registration_page'] ) ? $pParamHash['after_registration_page'] : '';
  577.         return( count( $this->mErrors ) == 0 );
  578.     }
  579.  
  580.     /**
  581.      * storeGroup
  582.      * 
  583.      * @param array $pParamHash 
  584.      * @access public
  585.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  586.      */
  587.     function storeGroup( &$pParamHash ) {
  588.         global $gBitSystem;
  589.         if ($this->verifyGroup( $pParamHash)) {
  590.             $this->mDb->StartTrans();
  591.             if( empty( $pParamHash['group_id'] ) ) {
  592.                 $pParamHash['group_id'] = $this->mDb->GenID( 'users_groups_id_seq' );
  593.                 $pParamHash['group_store']['group_id'] = $pParamHash['group_id'];
  594.                 $result = $this->mDb->associateInsert( BIT_DB_PREFIX.'users_groups', $pParamHash['group_store'] );
  595.             } else {
  596.                 $sql = "SELECT COUNT(*) FROM `".BIT_DB_PREFIX."users_groups` WHERE `group_id` = ?";
  597.                 $groupExists = $this->mDb->getOne($sql, array($pParamHash['group_id']));
  598.                 if ($groupExists) {
  599.                     $result = $this->mDb->associateUpdate( BIT_DB_PREFIX.'users_groups', $pParamHash['group_store'], array( "group_id" => $pParamHash['group_id'] ) );
  600.                 } else {
  601.                     // A group_id was specified but that group does not exist yet
  602.                     $pParamHash['group_store']['group_id'] = $pParamHash['group_id'];
  603.                     $result = $this->mDb->associateInsert(BIT_DB_PREFIX.'users_groups', $pParamHash['group_store']);
  604.                 }
  605.             }
  606.  
  607.             if( isset( $_REQUEST['batch_set_default'] ) and $_REQUEST['batch_set_default'] == 'on' ) {
  608.                 $gBitUser->batchSetUserDefaultGroup( $pParamHash['group_id'] );
  609.             }
  610.             $this->mDb->CompleteTrans();
  611.         }
  612.         return ( count( $this->mErrors ) == 0 );
  613.     }
  614.  
  615.     /**
  616.      * getGroupNameFromId
  617.      * 
  618.      * @param array $pGroupId 
  619.      * @param array $pColumns 
  620.      * @access public
  621.      * @return array of group data
  622.      */
  623.     public static function getGroupNameFromId( $pGroupId ) {
  624.         $ret = '';
  625.         if( static::verifyId( $pGroupId ) ) {
  626.             global $gBitDb;
  627.             $ret = $gBitDb->getOne( "SELECT `group_name` FROM `".BIT_DB_PREFIX."users_groups` WHERE `group_id`=?", array( $pGroupId ) );
  628.         }
  629.         return $ret;
  630.     }
  631.  
  632.     /**
  633.      * getGroupUserData
  634.      * 
  635.      * @param array $pGroupId 
  636.      * @param array $pColumns 
  637.      * @access public
  638.      * @return array of group data
  639.      */
  640.     function getGroupUserData( $pGroupId, $pColumns ) {
  641.         $ret = array();
  642.         if( @$this->verifyId( $pGroupId ) && !empty( $pColumns ) ) {
  643.             if( is_array( $pColumns ) ) {
  644.                 $col = implode( $pColumns, ',' );
  645.                 $exec = 'getAssoc';
  646.             } else {
  647.                 $col = '`'.$pColumns.'`';
  648.                 $exec = 'getArray';
  649.             }
  650.             $query = "
  651.                 SELECT $col
  652.                 FROM `".BIT_DB_PREFIX."users_users` uu
  653.                     INNER JOIN `".BIT_DB_PREFIX."users_groups_map` ugm ON (uu.`user_id`=ugm.`user_id`)
  654.                 WHERE ugm.`group_id` = ?";
  655.             $ret = $this->mDb->$exec( $query, array( $pGroupId ));
  656.         }
  657.         return $ret;
  658.     }
  659.  
  660.     // =-=-=-=-=-=-=-=-=-=-=-= PERMISSION FUNCTIONS =-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  661.     /**
  662.      * loadPermissions
  663.      * 
  664.      * @access public
  665.      * @return TRUE on success, FALSE if no perms were loaded
  666.      */
  667.     function loadPermissions( $pForceReload=FALSE ) {
  668.         if( $this->isValid() && (empty( $this->mPerms ) || $pForceReload) ) {
  669.             $this->mPerms = array();
  670.             // the double up.`perm_name` is intentional - the first is for hash key, the second is for hash value
  671.             $query = "
  672.                 SELECT up.`perm_name` AS `hash_key`, up.`perm_name`, up.`perm_desc`, up.`perm_level`, up.`package`
  673.                 FROM `".BIT_DB_PREFIX."users_permissions` up
  674.                     INNER JOIN `".BIT_DB_PREFIX."users_group_permissions` ugp ON ( ugp.`perm_name`=up.`perm_name` )
  675.                     INNER JOIN `".BIT_DB_PREFIX."users_groups` ug ON ( ug.`group_id`=ugp.`group_id` )
  676.                     LEFT OUTER JOIN `".BIT_DB_PREFIX."users_groups_map` ugm ON ( ugm.`group_id`=ugp.`group_id` AND ugm.`user_id` = ? )
  677.                 WHERE ug.`group_id`= ".ANONYMOUS_GROUP_ID." OR ugm.`group_id`=ug.`group_id`";
  678.             $this->mPerms = $this->mDb->getAssoc( $query, array( $this->mUserId ));
  679.             // Add in override permissions
  680.             if( !empty( $this->mPermsOverride ) ) {
  681.                 foreach( $this->mPermsOverride as $key => $val ) {
  682.                     $this->mPerms[$key] = $val;
  683.                 }
  684.             }
  685.         }
  686.         return( count( $this->mPerms ) );
  687.     }
  688.  
  689.     /**
  690.      * getUnassignedPerms
  691.      * 
  692.      * @access public
  693.      * @return array of permissions that have not been assigned to any group yet
  694.      */
  695.     function getUnassignedPerms() {
  696.         $query = "SELECT up.`perm_name` AS `hash_key`, up.*
  697.             FROM `".BIT_DB_PREFIX."users_permissions` up
  698.                 LEFT OUTER JOIN `".BIT_DB_PREFIX."users_group_permissions` ugp ON( up.`perm_name` = ugp.`perm_name` )
  699.             WHERE ugp.`group_id` IS NULL AND up.`perm_name` <> ?
  700.             ORDER BY `package`, up.`perm_name` ASC";
  701.         return( $this->mDb->getAssoc( $query, array( '' )));
  702.     }
  703.  
  704.     /**
  705.      * isAdmin
  706.      * 
  707.      * @param array $pCheckTicket 
  708.      * @access public
  709.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  710.      */
  711.     function isAdmin() {
  712.         // we can't use hasPermission here since it turn into an endless loop
  713.         return( !empty( $this->mPerms['p_admin'] ));
  714.     }
  715.  
  716.     /**
  717.      * hasPermission check to see if a user has a given permission
  718.      * 
  719.      * @param array $pPerm Perm name
  720.      * @access public
  721.      * @return TRUE if the user has a permission, FALSE if they don't
  722.      */
  723.     function hasPermission( $pPerm ) {
  724.         $ret = FALSE;
  725.         if( $this->isAdmin() ) {
  726.             $ret = TRUE;
  727.         } elseif( $this->isValid() ) {
  728.             $ret = isset( $this->mPerms[$pPerm] );
  729.         }
  730.         return ( $ret );
  731.     }
  732.  
  733.     /**
  734.      * verifyPermission check if a user has a given permission and if not
  735.      * it will display the error template and die()
  736.      * @param $pPermission value of a given permission
  737.      * @return none 
  738.      * @access public
  739.      */
  740.     function verifyPermission( $pPermission, $pMsg = NULL ) {
  741.         global $gBitSmarty, $gBitSystem, ${$pPermission};
  742.         if( empty( $pPermission ) || $this->hasPermission( $pPermission ) ) {
  743.             return TRUE;
  744.         } else {
  745.             $gBitSystem->fatalPermission( $pPermission, $pMsg );
  746.         }
  747.     }
  748.  
  749.     /**
  750.      * getGroupPermissions
  751.      * 
  752.      * @param array $pGroupId Group id, if unset, all groups are returned
  753.      * @param string $pPackage permissions to give group, if unset, all permissions are returned
  754.      * @param string $find search for a particular permission
  755.      * @param array $pSortMode sort mode of return hash
  756.      * @access public
  757.      * @return TRUE on success, FALSE on failure
  758.      */
  759.     function getGroupPermissions( $pParamHash = NULL ) {
  760.         global $gBitSystem;
  761.         $ret = $bindVars = array();
  762.         $whereSql = $selectSql = $fromSql = '';
  763.  
  764.         if( !empty( $pParamHash['sort_mode'] )) {
  765.             $sortMode = $this->mDb->convertSortmode( $pParamHash['sort_mode'] );
  766.         } else {
  767.             $sortMode = 'up.`package`, up.`perm_name` ASC';
  768.         }
  769.  
  770.         if( !empty( $pParamHash['package'] )) {
  771.             $whereSql = ' WHERE `package`= ? ';
  772.             $bindVars[] = $pParamHash['package'];
  773.         }
  774.  
  775.         if( @BitBase::verifyId( $pParamHash['group_id'] )) {
  776.             $selectSql = ', ugp.`perm_value` AS `hasPerm` ';
  777.             $fromSql = ' INNER JOIN `'.BIT_DB_PREFIX.'users_group_permissions` ugp ON ( ugp.`perm_name`=up.`perm_name` ) ';
  778.             if( $whereSql ) {
  779.                 $whereSql .= " AND  ugp.`group_id`=?";
  780.             } else {
  781.                 $whereSql .= " WHERE ugp.`group_id`=?";
  782.             }
  783.  
  784.             $bindVars[] = $pParamHash['group_id'];
  785.         }
  786.  
  787.         if( !empty( $pParamHash['find'] )) {
  788.             if( $whereSql ) {
  789.                 $whereSql .= " AND `perm_name` like ?";
  790.             } else {
  791.                 $whereSql .= " WHERE `perm_name` like ?";
  792.             }
  793.             $bindVars[] = '%'.$pParamHash['find'].'%';
  794.         }
  795.  
  796.         // the double up.`perm_name` is intentional - the first is for hash key, the second is for hash value
  797.         $query = "
  798.             SELECT up.`perm_name` AS `hash_key`, up.`perm_name`, up.`perm_desc`, up.`perm_level`, up.`package` $selectSql
  799.             FROM `".BIT_DB_PREFIX."users_permissions` up $fromSql $whereSql
  800.             ORDER BY $sortMode";
  801.         $perms = $this->mDb->getAssoc( $query, $bindVars );
  802.  
  803.         // weed out permissions of inactive packages
  804.         $ret = array();
  805.         foreach( $perms as $key => $perm ) {
  806.             if( $gBitSystem->isPackageActive( $perm['package'] )) {
  807.                 $ret[$key] = $perm;
  808.             }
  809.         }
  810.  
  811.         return $ret;
  812.     }
  813.  
  814.     /**
  815.      * assignLevelPermissions Assign the permissions of a given level to a given group
  816.      * 
  817.      * @param array $pGroupId Group we want to assign permissions to
  818.      * @param array $pLevel permission level we wish to assign from
  819.      * @param array $pPackage limit set of permissions to a given package
  820.      * @access public
  821.      * @return void 
  822.      */
  823.     function assignLevelPermissions( $pGroupId, $pLevel, $pPackage = NULL) {
  824.         if( @BitBase::verifyId( $pGroupId ) && !empty( $pLevel )) {
  825.             $bindvars = array( $pLevel );
  826.             $whereSql = '';
  827.             if( !empty( $pPackage ) ) {
  828.                 $whereSql = ' AND `package`=?';
  829.                 array_push( $bindvars, $pPackage );
  830.             }
  831.             $query = "SELECT `perm_name` FROM `".BIT_DB_PREFIX."users_permissions` WHERE `perm_level` = ? $whereSql";
  832.             if( $result = $this->mDb->query( $query, $bindvars ) ) {
  833.                 while( $row = $result->fetchRow() ) {
  834.                     $this->assignPermissionToGroup( $row['perm_name'], $pGroupId );
  835.                 }
  836.             }
  837.         }
  838.     }
  839.  
  840.     /**
  841.      * getPermissionPackages Get a list of packages that have their own set of permissions
  842.      * 
  843.      * @access public
  844.      * @return array of packages
  845.      */
  846.     function getPermissionPackages() {
  847.         return( $this->mDb->getCol( "SELECT DISTINCT(`package`) FROM `".BIT_DB_PREFIX."users_permissions` ORDER BY `package`" ) );
  848.     }
  849.  
  850.     /**
  851.      * assignPermissionToGroup
  852.      * 
  853.      * @param array $perm 
  854.      * @param array $pGroupId 
  855.      * @access public
  856.      * @return TRUE on success
  857.      */
  858.     function assignPermissionToGroup( $pPerm, $pGroupId ) {
  859.         if( @BitBase::verifyId( $pGroupId ) && !empty( $pPerm )) {
  860.             $query = "DELETE FROM `".BIT_DB_PREFIX."users_group_permissions` WHERE `group_id` = ? AND `perm_name` = ?";
  861.             $result = $this->mDb->query( $query, array( $pGroupId, $pPerm ));
  862.             $query = "INSERT INTO `".BIT_DB_PREFIX."users_group_permissions`(`group_id`, `perm_name`) VALUES(?, ?)";
  863.             $result = $this->mDb->query( $query, array( $pGroupId, $pPerm ));
  864.             return TRUE;
  865.         }
  866.     }
  867.  
  868.     /**
  869.      * removePermissionFromGroup
  870.      * 
  871.      * @param string $pPerm Perm name
  872.      * @param numeric $pGroupId Group ID
  873.      * @access public
  874.      * @return TRUE on success
  875.      */
  876.     function removePermissionFromGroup( $pPerm, $pGroupId ) {
  877.         if( @BitBase::verifyId( $pGroupId ) && !empty( $pPerm )) {
  878.             $query = "DELETE FROM `".BIT_DB_PREFIX."users_group_permissions` WHERE `perm_name` = ? AND `group_id` = ?";
  879.             $result = $this->mDb->query($query, array($pPerm, $pGroupId));
  880.             return TRUE;
  881.         }
  882.     }
  883.  
  884.     /**
  885.      * storeRegistrationChoice
  886.      * 
  887.      * @param mixed $pGroupMixed A single group ID or an array of group IDs
  888.      * @param array $pValue Value you wish to store - use NULL to delete a value
  889.      * @access public
  890.      * @return ADO record set on success, FALSE on failure
  891.      */
  892.     function storeRegistrationChoice( $pGroupMixed, $pValue = NULL ) {
  893.         if( !empty( $pGroupMixed )) {
  894.             $bindVars[] = $pValue;
  895.             if( is_array( $pGroupMixed )) {
  896.                 $mid = implode( ',', array_fill( 0, count( $pGroupMixed ),'?' ));
  897.                 $bindVars = array_merge( $bindVars, $pGroupMixed );
  898.             } else {
  899.                 $bindVars[] = $pGroupMixed;
  900.                 $mid = 'LIKE ?';
  901.             }
  902.             $query = "UPDATE `".BIT_DB_PREFIX."users_groups` SET `is_public`= ? where `group_id` IN ($mid)";
  903.             return $this->mDb->query( $query, $bindVars );
  904.         }
  905.     }
  906.  
  907.     /**
  908.      * Grant a single permission to a given value
  909.      */
  910.     function setPermissionOverride( $pPerm, $pValue = NULL ) {
  911.         if( $this->isAdmin() ) {
  912.             $this->mPerms[$pPerm] = TRUE;
  913.             $this->mPermsOverride[$pPerm] = TRUE;
  914.         } elseif( $this->isValid() ) {
  915.             if( $pValue == 'y' || $pValue == TRUE ) {
  916.                 $this->mPermsOverride[$pPerm] = TRUE;
  917.                 $this->mPerms[$pPerm] = TRUE;
  918.             } else {
  919.                 unset( $this->mPermsOverride[$pPerm] );
  920.                 unset( $this->mPerms[$pPerm] );
  921.             }
  922.         }
  923.     }
  924. }
  925.  
  926. /* vim: :set fdm=marker : */
  927. ?>

Documentation generated on Wed, 29 Jul 2015 13:55:58 +0000 by phpDocumentor 1.5.0-lsces