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

Source for file Simple.php

Documentation is available at Simple.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: Simple.php,v 1.41 2006/04/10 14:41:44 lsmith Exp $
  44.  * @link http://pear.php.net/LiveUser
  45.  */
  46.  
  47. /**
  48.  * Base class for permission handling. Provides the simplest
  49.  * set of permission handling features.
  50.  *
  51.  * This class provides a set of functions for implementing a user
  52.  * permission management system on live websites. All authorisation
  53.  * backends/containers must extend this base class.
  54.  *
  55.  * @category  authentication
  56.  * @package   LiveUser
  57.  * @author    Markus Wolff <wolff@21st.de>
  58.  * @author    Bjoern Kraus <krausbn@php.net>
  59.  * @copyright 2002-2006 Markus Wolff
  60.  * @license   http://www.gnu.org/licenses/lgpl.txt
  61.  * @version   Release: @package_version@
  62.  * @link http://pear.php.net/LiveUser
  63.  */
  64. {
  65.     /**
  66.      * Unique user ID, used to identify users from the auth container.
  67.      *
  68.      * @var string 
  69.      * @access public
  70.      */
  71.     var $perm_user_id = '';
  72.  
  73.     /**
  74.      * One-dimensional array containing current user's rights (direct and (sub)group).
  75.      * This already includes grouprights and possible overrides by
  76.      * individual right settings.
  77.      *
  78.      * Format: "RightId" => "Level"
  79.      *
  80.      * @var mixed 
  81.      * @access public
  82.      */
  83.     var $right_ids = false;
  84.  
  85.     /**
  86.      * One-dimensional array containing only the individual
  87.      * rights directly assigned to the user.
  88.      *
  89.      * Format: "RightId" => "Level"
  90.      *
  91.      * @var array 
  92.      * @access public
  93.      */
  94.     var $user_right_ids = array();
  95.  
  96.     /**
  97.      * Defines the user type. Depending on the value the user can gain certain
  98.      * rights automatically
  99.      *
  100.      * @var int 
  101.      * @access public
  102.      */
  103.     var $perm_type = LIVEUSER_ANONYMOUS_TYPE_ID;
  104.  
  105.     /**
  106.      * Error stack
  107.      *
  108.      * @var PEAR_ErrorStack 
  109.      * @access public
  110.      */
  111.     var $stack = null;
  112.  
  113.     /**
  114.      * Storage Container
  115.      *
  116.      * @var object 
  117.      * @access private
  118.      */
  119.     var $_storage = null;
  120.  
  121.     /**
  122.      * Class constructor. Feel free to override in backend subclasses.
  123.      */
  124.     function LiveUser_Perm_Simple()
  125.     {
  126.         $this->stack = &PEAR_ErrorStack::singleton('LiveUser');
  127.     }
  128.  
  129.     /**
  130.      * Load and initialize the storage container.
  131.      *
  132.      * @param array Array with the configuration
  133.      * @return bool true on success or false on failure
  134.      *
  135.      * @access public
  136.      */
  137.     function init(&$conf)
  138.     {
  139.         if (!array_key_exists('storage'$conf)) {
  140.             $this->stack->push(LIVEUSER_ERROR'exception',
  141.                 array('msg' => 'Missing storage configuration array'));
  142.             return false;
  143.         }
  144.  
  145.         if (is_array($conf)) {
  146.             $keys array_keys($conf);
  147.             foreach ($keys as $key{
  148.                 if (isset($this->$key)) {
  149.                     $this->$key =$conf[$key];
  150.                 }
  151.             }
  152.         }
  153.  
  154.         $this->_storage =LiveUser::storageFactory($conf['storage']);
  155.         if ($this->_storage === false{
  156.             end($conf['storage']);
  157.             $key key($conf['storage']);
  158.             $this->stack->push(LIVEUSER_ERROR'exception',
  159.                 array('msg' => 'Could not instanciate perm storage container: '.$key));
  160.             return false;
  161.         }
  162.  
  163.         return true;
  164.     }
  165.  
  166.     /**
  167.      * Tries to find the user with the given user ID in the permissions
  168.      * container. Will read all permission data and return true on success.
  169.      *
  170.      * @param  string user identifier
  171.      * @param  string name of the auth container
  172.      * @return bool true on success or false on failure
  173.      *
  174.      * @access public
  175.      */
  176.     function mapUser($auth_user_id = null$containerName = null)
  177.     {
  178.         $result $this->_storage->mapUser($auth_user_id$containerName);
  179.         if ($result === false{
  180.             return false;
  181.         }
  182.  
  183.         if (is_null($result)) {
  184.             return false;
  185.         }
  186.  
  187.         $this->perm_user_id = $result['perm_user_id'];
  188.         $this->perm_type    = $result['perm_type'];
  189.  
  190.         $this->readRights();
  191.  
  192.         return true;
  193.     }
  194.  
  195.     /**
  196.      * Reads all rights of current user into a
  197.      * two-dimensional associative array, having the
  198.      * area names as the key of the 1st dimension.
  199.      *
  200.      * @return array requested data or false on failure
  201.      *
  202.      * @access private
  203.      */
  204.     function readRights()
  205.     {
  206.         $this->right_ids = array();
  207.         $result $this->readUserRights($this->perm_user_id);
  208.         if ($result === false{
  209.             return false;
  210.         }
  211.         $this->right_ids = $result;
  212.         return $this->right_ids;
  213.     }
  214.  
  215.     /**
  216.      * Read all the user rights from the storage and puts them in a class
  217.      * member for later retrieval.
  218.      *
  219.      * @param int perm user id
  220.      * @return array requested data or false on failure
  221.      *
  222.      * @access private
  223.      */
  224.     function readUserRights($perm_user_id)
  225.     {
  226.         $this->user_right_ids = array();
  227.         $result $this->_storage->readUserRights($perm_user_id);
  228.         if ($result === false{
  229.             return false;
  230.         }
  231.         $this->user_right_ids = $result;
  232.         return $this->user_right_ids;
  233.     }
  234.  
  235.     /**
  236.      * Checks if the current user has a certain right.
  237.      *
  238.      * If the user is has an "area admin" type he will automatically be
  239.      * awarded the right.
  240.      *
  241.      * @param int Id of the right to check for.
  242.      * @return int level at which the user has the given right or
  243.      *                  false if the user does not have the right.
  244.      *
  245.      * @access public
  246.      */
  247.     function checkRight($right_id)
  248.     {
  249.         // check if the user is above areaadmin
  250.         if (!$right_id || $this->perm_type > LIVEUSER_AREAADMIN_TYPE_ID{
  251.             return LIVEUSER_MAX_LEVEL;
  252.         // If he does, look for the right in question.
  253.         elseif (is_array($this->right_ids&& array_key_exists($right_id$this->right_ids)) {
  254.             // We know the user has the right so the right level will be returned.
  255.             return $this->right_ids[$right_id];
  256.         }
  257.         return false;
  258.     }
  259.  
  260.     /**
  261.      * Function returns the inquired value if it exists in the class.
  262.      *
  263.      * @param  string  name of the property to be returned.
  264.      * @return mixed   null, a scalar or an array.
  265.      *
  266.      * @access public
  267.      */
  268.     function getProperty($what)
  269.     {
  270.         $that = null;
  271.         if (isset($this->$what)) {
  272.             $that $this->$what;
  273.         }
  274.         return $that;
  275.     }
  276.  
  277.     /**
  278.      * Stores all properties in an array.
  279.      *
  280.      * @param string name of the session in use.
  281.      * @return  array containing the property values
  282.      *
  283.      * @access public
  284.      */
  285.     function freeze($sessionName)
  286.     {
  287.         $propertyValues = array(
  288.             'perm_user_id' => $this->perm_user_id,
  289.             'right_ids' => $this->right_ids,
  290.             'user_right_ids' => $this->user_right_ids,
  291.             'group_right_ids' => $this->group_right_ids,
  292.             'perm_type' => $this->perm_type,
  293.             'group_ids' => $this->group_ids,
  294.         );
  295.         return $this->_storage->freeze($sessionName$propertyValues);
  296.     }
  297.  
  298.     /**
  299.      * Reinitializes properties from the storage container.
  300.      *
  301.      * @param string name of the key to use inside the session
  302.      * @param bool always returns true
  303.      *
  304.      * @access public
  305.      */
  306.     function unfreeze($sessionName)
  307.     {
  308.         $propertyValues $this->_storage->unfreeze($sessionName);
  309.         if ($propertyValues{
  310.             foreach ($propertyValues as $key => $value{
  311.                 $this->{$key$value;
  312.             }
  313.         }
  314.         return true;
  315.     }
  316.  
  317.     /**
  318.      * Properly disconnect from resources.
  319.      *
  320.      * @return bool true on success and false on failure
  321.      *
  322.      * @access public
  323.      */
  324.     function disconnect()
  325.     {
  326.         $this->_storage->disconnect();
  327.     }
  328. }
  329. ?>

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