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

Source for file Admin.php

Documentation is available at Admin.php

  1. <?php
  2. // LiveUser: A framework for authentication and authorization in PHP applications
  3. // Copyright (C) 2002-2003 Markus Wolff
  4. //
  5. // This library is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Lesser General Public
  7. // License as published by the Free Software Foundation; either
  8. // version 2.1 of the License, or (at your option) any later version.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. // Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public
  16. // License along with this library; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19. require_once 'LiveUser.php';
  20.  
  21. /**
  22.  * Attempt at a unified admin class
  23.  *
  24.  * Simple usage:
  25.  *
  26.  * <code>
  27.  * $admin = new LiveUser_Admin($conf, 'FR');
  28.  * $found = $admin->getUser(3);
  29.  *
  30.  * if ($found) {
  31.  *  var_dump($admin->perm->getRights());
  32.  * }
  33.  * </code>
  34.  *
  35.  * @see     LiveUser::factory()
  36.  * @author  Lukas Smith
  37.  * @author  Arnaud Limbourg
  38.  * @author
  39.  * @version $Id: Admin.php,v 1.53 2004/10/23 09:17:13 arnaud Exp $
  40.  * @package LiveUser
  41.  */
  42. {
  43.  
  44.      /**
  45.       * Name of the current selected auth container
  46.       *
  47.       * @access public
  48.       * @var    string 
  49.       */
  50.      var $authContainerName;
  51.  
  52.     /**
  53.      * Array containing the auth objects.
  54.      *
  55.      * @access private
  56.      * @var    array 
  57.      */
  58.     var $_authContainers = array();
  59.  
  60.     /**
  61.      * Admin perm object
  62.      *
  63.      * @access public
  64.      * @var    object 
  65.      */
  66.     var $perm = null;
  67.  
  68.     /**
  69.      * Auth admin object
  70.      *
  71.      * @access public
  72.      * @var    object 
  73.      */
  74.     var $auth = null;
  75.  
  76.     /**
  77.      * Configuration array
  78.      *
  79.      * @access private
  80.      * @var    array 
  81.      */
  82.      var $_conf = array();
  83.  
  84.      /**
  85.       * Language to be used
  86.       *
  87.       * @access public
  88.       * @var    string 
  89.       */
  90.      var $lang = '';
  91.  
  92.     /**
  93.      * Constructor
  94.      *
  95.      * @access protected
  96.      * @param  array  liveuser conf array
  97.      * @param  string two letters language code
  98.      * @return void 
  99.      */
  100.     function LiveUser_Admin($conf$lang)
  101.     {
  102.         if (is_array($conf)) {
  103.             $this->_conf $conf;
  104.         }
  105.         $this->lang = $lang;
  106.  
  107.         if (isset($this->_conf['autoInit']&& $this->_conf['autoInit'=== true{
  108.             $this->setAdminContainers();
  109.         }
  110.     }
  111.  
  112.     /**
  113.      * Merges the current configuration array with configuration array pases
  114.      * along with the method call.
  115.      *
  116.      * @param  array   configuration array
  117.      * @return boolean true upon success, false otherwise
  118.      */
  119.     function setConfArray($conf)
  120.     {
  121.         if (!is_array($conf)) {
  122.             return false;
  123.         }
  124.  
  125.         $this->_conf LiveUser::arrayMergeClobber($this->_conf$conf);
  126.         return true;
  127.     }
  128.  
  129.     /**
  130.     * Makes your instance global.
  131.     *
  132.     * <b>You MUST call this method with the $var = &LiveUser_Admin::singleton() syntax.
  133.     * Without the ampersand (&) in front of the method name, you will not get
  134.     * a reference, you will get a copy.</b>
  135.     *
  136.     * @access public
  137.     * @param  array  liveuser conf array
  138.     * @param  string two letters language code
  139.     * @return object      Returns an object of either LiveUser or PEAR_Error type
  140.     * @see    LiveUser_Admin::LiveUser_Admin
  141.     */
  142.     function &singleton($conf$lang)
  143.     {
  144.         static $instances;
  145.         if (!isset($instances)) $instances = array();
  146.  
  147.         $signature serialize(array($conf$lang));
  148.         if (!isset($instances[$signature])) {
  149.             $obj &LiveUser_Admin::LiveUser_Admin($conf$lang);
  150.             if(PEAR::isError($obj)) {
  151.                 return $obj;
  152.             }
  153.             $instances[$signature=$obj;
  154.         }
  155.  
  156.         return $instances[$signature];
  157.     }
  158.  
  159.     /**
  160.      * creates an instance of an auth object
  161.      *
  162.      * @access private
  163.      * @param  mixed    Array containing the configuration.
  164.      * @param  string   Name of the auth container.
  165.      * @return object   Returns an object of an auth container
  166.      */
  167.     function &_authFactory($conf$name = null)
  168.     {
  169.         if (!is_array($conf)) {
  170.             return false;
  171.         }
  172.         $classname 'LiveUser_Admin_Auth_Container_' $conf['type'];
  173.         if (!class_exists($classname)) {
  174.             $filename  'LiveUser/Admin/Auth/Container/' $conf['type''.php';
  175.             if (!LiveUser::fileExists($filename)) {
  176.                 $this->_error LiveUser_Admin::raiseError(LIVEUSER_ERROR_NOT_SUPPORTEDnullnull,
  177.                     'Missing file: '.$filename);
  178.                 return $this->_error;
  179.             }
  180.             include_once($filename);
  181.             if (!class_exists($classname)) {
  182.                 $this->_error LiveUser_Admin::raiseError(LIVEUSER_ERROR_NOT_SUPPORTEDnullnull,
  183.                     'Class not defined in file as expected: '.$filename'LiveUser');
  184.                 return $this->_error;
  185.             }
  186.         }
  187.         $auth &new $classname($conf$name);
  188.         return $auth;
  189.     }
  190.  
  191.     /**
  192.      * creates an instance of an perm object
  193.      *
  194.      * @access private
  195.      * @param  mixed    Name of array containing the configuration.
  196.      * @return object   Returns an object of a perm container
  197.      */
  198.     function &_permFactory($conf)
  199.     {
  200.         if (!is_array($conf)) {
  201.             return false;
  202.         }
  203.         $classname 'LiveUser_Admin_Perm_Container_' $conf['type'];
  204.         if (!class_exists($classname)) {
  205.             $filename 'LiveUser/Admin/Perm/Container/' $conf['type''.php';
  206.             if (!LiveUser::fileExists($filename)) {
  207.                 $this->_error LiveUser_Admin::raiseError(LIVEUSER_ERROR_NOT_SUPPORTEDnullnull,
  208.                     'Missing file: '.$filename);
  209.                 return $this->_error;
  210.             }
  211.             include_once($filename);
  212.             if (!class_exists($classname)) {
  213.                 $this->_error LiveUser_Admin::raiseError(LIVEUSER_NOT_SUPPORTEDnullnull,
  214.                     'Class not defined in file as expected: '.$filename'LiveUser');
  215.                 return $this->_error;
  216.             }
  217.         }
  218.         $perm &new $classname($conf);
  219.         return $perm;
  220.     }
  221.  
  222.     /**
  223.      * Sets the current auth container to the one with the given auth container name
  224.      *
  225.      * Upon success it will return true. You can then
  226.      * access the auth backend container by using the
  227.      * auth property of this class.
  228.      *
  229.      * e.g.: $admin->auth->addUser();
  230.      *
  231.      * @access public
  232.      * @param  string   auth container name
  233.      * @return boolean true upon success, false otherwise
  234.      */
  235.     function setAdminAuthContainer($authName)
  236.     {
  237.         if (!isset($this->_authContainers[$authName])
  238.             || !is_object($this->_authContainers[$authName])
  239.         {
  240.             if (!isset($this->_conf['authContainers'][$authName])) {
  241.                 return false;
  242.             }
  243.             $this->_authContainers[$authName=
  244.                 &$this->_authFactory($this->_conf['authContainers'][$authName]$authName);
  245.             if (LiveUser::isError($this->_authContainers[$authName])) {
  246.                 return false;
  247.             }
  248.         }
  249.         $this->authContainerName = $authName;
  250.         $this->auth = &$this->_authContainers[$authName];
  251.         return true;
  252.     }
  253.  
  254.     /**
  255.      * Sets the perm container
  256.      *
  257.      * Upon success it will return true. You can then
  258.      * access the perm backend container by using the
  259.      * perm properties of this class.
  260.      *
  261.      * e.g.: $admin->perm->addUser();
  262.      *
  263.      * @access public
  264.      * @return boolean true upon success, false otherwise
  265.      */
  266.     function setAdminPermContainer()
  267.     {
  268.         if (!is_array($this->_conf)) {
  269.             return false;
  270.         }
  271.  
  272.         $this->perm = &$this->_permFactory($this->_conf['permContainer']);
  273.         $this->perm->setCurrentLanguage($this->lang);
  274.         return true;
  275.     }
  276.  
  277.     /**
  278.      * Tries to find a user in any of the auth container.
  279.      *
  280.      * Upon success it will return true. You can then
  281.      * access the backend container by using the auth
  282.      * and perm properties of this class.
  283.      *
  284.      * e.g.: $admin->perm->updateAuthUserId();
  285.      *
  286.      * @access public
  287.      * @param  mixed   user auth id
  288.      * @param  string   auth container name
  289.      * @return boolean true upon success, false otherwise
  290.      */
  291.     function setAdminContainers($authId = null$authName = null)
  292.     {
  293.         if (!is_array($this->_conf)) {
  294.             return false;
  295.         }
  296.  
  297.         if (is_null($authName)) {
  298.             if (is_null($authId)) {
  299.                 reset($this->_conf['authContainers']);
  300.                 $authName key($this->_conf['authContainers']);
  301.             else {
  302.                 foreach ($this->_conf['authContainers'as $k => $v{
  303.                     if (!isset($this->_authContainers[$k]||
  304.                         !is_object($this->_authContainers[$k])
  305.                     {
  306.                         $this->_authContainers[$k&$this->_authFactory($v$k);
  307.                     }
  308.  
  309.                     if (!is_null($authId)) {
  310.                         $match $this->_authContainers[$k]->getUsers(array('auth_user_id' => $authId));
  311.                         if (is_array($match&& sizeof($match> 0{
  312.                             $authName $k;
  313.                             break;
  314.                         }
  315.                     }
  316.                 }
  317.             }
  318.         }
  319.  
  320.         if (isset($authName)) {
  321.             if (!isset($this->perm|| !is_object($this->perm)) {
  322.                 $perm_res $this->setAdminPermContainer();
  323.             }
  324.             $auth_res $this->setAdminAuthContainer($authName);
  325.             return (!$perm_res || !$auth_res? false : true;
  326.         }
  327.  
  328.         return false;
  329.     }
  330.  
  331.     /**
  332.      * Tries to add a user to both containers.
  333.      *
  334.      * If the optional $id parameter is passed it will be used
  335.      * for both containers.
  336.      *
  337.      * In any case the auth and perm id will be equal when using this method.
  338.      *
  339.      * If this behaviour doesn't suit your needs please consider
  340.      * using directly the concerned method. This method is just
  341.      * implement to simplify things a bit and should satisfy most
  342.      * user needs.
  343.      *
  344.      *  Note type is optional for DB, thus it's needed for MDB and MDB2,
  345.      *  we recommend that you use type even though you use DB, so if you change to MDB[2],
  346.      *  it will be no problem for you.
  347.      *  usage example for addUser:
  348.      * <code>
  349.      *       $optional = array('is_active' => true);
  350.      *       $user_id = $admin->addUser('johndoe', 'dummypass', $optional);
  351.      *  </code>
  352.      *
  353.      * Untested: it most likely doesn't work.
  354.      *
  355.      * @access public
  356.      * @param  string  user handle (username)
  357.      * @param  string  user password
  358.      * @param  array   values for the optional fields
  359.      * @param  array   values for the custom fields
  360.      * @param  int          ID
  361.      * @param  integer permission user type
  362.      * @return mixed   userid or false
  363.      */
  364.     function addUser($handle$password$optionalFields = array()$customFields = array(),
  365.                              $id = null$type = LIVEUSER_USER_TYPE_ID)
  366.     {
  367.         if (is_object($this->auth&& is_object($this->perm)) {
  368.             $authId $this->auth->addUser($handle$password$optionalFields,
  369.                                                             $customFields$id);
  370.  
  371.             if (LiveUser::isError($authId)) {
  372.                 return $authId;
  373.             }
  374.  
  375.             return $this->perm->addUser($authId$this->authContainerName$type);
  376.         }
  377.         return LiveUser_Admin::raiseError(LIVEUSER_ERRORnullnull,
  378.                     'Perm or Auth container couldn\t be started.');
  379.     }
  380.  
  381.     /**
  382.      * Tried to changes user data for both containers.
  383.      *
  384.      *  Note type is optional for DB, thus it's needed for MDB and MDB2,
  385.      *  we recommend that you use type even though you use DB, so if you change to MDB[2],
  386.      *  it will be no problem for you.
  387.      *  usage example for updateUser:
  388.      * <code>
  389.      *       $optional = array('is_active' => false);
  390.      *       $admin->updateUser($user_id, 'johndoe', 'dummypass');
  391.      * </code>
  392.      *
  393.      * Untested: it most likely doesn't work.
  394.      *
  395.      * @access public
  396.      * @param integer permission user id
  397.      * @param  string  user handle (username)
  398.      * @param  string  user password
  399.      * @param  array   values for the optional fields
  400.      * @param  array   values for the custom fields
  401.      * @param  integer permission user type
  402.      * @return mixed   error object or true
  403.      */
  404.     function updateUser($permId$handle$password$optionalFields = array(),
  405.                                   $customFields = array()$type = LIVEUSER_USER_TYPE_ID)
  406.     {
  407.         if (is_object($this->auth&& is_object($this->perm)) {
  408.             $authData $this->perm->getAuthUserId($permId);
  409.  
  410.             $auth $this->auth->updateUser($authData['auth_user_id']$handle$password,
  411.                                                              $optionalFields$customFields);
  412.  
  413.             if (LiveUser::isError($auth)) {
  414.                 return $auth;
  415.             }
  416.  
  417.             return $this->perm->updateUser($permId$authData['auth_user_id']$this->authContainerName$type);
  418.         }
  419.         return LiveUser_Admin::raiseError(LIVEUSER_ERRORnullnull,
  420.                     'Perm or Auth container couldn\t be started.');
  421.     }
  422.  
  423.     /**
  424.     * Removes user from both containers
  425.     *
  426.     * Untested: it most likely doesn't work.
  427.     *
  428.     * @access public
  429.     * @param  mixed Auth ID
  430.     * @return  mixed error object or true
  431.     */
  432.     function removeUser($permId)
  433.     {
  434.         if (is_object($this->auth&& is_object($this->perm)) {
  435.             $authData $this->perm->getAuthUserId($permId);
  436.  
  437.             if (LiveUser::isError($authData)) {
  438.                 return $authData;
  439.             }
  440.  
  441.             $result $this->auth->removeUser($authData['auth_user_id']);
  442.  
  443.             if (LiveUser::isError($result)) {
  444.                 return $result;
  445.             }
  446.  
  447.             return $this->perm->removeUser($permId);
  448.         }
  449.         return LiveUser_Admin::raiseError(LIVEUSER_ERRORnullnull,
  450.                     'Perm or Auth container couldn\t be started.');
  451.     }
  452.  
  453.     /**
  454.     * Searches users with given filters and returns
  455.     * all users found with their handle, passwd, auth_user_id
  456.     * lastlogin, is_active and the customFields if they are specified
  457.     *
  458.     * Untested: it most likely doesn't work.
  459.     *
  460.     * @access public
  461.     * @param   array   filters to apply to fetched data
  462.     * @param   string  if not null 'ORDER BY $order' will be appended to the query
  463.     * @param   boolean will return an associative array with the auth_user_id
  464.     *                   as the key by using DB::getAssoc() instead of DB::getAll()
  465.     * @return mixed error object or array
  466.     */
  467.     function searchUsers($filters = array()$order = null$rekey = false)
  468.     {
  469.         if (is_object($this->auth&& is_object($this->perm)) {
  470.             $search $this->auth->getUsers($filters$order$rekey);
  471.  
  472.             if (LiveUser::isError($search)) {
  473.                 return $search;
  474.             }
  475.  
  476.             return $search;
  477.         }
  478.         return LiveUser_Admin::raiseError(LIVEUSER_ERRORnullnull,
  479.                     'Perm or Auth container couldn\t be started.');
  480.     }
  481.  
  482.     /**
  483.     * Finds and gets userinfo by his userID, customFields can
  484.     *  also be gotten
  485.     *
  486.     * Untested: it most likely doesn't work.
  487.     *
  488.     * @access public
  489.     * @param  mixed  Perm User ID
  490.     * @return mixed Array with userinfo if found else error object
  491.     */
  492.     function getUser($permId$permFilter = array()$authFilter = array(),
  493.         $permOptions = array())
  494.     {
  495.         if (is_object($this->auth&& is_object($this->perm)) {
  496.             $permFilter['perm_user_id'$permId;
  497.             $permData $this->perm->getUsers($permFilter$permOptions);
  498.             if (LiveUser::isError($permData)) {
  499.                 return $permData;
  500.             }
  501.  
  502.             $permData array_shift($permData);
  503.  
  504.             $authFilter = array(
  505.                 array(
  506.                     'name' => $this->auth->authTableCols['required']['auth_user_id']['name'],
  507.                     'op' => '=',
  508.                     'value' => $permData['auth_user_id'],
  509.                     'cond' => 'AND',
  510.                     'type' => $this->auth->authTableCols['required']['auth_user_id']['type'],
  511.                 )
  512.             );
  513.  
  514.             $authData $this->auth->getUsers($authFilter);
  515.             if (LiveUser::isError($authData)) {
  516.                 return $authData;
  517.             }
  518.  
  519.             $authData array_shift($authData);
  520.  
  521.             return LiveUser::arrayMergeClobber($permData$authData);
  522.         }
  523.         return LiveUser_Admin::raiseError(LIVEUSER_ERRORnullnull,
  524.                     'Perm or Auth container couldn\t be started.');
  525.     }
  526.  
  527.     /**
  528.      * This method is used to communicate an error and invoke error
  529.      * callbacks etc.  Basically a wrapper for PEAR::raiseError
  530.      * without the message string.
  531.      *
  532.      * @param mixed    integer error code, or a PEAR error object (all
  533.      *                  other parameters are ignored if this parameter is
  534.      *                  an object
  535.      *
  536.      * @param int      error mode, see PEAR_Error docs
  537.      *
  538.      * @param mixed    If error mode is PEAR_ERROR_TRIGGER, this is the
  539.      *                  error level (E_USER_NOTICE etc).  If error mode is
  540.      *                  PEAR_ERROR_CALLBACK, this is the callback function,
  541.      *                  either as a function name, or as an array of an
  542.      *                  object and method name.  For other error modes this
  543.      *                  parameter is ignored.
  544.      *
  545.      * @param string   Extra debug information.  Defaults to the last
  546.      *                  query and native error code.
  547.      *
  548.      * @return object  PEAR error object
  549.      *
  550.      * @see PEAR_Error
  551.      */
  552.     function &raiseError($code = null$mode = null$options = null,
  553.                          $userinfo = null)
  554.     {
  555.         // The error is yet a LiveUser error object
  556.         if (is_object($code)) {
  557.             return PEAR::raiseError($codenullnullnullnullnulltrue);
  558.         }
  559.  
  560.         if (empty($code)) {
  561.             $code LIVEUSER_ERROR;
  562.         }
  563.         $msg LiveUser::errorMessage($code);
  564.         return PEAR::raiseError("LiveUser Error: $msg"$code$mode$options$userinfo);
  565.     }
  566. }

Documentation generated on Mon, 11 Mar 2019 13:57:34 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.