cvs: pear /Perm_LiveUser/Perm Complex.php Medium.php

From: Date: Mon, 08 Nov 2004 22:12:35 +0000
Subject: cvs: pear /Perm_LiveUser/Perm Complex.php Medium.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-25786@lists.php.net to get a copy of this message
lsmith Mon Nov 8 17:12:35 2004 EDT Added files: /pear/Perm_LiveUser/Perm Medium.php Complex.php Log: initial rather untested version

http://cvs.php.net/co.php/pear/Perm_LiveUser/Perm/Medium.php?r=1.1&p=1 Index: pear/Perm_LiveUser/Perm/Medium.php +++ pear/Perm_LiveUser/Perm/Medium.php <?php // LiveUser: A framework for authentication and authorization in PHP applications // Copyright (C) 2002-2003 Markus Wolff // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA /** * MDB2_Medium container for permission handling * * @package LiveUser * @category authentication */ /** * Require parent class definition. */ require_once 'LiveUser/Perm/Simple.php'; /** * Medium MDB2-based complexity driver for LiveUser. * * Description: * The MDB2_Medium provides the following functionalities * - users * - groups * - grouprights * - userrights * - authareas * * @author Arnaud Limbourg * @version $Id: Medium.php,v 1.1 2004/11/08 22:12:32 lsmith Exp $ * @package LiveUser * @category authentication */ class LiveUser_Perm_Medium extends LiveUser_Perm_Simple { /** * Constructor * * @access protected * @param mixed configuration array * @return void */ function LiveUser_Perm_Medium(&$confArray) { $this->LiveUser_Perm_Simple($confArray); } /** * Reads all rights of current user into an * associative array. * Group rights and invididual rights are being merged * in the process. * * @access private * @return void */ function readRights() { $this->rights = array(); $result = $this->readUserRights($this->permUserId); if (!$result) { return false; } if ($this->userType == LIVEUSER_AREAADMIN_TYPE_ID) { $result = $this->readAreaAdminAreas($this->permUserId); if (PEAR::isError($result)) { return false; } if (is_array($this->areaAdminAreas)) { if (is_array($this->userRights)) { $this->userRights = $this->areaAdminAreas + $this->userRights; } else { $this->userRights = $this->areaAdminAreas; } } } $result = $this->readGroups($this->permUserId); if (!$result) { return false; } $result = $this->readGroupRights($this->groupIds); if (!$result) { return false; } $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 $right => $level) { if (isset($tmpRights[$right])) { if ($level < 0) { // Revoking rights: A negative value indicates that the // right level is lowered or the right is even revoked // despite the group memberships of this user $tmpRights[$right] = $tmpRights[$right] + $level; } else { $tmpRights[$right] = max($tmpRights[$right], $level); } } else { $tmpRights[$right] = $level; } } } // Strip values from array if level is not greater than zero if (is_array($tmpRights)) { foreach ($tmpRights as $right => $level) { if ($level > 0) { $this->rights[$right] = $level; } } } return $this->rights; } // end func readRights function readAreaAdminAreas($permUserId) { $this->userRights = array(); $result = $this->_storage->readAreaAdminAreas($permUserId); if (!$result) { return false; } $this->areaAdminAreas = $result; return $result; } function readGroups($permUserId) { $this->groupIds = array(); $result = $this->_storage->readGroups($permUserId); if (!$result) { return false; } $this->groupIds = $result; return $result; } function readGroupRights($groupIds) { $this->groupRights = array(); $result = $this->_storage->readGroupRights($groupIds); if (!$result) { return false; } $this->groupRights = $result; return $result; } } // end class LiveUser_Perm_Container_MDB2_Medium ?> http://cvs.php.net/co.php/pear/Perm_LiveUser/Perm/Complex.php?r=1.1&p=1 Index: pear/Perm_LiveUser/Perm/Complex.php +++ pear/Perm_LiveUser/Perm/Complex.php <?php // LiveUser: A framework for authentication and authorization in PHP applications // Copyright (C) 2002-2003 Markus Wolff // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA /** * MDB2_Complex container for permission handling * * @package LiveUser * @category authentication */ /** * Require parent class definition. */ require_once 'LiveUser/Perm/Medium.php'; /** * This is a PEAR::MDB2 backend driver for the LiveUser class. * A PEAR::MDB2 connection object can be passed to the constructor to reuse an * existing connection. Alternatively, a DSN can be passed to open a new one. * * Requirements: * - File "Liveuser.php" (contains the parent class "LiveUser") * - Array of connection options or a PEAR::MDB2 connection object must be * passed to the constructor. * Example: array('dsn' => 'mysql://user:pass@host/db_name') * OR * &$conn (PEAR::MDB2 connection object) * * @author Lukas Smith <smith@backendmedia.com> * @author Bjoern Kraus <krausbn@php.net> * @version $Id: Complex.php,v 1.1 2004/11/08 22:12:32 lsmith Exp $ * @package LiveUser * @category authentication */ class LiveUser_Perm_Complex extends LiveUser_Perm_Medium { /** * Constructor * * @access protected * @param mixed configuration array * @return void */ function LiveUser_Perm_Complex(&$confArray) { $this->LiveUser_Perm_Medium($confArray); } /** * Reads all individual implied rights of current user into * an array of this format: * RightName -> Value * * @access private * @return array with rightIds as key and level as value */ function _readImpliedRights($rightIds, $table) { if (!count($rightIds)) { return $rightIds; } $queue = array(); $result = $this->_storage->readImplyingRights($rightIds, $table); if (!is_array($result)) { return false; } $queue = $result; while (count($queue)) { $currentRights = reset($queue); $currentLevel = key($queue); unset($queue[$currentLevel]); $result = $this->_storage->readImpliedRights($currentRights, $currentLevel); if (!is_array($result)) { return false; } foreach ($result as $val) { // only store the implied right if the right wasn't stored before // or if the level is higher if (!isset($rightIds[$val['right_id']]) || $rightIds[$val['right_id']] < $val['right_level'] ) { $rightIds[$val['right_id']] = $val['right_level']; if ($val['has_implied']) { $queue[$val['right_level']][] = $val['right_id']; } } } } return $rightIds; } // end func _readImpliedRights /** * Reads all individual rights of current user into * an array of this format: * RightName -> Value * * @access private * @see readRights() * @return void */ function readUserRights() { parent::readUserRights(); $this->userRights = $this->_readImpliedRights($this->userRights, 'user'); } // end func readUserRights /** * 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() { parent::readGroups(); $result = $this->groupIds; // get all subgroups recursively while (count($result)) { $result = $this->readSubGroups($this->groupIds, $result); if (is_array($result)) { $this->groupIds = array_merge($result, $this->groupIds); } } } // end func readGroups function readSubGroups($groupIds, $newGroupIds) { $result = $this->_storage->readSubGroups($groupIds, $newGroupIds); if (!$result) { return false; } return $result; } /** * Reads all individual rights of current user into * a two-dimensional array of this format: * "GroupName" => "RightName" -> "Level" * * @access private * @see readRights() * @return void */ function readGroupRights() { parent::readGroupRights(); $this->groupRights = $this->_readImpliedRights($this->groupRights, 'group'); } // end func readGroupRights /** * Checks if the current user has a certain right in a * given area at the necessary level. * * Level 1: requires that owner_user_id matches $this->permUserId * Level 2: requires that the $owner_group_id matches the id one of * the (sub)groups that $this->permUserId is a memember of * or requires that the $owner_user_id matches a perm_user_id of * a memeber of one of $this->permUserId's (sub)groups * Level 3: no requirements * * Important note: * Every ressource MAY be owned by a user and/or by a group. * Therefore, $owner_user_id and/or $owner_group_id can * either be an integer or null. * * @access private * @see checkRightLevel() * @param integer $level Level value as returned by checkRight(). * @param mixed $owner_user_id Id or array of Ids of the owner of the ressource for which the right is requested. * @param mixed $owner_group_id Id or array of Ids of the group of the * ressource for which the right is requested. * @return boolean level if the level is sufficient to grant access else false. */ function checkLevel($level, $owner_user_id, $owner_group_id) { // level above 0 if ($level <= 0) { return false; } // highest level (that is level 3) if ($level == LIVEUSER_MAX_LEVEL) { return $level; } // level 1 or higher if ((!is_array($owner_user_id) && $this->permUserId == $owner_user_id) || is_array($owner_user_id) && in_array($this->permUserId, $owner_user_id) ) { return $level; // level 2 or higher } if ($level >= 2) { // check if the ressource is owned by a (sub)group // that the user is part of if (is_array($owner_group_id)) { if (count(array_intersect($owner_group_id, $this->groupIds))) { return $level; } } elseif (in_array($owner_group_id, $this->groupIds)) { return $level; } } return false; } // end func checkLevel } ?>
« previous php.pear.cvs (#25786) next »