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.41 2004/06/19 16:57:59 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.         $filename  'LiveUser/Admin/Auth/Container/' $conf['type''.php';
  174.         @include_once($filename);
  175.         if (!class_exists($classname)) {
  176.             $this->_error = true;
  177.             $error LiveUser::raiseError(LIVEUSER_ERROR_NOT_SUPPORTEDnullnull,
  178.                 'Missing file: '.$filename);
  179.             return $error;
  180.         }
  181.         $auth &new $classname($conf$name);
  182.         return $auth;
  183.     }
  184.  
  185.     /**
  186.      * creates an instance of an perm object
  187.      *
  188.      * @access private
  189.      * @param  mixed    Name of array containing the configuration.
  190.      * @return object   Returns an object of a perm container
  191.      */
  192.     function &_permFactory($conf)
  193.     {
  194.         if (!is_array($conf)) {
  195.             return false;
  196.         }
  197.         $classname 'LiveUser_Admin_Perm_Container_' $conf['type'];
  198.         $filename 'LiveUser/Admin/Perm/Container/' $conf['type''.php';
  199.         @include_once($filename);
  200.         if (!class_exists($classname)) {
  201.             $this->_error = true;
  202.             $error LiveUser::raiseError(LIVEUSER_NOT_SUPPORTEDnullnull,
  203.                 'Missing file: '.$filename);
  204.             return $error;
  205.         }
  206.         $perm &new $classname($conf);
  207.         return $perm;
  208.     }
  209.  
  210.     /**
  211.      * Sets the current auth container to the one with the given auth container name
  212.      *
  213.      * Upon success it will return true. You can then
  214.      * access the auth backend container by using the
  215.      * auth property of this class.
  216.      *
  217.      * e.g.: $admin->auth->addUser();
  218.      *
  219.      * @access public
  220.      * @param  string   auth container name
  221.      * @return boolean true upon success, false otherwise
  222.      */
  223.     function setAdminAuthContainer($authName)
  224.     {
  225.         if (!isset($this->_authContainers[$authName])
  226.             || !is_object($this->_authContainers[$authName])
  227.         {
  228.             if (!isset($this->_conf['authContainers'][$authName])) {
  229.                 return false;
  230.             }
  231.             $this->_authContainers[$authName=
  232.                 &$this->_authFactory($this->_conf['authContainers'][$authName]$authName);
  233.         }
  234.         $this->authContainerName = $authName;
  235.         $this->auth = &$this->_authContainers[$authName];
  236.         return true;
  237.     }
  238.  
  239.     /**
  240.      * Sets the perm container
  241.      *
  242.      * Upon success it will return true. You can then
  243.      * access the perm backend container by using the
  244.      * perm properties of this class.
  245.      *
  246.      * e.g.: $admin->perm->addUser();
  247.      *
  248.      * @access public
  249.      * @return boolean true upon success, false otherwise
  250.      */
  251.     function setAdminPermContainer()
  252.     {
  253.         if (!is_array($this->_conf)) {
  254.             return false;
  255.         }
  256.  
  257.         $this->perm = &$this->_permFactory($this->_conf['permContainer']);
  258.         $this->perm->setCurrentLanguage($this->lang);
  259.         return true;
  260.     }
  261.  
  262.     /**
  263.      * Tries to find a user in any of the auth container.
  264.      *
  265.      * Upon success it will return true. You can then
  266.      * access the backend container by using the auth
  267.      * and perm properties of this class.
  268.      *
  269.      * e.g.: $admin->perm->updateAuthUserId();
  270.      *
  271.      * @access public
  272.      * @param  mixed   user auth id
  273.      * @param  string   auth container name
  274.      * @return boolean true upon success, false otherwise
  275.      */
  276.     function setAdminContainers($authId = null$authName = null)
  277.     {
  278.         if (!is_array($this->_conf)) {
  279.             return false;
  280.         }
  281.  
  282.         if (is_null($authName)) {
  283.             if (is_null($authId)) {
  284.                 reset($this->_conf['authContainers']);
  285.                 $authName key($this->_conf['authContainers']);
  286.             else {
  287.                 foreach ($this->_conf['authContainers'as $k => $v{
  288.                     if (!isset($this->_authContainers[$k]||
  289.                         !is_object($this->_authContainers[$k])
  290.                     {
  291.                         $this->_authContainers[$k&$this->_authFactory($v$k);
  292.                     }
  293.     
  294.                     if (!is_null($authId)) {
  295.                         $match $this->_authContainers[$k]->getUsers(array('auth_user_id' => $authId));
  296.                         if (is_array($match&& sizeof($match> 0{
  297.                             $authName $k;
  298.                             break;
  299.                         }
  300.                     }
  301.                 }
  302.             }
  303.         }
  304.  
  305.         if (isset($authName)) {
  306.             if (!isset($this->perm|| !is_object($this->perm)) {
  307.                 $this->setAdminPermContainer();
  308.             }
  309.             $this->setAdminAuthContainer($authName);
  310.             return true;
  311.         }
  312.  
  313.         return false;
  314.     }
  315.  
  316.     /**
  317.      * Tries to add a user to both containers.
  318.      *
  319.      * If the optional $id parameter is passed it will be used
  320.      * for both containers.
  321.      *
  322.      * In any case the auth and perm id will be equal when using this method.
  323.      *
  324.      * If this behaviour doesn't suit your needs please consider
  325.      * using directly the concerned method. This method is just
  326.      * implement to simplify things a bit and should satisfy most
  327.      * user needs.
  328.      *
  329.      *  Note type is optional for DB, thus it's needed for MDB and MDB2,
  330.      *  we recommend that you use type even though you use DB, so if you change to MDB[2],
  331.      *  it will be no problem for you.
  332.      *  usage example for addUser:
  333.      * <code>
  334.      *       $user_id = $admin->addUser('johndoe', 'dummypass', true, null, null, null);
  335.      *  </code>
  336.      *
  337.      * Untested: it most likely doesn't work.
  338.      *
  339.      * @access public
  340.      * @param  string  user handle (username)
  341.      * @param  string  user password
  342.      * @param  integer permission user type
  343.      * @param  boolean is account active ?
  344.      * @param  int          ID
  345.      * @param  integer ID of the owning user.
  346.      * @param  integer ID of the owning group.
  347.      * @param  array   values for the custom fields
  348.      * @return mixed   userid or false
  349.      */
  350.     function addUser($handle$password$type = null$active = true$id = null$owner_user_id = null,
  351.                     $owner_group_id = null$customFields = array())
  352.     {
  353.         if (is_object($this->auth&& is_object($this->perm)) {
  354.             $authId $this->auth->addUser($handle$password$active$owner_user_id,
  355.                                             $owner_group_id$id$customFields);
  356.  
  357.             if (LiveUser::isError($authId)) {
  358.                 return $authId;
  359.             }
  360.  
  361.             return $this->perm->addUser($authId$this->authContainerName$type);
  362.         }
  363.         return LiveUser::raiseError(LIVEUSER_ERRORnullnull,
  364.                     'Perm or Auth container couldn\t be started.');
  365.     }
  366.  
  367.     /**
  368.      * Tried to changes user data for both containers.
  369.      *
  370.      *  Note type is optional for DB, thus it's needed for MDB and MDB2,
  371.      *  we recommend that you use type even though you use DB, so if you change to MDB[2],
  372.      *  it will be no problem for you.
  373.      *  usage example for updateUser:
  374.      * <code>
  375.      *       $admin->updateUser($user_id, 'johndoe', 'dummypass', true, null, null);
  376.      * </code>
  377.      *
  378.      * Untested: it most likely doesn't work.
  379.      *
  380.      * @access public
  381.      * @param  string  user handle (username)
  382.      * @param  string  user password
  383.      * @param  integer permission user type
  384.      * @param  boolean is account active ?
  385.      * @param  int          ID
  386.      * @param  integer  ID of the owning user.
  387.      * @param  integer  ID of the owning group.
  388.      * @param  array   values for the custom fields
  389.      * @return mixed   error object or true
  390.      */
  391.     function updateUser($permId$handle$password$type = null$active = true
  392.         $owner_user_id = null$owner_group_id = null$customFields = array())
  393.     {
  394.         if (is_object($this->auth&& is_object($this->perm)) {
  395.             $authData $this->perm->getAuthUserId($permId);
  396.  
  397.             $auth $this->auth->updateUser($authData['auth_user_id']$handle$password$active,
  398.                                         $owner_user_id$owner_group_id$customFields);
  399.  
  400.             if (LiveUser::isError($auth)) {
  401.                 return $auth;
  402.             }
  403.  
  404.             return $this->perm->updateUser($permId$authData['auth_user_id']$this->authContainerName$type);
  405.         }
  406.         return LiveUser::raiseError(LIVEUSER_ERRORnullnull,
  407.                     'Perm or Auth container couldn\t be started.');
  408.     }
  409.  
  410.     /**
  411.     * Removes user from both containers
  412.     *
  413.     * Untested: it most likely doesn't work.
  414.     *
  415.     * @access public
  416.     * @param  mixed Auth ID
  417.     * @return  mixed error object or true
  418.     */
  419.     function removeUser($permId)
  420.     {
  421.         if (is_object($this->auth&& is_object($this->perm)) {
  422.             $authData $this->perm->getAuthUserId($permId);
  423.  
  424.             if (LiveUser::isError($authData)) {
  425.                 return $authData;
  426.             }
  427.  
  428.             $result $this->auth->removeUser($authData['auth_user_id']);
  429.  
  430.             if (LiveUser::isError($result)) {
  431.                 return $result;
  432.             }
  433.  
  434.             return $this->perm->removeUser($permId);
  435.         }
  436.         return LiveUser::raiseError(LIVEUSER_ERRORnullnull,
  437.                     'Perm or Auth container couldn\t be started.');
  438.     }
  439.  
  440.     /**
  441.     * Searches users with given filters and returns
  442.     * all users found with their handle, passwd, auth_user_id
  443.     * lastlogin, is_active and the customFields if they are specified
  444.     *
  445.     * Untested: it most likely doesn't work.
  446.     *
  447.     * @access public
  448.     * @param   array   filters to apply to fetched data
  449.     * @param   string  if not null 'ORDER BY $order' will be appended to the query
  450.     * @param   boolean will return an associative array with the auth_user_id
  451.     *                   as the key by using DB::getAssoc() instead of DB::getAll()
  452.     * @return mixed error object or array
  453.     */
  454.     function searchUsers($filters = array()$order = null$rekey = false)
  455.     {
  456.         if (is_object($this->auth&& is_object($this->perm)) {
  457.             $search $this->auth->getUsers($filters$order$rekey);
  458.  
  459.             if (LiveUser::isError($search)) {
  460.                 return $search;
  461.             }
  462.  
  463.             return $search;
  464.         }
  465.         return LiveUser::raiseError(LIVEUSER_ERRORnullnull,
  466.                     'Perm or Auth container couldn\t be started.');
  467.     }
  468.  
  469.     /**
  470.     * Finds and gets userinfo by his userID, customFields can
  471.     *  also be gotten
  472.     *
  473.     * Untested: it most likely doesn't work.
  474.     *
  475.     * @access public
  476.     * @param  mixed  Perm User ID
  477.     * @return mixed Array with userinfo if found else error object
  478.     */
  479.     function getUser($permId)
  480.     {
  481.         if (is_object($this->auth&& is_object($this->perm)) {
  482.             $user_auth_id $this->auth->authTableCols['required']['auth_user_id']['name'];
  483.             $type = isset($this->auth->authTableCols['required']['auth_user_id']['type'])
  484.                 ? $this->auth->authTableCols['required']['auth_user_id']['type''';
  485.  
  486.             $authData $this->perm->getAuthUserId($permId);
  487.  
  488.             $filters = array(
  489.                 $user_auth_id => array('name' => $user_auth_id'op' => '=''value' => $authData['auth_user_id']'cond' => '''type' => $type)
  490.             );
  491.  
  492.             $search $this->auth->getUsers($filters);
  493.  
  494.             if (LiveUser::isError($search)) {
  495.                 return $search;
  496.             }
  497.  
  498.             return $search;
  499.         }
  500.         return LiveUser::raiseError(LIVEUSER_ERRORnullnull,
  501.                     'Perm or Auth container couldn\t be started.');
  502.     }
  503. }

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