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.45 2004/08/30 08:45:27 lsmith 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::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::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::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::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.         }
  246.         $this->authContainerName = $authName;
  247.         $this->auth = &$this->_authContainers[$authName];
  248.         return true;
  249.     }
  250.  
  251.     /**
  252.      * Sets the perm container
  253.      *
  254.      * Upon success it will return true. You can then
  255.      * access the perm backend container by using the
  256.      * perm properties of this class.
  257.      *
  258.      * e.g.: $admin->perm->addUser();
  259.      *
  260.      * @access public
  261.      * @return boolean true upon success, false otherwise
  262.      */
  263.     function setAdminPermContainer()
  264.     {
  265.         if (!is_array($this->_conf)) {
  266.             return false;
  267.         }
  268.  
  269.         $this->perm = &$this->_permFactory($this->_conf['permContainer']);
  270.         $this->perm->setCurrentLanguage($this->lang);
  271.         return true;
  272.     }
  273.  
  274.     /**
  275.      * Tries to find a user in any of the auth container.
  276.      *
  277.      * Upon success it will return true. You can then
  278.      * access the backend container by using the auth
  279.      * and perm properties of this class.
  280.      *
  281.      * e.g.: $admin->perm->updateAuthUserId();
  282.      *
  283.      * @access public
  284.      * @param  mixed   user auth id
  285.      * @param  string   auth container name
  286.      * @return boolean true upon success, false otherwise
  287.      */
  288.     function setAdminContainers($authId = null$authName = null)
  289.     {
  290.         if (!is_array($this->_conf)) {
  291.             return false;
  292.         }
  293.  
  294.         if (is_null($authName)) {
  295.             if (is_null($authId)) {
  296.                 reset($this->_conf['authContainers']);
  297.                 $authName key($this->_conf['authContainers']);
  298.             else {
  299.                 foreach ($this->_conf['authContainers'as $k => $v{
  300.                     if (!isset($this->_authContainers[$k]||
  301.                         !is_object($this->_authContainers[$k])
  302.                     {
  303.                         $this->_authContainers[$k&$this->_authFactory($v$k);
  304.                     }
  305.     
  306.                     if (!is_null($authId)) {
  307.                         $match $this->_authContainers[$k]->getUsers(array('auth_user_id' => $authId));
  308.                         if (is_array($match&& sizeof($match> 0{
  309.                             $authName $k;
  310.                             break;
  311.                         }
  312.                     }
  313.                 }
  314.             }
  315.         }
  316.  
  317.         if (isset($authName)) {
  318.             if (!isset($this->perm|| !is_object($this->perm)) {
  319.                 $this->setAdminPermContainer();
  320.             }
  321.             $this->setAdminAuthContainer($authName);
  322.             return true;
  323.         }
  324.  
  325.         return false;
  326.     }
  327.  
  328.     /**
  329.      * Tries to add a user to both containers.
  330.      *
  331.      * If the optional $id parameter is passed it will be used
  332.      * for both containers.
  333.      *
  334.      * In any case the auth and perm id will be equal when using this method.
  335.      *
  336.      * If this behaviour doesn't suit your needs please consider
  337.      * using directly the concerned method. This method is just
  338.      * implement to simplify things a bit and should satisfy most
  339.      * user needs.
  340.      *
  341.      *  Note type is optional for DB, thus it's needed for MDB and MDB2,
  342.      *  we recommend that you use type even though you use DB, so if you change to MDB[2],
  343.      *  it will be no problem for you.
  344.      *  usage example for addUser:
  345.      * <code>
  346.      *       $user_id = $admin->addUser('johndoe', 'dummypass', true, null, null, null);
  347.      *  </code>
  348.      *
  349.      * Untested: it most likely doesn't work.
  350.      *
  351.      * @access public
  352.      * @param  string  user handle (username)
  353.      * @param  string  user password
  354.      * @param  integer permission user type
  355.      * @param  boolean is account active ?
  356.      * @param  int          ID
  357.      * @param  integer ID of the owning user.
  358.      * @param  integer ID of the owning group.
  359.      * @param  array   values for the custom fields
  360.      * @return mixed   userid or false
  361.      */
  362.     function addUser($handle$password$type = null$active = true$id = null$owner_user_id = null,
  363.                     $owner_group_id = null$customFields = array())
  364.     {
  365.         if (is_object($this->auth&& is_object($this->perm)) {
  366.             $authId $this->auth->addUser($handle$password$active$owner_user_id,
  367.                                             $owner_group_id$id$customFields);
  368.  
  369.             if (LiveUser::isError($authId)) {
  370.                 return $authId;
  371.             }
  372.  
  373.             return $this->perm->addUser($authId$this->authContainerName$type);
  374.         }
  375.         return LiveUser::raiseError(LIVEUSER_ERRORnullnull,
  376.                     'Perm or Auth container couldn\t be started.');
  377.     }
  378.  
  379.     /**
  380.      * Tried to changes user data for both containers.
  381.      *
  382.      *  Note type is optional for DB, thus it's needed for MDB and MDB2,
  383.      *  we recommend that you use type even though you use DB, so if you change to MDB[2],
  384.      *  it will be no problem for you.
  385.      *  usage example for updateUser:
  386.      * <code>
  387.      *       $admin->updateUser($user_id, 'johndoe', 'dummypass', true, null, null);
  388.      * </code>
  389.      *
  390.      * Untested: it most likely doesn't work.
  391.      *
  392.      * @access public
  393.      * @param  string  user handle (username)
  394.      * @param  string  user password
  395.      * @param  integer permission user type
  396.      * @param  boolean is account active ?
  397.      * @param  int          ID
  398.      * @param  integer  ID of the owning user.
  399.      * @param  integer  ID of the owning group.
  400.      * @param  array   values for the custom fields
  401.      * @return mixed   error object or true
  402.      */
  403.     function updateUser($permId$handle$password$type = null$active = true
  404.         $owner_user_id = null$owner_group_id = null$customFields = array())
  405.     {
  406.         if (is_object($this->auth&& is_object($this->perm)) {
  407.             $authData $this->perm->getAuthUserId($permId);
  408.  
  409.             $auth $this->auth->updateUser($authData['auth_user_id']$handle$password$active,
  410.                                         $owner_user_id$owner_group_id$customFields);
  411.  
  412.             if (LiveUser::isError($auth)) {
  413.                 return $auth;
  414.             }
  415.  
  416.             return $this->perm->updateUser($permId$authData['auth_user_id']$this->authContainerName$type);
  417.         }
  418.         return LiveUser::raiseError(LIVEUSER_ERRORnullnull,
  419.                     'Perm or Auth container couldn\t be started.');
  420.     }
  421.  
  422.     /**
  423.     * Removes user from both containers
  424.     *
  425.     * Untested: it most likely doesn't work.
  426.     *
  427.     * @access public
  428.     * @param  mixed Auth ID
  429.     * @return  mixed error object or true
  430.     */
  431.     function removeUser($permId)
  432.     {
  433.         if (is_object($this->auth&& is_object($this->perm)) {
  434.             $authData $this->perm->getAuthUserId($permId);
  435.  
  436.             if (LiveUser::isError($authData)) {
  437.                 return $authData;
  438.             }
  439.  
  440.             $result $this->auth->removeUser($authData['auth_user_id']);
  441.  
  442.             if (LiveUser::isError($result)) {
  443.                 return $result;
  444.             }
  445.  
  446.             return $this->perm->removeUser($permId);
  447.         }
  448.         return LiveUser::raiseError(LIVEUSER_ERRORnullnull,
  449.                     'Perm or Auth container couldn\t be started.');
  450.     }
  451.  
  452.     /**
  453.     * Searches users with given filters and returns
  454.     * all users found with their handle, passwd, auth_user_id
  455.     * lastlogin, is_active and the customFields if they are specified
  456.     *
  457.     * Untested: it most likely doesn't work.
  458.     *
  459.     * @access public
  460.     * @param   array   filters to apply to fetched data
  461.     * @param   string  if not null 'ORDER BY $order' will be appended to the query
  462.     * @param   boolean will return an associative array with the auth_user_id
  463.     *                   as the key by using DB::getAssoc() instead of DB::getAll()
  464.     * @return mixed error object or array
  465.     */
  466.     function searchUsers($filters = array()$order = null$rekey = false)
  467.     {
  468.         if (is_object($this->auth&& is_object($this->perm)) {
  469.             $search $this->auth->getUsers($filters$order$rekey);
  470.  
  471.             if (LiveUser::isError($search)) {
  472.                 return $search;
  473.             }
  474.  
  475.             return $search;
  476.         }
  477.         return LiveUser::raiseError(LIVEUSER_ERRORnullnull,
  478.                     'Perm or Auth container couldn\t be started.');
  479.     }
  480.  
  481.     /**
  482.     * Finds and gets userinfo by his userID, customFields can
  483.     *  also be gotten
  484.     *
  485.     * Untested: it most likely doesn't work.
  486.     *
  487.     * @access public
  488.     * @param  mixed  Perm User ID
  489.     * @return mixed Array with userinfo if found else error object
  490.     */
  491.     function getUser($permId$permFilter = array()$authFilter = array(),
  492.         $permOptions = array())
  493.     {
  494.         if (is_object($this->auth&& is_object($this->perm)) {
  495.             $permFilter['perm_user_id'$permId;
  496.             $permData $this->perm->getUsers($permFilter$permOptions);
  497.             if (LiveUser::isError($permData)) {
  498.                 return $permData;
  499.             }
  500.  
  501.             $permData array_shift($permData);
  502.  
  503.             $authFilter = array(
  504.                 array(
  505.                     'name' => $this->auth->authTableCols['required']['auth_user_id']['name'],
  506.                     'op' => '=',
  507.                     'value' => $permData['auth_user_id'],
  508.                     'cond' => 'AND',
  509.                     'type' => $this->auth->authTableCols['required']['auth_user_id']['type'],
  510.                 )
  511.             );
  512.  
  513.             $authData $this->auth->getUsers($authFilter);
  514.             if (LiveUser::isError($authData)) {
  515.                 return $authData;
  516.             }
  517.  
  518.             $authData array_shift($authData);
  519.  
  520.             return LiveUser::arrayMergeClobber($permData$authData);
  521.         }
  522.         return LiveUser::raiseError(LIVEUSER_ERRORnullnull,
  523.                     'Perm or Auth container couldn\t be started.');
  524.     }
  525. }

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