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.  * Error related constants definition
  23.  *
  24.  * @var integer
  25.  */
  26. define('LIVEUSER_ADMIN_ERROR',                  -1);
  27. define('LIVEUSER_ADMIN_ERROR_FILTER',           -2);
  28. define('LIVEUSER_ADMIN_ERROR_DATA',             -3);
  29. define('LIVEUSER_ADMIN_ERROR_QUERY_BUILDER',    -4);
  30. define('LIVEUSER_ADMIN_ERROR_ALREADY_ASSIGNED'-5);
  31. define('LIVEUSER_ADMIN_ERROR_NOT_SUPPORTED',    -6);
  32. /**#@-*/
  33.  
  34. /**
  35.  * Attempt at a unified admin class
  36.  *
  37.  * Simple usage:
  38.  *
  39.  * <code>
  40.  * $admin = new LiveUser_Admin::factory($conf);
  41.  * $found = $admin->getUser(3);
  42.  *
  43.  * if ($found) {
  44.  *  var_dump($admin->perm->getRights());
  45.  * }
  46.  * </code>
  47.  *
  48.  * @see     LiveUser::factory()
  49.  * @author  Lukas Smith
  50.  * @author  Arnaud Limbourg
  51.  * @author
  52.  * @version $Id: Admin.php,v 1.19 2004/12/18 22:05:27 lsmith Exp $
  53.  * @package LiveUser
  54.  */
  55. {
  56.  
  57.      /**
  58.       * Name of the current selected auth container
  59.       *
  60.       * @access public
  61.       * @var    string 
  62.       */
  63.      var $authContainerName;
  64.  
  65.     /**
  66.      * Array containing the auth objects.
  67.      *
  68.      * @access private
  69.      * @var    array 
  70.      */
  71.     var $_authContainers = array();
  72.  
  73.     /**
  74.      * Admin perm object
  75.      *
  76.      * @access public
  77.      * @var    object 
  78.      */
  79.     var $perm = null;
  80.  
  81.     /**
  82.      * Auth admin object
  83.      *
  84.      * @access public
  85.      * @var    object 
  86.      */
  87.     var $auth = null;
  88.  
  89.     /**
  90.      * Configuration array
  91.      *
  92.      * @access private
  93.      * @var    array 
  94.      */
  95.      var $_conf = array();
  96.  
  97.     /**
  98.      * Error codes to message mapping array
  99.      *
  100.      * @access private
  101.      * @var    array 
  102.      */
  103.     var $_errorMessages = array(
  104.         LIVEUSER_ADMIN_ERROR                  => 'An error occurred %msg%',
  105.         LIVEUSER_ADMIN_ERROR_FILTER           => 'There\'s something obscure with the filter array, key %key%',
  106.         LIVEUSER_ADMIN_ERROR_DATA             => 'There\'s something obscure with the data array, key %key%',
  107.         LIVEUSER_ADMIN_ERROR_QUERY_BUILDER    => 'Couldn\'t create the query, reason: %reason%',
  108.         LIVEUSER_ADMIN_ERROR_ALREADY_ASSIGNED => 'That given %field1% has already been assigned to %field2%',
  109.         LIVEUSER_ADMIN_ERROR_NOT_SUPPORTED    => 'This method is not supported'
  110.     );
  111.  
  112.     /**
  113.      * PEAR::Log object
  114.      * used for error logging by ErrorStack
  115.      *
  116.      * @access private
  117.      * @var    Log 
  118.      */
  119.     var $_log = null;
  120.  
  121.     function LiveUser_Admin()
  122.     {
  123.         $this->_stack &PEAR_ErrorStack::singleton('LiveUser_Admin');
  124.  
  125.         if ($GLOBALS['_LIVEUSER_DEBUG']{
  126.             if (!is_object($this->_log)) {
  127.                 $this->loadPEARLog();
  128.             }
  129.             $winlog &Log::factory('win''LiveUser_Admin');
  130.             $this->_log->addChild($winlog);
  131.         }
  132.  
  133.         $this->_stack->setErrorMessageTemplate($this->_errorMessages);
  134.     }
  135.  
  136.     /**
  137.      * This method lazy loads PEAR::Log
  138.      *
  139.      * @access protected
  140.      * @return void 
  141.      */
  142.     function loadPEARLog()
  143.     {
  144.         require_once 'Log.php';
  145.         $this->_log &Log::factory('composite');
  146.         $this->_stack->setLogger($this->_log);
  147.     }
  148.  
  149.     /**
  150.      * Add error logger for use by Errorstack.
  151.      *
  152.      * Be aware that if you need add a log
  153.      * at the beginning of your code if you
  154.      * want it to be effective. A log will only
  155.      * be taken into account after it's added.
  156.      *
  157.      * Sample usage:
  158.      * <code>
  159.      * $lu_object = &LiveUser_Admin::singleton($conf);
  160.      * $logger = &Log::factory('mail', 'bug@example.com',
  161.      *      'myapp_debug_mail_log', array('from' => 'application_bug@example.com'));
  162.      * $lu_object->addErrorLog($logger);
  163.      * </code>
  164.      *
  165.      * @access public
  166.      * @param  Log     logger instance
  167.      * @return boolean true on success or false on failure
  168.      */
  169.     function addErrorLog(&$log)
  170.     {
  171.         if (!is_object($this->_log)) {
  172.             $this->loadPEARLog();
  173.         }
  174.         return $this->_log->addChild($log);
  175.     }
  176.  
  177.     /**
  178.      *
  179.      * @access public
  180.      * @return object 
  181.      */
  182.     function &factory($conf)
  183.     {
  184.         $obj &new LiveUser_Admin;
  185.  
  186.         if (is_array($conf)) {
  187.             $obj->_conf = $conf;
  188.         }
  189.  
  190.         if (isset($obj->_options['autoInit']&& $obj->_options['autoInit']{
  191.             $this->setAdminContainers();
  192.         }
  193.  
  194.         return $obj;
  195.     }
  196.  
  197.     /**
  198.      *
  199.      * @access public
  200.      * @return object 
  201.      */
  202.     function &singleton($conf)
  203.     {
  204.         static $instance;
  205.  
  206.         if (!isset($instance)) {
  207.             $obj &LiveUser_Admin::factory($conf);
  208.             $instance =$obj;
  209.         }
  210.  
  211.         return $instance;
  212.     }
  213.  
  214.     /**
  215.      * Merges the current configuration array with configuration array pases
  216.      * along with the method call.
  217.      *
  218.      * @param  array   configuration array
  219.      * @return boolean true upon success, false otherwise
  220.      */
  221.     function setConfArray($conf)
  222.     {
  223.         if (!is_array($conf)) {
  224.             return false;
  225.         }
  226.  
  227.         $this->_conf = LiveUser::arrayMergeClobber($this->_conf$conf);
  228.         return true;
  229.     }
  230.  
  231.     /**
  232.      * Sets the current auth container to the one with the given auth container name
  233.      *
  234.      * Upon success it will return true. You can then
  235.      * access the auth backend container by using the
  236.      * auth property of this class.
  237.      *
  238.      * e.g.: $admin->auth->addUser();
  239.      *
  240.      * @access public
  241.      * @param  string   auth container name
  242.      * @return boolean true upon success, false otherwise
  243.      */
  244.     function setAdminAuthContainer($authName)
  245.     {
  246.         if (!isset($this->_authContainers[$authName])
  247.             || !is_object($this->_authContainers[$authName])
  248.         {
  249.             if (!isset($this->_conf['authContainers'][$authName])) {
  250.                 return false;
  251.             }
  252.             $auth &LiveUser::authFactory(
  253.                 $this->_conf['authContainers'][$authName],
  254.                 $authName,
  255.                 'LiveUser_Admin_'
  256.             );
  257.             if (!is_object($auth)) {
  258.                 $this->_stack->push(LIVEUSER_ADMIN_ERROR'exception',
  259.                     array('msg' => 'Could not create auth container instance'));
  260.                 return false;
  261.             }
  262.             $this->_authContainers[$authName&$auth;
  263.         }
  264.         $this->authContainerName = $authName;
  265.         $this->auth = &$this->_authContainers[$authName];
  266.         return true;
  267.     }
  268.  
  269.     /**
  270.      * Sets the perm container
  271.      *
  272.      * Upon success it will return true. You can then
  273.      * access the perm backend container by using the
  274.      * perm properties of this class.
  275.      *
  276.      * e.g.: $admin->perm->addUser();
  277.      *
  278.      * @access public
  279.      * @return boolean true upon success, false otherwise
  280.      */
  281.     function setAdminPermContainer()
  282.     {
  283.         if (!is_array($this->_conf)) {
  284.             return false;
  285.         }
  286.  
  287.         $this->perm = &LiveUser::permFactory(
  288.             $this->_conf['permContainer'],
  289.             'LiveUser_Admin_'
  290.         );
  291.         return true;
  292.     }
  293.  
  294.     /**
  295.      * Tries to find a user in any of the auth container.
  296.      *
  297.      * Upon success it will return true. You can then
  298.      * access the backend container by using the auth
  299.      * and perm properties of this class.
  300.      *
  301.      * e.g.: $admin->perm->updateAuthUserId();
  302.      *
  303.      * @access public
  304.      * @param  mixed   user auth id
  305.      * @param  string   auth container name
  306.      * @return boolean true upon success, false otherwise
  307.      */
  308.     function setAdminContainers($authId = null$authName = null)
  309.     {
  310.         if (!is_array($this->_conf)) {
  311.             return false;
  312.         }
  313.  
  314.         if (is_null($authName)) {
  315.             if (is_null($authId)) {
  316.                 reset($this->_conf['authContainers']);
  317.                 $authName key($this->_conf['authContainers']);
  318.             else {
  319.                 foreach ($this->_conf['authContainers'as $key => $value{
  320.                     if (!isset($this->_authContainers[$key]||
  321.                         !is_object($this->_authContainers[$key])
  322.                     {
  323.                         $this->_authContainers[$key&LiveUser::authFactory(
  324.                             $value,
  325.                             $key,
  326.                             'LiveUser_Admin_'
  327.                         );
  328.                     }
  329.  
  330.                     if (!is_null($authId)) {
  331.                         $match $this->_authContainers[$key]->getUsers(
  332.                             array('auth_user_id' => $authId)
  333.                         );
  334.                         if (is_array($match&& sizeof($match> 0{
  335.                             $authName $key;
  336.                             break;
  337.                         }
  338.                     }
  339.                 }
  340.             }
  341.         }
  342.  
  343.         if (isset($authName)) {
  344.             if (!isset($this->perm|| !is_object($this->perm)) {
  345.                 $perm_res $this->setAdminPermContainer();
  346.             }
  347.             $auth_res $this->setAdminAuthContainer($authName);
  348.             return (!$perm_res || !$auth_res? false : true;
  349.         }
  350.  
  351.         return false;
  352.     }
  353.  
  354.     /**
  355.      * Tries to add a user to both containers.
  356.      *
  357.      * If the optional $id parameter is passed it will be used
  358.      * for both containers.
  359.      *
  360.      * In any case the auth and perm id will be equal when using this method.
  361.      *
  362.      * If this behaviour doesn't suit your needs please consider
  363.      * using directly the concerned method. This method is just
  364.      * implement to simplify things a bit and should satisfy most
  365.      * user needs.
  366.      *
  367.      *  Note type is optional for DB, thus it's needed for MDB and MDB2,
  368.      *  we recommend that you use type even though you use DB, so if you change to MDB[2],
  369.      *  it will be no problem for you.
  370.      *  usage example for addUser:
  371.      * <code>
  372.      *       $optional = array('is_active' => true);
  373.      *       $user_id = $admin->addUser('johndoe', 'dummypass', $optional);
  374.      *  </code>
  375.      *
  376.      * Untested: it most likely doesn't work.
  377.      *
  378.      * @access public
  379.      * @param  string  user handle (username)
  380.      * @param  string  user password
  381.      * @param  array   values for the optional fields
  382.      * @param  array   values for the custom fields
  383.      * @param  int          ID
  384.      * @param  integer permission user type
  385.      * @return mixed   userid or false
  386.      */
  387.     function addUser($handle$password$optionalFields = array()$customFields = array(),
  388.                              $id = null$type = LIVEUSER_USER_TYPE_ID)
  389.     {
  390.         if (is_object($this->auth&& is_object($this->perm)) {
  391.             $authId $this->auth->addUser($handle$password$optionalFields,
  392.                                                             $customFields$id);
  393.  
  394.             if (!$authId{
  395.                 return false;
  396.             }
  397.  
  398.             $data = array(
  399.                 'auth_user_id' => $authId,
  400.                 'auth_container_name' => $this->authContainerName,
  401.                 'perm_type' => $type
  402.             );
  403.             return $this->perm->addUser($data);
  404.         }
  405.  
  406.         $this->_stack->push(LIVEUSER_ADMIN_ERROR'exception'array('msg' => 'Perm or Auth container couldn\t be started.'));
  407.         return false;
  408.     }
  409.  
  410.     /**
  411.      * Tried to changes user data for both containers.
  412.      *
  413.      *  Note type is optional for DB, thus it's needed for MDB and MDB2,
  414.      *  we recommend that you use type even though you use DB, so if you change to MDB[2],
  415.      *  it will be no problem for you.
  416.      *  usage example for updateUser:
  417.      * <code>
  418.      *       $optional = array('is_active' => false);
  419.      *       $admin->updateUser($user_id, 'johndoe', 'dummypass');
  420.      * </code>
  421.      *
  422.      * Untested: it most likely doesn't work.
  423.      *
  424.      * @access public
  425.      * @param integer permission user id
  426.      * @param  string  user handle (username)
  427.      * @param  string  user password
  428.      * @param  array   values for the optional fields
  429.      * @param  array   values for the custom fields
  430.      * @param  integer permission user type
  431.      * @return mixed   error object or true
  432.      */
  433.     function updateUser($permId$handle$password$optionalFields = array(),
  434.                                   $customFields = array()$type = LIVEUSER_USER_TYPE_ID)
  435.     {
  436.         if (is_object($this->auth&& is_object($this->perm)) {
  437.             $authData $this->perm->getUsers(array(
  438.                 'filters' => array('perm_user_id' => $permId),
  439.                 'fields' => array('auth_user_id'))
  440.              );
  441.  
  442.             if (!$authData{
  443.                 return $authData;
  444.             }
  445.  
  446.             $authData reset($authData);
  447.             $auth $this->auth->updateUser($authData['auth_user_id']$handle$password,
  448.                                                              $optionalFields$customFields);
  449.  
  450.             if ($auth === false{
  451.                 return false;
  452.             }
  453.  
  454.             $data = array(
  455.                 'perm_type' => $type
  456.             );
  457.             $filters = array('perm_user_id' => $permId);
  458.             return $this->perm->updateUser($data$filters);
  459.         }
  460.  
  461.         $this->_stack->push(LIVEUSER_ADMIN_ERROR'exception'array('msg' => 'Perm or Auth container couldn\t be started.'));
  462.         return false;
  463.     }
  464.  
  465.     /**
  466.     * Removes user from both containers
  467.     *
  468.     * Untested: it most likely doesn't work.
  469.     *
  470.     * @access public
  471.     * @param  mixed Auth ID
  472.     * @return  mixed error object or true
  473.     */
  474.     function removeUser($permId)
  475.     {
  476.         if (is_object($this->auth&& is_object($this->perm)) {
  477.             $authData $this->perm->getUsers(array(
  478.                 'filters' => array('perm_user_id' => $permId),
  479.                 'fields' => array('auth_user_id'))
  480.              );
  481.  
  482.             if (!$authData{
  483.                 return $authData;
  484.             }
  485.  
  486.             $authData reset($authData);
  487.             $result $this->auth->removeUser($authData['auth_user_id']);
  488.  
  489.             if ($result === false{
  490.                 return $result;
  491.             }
  492.  
  493.             $filters = array('perm_user_id' => $permId);
  494.             return $this->perm->removeUser($filters);
  495.         }
  496.  
  497.         $this->_stack->push(LIVEUSER_ADMIN_ERROR'exception'array('msg' => 'Perm or Auth container couldn\t be started.'));
  498.         return false;
  499.     }
  500.  
  501.     /**
  502.     * Searches users with given filters and returns
  503.     * all users found with their handle, passwd, auth_user_id
  504.     * lastlogin, is_active and the customFields if they are specified
  505.     *
  506.     * Untested: it most likely doesn't work.
  507.     *
  508.     * @access public
  509.     * @param   array   filters to apply to fetched data
  510.     * @param   string  if not null 'ORDER BY $order' will be appended to the query
  511.     * @param   boolean will return an associative array with the auth_user_id
  512.     *                   as the key by using DB::getAssoc() instead of DB::getAll()
  513.     * @return mixed error object or array
  514.     */
  515.     function searchUsers($filters = array()$order = null$rekey = false)
  516.     {
  517.         if (is_object($this->auth&& is_object($this->perm)) {
  518.             $search $this->auth->getUsers($filters$order$rekey);
  519.  
  520.             if ($search === false{
  521.                 return $search;
  522.             }
  523.  
  524.             foreach ($search as $key => $user{
  525.                 $permFilter['auth_user_id'$user['auth_user_id'];
  526.                 $permData $this->perm->getUsers(array('filters' => $permFilter));
  527.                 if (!$permData{
  528.                     return false;
  529.                 }
  530.                 $search[$key= LiveUser::arrayMergeClobber(reset($permData)$user);
  531.             }
  532.             return $search;
  533.         }
  534.  
  535.         $this->_stack->push(LIVEUSER_ADMIN_ERROR'exception'array('msg' => 'Perm or Auth container couldn\t be started.'));
  536.         return false;
  537.     }
  538.  
  539.     /**
  540.     * Finds and gets userinfo by his userID, customFields can
  541.     *  also be gotten
  542.     *
  543.     * Untested: it most likely doesn't work.
  544.     *
  545.     * @access public
  546.     * @param  mixed  Perm User ID
  547.     * @return mixed Array with userinfo if found else error object
  548.     */
  549.     function getUser($permId$permFilter = array()$authFilter = array(),
  550.         $permOptions = array())
  551.     {
  552.         if (is_object($this->auth&& is_object($this->perm)) {
  553.             $permFilter['perm_user_id'$permId;
  554.             $permData $this->perm->getUsers(array('filters' => $permFilter));
  555.             if (!$permData{
  556.                 return false;
  557.             }
  558.  
  559.             $permData array_shift($permData);
  560.  
  561.             $authFilter = array(
  562.                 array(
  563.                     'name' => $this->auth->authTableCols['required']['auth_user_id']['name'],
  564.                     'op' => '=',
  565.                     'value' => $permData['auth_user_id'],
  566.                     'cond' => 'AND',
  567.                     'type' => $this->auth->authTableCols['required']['auth_user_id']['type'],
  568.                 )
  569.             );
  570.  
  571.             $authData $this->auth->getUsers($authFilter);
  572.             if ($authData === false{
  573.                 return $authData;
  574.             }
  575.  
  576.             $authData array_shift($authData);
  577.  
  578.             return LiveUser::arrayMergeClobber($permData$authData);
  579.         }
  580.  
  581.         $this->_stack->push(LIVEUSER_ADMIN_ERROR'exception'array('msg' => 'Perm or Auth container couldn\t be started.'));
  582.         return false;
  583.     }
  584.  
  585.     /**
  586.      * Wrapper method to get the Error Stack
  587.      *
  588.      * @access public
  589.      * @return array  an array of the errors
  590.      */
  591.     function getErrors()
  592.     {
  593.         return $this->_stack->getErrors();
  594.     }
  595. }

Documentation generated on Mon, 11 Mar 2019 14:00:08 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.