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

Source for file RolePermUser.php

Documentation is available at RolePermUser.php

  1. <?php
  2. /**
  3.  * $Header$
  4.  *
  5.  * Lib for user administration, roles 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.'/RoleUser.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  RolePermUser
  30.  */
  31. class RolePermUser extends BitUser {
  32.  
  33.     var $mPerms;
  34.  
  35.     /**
  36.      * RolePermUser 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 RolePermUser( $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 role, eg customer support representatives.
  69.         if( $gBitUser->hasPermission( 'p_users_admin' ) ) {
  70.             $assumeUser = new RolePermUser( $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->loadRoles();
  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.             $defaultRoles = $this->getDefaultRole();
  133.             $this->addUserToRole( $this->mUserId, $defaultRoles );
  134.             if( $gBitSystem->isFeatureActive( 'users_eponymous_roles' ) ) {
  135.                 // Create a role just for this user, for permissions assignment.
  136.                 $roleParams = array(
  137.                     'user_id' => $this->mUserId,
  138.                     'name'    => $pParamHash['user_store']['login'],
  139.                     'desc'    => "Personal role for ".( !empty( $pParamHash['user_store']['real_name'] ) ? $pParamHash['user_store']['real_name'] : $pParamHash['user_store']['login'] )
  140.                 );
  141.                 if( $this->storeRole( $roleParams ) ) {
  142.                     $this->addUserToRole( $this->mUserId, $roleParams['role_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.      * roleExists work out if a given role exists
  157.      * 
  158.      * @param string $pRoleName 
  159.      * @param numeric $pUserId 
  160.      * @access public
  161.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  162.      */
  163.     function roleExists( $pRoleName, $pUserId = ROOT_USER_ID ) {
  164.         static $sRoles = array();
  165.         if( !isset( $sRoles[$pUserId][$pRoleName] ) ) {
  166.             $bindVars = array( $pRoleName );
  167.             $whereSql = '';
  168.             if( $pUserId != '*' ) {
  169.                 $whereSql = 'AND `user_id`=?';
  170.                 $bindVars[] = $pUserId;
  171.             }
  172.             $query = "
  173.                 SELECT ur.`role_name`, ur.`role_id`,  ur.`user_id`
  174.                 FROM `".BIT_DB_PREFIX."users_roles` ur
  175.                 WHERE `role_name`=? $whereSql";
  176.             if( $result = $this->mDb->getAssoc( $query, $bindVars ) ) {
  177.                 if( empty( $sRoles[$pUserId] ) ) {
  178.                     $sRoles[$pUserId] = array();
  179.                 }
  180.                 $sRoles[$pUserId][$pRoleName] = $result[$pRoleName];
  181.             } else {
  182.                 $sRoles[$pUserId][$pRoleName]['role_id'] = NULL;
  183.             }
  184.         }
  185.         return( $sRoles[$pUserId][$pRoleName]['role_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_roles_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.     // =-=-=-=-=-=-=-=-=-=-=-= Role Functions =-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  229.     /**
  230.      * loadRoles load roles into $this->mRoles
  231.      * 
  232.      * @param boolean $pForceRefresh 
  233.      * @access public
  234.      * @return void 
  235.      */
  236.     function loadRoles( $pForceRefresh = FALSE ) {
  237.         if( $this->isValid() ) {
  238.             $this->mRoles = $this->getRoles( NULL, $pForceRefresh );
  239.         }
  240.     }
  241.  
  242.     /**
  243.      * isInRole work out if a given user is assigned to a role
  244.      * 
  245.      * @param mixed $pRoleMixed Role ID or Role Name (deprecated)
  246.      * @access public
  247.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  248.      */
  249.     function isInRole( $pRoleMixed ) {
  250.         $ret = FALSE;
  251.         if( $this->isAdmin() ) {
  252.             $ret = TRUE;
  253.         } if( $this->isValid() ) {
  254.             if( empty( $this->mRoles ) ) {
  255.                 $this->loadRoles();
  256.             }
  257.             if( preg_match( '/A-Za-z/', $pRoleMixed ) ) {
  258.                 // Old style role name passed in
  259.                 deprecated( "Please use the Role ID instead of the Role name." );
  260.                 $ret = in_array( $pRoleMixed, $this->mRoles );
  261.             } else {
  262.                 $ret = isset( $this->mRoles[$pRoleMixed] );
  263.             }
  264.         }
  265.         return $ret;
  266.     }
  267.  
  268.     /**
  269.      * getAllRoless Get a list of all Roles
  270.      * 
  271.      * @param array $pListHash List Hash
  272.      * @access public
  273.      * @return array of roles
  274.      */
  275.     function getAllRoles( &$pListHash ) {
  276.         if( empty(  $pListHash['sort_mode'] ) || $pListHash['sort_mode'] == 'name_asc' ) {
  277.             $pListHash['sort_mode'] = 'role_name_asc';
  278.         }
  279.         LibertyContent::prepGetList( $pListHash );
  280.         $sortMode = $this->mDb->convertSortmode( $pListHash['sort_mode'] );
  281.         if( !empty( $pListHash['find_roles'] ) ) {
  282.             $mid = " AND UPPER(`role_name`) like ?";
  283.             $bindvars[] = "%".strtoupper( $pListHash['find_roles'] )."%";
  284.         } elseif( !empty( $pListHash['find'] ) ) {
  285.             $mid = " AND  UPPER(`role_name`) like ?";
  286.             $bindvars[] = "%".strtoupper( $pListHash['find'] )."%";
  287.         } else {
  288.             $mid = '';
  289.             $bindvars = array();
  290.         }
  291.  
  292.         if( !empty( $pListHash['hide_root_roles'] )) {
  293.             $mid .= ' AND `user_id` <> '.ROOT_USER_ID;
  294.         } elseif( !empty( $pListHash['only_root_roles'] )) {
  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`, `role_id`, `role_name` , `role_desc`, `role_home`, `is_default`, `is_public`
  318.             FROM `".BIT_DB_PREFIX."users_roles` $mid
  319.             ORDER BY $sortMode";
  320.         $ret = array();
  321.         if( $rs = $this->mDb->query( $query, $bindvars ) ) {
  322.             while( $row = $rs->fetchRow() ) {
  323.                 $roleId = $row['role_id'];
  324.                 $ret[$roleId] = $row;
  325.                 $ret[$roleId]['perms'] = $this->getRolePermissions( array( 'role_id' => $roleId ));
  326.             }
  327.         }
  328.  
  329.         $pListHash['cant'] = $this->mDb->getOne( "SELECT COUNT(*) FROM `".BIT_DB_PREFIX."users_roles` $mid", $bindvars );
  330.  
  331.         return $ret;
  332.     }
  333.  
  334.     /**
  335.      * getAllUserRoles
  336.      * 
  337.      * @param numeric $pUserId 
  338.      * @access public
  339.      * @return array of roles a user belongs to
  340.      */
  341.     function getAllUserRoles( $pUserId = NULL ) {
  342.         if( empty( $pUserId ) ) {
  343.             $pUserId = $this->mUserId;
  344.         }
  345.  
  346.         $sql = "
  347.             SELECT ur.`role_id` AS `hash_key`, ur.* FROM `".BIT_DB_PREFIX."users_roles` ur
  348.             WHERE `user_id`=?
  349.             ORDER BY ur.`role_name` ASC";
  350.         return $this->mDb->getAssoc( $sql, array( $pUserId ));
  351.     }
  352.  
  353.     /**
  354.      * expungeRole remove a role
  355.      * 
  356.      * @param numeric $pRoleId 
  357.      * @access public
  358.      * @return TRUE on success, FALSE on failure
  359.      */
  360.     function expungeRole( $pRoleId ) {
  361.         // we cannot remove the anonymous role
  362.         if( $pRoleId != ANONYMOUS_TEAM_ID ) {
  363.             $query = "DELETE FROM `".BIT_DB_PREFIX."users_roles_map` WHERE `role_id` = ?";
  364.             $result = $this->mDb->query( $query, array( $pRoleId ));
  365.             $query = "DELETE FROM `".BIT_DB_PREFIX."users_role_permissions` WHERE `role_id` = ?";
  366.             $result = $this->mDb->query( $query, array( $pRoleId ));
  367.             $query = "DELETE FROM `".BIT_DB_PREFIX."users_roles` WHERE `role_id` = ?";
  368.             $result = $this->mDb->query( $query, array( $pRoleId ));
  369.             return TRUE;
  370.         }
  371.     }
  372.  
  373.     /**
  374.      * getDefaultRole get the default role of a given user
  375.      * 
  376.      * @param array $pRoleId pass in a Role ID to make conditional function
  377.      * @access public
  378.      * @return Default Role ID if one is set
  379.      */
  380.     function getDefaultRole( $pRoleId = NULL ) {
  381.         $bindvars = NULL;
  382.         $whereSql = '';
  383.         if( @BitBase::verifyId( $pRoleId )) {
  384.             $whereSql = "AND `role_id`=? ";
  385.             $bindvars = array( $pRoleId );
  386.         }
  387.         return( $this->mDb->getAssoc( "SELECT `role_id`, `role_name` FROM `".BIT_DB_PREFIX."users_roles` WHERE `is_default` = 'y' $whereSql ", $bindvars ) );
  388.     }
  389.  
  390.     /**
  391.      * getRoleUsers Get a list of users who share a given role id
  392.      * 
  393.      * @param array $pRoleId 
  394.      * @access public
  395.      * @return list of users who are in the role id
  396.      */
  397.     function getRoleUsers( $pRoleId ) {
  398.         $ret = array();
  399.         if( @BitBase::verifyId( $pRoleId )) {
  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_roles_map` ur ON (uu.`user_id`=ur.`user_id`)
  404.                 WHERE `role_id`=?";
  405.             $ret = $this->mDb->getAssoc( $query, array( $pRoleId ));
  406.         }
  407.         return $ret;
  408.     }
  409.  
  410.     /**
  411.      * getHomeRole get the URL where a user of that role should be sent
  412.      * 
  413.      * @param array $pRoleId 
  414.      * @access public
  415.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  416.      */
  417.     function getHomeRole( $pRoleId ) {
  418.         $ret = FALSE;
  419.         if( @BitBase::verifyId( $pRoleId )) {
  420.             $query = "SELECT `role_home` FROM `".BIT_DB_PREFIX."users_roles` WHERE `role_id`=?";
  421.             $ret = $this->mDb->getOne( $query,array( $pRoleId ) );
  422.         }
  423.         return $ret;
  424.     }
  425.  
  426.     /**
  427.      * storeUserDefaultRole
  428.      * 
  429.      * @param array $pUserId 
  430.      * @param array $pRoleId 
  431.      * @access public
  432.      * @return TRUE on success, FALSE on failure
  433.      */
  434.     function storeUserDefaultRole( $pUserId, $pRoleId ) {
  435.         if( @BitBase::verifyId( $pUserId ) && @BitBase::verifyId( $pRoleId )) {
  436.             $query = "UPDATE `".BIT_DB_PREFIX."users_users` SET `default_role_id` = ? WHERE `user_id` = ?";
  437.             return $this->mDb->query( $query, array( $pRoleId, $pUserId ));
  438.         }
  439.     }
  440.  
  441.     /**
  442.      * batchAssignUsersToRole assign all users to a given role
  443.      * 
  444.      * @param array $pRoleId 
  445.      * @access public
  446.      * @return void 
  447.      */
  448.     function batchAssignUsersToRole( $pRoleId ) {
  449.         if( @BitBase::verifyId( $pRoleId )) {
  450.             $users = $this->getRoleUsers( $pRoleId );
  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->addUserToRole( $userId, $pRoleId );
  455.                 }
  456.             }
  457.         }
  458.     }
  459.  
  460.     /**
  461.      * batchSetUserDefaultRole
  462.      * 
  463.      * @param array $pRoleId 
  464.      * @access public
  465.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  466.      */
  467.     function batchSetUserDefaultRole( $pRoleId ) {
  468.         if( @BitBase::verifyId( $pRoleId )) {
  469.             $users = $this->getRoleUsers($pRoleId);
  470.             foreach( array_keys( $users ) as $userId ) {
  471.                 $this->storeUserDefaultRole( $userId, $pRoleId );
  472.             }
  473.         }
  474.     }
  475.  
  476.     /**
  477.      * getRoleInfo
  478.      * 
  479.      * @param array $pRoleId 
  480.      * @access public
  481.      * @return role information
  482.      */
  483.     function getRoleInfo( $pRoleId ) {
  484.         if( @BitBase::verifyId( $pRoleId )) {
  485.             $sql = "SELECT * FROM `".BIT_DB_PREFIX."users_roles` WHERE `role_id` = ?";
  486.             $ret = $this->mDb->getRow( $sql, array( $pRoleId ));
  487.  
  488.             $listHash = array(
  489.                 'role_id' => $pRoleId,
  490.                 'sort_mode' => 'up.perm_name_asc',
  491.             );
  492.             $ret["perms"] = $this->getRolePermissions( $listHash );
  493.  
  494.             $sql = "SELECT COUNT(*) FROM `".BIT_DB_PREFIX."users_roles_map` WHERE `role_id` = ?";
  495.             $ret['num_members'] = $this->mDb->getOne( $sql, array( $pRoleId ));
  496.  
  497.             return $ret;
  498.         }
  499.     }
  500.  
  501.     /**
  502.      * addUserToRole Adds user pUserId to role(s) pRoleMixed.
  503.      * 
  504.      * @param numeric $pUserId User ID
  505.      * @param mixed $pRoleMixed A single role ID or an array of role IDs
  506.      * @access public
  507.      * @return Either an ADO RecordSet (success) or FALSE (failure).
  508.      */
  509.     function addUserToRole( $pUserId, $pRoleMixed ) {
  510.         $result = FALSE;
  511.         if( @BitBase::verifyId( $pUserId ) && !empty( $pRoleMixed )) {
  512.             $result = TRUE;
  513.             $addRoles = array();
  514.             if( is_array( $pRoleMixed ) ) {
  515.                 $addRoles = array_keys( $pRoleMixed );
  516.             } elseif( @BitBase::verifyId($pRoleMixed) ) {
  517.                 $addRoles = array( $pRoleMixed );
  518.             }
  519.             $currentUserRoles = $this->getRoles( $pUserId );
  520.             foreach( $addRoles AS $roleId ) {
  521.                 $isInRole = FALSE;
  522.                 if( $currentUserRoles ) {
  523.                     foreach( $currentUserRoles as $curRoleId => $curRoleInfo ) {
  524.                         if( $curRoleId == $roleId ) {
  525.                             $isInRole = TRUE;
  526.                         }
  527.                     }
  528.                 }
  529.                 if( !$isInRole ) {
  530.                     $query = "INSERT INTO `".BIT_DB_PREFIX."users_roles_map` (`user_id`,`role_id`) VALUES(?,?)";
  531.                     $result = $this->mDb->query( $query, array( $pUserId, $roleId ));
  532.                 }
  533.             }
  534.         }
  535.         return $result;
  536.     }
  537.  
  538.     /**
  539.      * removeUserFromRole
  540.      * 
  541.      * @param array $pUserId 
  542.      * @param array $pRoleId 
  543.      * @access public
  544.      * @return void 
  545.      */
  546.     function removeUserFromRole( $pUserId, $pRoleId ) {
  547.         if( @BitBase::verifyId( $pUserId ) && @BitBase::verifyId( $pRoleId )) {
  548.             $query = "DELETE FROM `".BIT_DB_PREFIX."users_roles_map` WHERE `user_id` = ? AND `role_id` = ?";
  549.             $result = $this->mDb->query( $query, array( $pUserId, $pRoleId ));
  550.             $default = $this->getDefaultRole();
  551.             if( $pRoleId == key( $default )) {
  552.                 $query = "UPDATE `".BIT_DB_PREFIX."users_users` SET `default_role_id` = NULL WHERE `user_id` = ?";
  553.                 $this->mDb->query( $query, array( $pUserId ));
  554.             }
  555.         }
  556.     }
  557.  
  558.     /**
  559.      * verifyRole
  560.      * 
  561.      * @param array $pParamHash 
  562.      * @access public
  563.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  564.      */
  565.     function verifyRole( &$pParamHash ) {
  566.         if( !empty($pParamHash['role_id'] )) {
  567.             if( @$this->verifyId( $pParamHash['role_id'] )) {
  568.                 $pParamHash['role_store']['role_id'] = $pParamHash['role_id'];
  569.             } else {
  570.                 $this->mErrors['roles'] = 'Unknown Role';
  571.             }
  572.         }
  573.  
  574.         if( !empty( $pParamHash["name"] )) {
  575.             $pParamHash['role_store']['role_name'] = substr( $pParamHash["name"], 0, 30 );
  576.         }
  577.         if( !empty( $pParamHash["desc"] )) {
  578.             $pParamHash['role_store']['role_desc'] = substr( $pParamHash["desc"], 0, 255 );;
  579.         }
  580.         $pParamHash['role_store']['role_home']              = !empty( $pParamHash["home"] )                    ? $pParamHash["home"]                    : '';
  581.         $pParamHash['role_store']['is_default']              = !empty( $pParamHash["is_default"] )              ? $pParamHash["is_default"]              : NULL;
  582.         $pParamHash['role_store']['user_id']                 = @$this->verifyId( $pParamHash["user_id"] )       ? $pParamHash["user_id"]                 : $this->mUserId;
  583.         $pParamHash['role_store']['is_public']               = !empty( $pParamHash['is_public'] )               ? $pParamHash['is_public']               : NULL;
  584.         $pParamHash['role_store']['after_registration_page'] = !empty( $pParamHash['after_registration_page'] ) ? $pParamHash['after_registration_page'] : '';
  585.         return( count( $this->mErrors ) == 0 );
  586.     }
  587.  
  588.     /**
  589.      * storeRole
  590.      * 
  591.      * @param array $pParamHash 
  592.      * @access public
  593.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  594.      */
  595.     function storeRole( &$pParamHash ) {
  596.         global $gBitSystem;
  597.         if ($this->verifyRole( $pParamHash)) {
  598.             $this->mDb->StartTrans();
  599.             if( empty( $pParamHash['role_id'] ) ) {
  600.                 $pParamHash['role_id'] = $this->mDb->GenID( 'users_roles_id_seq' );
  601.                 $pParamHash['role_store']['role_id'] = $pParamHash['role_id'];
  602.                 $result = $this->mDb->associateInsert( BIT_DB_PREFIX.'users_roles', $pParamHash['role_store'] );
  603.             } else {
  604.                 $sql = "SELECT COUNT(*) FROM `".BIT_DB_PREFIX."users_roles` WHERE `role_id` = ?";
  605.                 $roleExists = $this->mDb->getOne($sql, array($pParamHash['role_id']));
  606.                 if ($roleExists) {
  607.                     $result = $this->mDb->associateUpdate( BIT_DB_PREFIX.'users_roles', $pParamHash['role_store'], array( "role_id" => $pParamHash['role_id'] ) );
  608.                 } else {
  609.                     // A role_id was specified but that role does not exist yet
  610.                     $pParamHash['role_store']['role_id'] = $pParamHash['role_id'];
  611.                     $result = $this->mDb->associateInsert(BIT_DB_PREFIX.'users_roles', $pParamHash['role_store']);
  612.                 }
  613.             }
  614.  
  615.             if( isset( $_REQUEST['batch_set_default'] ) and $_REQUEST['batch_set_default'] == 'on' ) {
  616.                 $gBitUser->batchSetUserDefaultRole( $pParamHash['role_id'] );
  617.             }
  618.             $this->mDb->CompleteTrans();
  619.         }
  620.         return ( count( $this->mErrors ) == 0 );
  621.     }
  622.  
  623.     /**
  624.      * getRoleNameFromId
  625.      * 
  626.      * @param array $pRoleId 
  627.      * @param array $pColumns 
  628.      * @access public
  629.      * @return array of group data
  630.      */
  631.     public static function getRoleNameFromId( $pRoleId ) {
  632.         $ret = '';
  633.         if( static::verifyId( $pRoleId ) ) {
  634.             global $gBitDb;
  635.             $ret = $gBitDb->getOne( "SELECT `role_name` FROM `".BIT_DB_PREFIX."users_roles` WHERE `role_id`=?", array( $pRoleId ) );
  636.         }
  637.         return $ret;
  638.     }
  639.  
  640.     /**
  641.      * getRoleUserData
  642.      * 
  643.      * @param array $pRoleId 
  644.      * @param array $pColumns 
  645.      * @access public
  646.      * @return array of role data
  647.      */
  648.     function getRoleUserData( $pRoleId, $pColumns ) {
  649.         $ret = array();
  650.         if( @$this->verifyId( $pRoleId ) && !empty( $pColumns ) ) {
  651.             if( is_array( $pColumns ) ) {
  652.                 $col = implode( $pColumns, ',' );
  653.                 $exec = 'getAssoc';
  654.             } else {
  655.                 $col = '`'.$pColumns.'`';
  656.                 $exec = 'getArray';
  657.             }
  658.             $query = "
  659.                 SELECT $col
  660.                 FROM `".BIT_DB_PREFIX."users_users` uu
  661.                     INNER JOIN `".BIT_DB_PREFIX."users_roles_map` urm ON (uu.`user_id`=urm.`user_id`)
  662.                 WHERE urm.`role_id` = ?";
  663.             $ret = $this->mDb->$exec( $query, array( $pRoleId ));
  664.         }
  665.         return $ret;
  666.     }
  667.  
  668.     // =-=-=-=-=-=-=-=-=-=-=-= PERMISSION FUNCTIONS =-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  669.     /**
  670.      * loadPermissions
  671.      * 
  672.      * @access public
  673.      * @return TRUE on success, FALSE if no perms were loaded
  674.      */
  675.     function loadPermissions( $pForceReload=FALSE ) {
  676.         if( $this->isValid() && (empty( $this->mPerms ) || $pForceReload) ) {
  677.             $this->mPerms = array();
  678.             // the double up.`perm_name` is intentional - the first is for hash key, the second is for hash value
  679.             $query = "
  680.                 SELECT up.`perm_name` AS `hash_key`, up.`perm_name`, up.`perm_desc`, up.`perm_level`, up.`package`
  681.                 FROM `".BIT_DB_PREFIX."users_permissions` up
  682.                     INNER JOIN `".BIT_DB_PREFIX."users_role_permissions` urp ON ( urp.`perm_name`=up.`perm_name` )
  683.                     INNER JOIN `".BIT_DB_PREFIX."users_roles` ur ON ( ur.`role_id`=urp.`role_id` )
  684.                     LEFT OUTER JOIN `".BIT_DB_PREFIX."users_roles_map` urm ON ( urm.`role_id`=urp.`role_id` AND urm.`user_id` = ? )
  685.                 WHERE ur.`role_id`= ".ANONYMOUS_TEAM_ID." OR urm.`role_id`=ur.`role_id`";
  686.             $this->mPerms = $this->mDb->getAssoc( $query, array( $this->mUserId ));
  687.             // Add in override permissions
  688.             if( !empty( $this->mPermsOverride ) ) {
  689.                 foreach( $this->mPermsOverride as $key => $val ) {
  690.                     $this->mPerms[$key] = $val;
  691.                 }
  692.             }
  693.         }
  694.         return( count( $this->mPerms ) );
  695.     }
  696.  
  697.     /**
  698.      * getUnassignedPerms
  699.      * 
  700.      * @access public
  701.      * @return array of permissions that have not been assigned to any role yet
  702.      */
  703.     function getUnassignedPerms() {
  704.         $query = "SELECT up.`perm_name` AS `hash_key`, up.*
  705.             FROM `".BIT_DB_PREFIX."users_permissions` up
  706.                 LEFT OUTER JOIN `".BIT_DB_PREFIX."users_role_permissions` urp ON( up.`perm_name` = urp.`perm_name` )
  707.             WHERE urp.`role_id` IS NULL AND up.`perm_name` <> ?
  708.             ORDER BY `package`, up.`perm_name` ASC";
  709.         return( $this->mDb->getAssoc( $query, array( '' )));
  710.     }
  711.  
  712.     /**
  713.      * isAdmin
  714.      * 
  715.      * @param array $pCheckTicket 
  716.      * @access public
  717.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  718.      */
  719.     function isAdmin() {
  720.         // we can't use hasPermission here since it turn into an endless loop
  721.         return( !empty( $this->mPerms['p_admin'] ));
  722.     }
  723.  
  724.     /**
  725.      * hasPermission check to see if a user has a given permission
  726.      * 
  727.      * @param array $pPerm Perm name
  728.      * @access public
  729.      * @return TRUE if the user has a permission, FALSE if they don't
  730.      */
  731.     function hasPermission( $pPerm ) {
  732.         $ret = FALSE;
  733.         if( $this->isAdmin() ) {
  734.             $ret = TRUE;
  735.         } elseif( $this->isValid() ) {
  736.             $ret = isset( $this->mPerms[$pPerm] );
  737.         }
  738.         return ( $ret );
  739.     }
  740.  
  741.     /**
  742.      * verifyPermission check if a user has a given permission and if not
  743.      * it will display the error template and die()
  744.      * @param $pPermission value of a given permission
  745.      * @return none 
  746.      * @access public
  747.      */
  748.     function verifyPermission( $pPermission, $pMsg = NULL ) {
  749.         global $gBitSmarty, $gBitSystem, ${$pPermission};
  750.         if( empty( $pPermission ) || $this->hasPermission( $pPermission ) ) {
  751.             return TRUE;
  752.         } else {
  753.             $gBitSystem->fatalPermission( $pPermission, $pMsg );
  754.         }
  755.     }
  756.  
  757.     /**
  758.      * getRolePermissions
  759.      * 
  760.      * @param array $pRoleId Role id, if unset, all roles are returned
  761.      * @param string $pPackage permissions to give role, if unset, all permissions are returned
  762.      * @param string $find search for a particular permission
  763.      * @param array $pSortMode sort mode of return hash
  764.      * @access public
  765.      * @return TRUE on success, FALSE on failure
  766.      */
  767.     function getRolePermissions( $pParamHash = NULL ) {
  768.         global $gBitSystem;
  769.         $ret = $bindVars = array();
  770.         $whereSql = $selectSql = $fromSql = '';
  771.  
  772.         if( !empty( $pParamHash['sort_mode'] )) {
  773.             $sortMode = $this->mDb->convertSortmode( $pParamHash['sort_mode'] );
  774.         } else {
  775.             $sortMode = 'up.`package`, up.`perm_name` ASC';
  776.         }
  777.  
  778.         if( !empty( $pParamHash['package'] )) {
  779.             $whereSql = ' WHERE `package`= ? ';
  780.             $bindVars[] = $pParamHash['package'];
  781.         }
  782.  
  783.         if( @BitBase::verifyId( $pParamHash['role_id'] )) {
  784.             $selectSql = ', urp.`perm_value` AS `hasPerm` ';
  785.             $fromSql = ' INNER JOIN `'.BIT_DB_PREFIX.'users_role_permissions` urp ON ( urp.`perm_name`=up.`perm_name` ) ';
  786.             if( $whereSql ) {
  787.                 $whereSql .= " AND  urp.`role_id`=?";
  788.             } else {
  789.                 $whereSql .= " WHERE urp.`role_id`=?";
  790.             }
  791.  
  792.             $bindVars[] = $pParamHash['role_id'];
  793.         }
  794.  
  795.         if( !empty( $pParamHash['find'] )) {
  796.             if( $whereSql ) {
  797.                 $whereSql .= " AND `perm_name` like ?";
  798.             } else {
  799.                 $whereSql .= " WHERE `perm_name` like ?";
  800.             }
  801.             $bindVars[] = '%'.$pParamHash['find'].'%';
  802.         }
  803.  
  804.         // the double up.`perm_name` is intentional - the first is for hash key, the second is for hash value
  805.         $query = "
  806.             SELECT up.`perm_name` AS `hash_key`, up.`perm_name`, up.`perm_desc`, up.`perm_level`, up.`package` $selectSql
  807.             FROM `".BIT_DB_PREFIX."users_permissions` up $fromSql $whereSql
  808.             ORDER BY $sortMode";
  809.         $perms = $this->mDb->getAssoc( $query, $bindVars );
  810.  
  811.         // weed out permissions of inactive packages
  812.         $ret = array();
  813.         foreach( $perms as $key => $perm ) {
  814.             if( $gBitSystem->isPackageActive( $perm['package'] )) {
  815.                 $ret[$key] = $perm;
  816.             }
  817.         }
  818.  
  819.         return $ret;
  820.     }
  821.  
  822.     /**
  823.      * assignLevelPermissions Assign the permissions of a given level to a given role
  824.      * 
  825.      * @param array $pRoleId Role we want to assign permissions to
  826.      * @param array $pLevel permission level we wish to assign from
  827.      * @param array $pPackage limit set of permissions to a given package
  828.      * @access public
  829.      * @return void 
  830.      */
  831.     function assignLevelPermissions( $pRoleId, $pLevel, $pPackage = NULL) {
  832.         if( @BitBase::verifyId( $pRoleId ) && !empty( $pLevel )) {
  833.             $bindvars = array( $pLevel );
  834.             $whereSql = '';
  835.             if( !empty( $pPackage ) ) {
  836.                 $whereSql = ' AND `package`=?';
  837.                 array_push( $bindvars, $pPackage );
  838.             }
  839.             $query = "SELECT `perm_name` FROM `".BIT_DB_PREFIX."users_permissions` WHERE `perm_level` = ? $whereSql";
  840.             if( $result = $this->mDb->query( $query, $bindvars ) ) {
  841.                 while( $row = $result->fetchRow() ) {
  842.                     $this->assignPermissionToRole( $row['perm_name'], $pRoleId );
  843.                 }
  844.             }
  845.         }
  846.     }
  847.  
  848.     /**
  849.      * getPermissionPackages Get a list of packages that have their own set of permissions
  850.      * 
  851.      * @access public
  852.      * @return array of packages
  853.      */
  854.     function getPermissionPackages() {
  855.         return( $this->mDb->getCol( "SELECT DISTINCT(`package`) FROM `".BIT_DB_PREFIX."users_permissions` ORDER BY `package`" ) );
  856.     }
  857.  
  858.     /**
  859.      * assignPermissionToRole
  860.      * 
  861.      * @param array $perm 
  862.      * @param array $pRoleId 
  863.      * @access public
  864.      * @return TRUE on success
  865.      */
  866.     function assignPermissionToRole( $pPerm, $pRoleId ) {
  867.         if( @BitBase::verifyId( $pRoleId ) && !empty( $pPerm )) {
  868.             $query = "DELETE FROM `".BIT_DB_PREFIX."users_role_permissions` WHERE `role_id` = ? AND `perm_name` = ?";
  869.             $result = $this->mDb->query( $query, array( $pRoleId, $pPerm ));
  870.             $query = "INSERT INTO `".BIT_DB_PREFIX."users_role_permissions`(`role_id`, `perm_name`) VALUES(?, ?)";
  871.             $result = $this->mDb->query( $query, array( $pRoleId, $pPerm ));
  872.             return TRUE;
  873.         }
  874.     }
  875.  
  876.     /**
  877.      * removePermissionFromRole
  878.      * 
  879.      * @param string $pPerm Perm name
  880.      * @param numeric $pRoleId Role ID
  881.      * @access public
  882.      * @return TRUE on success
  883.      */
  884.     function removePermissionFromRole( $pPerm, $pRoleId ) {
  885.         if( @BitBase::verifyId( $pRoleId ) && !empty( $pPerm )) {
  886.             $query = "DELETE FROM `".BIT_DB_PREFIX."users_role_permissions` WHERE `perm_name` = ? AND `role_id` = ?";
  887.             $result = $this->mDb->query($query, array($pPerm, $pRoleId));
  888.             return TRUE;
  889.         }
  890.     }
  891.  
  892.     /**
  893.      * storeRegistrationChoice
  894.      * 
  895.      * @param mixed $pRoleMixed A single role ID or an array of role IDs
  896.      * @param array $pValue Value you wish to store - use NULL to delete a value
  897.      * @access public
  898.      * @return ADO record set on success, FALSE on failure
  899.      */
  900.     function storeRegistrationChoice( $pRoleMixed, $pValue = NULL ) {
  901.         if( !empty( $pRoleMixed )) {
  902.             $bindVars[] = $pValue;
  903.             if( is_array( $pRoleMixed )) {
  904.                 $mid = implode( ',', array_fill( 0, count( $pRoleMixed ),'?' ));
  905.                 $bindVars = array_merge( $bindVars, $pRoleMixed );
  906.             } else {
  907.                 $bindVars[] = $pRoleMixed;
  908.                 $mid = 'LIKE ?';
  909.             }
  910.             $query = "UPDATE `".BIT_DB_PREFIX."users_roles` SET `is_public`= ? where `role_id` IN ($mid)";
  911.             return $this->mDb->query( $query, $bindVars );
  912.         }
  913.     }
  914.  
  915.     /**
  916.      * Grant a single permission to a given value
  917.      */
  918.     function setPermissionOverride( $pPerm, $pValue = NULL ) {
  919.         if( $this->isAdmin() ) {
  920.             $this->mPerms[$pPerm] = TRUE;
  921.             $this->mPermsOverride[$pPerm] = TRUE;
  922.         } elseif( $this->isValid() ) {
  923.             if( $pValue == 'y' || $pValue == TRUE ) {
  924.                 $this->mPermsOverride[$pPerm] = TRUE;
  925.                 $this->mPerms[$pPerm] = TRUE;
  926.             } else {
  927.                 unset( $this->mPermsOverride[$pPerm] );
  928.                 unset( $this->mPerms[$pPerm] );
  929.             }
  930.         }
  931.     }
  932. }
  933.  
  934. /* vim: :set fdm=marker : */
  935. ?>

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