cvs: pear /Perm_LiveUser/Perm/Container DB_Medium.php
| From: | Arnaud Limbourg | Date: | Sat, 07 Dec 2002 18:07:07 +0000 |
| Subject: | cvs: pear /Perm_LiveUser/Perm/Container DB_Medium.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-7768@lists.php.net to get a copy of this message | ||
arnaud Sat Dec 7 13:07:07 2002 EDT
Added files:
/pear/Perm_LiveUser/Perm/Container DB_Medium.php
Log:
* first version of the medium driver
Index: pear/Perm_LiveUser/Perm/Container/DB_Medium.php +++ pear/Perm_LiveUser/Perm/Container/DB_Medium.php <?php /** * Medium DB-based complexity driver for LiveUser. * * Description: * The DB_Medium provides the following functionalities * - users * - groups * - grouprights * - userrights * - AuthAreas * * @author Arnaud Limbourg * @version $Id: DB_Medium.php,v 1.1 2002/12/07 18:07:07 arnaud Exp $ */ /** * Required files */ require_once dirname(__FILE__) . '/../Common.php'; require_once 'DB.php'; /** * Class definition */ class LiveUser_Perm_Container_DB_Medium { /** * Constructor * * @param mixed $connectOptions an array or a PEAR::DB object */ function &LiveUser_Perm_Container_DB_Medium(&$connectOptions) { $this->init_ok = false; if (DB::isConnection($connectOptions)) { $this->dbc = &$connectOptions; $this->init_ok = true; } elseif (is_array($connectOptions)) { $this->dsn = $connectOptions['dsn']; $this->dbc = DB::connect($connectOptions['dsn']); if (!DB::isError($this->dbc)) { $this->init_ok = true; } } } // end func LiveUser_Perm_Container_DB_Medium /** * Tries to find the user with the given user ID in the permissions * container. Will read all permission data and return TRUE on success. * * @access public * @param string $uid user identifier * @return mixed true on success or a PEAR_Error object */ function init($uid) { $success = true; $query = ' SELECT LU.user_id AS userid, LU.type AS usertype FROM liveuser_perm_users LU WHERE auth_user_id=?'; $result = $this->dbc->getRow($query, array($uid), DB_FETCHMODE_ASSOC); if (DB::isError($result)) { return $result; } $this->user_id = $result['userid']; $this->user_type = $result['usertype']; if ($this->user_type == LIVEUSER_AREAADMIN_TYPE_ID) { // get all areas in which the user is area admin $query = ' SELECT LU.area_id FROM liveuser_area_admin_areas LU WHERE LU.user_id=? '; $result = $this->dbc->getCol($query, null, array($this->user_id)); if (DB::isError($result)) { return $result; } $this->area_admin = $result; } $this->readRights(); return $success; } // end func init /** * Checks if the current user has a rights in a * given area. If no third parameter is given, this also * checks if the user is logged in and will only return * TRUE if he has the right in question AND is currently * logged in. Set the third parameter to FALSE if you * just want to check wheter the user has a certain * right or not while ignoring the login status. * * @access public * @param mixed $area_name the area name or id * @param boolean $checkLogin whether we should also check is the user is logged in as well * @return boolean true if user has a right, false otherwise */ function checkArea($area_name, $checkLogin = true) { $hasright = false; if ($this->rights) { if ($this->user_type > LIVEUSER_AREAADMIN_TYPE_ID || ($this->user_type == LIVEUSER_AREAADMIN_TYPE_ID && $this->area_admin && in_array($area_name, $this->area_admin))) { $hasright = true; // Check if user has rights in this area at all. } elseif (is_array($this->rights[$area_name])) { // Now let's see if we shall check the login status, too if ($checkLogin === true) { // Yep, login has to be checked, so we'll only return TRUE // if the user is currently logged in if ($this->logged_in == true) { $hasright = true; } } else { // We know the user has the right and we don't want to check // the login status, so TRUE will be returned. $hasright = true; } } } return $hasright; } // end func checkArea /** * Checks for a user right or rights * for a supplied area or takes the * currentArea set with setCurrentArea * * @access public * @param mixed $rights The name of the right within the AuthArea * to check for or an array of rights. * @param string $area_name The name of the AuthArea to use * @return boolean true if the user has one of the rights * false otherwise */ function checkRight($rights, $area_name = false) { $hasright = false; if ($this->rights) { if ($this->user_type > LIVEUSER_AREAADMIN_TYPE_ID || ($this->user_type == LIVEUSER_AREAADMIN_TYPE_ID && $this->area_admin && in_array($area_name, $this->area_admin))) { $hasright = LIVEUSER_MAX_LEVEL; // Check if user has rights in this area at all. } elseif (is_array($this->rights[$area_name])) { // If he does, look for the right in question if (in_array($rights, array_keys($this->rights[$area_name]))) { // We know the user has the right and we don't want to check // the login status, so the right level will be returned. $hasright = $this->rights[$area_name][$rights]; } } } return $hasright; } // end func checkRight /** * Reads the group rights * and put them in the array * * area => right -> 1 * * @access public * @return mixed DB_Error on failure or nothing */ function readGroupRights() { $query = ' SELECT R.area_id AS areaid, R.right_id AS rightid FROM liveuser_groupusers GU INNER JOIN liveuser_grouprights GR ON GU.group_id=GR.group_id INNER JOIN liveuser_rights R ON GR.right_id=R.right_id WHERE GU.user_id=? '; $result = $this->dbc->query($query, array($this->user_id)); if (DB::isError($result)) { return $result; } // if $allRights = array(); while ($result->fetchInto($record, DB_FETCHMODE_ASSOC)) { $allRights[$record['areaid']][$record['rightid']] = 1; } // while $this->groupRights = $allRights; $result->free(); } // end func readGroupRights /** * Reads all rights of current user into a * two-dimensional associative array, having the * area names as the key of the 1st dimension. * Group rights and invididual rights are being merged * in the process. * * @access public */ function readRights() { // reset rights $this->rights = array(); $this->readUserRights(); $this->readGroupRights(); $tmpRights = $this->groupRights; // Check if user has individual rights... if (is_array($this->userRights)) { // Overwrite values from temporary array with values from userrights foreach ($this->userRights as $currentArea => $areaRights) { foreach ($areaRights as $rightName => $level) { if(isset($tmpRights[$currentArea][$rightName])) { if($level < 0) { $tmpRights[$currentArea][$rightName] = $tmpRights[$currentArea][$rightName] - $level; } else { $tmpRights[$currentArea][$rightName] = max($tmpRights[$currentArea][$rightName], $level); } } else { $tmpRights[$currentArea][$rightName] = $level; } } } } else { $this->userRights = false; } // Strip values from array if level is not greater than zero $cRights = array(); if (isset($tmpRights) && is_array($tmpRights)) { foreach ($tmpRights as $currentArea => $areaRights) { foreach ($areaRights as $rightName => $level) { if ($level > 0) { $cRights[$currentArea][$rightName] = $level; } } } } if (sizeof($cRights) > 0) { $this->rights = $cRights; } else { $this->rights = array(); } } // end func readRights /** * Reads the user rights and put them in an array * * area => right -> level * * This level is either 1 or -1 * i.e. you grant the user the right * or you do not allow him to have * the right. * * @access public * @return mixed DB_Error on failure or nothing */ function readUserRights() { $query = ' SELECT R.area_id AS areaid, R.right_id AS rightid, UR.right_level AS level FROM liveuser_userrights UR INNER JOIN liveuser_rights R ON UR.right_id=R.right_id WHERE UR.user_id=? '; $result = $this->dbc->query($query, array($this->user_id)); if (DB::isError($result)) { return $result; } // if $allRights = array(); while ($result->fetchInto($record, DB_FETCHMODE_ASSOC)) { $allRights[$record['areaid']][$record['rightid']] = $record['level']; } // while $this->userRights = $allRights; $result->free(); } // end func readUserRights /** * Prepares the object for being stored in a session (properly * disconnect from the database, free up resources). * * @access public */ function freeze() { if($this->dsn) { $this->dbc->disconnect(); } $this->dbc = NULL; } // end func freeze /** * Reinitializes database connection after the object had been * retrieved from the session. * * @access public * @param array $connectOptions connection options */ function unfreeze(&$connectOptions) { $this->LiveUser_Perm_Container_DB_Medium($connectOptions); } // end func unfreeze } // end class LiveUser_Perm_Container_DB_Medium ?>
Index: pear/Perm_LiveUser/Perm/Container/DB_Medium.php +++ pear/Perm_LiveUser/Perm/Container/DB_Medium.php <?php /** * Medium DB-based complexity driver for LiveUser. * * Description: * The DB_Medium provides the following functionalities * - users * - groups * - grouprights * - userrights * - AuthAreas * * @author Arnaud Limbourg * @version $Id: DB_Medium.php,v 1.1 2002/12/07 18:07:07 arnaud Exp $ */ /** * Required files */ require_once dirname(__FILE__) . '/../Common.php'; require_once 'DB.php'; /** * Class definition */ class LiveUser_Perm_Container_DB_Medium { /** * Constructor * * @param mixed $connectOptions an array or a PEAR::DB object */ function &LiveUser_Perm_Container_DB_Medium(&$connectOptions) { $this->init_ok = false; if (DB::isConnection($connectOptions)) { $this->dbc = &$connectOptions; $this->init_ok = true; } elseif (is_array($connectOptions)) { $this->dsn = $connectOptions['dsn']; $this->dbc = DB::connect($connectOptions['dsn']); if (!DB::isError($this->dbc)) { $this->init_ok = true; } } } // end func LiveUser_Perm_Container_DB_Medium /** * Tries to find the user with the given user ID in the permissions * container. Will read all permission data and return TRUE on success. * * @access public * @param string $uid user identifier * @return mixed true on success or a PEAR_Error object */ function init($uid) { $success = true; $query = ' SELECT LU.user_id AS userid, LU.type AS usertype FROM liveuser_perm_users LU WHERE auth_user_id=?'; $result = $this->dbc->getRow($query, array($uid), DB_FETCHMODE_ASSOC); if (DB::isError($result)) { return $result; } $this->user_id = $result['userid']; $this->user_type = $result['usertype']; if ($this->user_type == LIVEUSER_AREAADMIN_TYPE_ID) { // get all areas in which the user is area admin $query = ' SELECT LU.area_id FROM liveuser_area_admin_areas LU WHERE LU.user_id=? '; $result = $this->dbc->getCol($query, null, array($this->user_id)); if (DB::isError($result)) { return $result; } $this->area_admin = $result; } $this->readRights(); return $success; } // end func init /** * Checks if the current user has a rights in a * given area. If no third parameter is given, this also * checks if the user is logged in and will only return * TRUE if he has the right in question AND is currently * logged in. Set the third parameter to FALSE if you * just want to check wheter the user has a certain * right or not while ignoring the login status. * * @access public * @param mixed $area_name the area name or id * @param boolean $checkLogin whether we should also check is the user is logged in as well * @return boolean true if user has a right, false otherwise */ function checkArea($area_name, $checkLogin = true) { $hasright = false; if ($this->rights) { if ($this->user_type > LIVEUSER_AREAADMIN_TYPE_ID || ($this->user_type == LIVEUSER_AREAADMIN_TYPE_ID && $this->area_admin && in_array($area_name, $this->area_admin))) { $hasright = true; // Check if user has rights in this area at all. } elseif (is_array($this->rights[$area_name])) { // Now let's see if we shall check the login status, too if ($checkLogin === true) { // Yep, login has to be checked, so we'll only return TRUE // if the user is currently logged in if ($this->logged_in == true) { $hasright = true; } } else { // We know the user has the right and we don't want to check // the login status, so TRUE will be returned. $hasright = true; } } } return $hasright; } // end func checkArea /** * Checks for a user right or rights * for a supplied area or takes the * currentArea set with setCurrentArea * * @access public * @param mixed $rights The name of the right within the AuthArea * to check for or an array of rights. * @param string $area_name The name of the AuthArea to use * @return boolean true if the user has one of the rights * false otherwise */ function checkRight($rights, $area_name = false) { $hasright = false; if ($this->rights) { if ($this->user_type > LIVEUSER_AREAADMIN_TYPE_ID || ($this->user_type == LIVEUSER_AREAADMIN_TYPE_ID && $this->area_admin && in_array($area_name, $this->area_admin))) { $hasright = LIVEUSER_MAX_LEVEL; // Check if user has rights in this area at all. } elseif (is_array($this->rights[$area_name])) { // If he does, look for the right in question if (in_array($rights, array_keys($this->rights[$area_name]))) { // We know the user has the right and we don't want to check // the login status, so the right level will be returned. $hasright = $this->rights[$area_name][$rights]; } } } return $hasright; } // end func checkRight /** * Reads the group rights * and put them in the array * * area => right -> 1 * * @access public * @return mixed DB_Error on failure or nothing */ function readGroupRights() { $query = ' SELECT R.area_id AS areaid, R.right_id AS rightid FROM liveuser_groupusers GU INNER JOIN liveuser_grouprights GR ON GU.group_id=GR.group_id INNER JOIN liveuser_rights R ON GR.right_id=R.right_id WHERE GU.user_id=? '; $result = $this->dbc->query($query, array($this->user_id)); if (DB::isError($result)) { return $result; } // if $allRights = array(); while ($result->fetchInto($record, DB_FETCHMODE_ASSOC)) { $allRights[$record['areaid']][$record['rightid']] = 1; } // while $this->groupRights = $allRights; $result->free(); } // end func readGroupRights /** * Reads all rights of current user into a * two-dimensional associative array, having the * area names as the key of the 1st dimension. * Group rights and invididual rights are being merged * in the process. * * @access public */ function readRights() { // reset rights $this->rights = array(); $this->readUserRights(); $this->readGroupRights(); $tmpRights = $this->groupRights; // Check if user has individual rights... if (is_array($this->userRights)) { // Overwrite values from temporary array with values from userrights foreach ($this->userRights as $currentArea => $areaRights) { foreach ($areaRights as $rightName => $level) { if(isset($tmpRights[$currentArea][$rightName])) { if($level < 0) { $tmpRights[$currentArea][$rightName] = $tmpRights[$currentArea][$rightName] - $level; } else { $tmpRights[$currentArea][$rightName] = max($tmpRights[$currentArea][$rightName], $level); } } else { $tmpRights[$currentArea][$rightName] = $level; } } } } else { $this->userRights = false; } // Strip values from array if level is not greater than zero $cRights = array(); if (isset($tmpRights) && is_array($tmpRights)) { foreach ($tmpRights as $currentArea => $areaRights) { foreach ($areaRights as $rightName => $level) { if ($level > 0) { $cRights[$currentArea][$rightName] = $level; } } } } if (sizeof($cRights) > 0) { $this->rights = $cRights; } else { $this->rights = array(); } } // end func readRights /** * Reads the user rights and put them in an array * * area => right -> level * * This level is either 1 or -1 * i.e. you grant the user the right * or you do not allow him to have * the right. * * @access public * @return mixed DB_Error on failure or nothing */ function readUserRights() { $query = ' SELECT R.area_id AS areaid, R.right_id AS rightid, UR.right_level AS level FROM liveuser_userrights UR INNER JOIN liveuser_rights R ON UR.right_id=R.right_id WHERE UR.user_id=? '; $result = $this->dbc->query($query, array($this->user_id)); if (DB::isError($result)) { return $result; } // if $allRights = array(); while ($result->fetchInto($record, DB_FETCHMODE_ASSOC)) { $allRights[$record['areaid']][$record['rightid']] = $record['level']; } // while $this->userRights = $allRights; $result->free(); } // end func readUserRights /** * Prepares the object for being stored in a session (properly * disconnect from the database, free up resources). * * @access public */ function freeze() { if($this->dsn) { $this->dbc->disconnect(); } $this->dbc = NULL; } // end func freeze /** * Reinitializes database connection after the object had been * retrieved from the session. * * @access public * @param array $connectOptions connection options */ function unfreeze(&$connectOptions) { $this->LiveUser_Perm_Container_DB_Medium($connectOptions); } // end func unfreeze } // end class LiveUser_Perm_Container_DB_Medium ?>