cvs: pear /Perm_LiveUser/Perm Simple.php Storage.php /Perm_LiveUser/Perm/Storage Cache.php MDB2.php SQL.php XML.php

From: Date: Mon, 08 Nov 2004 22:15:41 +0000
Subject: cvs: pear /Perm_LiveUser/Perm Simple.php Storage.php /Perm_LiveUser/Perm/Storage Cache.php MDB2.php SQL.php XML.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-25787@lists.php.net to get a copy of this message
lsmith Mon Nov 8 17:15:41 2004 EDT Modified files: /pear/Perm_LiveUser/Perm Simple.php Storage.php /pear/Perm_LiveUser/Perm/Storage Cache.php MDB2.php SQL.php XML.php Log: - cleanups from the last commit

http://cvs.php.net/diff.php/pear/Perm_LiveUser/Perm/Simple.php?r1=1.1&r2=1.2&ty=u Index: pear/Perm_LiveUser/Perm/Simple.php diff -u pear/Perm_LiveUser/Perm/Simple.php:1.1 pear/Perm_LiveUser/Perm/Simple.php:1.2 --- pear/Perm_LiveUser/Perm/Simple.php:1.1 Mon Nov 8 10:57:34 2004 +++ pear/Perm_LiveUser/Perm/Simple.php Mon Nov 8 17:15:39 2004 @@ -56,7 +56,7 @@ * * @author Markus Wolff <wolff@21st.de> * @author Bjoern Kraus <krausbn@php.net> - * @version $Id: Simple.php,v 1.1 2004/11/08 15:57:34 lsmith Exp $ + * @version $Id: Simple.php,v 1.2 2004/11/08 22:15:39 lsmith Exp $ * @package LiveUser * @category authentication */ @@ -91,18 +91,6 @@ var $userRights = array(); /** - * Two-dimensional array containing all groups that - * the user belongs to and the grouprights. - * - * Format: "GroupId" => "RightId" => "Level" - * - * @var array - * @see $userRights - * @see $rights - */ - var $groupRights = array(); - - /** * Defines the user type. * * @var integer @@ -110,20 +98,6 @@ var $userType = LIVEUSER_ANONYMOUS_TYPE_ID; /** - * Defines the (sub)groups in which the user is a member - * - * @var mixed - */ - var $groupIds = false; - - /** - * Defines if the user rights should be retrieved ondemand. - * - * @var boolean - */ - var $ondemand = false; - - /** * Error stack * * @var PEAR_ErrorStack @@ -133,7 +107,7 @@ /** * Class constructor. Feel free to override in backend subclasses. */ - function LiveUser_Perm_Simple($confArray) + function LiveUser_Perm_Simple(&$confArray) { $this->_stack = &PEAR_ErrorStack::singleton('LiveUser'); $this->_storage = LiveUser::storageFactory($confArray); @@ -172,13 +146,28 @@ */ function readRights() { - $result = $this->_storage->readUserRights($this->permUserId); + $this->rights = array(); + + $result = $this->readUserRights($this->permUserId); if (!$result) { return false; } $this->rights = $result; - return true; + return $result; + } + + function readUserRights($permUserId) + { + $this->userRights = array(); + + $result = $this->_storage->readUserRights($permUserId); + if (!$result) { + return false; + } + + $this->userRights = $result; + return $result; } /** http://cvs.php.net/diff.php/pear/Perm_LiveUser/Perm/Storage.php?r1=1.1&r2=1.2&ty=u Index: pear/Perm_LiveUser/Perm/Storage.php diff -u pear/Perm_LiveUser/Perm/Storage.php:1.1 pear/Perm_LiveUser/Perm/Storage.php:1.2 --- pear/Perm_LiveUser/Perm/Storage.php:1.1 Mon Nov 8 10:57:34 2004 +++ pear/Perm_LiveUser/Perm/Storage.php Mon Nov 8 17:15:39 2004 @@ -38,55 +38,12 @@ * * @author Lukas Smith <smith@backendmedia.com> * @author Bjoern Kraus <krausbn@php.net> - * @version $Id: Storage.php,v 1.1 2004/11/08 15:57:34 lsmith Exp $ + * @version $Id: Storage.php,v 1.2 2004/11/08 22:15:39 lsmith Exp $ * @package LiveUser * @category authentication */ class LiveUser_Perm_Storage { - /** - * dsn that was connected to - * @var object - * @access private - */ - var $dsn = null; - - /** - * disconnect - * @var object - * @access private - */ - var $disconnect = null; - - /** - * PEAR::MDB2 connection object. - * - * @var object - * @access private - */ - var $dbc = null; - - /** - * Table prefix - * Prefix for all db tables the container has. - * - * @var string - * @access public - */ - var $prefix = 'liveuser_'; - - /** - * Indicates if backend module initialized correctly. If yes, - * true, if not false. Backend module won't initialize if the - * init value (usually an object or resource handle that - * identifies the backend to be used) is not of the required - * type. - * - * @var boolean - * @access public - */ - var $init_ok = false; - var $session_name; /** @@ -127,7 +84,7 @@ if (is_array($conf)) { foreach ($conf as $key => $value) { if (isset($this->$key)) { - $this->$key = $value; + $this->$key = $conf[$key]; } } } @@ -148,6 +105,35 @@ { } + function readAreaAdminAreas($permUserId) + { + } + + /** + * Reads all the group ids in that the user is also a member of + * (all groups that are subgroups of these are also added recursively) + * + * @access private + * @see readRights() + * @return void + */ + function readGroups($permUserId) + { + } // end func readGroups + + /** + * Reads the group rights + * and put them in the array + * + * right => 1 + * + * @access public + * @return mixed MDB2_Error on failure or nothing + */ + function readGroupRights($groupIds) + { + } // end func readGroupRights + /** * store all properties in an array * http://cvs.php.net/diff.php/pear/Perm_LiveUser/Perm/Storage/Cache.php?r1=1.1&r2=1.2&ty=u Index: pear/Perm_LiveUser/Perm/Storage/Cache.php diff -u pear/Perm_LiveUser/Perm/Storage/Cache.php:1.1 pear/Perm_LiveUser/Perm/Storage/Cache.php:1.2 --- pear/Perm_LiveUser/Perm/Storage/Cache.php:1.1 Mon Nov 8 10:57:34 2004 +++ pear/Perm_LiveUser/Perm/Storage/Cache.php Mon Nov 8 17:15:40 2004 @@ -43,7 +43,7 @@ * * @author Lukas Smith <smith@backendmedia.com> * @author Bjoern Kraus <krausbn@php.net> - * @version $Id: Cache.php,v 1.1 2004/11/08 15:57:34 lsmith Exp $ + * @version $Id: Cache.php,v 1.2 2004/11/08 22:15:40 lsmith Exp $ * @package LiveUser * @category authentication */ @@ -97,6 +97,101 @@ return $result; } + function readAreaAdminAreas($permUserId) + { + if (in_cache) { + return cache; + } + $result = $this->_storage->readAreaAdminAreas($permUserId); + if (!$result) { + return false; + } + write_into_cache + return $result; + } + + /** + * Reads all the group ids in that the user is also a member of + * (all groups that are subgroups of these are also added recursively) + * + * @access private + * @see readRights() + * @return void + */ + function readGroups($permUserId) + { + if (in_cache) { + return cache; + } + $result = $this->_storage->readGroups($permUserId); + if (!$result) { + return false; + } + write_into_cache + return $result; + } // end func readGroups + + /** + * Reads the group rights + * and put them in the array + * + * right => 1 + * + * @access public + * @return mixed MDB2_Error on failure or nothing + */ + function readGroupRights($groupIds) + { + if (in_cache) { + return cache; + } + $result = $this->_storage->readGroupRights($groupIds); + if (!$result) { + return false; + } + write_into_cache + return $result; + } // end func readGroupRights + + function readSubGroups($groupIds, $newGroupIds) + { + if (in_cache) { + return cache; + } + $result = $this->_storage->readSubGroups($groupIds, $newGroupIds); + if (!$result) { + return false; + } + write_into_cache + return $result; + } + + function readImplyingRights($rightIds, $table) + { + if (in_cache) { + return cache; + } + $result = $this->_storage->readImplyingRights($rightIds, $table); + if (!$result) { + return false; + } + write_into_cache + return $result; + } + + function readImpliedRights($currentRights, $currentLevel) + { + if (in_cache) { + return cache; + } + $result = $this->_storage->readImpliedRights($currentRights, $currentLevel); + if (!$result) { + return false; + } + write_into_cache + return $result; + } + /** * properly disconnect from resources * http://cvs.php.net/diff.php/pear/Perm_LiveUser/Perm/Storage/MDB2.php?r1=1.1&r2=1.2&ty=u Index: pear/Perm_LiveUser/Perm/Storage/MDB2.php diff -u pear/Perm_LiveUser/Perm/Storage/MDB2.php:1.1 pear/Perm_LiveUser/Perm/Storage/MDB2.php:1.2 --- pear/Perm_LiveUser/Perm/Storage/MDB2.php:1.1 Mon Nov 8 10:57:34 2004 +++ pear/Perm_LiveUser/Perm/Storage/MDB2.php Mon Nov 8 17:15:40 2004 @@ -43,82 +43,13 @@ * * @author Lukas Smith <smith@backendmedia.com> * @author Bjoern Kraus <krausbn@php.net> - * @version $Id: MDB2.php,v 1.1 2004/11/08 15:57:34 lsmith Exp $ + * @version $Id: MDB2.php,v 1.2 2004/11/08 22:15:40 lsmith Exp $ * @package LiveUser * @category authentication */ class LiveUser_Perm_Storage_MDB2 extends LiveUser_Perm_Storage_SQL { /** - * dsn that was connected to - * @var object - * @access private - */ - var $dsn = null; - - /** - * disconnect - * @var object - * @access private - */ - var $disconnect = null; - - /** - * PEAR::MDB2 connection object. - * - * @var object - * @access private - */ - var $dbc = null; - - /** - * Table prefix - * Prefix for all db tables the container has. - * - * @var string - * @access public - */ - var $prefix = 'liveuser_'; - - /** - * Indicates if backend module initialized correctly. If yes, - * true, if not false. Backend module won't initialize if the - * init value (usually an object or resource handle that - * identifies the backend to be used) is not of the required - * type. - * - * @var boolean - * @access public - */ - var $init_ok = false; - - /** - * Columns of the perm table. - * Associative array with the names of the perm table columns. - * The 'group_id' and 'group_define_name' fields have to be set. - * 'group_type', 'is_active', 'owner_user_id' and 'owner_group_id' are optional. - * It doesn't make sense to set only one of the time columns without the - * other. - * - * The type attribute is only useful when using MDB or MDB2. - * - * @access public - * @var array - */ - var $groupTableCols = array( - 'required' => array( - 'group_id' => array('type' => 'integer', 'name' => 'group_id'), - 'group_define_name' => array('type' => 'text', 'name' => 'group_define_name') - ), - 'optional' => array( - 'group_type' => array('type' => 'integer', 'name' => 'group_type'), - 'is_active' => array('type' => 'boolean', 'name' => 'is_active'), - 'owner_user_id' => array('type' => 'integer', 'name' => 'owner_user_id'), - 'owner_group_id' => array('type' => 'integer', 'name' => 'owner_group_id') - ) - ); - - /** * Constructor * * @access protected @@ -128,36 +59,28 @@ function LiveUser_Perm_Storage_MDB2(&$conf) { $this->LiveUser_Perm_Storage_SQL(); - if (is_array($conf)) { - foreach ($conf as $key => $value) { - if (isset($this->$key)) { - $this->$key = $value; - } + if (isset($conf['connection']) && + MDB2::isConnection($conf['connection']) + ) { + $this->dbc = &$conf['connection']; + } elseif (isset($conf['dsn'])) { + $this->dsn = $conf['dsn']; + $function = null; + if (isset($conf['function'])) { + $function = $conf['function']; + } + $options = null; + if (isset($conf['options'])) { + $options = $conf['options']; } - if (isset($conf['connection']) && - MDB2::isConnection($conf['connection']) - ) { - $this->dbc = &$conf['connection']; - $this->init_ok = true; - } elseif (isset($conf['dsn'])) { - $this->dsn = $conf['dsn']; - $function = null; - if (isset($conf['function'])) { - $function = $conf['function']; - } - $options = null; - if (isset($conf['options'])) { - $options = $conf['options']; - } - $options['portability'] = MDB2_PORTABILITY_ALL; - if ($function == 'singleton') { - $this->dbc =& MDB2::singleton($conf['dsn'], $options); - } else { - $this->dbc =& MDB2::connect($conf['dsn'], $options); - } - if (!MDB2::isError($this->dbc)) { - $this->init_ok = true; - } + $options['portability'] = MDB2_PORTABILITY_ALL; + if ($function == 'singleton') { + $this->dbc =& MDB2::singleton($conf['dsn'], $options); + } else { + $this->dbc =& MDB2::connect($conf['dsn'], $options); + } + if (PEAR::isError($this->dbc)) { + return false; } } } @@ -180,6 +103,7 @@ if (PEAR::isError($result)) { return false; } + return $result; } @@ -194,35 +118,201 @@ { $query = ' SELECT - R.right_id AS rightid, '.LIVEUSER_MAX_LEVEL.' + R.right_id, + U.right_level FROM - '.$this->prefix.'userrights UR - INNER JOIN + '.$this->prefix.'rights R, + '.$this->prefix.'userrights U + WHERE + R.right_id=U.right_id + AND + U.perm_user_id='.$this->dbc->quote($permUserId, 'integer'); + + $types = array('integer', 'integer'); + $result = $this->dbc->queryAll($query, $types, MDB2_FETCHMODE_ORDERED, true); + + if (PEAR::isError($result)) { + return false; + } + + return $result; + } + + function readAreaAdminAreas($permUserId) + { + // get all areas in which the user is area admin + $query = ' + SELECT + R.right_id, + '.LIVEUSER_MAX_LEVEL.' AS right_level + FROM + '.$this->prefix.'area_admin_areas AAA, '.$this->prefix.'rights R - ON - UR.right_id=R.right_id WHERE - UR.perm_user_id='.$this->dbc->quote($this->permUserId, 'integer') . ' + AAA.area_id=R.area_id AND - UR.right_level > 0'; - $types = array('integer', 'integer'); + AAA.perm_user_id='.$this->dbc->quote($permUserId, 'integer'); + + $types = array('integer', 'integer'); $result = $this->dbc->queryAll($query, $types, MDB2_FETCHMODE_ORDERED, true); if (PEAR::isError($result)) { return false; - } // if + } return $result; } /** - * properly disconnect from resources + * Reads all the group ids in that the user is also a member of + * (all groups that are subgroups of these are also added recursively) + * + * @access private + * @see readRights() + * @return void + */ + function readGroups($permUserId) + { + $query = ' + SELECT + GU.group_id + FROM + '.$this->prefix.'groupusers GU, + '.$this->prefix.'groups G + WHERE + GU.group_id=G.group_id + AND + G.is_active='.$this->dbc->quote(true, 'boolean').' + AND + perm_user_id='.$this->dbc->quote($permUserId, 'integer'); + + $result = $this->dbc->queryCol($query); + + if (PEAR::isError($result)) { + return false; + } + + return $result; + } // end func readGroups + + /** + * Reads the group rights + * and put them in the array + * + * right => 1 * * @access public + * @return mixed MDB2_Error on failure or nothing */ - function disconnect() + function readGroupRights($groupIds) + { + $query = ' + SELECT + GR.right_id, + MAX(GR.right_level) + FROM + '.$this->prefix.'grouprights GR + WHERE + GR.group_id IN('.implode(',', $groupIds).') + GROUP BY + GR.right_id'; + + $types = array('integer', 'integer'); + $result = $this->dbc->queryAll($query, $types, MDB2_FETCHMODE_ORDERED, true); + + if (PEAR::isError($result)) { + return false; + } + + return $result; + } // end func readGroupRights + + function readSubGroups($groupIds, $newGroupIds) { - $this->dbc->disconnect(); + $query = ' + SELECT + DISTINCT SG.subgroup_id + FROM + '.$this->prefix.'groups G, + '.$this->prefix.'group_subgroups SG + WHERE + SG.subgroup_id = G.' . $this->groupTableCols['required']['group_id']['name'] . ' + AND + SG.group_id IN ('.$this->dbc->datatype->implodeArray($newGroupIds, $this->groupTableCols['required']['group_id']['type']).') + AND + SG.subgroup_id NOT IN ('.$this->dbc->datatype->implodeArray($groupIds, $this->groupTableCols['required']['group_id']['type']).')'; + + if (isset($this->groupTableCols['optional']['is_active'])) { + $query .= 'AND + G.' . $this->groupTableCols['optional']['is_active']['name'] . '=' . + $this->dbc->quote(true, $this->groupTableCols['optional']['is_active']['type']); + } + + $result = $this->dbc->queryCol($query); + + if (PEAR::isError($result)) { + return false; + } + + return $result; + } + + function readImplyingRights($rightIds, $table) + { + $query = ' + SELECT + DISTINCT + TR.right_level, + TR.right_id + FROM + '.$this->prefix.'rights R, + '.$this->prefix.$table.'rights TR + WHERE + TR.right_id=R.right_id + AND + R.right_id IN ('.implode(',', array_keys($rightIds)).') + AND + R.has_implied='.$this->dbc->quote(true, 'boolean'); + + if ($table == 'user') { + $query .= ' AND TR.perm_user_id = '.$this->dbc->quote($this->permUserId, 'integer'); + } else { + $query .= ' AND TR.group_id IN ('.implode(',', $this->groupIds).')'; + } + + $types = array('integer', 'integer'); + $result = $this->dbc->queryAll($query, $types, MDB2_FETCHMODE_ORDERED, true, false, true); + + if (PEAR::isError($result)) { + return false; + } + + return $result; + } + + function readImpliedRights($currentRights, $currentLevel) + { + $query = ' + SELECT + RI.implied_right_id AS right_id, + '.$currentLevel.' AS right_level, + R.has_implied + FROM + '.$this->prefix.'rights R, + '.$this->prefix.'right_implied RI + WHERE + RI.implied_right_id=R.right_id + AND + RI.right_id IN ('.$this->dbc->datatype->implodeArray($currentRights, 'integer').')'; + + $types = array('integer', 'integer', 'boolean'); + $result = $this->dbc->queryAll($query, $types, MDB2_FETCHMODE_ASSOC); + + if (PEAR::isError($result)) { + return false; + } + + return $result; } } ?> \ No newline at end of file http://cvs.php.net/diff.php/pear/Perm_LiveUser/Perm/Storage/SQL.php?r1=1.1&r2=1.2&ty=u Index: pear/Perm_LiveUser/Perm/Storage/SQL.php diff -u pear/Perm_LiveUser/Perm/Storage/SQL.php:1.1 pear/Perm_LiveUser/Perm/Storage/SQL.php:1.2 --- pear/Perm_LiveUser/Perm/Storage/SQL.php:1.1 Mon Nov 8 10:57:34 2004 +++ pear/Perm_LiveUser/Perm/Storage/SQL.php Mon Nov 8 17:15:41 2004 @@ -43,7 +43,7 @@ * * @author Lukas Smith <smith@backendmedia.com> * @author Bjoern Kraus <krausbn@php.net> - * @version $Id: SQL.php,v 1.1 2004/11/08 15:57:34 lsmith Exp $ + * @version $Id: SQL.php,v 1.2 2004/11/08 22:15:41 lsmith Exp $ * @package LiveUser * @category authentication */ @@ -57,13 +57,6 @@ var $dsn = null; /** - * disconnect - * @var object - * @access private - */ - var $disconnect = null; - - /** * PEAR::MDB2 connection object. * * @var object @@ -81,18 +74,6 @@ var $prefix = 'liveuser_'; /** - * Indicates if backend module initialized correctly. If yes, - * true, if not false. Backend module won't initialize if the - * init value (usually an object or resource handle that - * identifies the backend to be used) is not of the required - * type. - * - * @var boolean - * @access public - */ - var $init_ok = false; - - /** * Columns of the perm table. * Associative array with the names of the perm table columns. * The 'group_id' and 'group_define_name' fields have to be set. @@ -127,6 +108,7 @@ */ function LiveUser_Perm_Storage_MDB2(&$conf) { + $this->LiveUser_Perm_Storage(); } function mapUser($uid, $containerName) http://cvs.php.net/diff.php/pear/Perm_LiveUser/Perm/Storage/XML.php?r1=1.1&r2=1.2&ty=u Index: pear/Perm_LiveUser/Perm/Storage/XML.php diff -u pear/Perm_LiveUser/Perm/Storage/XML.php:1.1 pear/Perm_LiveUser/Perm/Storage/XML.php:1.2 --- pear/Perm_LiveUser/Perm/Storage/XML.php:1.1 Mon Nov 8 10:57:34 2004 +++ pear/Perm_LiveUser/Perm/Storage/XML.php Mon Nov 8 17:15:41 2004 @@ -43,7 +43,7 @@ * * @author Lukas Smith <smith@backendmedia.com> * @author Bjoern Kraus <krausbn@php.net> - * @version $Id: XML.php,v 1.1 2004/11/08 15:57:34 lsmith Exp $ + * @version $Id: XML.php,v 1.2 2004/11/08 22:15:41 lsmith Exp $ * @package LiveUser * @category authentication */ @@ -74,18 +74,6 @@ var $userObj = null; /** - * Indicates if backend module initialized correctly. If yes, - * true, if not false. Backend module won't initialize if the - * init value (usually an object or resource handle that - * identifies the backend to be used) is not of the required - * type. - * - * @var boolean - * @access public - */ - var $init_ok = false; - - /** * Columns of the perm table. * Associative array with the names of the perm table columns. * The 'group_id' and 'group_define_name' fields have to be set. @@ -122,38 +110,30 @@ { $this->LiveUser_Perm_Storage(&$conf); if (!is_file($this->file)) { - if (is_file(getenv('DOCUMENT_ROOT') . $this->file)) { - $this->file = getenv('DOCUMENT_ROOT') . $this->file; - } else { - $this->_stack->push(LIVEUSER_ERROR_MISSING_DEPS, 'exception', array(), "Perm initialisation failed. Can't find xml file."); + if (!is_file(getenv('DOCUMENT_ROOT') . $this->file)) { + $this->_stack->push(LIVEUSER_ERROR_MISSING_DEPS, 'exception', array(), + "Perm initialisation failed. Can't find xml file."); return false; } + $this->file = getenv('DOCUMENT_ROOT') . $this->file; } - if ($this->file) { - if (class_exists('XML_Tree')) { - $tree =& new XML_Tree($this->file); - $err =& $tree->getTreeFromFile(); - if (PEAR::isError($err)) { - return false; - } else { - $this->tree = $tree; - $this->init_ok = true; - } - } else { - $this->_stack->push(LIVEUSER_ERROR_MISSING_DEPS, 'exception', array(), "Perm initialisation failed. Can't find XML_Tree class"); - return false; - } - } else { - $this->_stack->push(LIVEUSER_ERROR_MISSING_DEPS, 'exception', array(), "Perm initialisation failed. Can't find XML_Tree class"); + if (!class_exists('XML_Tree')) { + $this->_stack->push(LIVEUSER_ERROR_MISSING_DEPS, 'exception', array(), + "Perm initialisation failed. Can't find XML_Tree class"); + return false; + } + $tree =& new XML_Tree($this->file); + $err =& $tree->getTreeFromFile(); + if (PEAR::isError($err)) { return false; } + $this->tree = $tree; } function mapUser($uid, $containerName) { $nodeIndex = 0; $userIndex = 0; - $result = false; foreach ($this->tree->root->children as $node) { if ($node->name == 'users') { @@ -167,7 +147,7 @@ $result['userid'] = $user->attributes['userId']; $result['usertype'] = $user->attributes['type']; $this->userObj =& $this->tree->root->getElement(array($nodeIndex, $userIndex)); - break; + return $result; } $userIndex++; } @@ -175,7 +155,7 @@ $nodeIndex++; } - return $result; + return false; } /** @@ -192,7 +172,13 @@ if ($node->name == 'rights') { $tmp = explode(',', $node->content); foreach ($tmp as $value) { - $result[$value] = 1; + $level = LIVEUSER_MAX_LEVEL; + // level syntax: 10(2) => right id 10 at level 2 + if (preg_match('/(\d+)\((\d+)\)/', $value, $match)) { + $value = $match[1]; + $level = $match[2]; + } + $result[$value] = $level; } } }
« previous php.pear.cvs (#25787) next »