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

Source for file Medium.php

Documentation is available at Medium.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * A framework for authentication and authorization in PHP applications
  6.  *
  7.  * LiveUser is an authentication/permission framework designed
  8.  * to be flexible and easily extendable.
  9.  *
  10.  * Since it is impossible to have a
  11.  * "one size fits all" it takes a container
  12.  * approach which should enable it to
  13.  * be versatile enough to meet most needs.
  14.  *
  15.  * PHP version 4 and 5
  16.  *
  17.  * LICENSE: This library is free software; you can redistribute it and/or
  18.  * modify it under the terms of the GNU Lesser General Public
  19.  * License as published by the Free Software Foundation; either
  20.  * version 2.1 of the License, or (at your option) any later version.
  21.  *
  22.  * This library is distributed in the hope that it will be useful,
  23.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  25.  * Lesser General Public License for more details.
  26.  *
  27.  * You should have received a copy of the GNU Lesser General Public
  28.  * License along with this library; if not, write to the Free Software
  29.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  30.  * MA  02111-1307  USA
  31.  *
  32.  *
  33.  * @category authentication
  34.  * @package LiveUser
  35.  * @author  Markus Wolff <wolff@21st.de>
  36.  * @author  Helgi Þormar Þorbjörnsson <dufuz@php.net>
  37.  * @author  Lukas Smith <smith@pooteeweet.org>
  38.  * @author  Arnaud Limbourg <arnaud@php.net>
  39.  * @author  Pierre-Alain Joye <pajoye@php.net>
  40.  * @author  Bjoern Kraus <krausbn@php.net>
  41.  * @copyright 2002-2006 Markus Wolff
  42.  * @license http://www.gnu.org/licenses/lgpl.txt
  43.  * @version CVS: $Id: Medium.php,v 1.27 2006/04/10 14:41:44 lsmith Exp $
  44.  * @link http://pear.php.net/LiveUser
  45.  */
  46.  
  47. /**
  48.  
  49.  *
  50.  * @package LiveUser
  51.  * @category authentication
  52.  */
  53.  
  54. /**
  55.  * Require parent class definition.
  56.  */
  57. require_once 'LiveUser/Perm/Simple.php';
  58.  
  59. /**
  60.  * Medium container for permission handling
  61.  *
  62.  * Medium permission complexity driver for LiveUser.
  63.  *
  64.  * @category authentication
  65.  * @package LiveUser
  66.  * @author   Arnaud Limbourg
  67.  * @copyright 2002-2006 Markus Wolff
  68.  * @license http://www.gnu.org/licenses/lgpl.txt
  69.  * @version Release: @package_version@
  70.  * @link http://pear.php.net/LiveUser
  71.  */
  72. {
  73.     /**
  74.      * One-dimensional array containing all the groups
  75.      * ids for the actual user.
  76.      *
  77.      * @var array 
  78.      * @access public
  79.      */
  80.     var $group_ids = array();
  81.  
  82.     /**
  83.      * One-dimensional array containing all the groups
  84.      * ids for the actual user without subgroups.
  85.      *
  86.      * @var array 
  87.      * @access public
  88.      */
  89.     var $user_group_ids = array();
  90.  
  91.     /**
  92.      * One-dimensional array containing only the group
  93.      * rights for the actual user.
  94.      *
  95.      * Format: "RightId" => "Level"
  96.      *
  97.      * @var array 
  98.      * @access public
  99.      */
  100.     var $group_right_ids = array();
  101.  
  102.     /**
  103.      * Reads all rights of current user into an
  104.      * associative array.
  105.      * Group rights and invididual rights are being merged
  106.      * in the process.
  107.      *
  108.      * @return bool true on success or false on failure
  109.      *
  110.      * @access private
  111.      */
  112.     function readRights()
  113.     {
  114.         $this->right_ids = array();
  115.  
  116.         $result $this->readUserRights($this->perm_user_id);
  117.         if ($result === false{
  118.             return false;
  119.         }
  120.  
  121.         $result $this->readGroups($this->perm_user_id);
  122.         if ($result === false{
  123.             return false;
  124.         }
  125.  
  126.         $result $this->readGroupRights($this->group_ids);
  127.         if ($result === false{
  128.             return false;
  129.         }
  130.  
  131.         $groupRights is_array($this->group_right_ids$this->group_right_ids : array();
  132.  
  133.         // Check if user has individual rights...
  134.         if (is_array($this->user_right_ids)) {
  135.             // Overwrite values from temporary array with values from userrights
  136.             foreach ($this->user_right_ids as $right => $level{
  137.                 if (array_key_exists($right$groupRights)) {
  138.                     if ($level < 0{
  139.                         // Revoking rights: A negative value indicates a maximum
  140.                         // possible right level
  141.                         $max_allowed_level LIVEUSER_MAX_LEVEL + $level;
  142.                         $this->right_ids[$rightmin($groupRights[$right]$max_allowed_level);
  143.                     elseif ($level > 0{
  144.                         $this->right_ids[$rightmax($groupRights[$right]$level);
  145.                     elseif ($level == 0{
  146.                         unset($this->right_ids[$right]);
  147.                     }
  148.                     unset($groupRights[$right]);
  149.                 elseif ($level < 0{
  150.                     $this->right_ids[$rightLIVEUSER_MAX_LEVEL + $level;
  151.                 elseif ($level > 0{
  152.                     $this->right_ids[$right$level;
  153.                 elseif ($level == 0{
  154.                     unset($this->right_ids[$right]);
  155.                 }
  156.             }
  157.         }
  158.  
  159.         $this->right_ids+= $groupRights;
  160.  
  161.         return $this->right_ids;
  162.     }
  163.  
  164.     /**
  165.      * Read all the groups in which the user is a member
  166.      *
  167.      * @param int perm user id
  168.      * @return array requested data or false on failure
  169.      *
  170.      * @access private
  171.      */
  172.     function readGroups($perm_user_id)
  173.     {
  174.         $this->group_ids = $this->user_group_ids = array();
  175.  
  176.         $result $this->_storage->readGroups($perm_user_id);
  177.         if ($result === false{
  178.             return false;
  179.         }
  180.  
  181.         $this->group_ids = $this->user_group_ids = $result;
  182.         return $this->group_ids;
  183.     }
  184.  
  185.     /**
  186.      * Reads all rights of the groups into an
  187.      * associative array.
  188.      *
  189.      * @param array group ids
  190.      * @return array requested data or false on failure
  191.      *
  192.      * @access private
  193.      */
  194.     function readGroupRights($group_ids)
  195.     {
  196.         $this->group_right_ids = array();
  197.  
  198.         if (!is_array($group_ids|| !count($group_ids)) {
  199.             return null;
  200.         }
  201.  
  202.         $result $this->_storage->readGroupRights($group_ids);
  203.         if ($result === false{
  204.             return false;
  205.         }
  206.  
  207.         $this->group_right_ids = $result;
  208.         return $this->group_right_ids;
  209.     }
  210.  
  211.     /**
  212.      * Checks if the current user is a member of a certain group
  213.      * If $this->ondemand and $ondemand is true, the groups will be loaded on
  214.      * the fly.
  215.      *
  216.      * @param int Id of the group to check for.
  217.      *
  218.      * @access private
  219.      */
  220.     function checkGroup($group_id)
  221.     {
  222.         if (is_array($this->group_ids)) {
  223.             return in_array($group_id$this->group_ids);
  224.         }
  225.         return false;
  226.     }
  227. }
  228. ?>

Documentation generated on Mon, 28 Jan 2008 03:30:27 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.