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

Source for file Complex.php

Documentation is available at Complex.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: Complex.php,v 1.27 2006/04/10 14:41:44 lsmith Exp $
  44.  * @link http://pear.php.net/LiveUser
  45.  */
  46.  
  47. /**
  48.  * Require parent class definition.
  49.  */
  50. require_once 'LiveUser/Perm/Medium.php';
  51.  
  52. /**
  53.  * Complex container for permission handling
  54.  *
  55.  * Complex permission complexity driver for LiveUser.
  56.  *
  57.  * @category authentication
  58.  * @package LiveUser
  59.  * @author  Lukas Smith <smith@pooteeweet.org>
  60.  * @author  Bjoern Kraus <krausbn@php.net>
  61.  * @version $Id: Complex.php,v 1.27 2006/04/10 14:41:44 lsmith Exp $
  62.  * @copyright 2002-2006 Markus Wolff
  63.  * @license http://www.gnu.org/licenses/lgpl.txt
  64.  * @version Release: @package_version@
  65.  * @link http://pear.php.net/LiveUser
  66.  */
  67. {
  68.     /**
  69.      * Reads all individual implied rights of current user into
  70.      * an array of this format:
  71.      * RightName -> Value
  72.      *
  73.      * @param array $rightIds 
  74.      * @param string $table 
  75.      * @return array with rightIds as key and level as value
  76.      *
  77.      * @access private
  78.      */
  79.     function _readImpliedRights($rightIds$table)
  80.     {
  81.         if (!is_array($rightIds|| !count($rightIds)) {
  82.             return null;
  83.         }
  84.  
  85.         $result $this->_storage->readImplyingRights($rightIds$table);
  86.         if ($result === false{
  87.             return false;
  88.         }
  89.  
  90.         $queue $result;
  91.         while (count($queue)) {
  92.             $currentRights reset($queue);
  93.             $currentLevel key($queue);
  94.             unset($queue[$currentLevel]);
  95.  
  96.             $result $this->_storage->readImpliedRights($currentRights$currentLevel);
  97.             if (!is_array($result)) {
  98.                 return false;
  99.             }
  100.             foreach ($result as $val{
  101.                 // only store the implied right if the right wasn't stored before
  102.                 // or if the level is higher
  103.                 if (!array_key_exists($val['right_id']$rightIds)
  104.                     || $rightIds[$val['right_id']] $val['right_level']
  105.                 {
  106.                     $rightIds[$val['right_id']] $val['right_level'];
  107.                     if ($val['has_implied']{
  108.                         $queue[$val['right_level']][$val['right_id'];
  109.                     }
  110.                 }
  111.             }
  112.         }
  113.         return $rightIds;
  114.     }
  115.  
  116.     /**
  117.      * Reads all individual rights of current user into
  118.      * an array of this format:
  119.      * RightName -> Value
  120.      *
  121.      * @param int perm user id
  122.      * @see    readRights()
  123.      * @return array requested data or false on failure
  124.      *
  125.      * @access private
  126.      */
  127.     function readUserRights($perm_user_id)
  128.     {
  129.         $result = parent::readUserRights($perm_user_id);
  130.          if ($result === false{
  131.             return false;
  132.          }
  133.  
  134.         if ($this->perm_type == LIVEUSER_AREAADMIN_TYPE_ID{
  135.             $result $this->readAreaAdminAreas($this->perm_user_id);
  136.             if ($result === false{
  137.                return false;
  138.             }
  139.  
  140.             if (is_array($this->area_admin_areas)) {
  141.                 if (is_array($this->user_right_ids)) {
  142.                     $this->user_right_ids = $this->area_admin_areas $this->user_right_ids;
  143.                 else {
  144.                     $this->user_right_ids = $this->area_admin_areas;
  145.                 }
  146.             }
  147.         }
  148.  
  149.         $this->user_right_ids = $this->_readImpliedRights($this->user_right_ids'user');
  150.  
  151.         return $this->user_right_ids;
  152.     }
  153.  
  154.     /**
  155.      * Reads all the group ids in that the user is also a member of
  156.      * (all groups that are subgroups of these are also added recursively)
  157.      *
  158.      * @param int perm user id
  159.      * @see    readRights()
  160.      * @return array requested data or false on failure
  161.      *
  162.      * @access private
  163.      */
  164.     function readGroups($perm_user_id)
  165.     {
  166.         $result = parent::readGroups($perm_user_id);
  167.  
  168.         // get all subgroups recursively
  169.         while (count($result)) {
  170.             $result $this->readSubGroups($this->group_ids$result);
  171.             if (is_array($result)) {
  172.                 $this->group_ids = array_merge($result$this->group_ids);
  173.             }
  174.         }
  175.         return $this->group_ids;
  176.     }
  177.  
  178.     /**
  179.      * Read the sub groups of the groups where the user is a member in
  180.      *
  181.      * @param array group ids
  182.      * @param array new group ids
  183.      * @return array requested data or false on failure
  184.      *
  185.      * @access private
  186.      */
  187.     function readSubGroups($group_ids$newGroupIds)
  188.     {
  189.         $result $this->_storage->readSubGroups($group_ids$newGroupIds);
  190.         if ($result === false{
  191.             return false;
  192.         }
  193.         return $result;
  194.     }
  195.  
  196.     /**
  197.      * Reads all individual rights of current user into
  198.      * a two-dimensional array of this format:
  199.      * "GroupName" => "RightName" -> "Level"
  200.      *
  201.      * @param   array id's for the groups that rights will be read from
  202.      * @see    readRights()
  203.      * @return array requested data or false on failure
  204.      *
  205.       * @access private
  206.      */
  207.     function readGroupRights($group_ids)
  208.     {
  209.         $group_right_ids = parent::readGroupRights($group_ids);
  210.         $this->group_right_ids = $this->_readImpliedRights($group_right_ids'group');
  211.  
  212.         return $this->group_right_ids;
  213.     }
  214.  
  215.     /**
  216.      * Checks if the current user has a certain right in a
  217.      * given area at the necessary level.
  218.      *
  219.      * Level 1: requires that owner_user_id matches $this->perm_user_id
  220.      * Level 2: requires that the $owner_group_id matches the id one of
  221.      *          the (sub)groups that $this->perm_user_id is a member of
  222.      *          or requires that the $owner_user_id matches a perm_user_id of
  223.      *          a member of one of $this->perm_user_id's (sub)groups
  224.      * Level 3: no requirements
  225.      *
  226.      * Important note:
  227.      *          Every ressource MAY be owned by a user and/or by a group.
  228.      *          Therefore, $owner_user_id and/or $owner_group_id can
  229.      *          either be an integer or null.
  230.      *
  231.      * @see    checkRightLevel()
  232.      * @param int       Level value as returned by checkRight().
  233.      * @param int|arrayId or array of Ids of the owner of the
  234.                         ressource for which the right is requested.
  235.      * @param int|arrayId or array of Ids of the group of the
  236.      *                   ressource for which the right is requested.
  237.      * @return bool level if the level is sufficient to grant access else false.
  238.      *
  239.      * @access public
  240.      */
  241.     function checkLevel($level$owner_user_id$owner_group_id)
  242.     {
  243.         // level above 0
  244.         if ($level <= 0{
  245.             return false;
  246.         }
  247.         // highest level (that is level 3) or no owner id's passed
  248.         if ($level == LIVEUSER_MAX_LEVEL
  249.             || (is_null($owner_user_id&& is_null($owner_group_id))
  250.         {
  251.             return $level;
  252.         }
  253.         // level 1 or higher
  254.         if ((!is_array($owner_user_id&& $this->perm_user_id == $owner_user_id)
  255.             || is_array($owner_user_id&& in_array($this->perm_user_id$owner_user_id)
  256.         {
  257.             return $level;
  258.         // level 2 or higher
  259.         }
  260.         if ($level >= 2{
  261.             // check if the ressource is owned by a (sub)group
  262.             // that the user is part of
  263.             if (is_array($owner_group_id)) {
  264.                 if (count(array_intersect($owner_group_id$this->group_ids))) {
  265.                     return $level;
  266.                 }
  267.             elseif (in_array($owner_group_id$this->group_ids)) {
  268.                 return $level;
  269.             }
  270.         }
  271.         return false;
  272.     }
  273.  
  274.     /**
  275.      * Read all the areas in which the user is an area admin
  276.      *
  277.      * @param int perm user id
  278.      * @return array requested data or false on failure
  279.      *
  280.      * @access private
  281.      */
  282.     function readAreaAdminAreas($perm_user_id)
  283.     {
  284.         $result $this->_storage->readAreaAdminAreas($perm_user_id);
  285.         if ($result === false{
  286.             return false;
  287.         }
  288.  
  289.         $this->area_admin_areas $result;
  290.         return $this->area_admin_areas;
  291.     }
  292. }
  293. ?>

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