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. /**
  20.  * Attempt at a unified admin class
  21.  *
  22.  * Simple usage:
  23.  *
  24.  * <code>
  25.  * $conf = array(
  26.  *  'autoInit' => false/true,
  27.  *  'session'  => array(
  28.  *      'name'    => 'liveuser session name',
  29.  *      'varname' => 'liveuser session var name'
  30.  *  ),
  31.  *  'login' => array(
  32.  *      'method'   => 'get or post',
  33.  *      'username' => 'Form input containing user handle',
  34.  *      'password' => 'Form input containing password',
  35.  *      'remember' => '(optional) Form checkbox containing <Remember Me> info',
  36.  *      'function' => '(optional) Function to be called when accessing a page without logging in first',
  37.  *      'force'    => 'Should the user be forced to login'
  38.  *  ),
  39.  *  'logout' => array(
  40.  *      'trigger'  => 'GET or POST var that triggers the logout process',
  41.  *      'redirect' => 'Page path to be redirected to after logout',
  42.  *      'function' => '(optional) Function to be called when accessing a page without logging in first',
  43.  *      'destroy'  => 'Whether to destroy the session on logout'
  44.  *  ),
  45.  * // The cookie options are optional. If they are specified, the Remember Me
  46.  * // feature is activated.
  47.  *  'cookie' => array(
  48.  *      'name'     => 'Name of Remember Me cookie',
  49.  *      'lifetime' => 'Cookie lifetime in days',
  50.  *      'path'     => 'Cookie path',
  51.  *      'domain'   => 'Cookie domain',
  52.  *      'secret'   => 'Secret key used for cookie value encryption'
  53.  *  ),
  54.  *  'authContainers' => array(
  55.  *      'name' => array(
  56.  *            'type' => 'DB',
  57.  *            'connection'    => 'db connection object, use this or dsn',
  58.  *            'dsn'           => 'database dsn, use this or connection',
  59.  *            'loginTimeout'  => 0,
  60.  *            'expireTime'    => 3600,
  61.  *            'idleTime'      => 1800,
  62.  *            'allowDuplicateHandles' => 0,
  63.  *            'authTable'     => 'liveuser_users',
  64.  *            'authTableCols' => array('user_id'   => 'auth_user_id',
  65.  *                                     'handle'    => 'handle',
  66.  *                                     'passwd'    => 'passwd',
  67.  *                                     'lastlogin' => 'lastlogin'
  68.  *            )
  69.  *      )
  70.  *  ),
  71.  *  'permContainer' => array(
  72.  *      'type'       => 'DB_Complex',
  73.  *      'connection' => 'db connection object, use this or dsn',
  74.  *      'dsn'        => 'database dsn, use this or connection',
  75.  *      'prefix'     => 'liveuser_'
  76.  *  )
  77.  *
  78.  * $admin = new LiveUser_Admin($conf, 'FR');
  79.  * $found = $admin->getUser(3);
  80.  *
  81.  * if ($found) {
  82.  *  var_dump($admin->perm->getRights());
  83.  * }
  84.  * </code>
  85.  *
  86.  * @author  Lukas Smith
  87.  * @author  Arnaud Limbourg
  88.  * @author
  89.  * @version $Id: Admin.php,v 1.23 2004/04/25 17:12:08 dufuz Exp $
  90.  * @package LiveUser
  91.  */
  92. {
  93.  
  94.      /**
  95.       * Name of the current selected auth container
  96.       *
  97.       * @access public
  98.       * @var    string 
  99.       */
  100.      var $authContainerName;
  101.  
  102.     /**
  103.      * Array containing the auth objects.
  104.      *
  105.      * @access private
  106.      * @var    array 
  107.      */
  108.     var $_authContainers = array();
  109.  
  110.     /**
  111.      * Admin perm object
  112.      *
  113.      * @access public
  114.      * @var    object 
  115.      */
  116.     var $perm = null;
  117.  
  118.     /**
  119.      * Auth admin object
  120.      *
  121.      * @access public
  122.      * @var    object 
  123.      */
  124.     var $auth = null;
  125.  
  126.     /**
  127.      * Configuration array
  128.      *
  129.      * @access private
  130.      * @var    array 
  131.      */
  132.      var $_conf = array();
  133.  
  134.      /**
  135.       * Language to be used
  136.       *
  137.       * @access public
  138.       * @var    string 
  139.       */
  140.      var $lang = '';
  141.  
  142.     /**
  143.      * Constructor
  144.      *
  145.      * @access protected
  146.      * @param  array  liveuser conf array
  147.      * @param  string two letters language code
  148.      * @return void 
  149.      */
  150.     function LiveUser_Admin($conf$lang)
  151.     {
  152.         if (is_array($conf)) {
  153.             $this->_conf $conf;
  154.         }
  155.         $this->lang = $lang;
  156.  
  157.         if (isset($this->_conf['autoInit']&& $this->_conf['autoInit'=== true{
  158.             $this->setAdminContainers();
  159.         }
  160.     }
  161.  
  162.     /**
  163.      * Merges the current configuration array with configuration array pases
  164.      * along with the method call.
  165.      *
  166.      * @param  array   configuration array
  167.      * @return boolean true upon success, false otherwise
  168.      */
  169.     function setConfArray($conf)
  170.     {
  171.         if (!is_array($conf)) {
  172.             return false;
  173.         }
  174.  
  175.         $this->_conf LiveUser::arrayMergeClobber($this->_conf$conf);
  176.         return true;
  177.     }
  178.  
  179.     /**
  180.      * creates an instance of an auth object
  181.      *
  182.      * @access private
  183.      * @param  mixed    Array containing the configuration.
  184.      * @param  string   Name of the auth container.
  185.      * @return object   Returns an object of an auth container
  186.      */
  187.     function &_authFactory($conf$name = null)
  188.     {
  189.         if (!is_array($conf)) {
  190.             return false;
  191.         }
  192.         $classname 'LiveUser_Admin_Auth_Container_' $conf['type'];
  193.         $filename  'LiveUser/Admin/Auth/Container/' $conf['type''.php';
  194.         @include_once($filename);
  195.         if (!class_exists($classname)) {
  196.             $this->_error = true;
  197.             $error LiveUser::raiseError(LIVEUSER_ERROR_NOT_SUPPORTEDnullnull,
  198.                 'Missing file: '.$filename);
  199.             return $error;
  200.         }
  201.         $auth &new $classname($conf$name);
  202.         return $auth;
  203.     }
  204.  
  205.     /**
  206.      * creates an instance of an perm object
  207.      *
  208.      * @access private
  209.      * @param  mixed    Name of array containing the configuration.
  210.      * @return object   Returns an object of a perm container
  211.      */
  212.     function &_permFactory($conf)
  213.     {
  214.         if (!is_array($conf)) {
  215.             return false;
  216.         }
  217.         $classname 'LiveUser_Admin_Perm_Container_' $conf['type'];
  218.         $filename 'LiveUser/Admin/Perm/Container/' $conf['type''.php';
  219.         @include_once($filename);
  220.         if (!class_exists($classname)) {
  221.             $this->_error = true;
  222.             $error LiveUser::raiseError(LIVEUSER_NOT_SUPPORTEDnullnull,
  223.                 'Missing file: '.$filename);
  224.             return $error;
  225.         }
  226.         $perm &new $classname($conf);
  227.         return $perm;
  228.     }
  229.  
  230.     /**
  231.      * Sets the current auth container to the one with the given auth container name
  232.      *
  233.      * Upon success it will return true. You can then
  234.      * access the auth backend container by using the
  235.      * auth property of this class.
  236.      *
  237.      * e.g.: $admin->auth->addUser();
  238.      *
  239.      * @access public
  240.      * @param  string   auth container name
  241.      * @return boolean true upon success, false otherwise
  242.      */
  243.     function setAdminAuthContainer($authName)
  244.     {
  245.         if (!isset($this->_authContainers[$authName])
  246.             || !is_object($this->_authContainers[$authName])
  247.         {
  248.             if (!isset($this->_conf['authContainers'][$authName])) {
  249.                 return false;
  250.             }
  251.             $this->_authContainers[$authName=
  252.                 &$this->_authFactory($this->_conf['authContainers'][$authName]$authName);
  253.         }
  254.         $this->authContainerName = $authName;
  255.         $this->auth = &$this->_authContainers[$authName];
  256.         return true;
  257.     }
  258.  
  259.     /**
  260.      * Sets the perm container
  261.      *
  262.      * Upon success it will return true. You can then
  263.      * access the perm backend container by using the
  264.      * perm properties of this class.
  265.      *
  266.      * e.g.: $admin->perm->addUser();
  267.      *
  268.      * @access public
  269.      * @return boolean true upon success, false otherwise
  270.      */
  271.     function setAdminPermContainer()
  272.     {
  273.         if (!is_array($this->_conf)) {
  274.             return false;
  275.         }
  276.  
  277.         $this->perm = &$this->_permFactory($this->_conf['permContainer']);
  278.         $this->perm->authName = $this->_conf['permContainer']['type'];
  279.         $this->perm->setCurrentLanguage($this->lang);
  280.         return true;
  281.     }
  282.  
  283.     /**
  284.      * Tries to find a user in any of the auth container.
  285.      *
  286.      * Upon success it will return true. You can then
  287.      * access the backend container by using the auth
  288.      * and perm properties of this class.
  289.      *
  290.      * e.g.: $admin->perm->updateAuthUserId();
  291.      *
  292.      * @access public
  293.      * @param  mixed   user auth id
  294.      * @return boolean true upon success, false otherwise
  295.      */
  296.     function setAdminContainers($authId = null)
  297.     {
  298.         if (!is_array($this->_conf)) {
  299.             return false;
  300.         }
  301.  
  302.         if (is_null($authId)) {
  303.             reset($this->_conf['authContainers']);
  304.             $authName key($this->_conf['authContainers']);
  305.         else {
  306.             foreach ($this->_conf['authContainers'as $k => $v{
  307.                 if (!isset($this->_authContainers[$k]||
  308.                     !is_object($this->_authContainers[$k])
  309.                 {
  310.                     $this->_authContainers[$k&$this->_authFactory($v$k);
  311.                 }
  312.  
  313.                 if (!is_null($authId)) {
  314.                     $match $this->_authContainers[$k]->getUsers(array('auth_user_id' => $authId));
  315.                     if (is_array($match&& sizeof($match> 0{
  316.                         $authName $k;
  317.                         break;
  318.                     }
  319.                 }
  320.             }
  321.         }
  322.  
  323.         if (isset($authName)) {
  324.             if (!isset($this->perm|| !is_object($this->perm)) {
  325.                 $this->setAdminPermContainer();
  326.             }
  327.             $this->setAdminAuthContainer($authName);
  328.             return true;
  329.         }
  330.  
  331.         return false;
  332.     }
  333.  
  334.     /**
  335.      * Tries to add a user to both containers.
  336.      *
  337.      * If the optional $id parameter is passed it will be used
  338.      * for both containers.
  339.      *
  340.      * In any case the auth and perm id will be equal when using this method.
  341.      *
  342.      * If this behaviour doesn't suit your needs please consider
  343.      * using directly the concerned method. This method is just
  344.      * implement to simplify things a bit and should satisfy most
  345.      * user needs.
  346.      *
  347.      *  Note type is optional for DB, thus it's needed for MDB and MDB2,
  348.      *  we recommend that you use type even though you use DB, so if you change to MDB[2],
  349.      *  it will be no problem for you.
  350.      *  usage example for addUser:
  351.      * <code>
  352.      *        $custom = array(
  353.      *          array('name' => 'name', 'value' => 'asdf', 'type' => 'text'),
  354.      *           array('name' => 'email', 'value' => 'fleh@example.com', 'type' => 'text')
  355.      *      );
  356.      *       $user_id = $admin->addUser('johndoe', 'dummypass', true, null, null, null, $custom);
  357.      *  </code>
  358.      *
  359.      * Untested: it most likely doesn't work.
  360.      *
  361.      * @access public
  362.      * @param  string  user handle (username)
  363.      * @param  string  user password
  364.      * @param  boolean is account active ?
  365.      * @param  int          ID
  366.      * @param  integer ID of the owning user.
  367.      * @param  integer ID of the owning group.
  368.      * @param  array  Array of custom fields to be added
  369.      * @return mixed   userid or false
  370.      */
  371.     function addUser($handle$password$active = true$id = null$owner_user_id = null,
  372.                     $owner_group_id = null$customFields = array())
  373.     {
  374.         if (is_object($this->auth&& is_object($this->perm)) {
  375.             $authId $this->auth->addUser($handle$password$active$owner_user_id,
  376.                                             $owner_group_id$id$customFields);
  377.  
  378.             if (LiveUser::isError($authId)) {
  379.                 return $authId;
  380.             }
  381.  
  382.             $permId $this->perm->addUser($authId$this->authContainerNameLIVEUSER_USER_TYPE_ID$authId);
  383.  
  384.             if (LiveUser::isError($permId)) {
  385.                 return $permId;
  386.             }
  387.  
  388.             return $authId;
  389.         }
  390.         return LiveUser::raiseError(LIVEUSER_ERRORnullnull,
  391.                     'Perm or Auth container couldn\t be started.');
  392.     }
  393.  
  394.     /**
  395.     * Tried to changes user data for both containers.
  396.     *
  397.     *  Note type is optional for DB, thus it's needed for MDB and MDB2,
  398.     *  we recommend that you use type even though you use DB, so if you change to MDB[2],
  399.     *  it will be no problem for you.
  400.     *  usage example for updateUser:
  401.     * <code>
  402.     *       $custom = array(
  403.     *           array('name' => 'name', 'value' => 'asdfUpdated', 'type' => 'text'),
  404.     *           array('name' => 'email', 'value' => 'fleh@example.comUpdated', 'type' => 'text')
  405.     *       );
  406.     *       $admin->updateUser($user_id, 'johndoe', 'dummypass', true, null, null, $custom);
  407.     * </code>
  408.     *
  409.     * Untested: it most likely doesn't work.
  410.     *
  411.     * @access public
  412.     * @param  string  user handle (username)
  413.     * @param  string  user password
  414.     * @param  boolean is account active ?
  415.     * @param  int          ID
  416.     * @param  integer  ID of the owning user.
  417.     * @param  integer  ID of the owning group.
  418.     * @param  array  Array of custom fields to be updated
  419.     * @return mixed   error object or true
  420.     */
  421.     function updateUser($authId$handle$password$active = true$owner_user_id = null,
  422.                     $owner_group_id = null$customFields = array())
  423.     {
  424.         if (is_object($this->auth&& is_object($this->perm)) {
  425.             $auth $this->auth->updateUser($authId$handle$password$active,
  426.                                         $owner_user_id$owner_group_id$customFields);
  427.  
  428.             if (LiveUser::isError($auth)) {
  429.                 return $auth;
  430.             }
  431.  
  432.             $permId $this->perm->getPermUserId($authId$this->authContainerName);
  433.             $perm $this->perm->updateAuthUserId($permId$authId$this->authContainerName);
  434.  
  435.             if (LiveUser::isError($perm)) {
  436.                 return $perm;
  437.             }
  438.  
  439.             return true;
  440.         }
  441.         return LiveUser::raiseError(LIVEUSER_ERRORnullnull,
  442.                     'Perm or Auth container couldn\t be started.');
  443.     }
  444.  
  445.     /**
  446.     * Removes user from both containers
  447.     *
  448.     * Untested: it most likely doesn't work.
  449.     *
  450.     * @access public
  451.     * @param  mixed Auth ID
  452.     * @return  mixed error object or true
  453.     */
  454.     function removeUser($authId)
  455.     {
  456.         if (is_object($this->auth&& is_object($this->perm)) {
  457.             $this->auth->removeUser($authId);
  458.  
  459.             if (LiveUser::isError($authId)) {
  460.                 return $authId;
  461.             }
  462.  
  463.             $permId $this->perm->getPermUserId($authId$this->perm->authName);
  464.             $this->perm->removeUser($permId);
  465.  
  466.             if (LiveUser::isError($permId)) {
  467.                 return $permId;
  468.             }
  469.  
  470.             return true;
  471.         }
  472.         return LiveUser::raiseError(LIVEUSER_ERRORnullnull,
  473.                     'Perm or Auth container couldn\t be started.');
  474.     }
  475.  
  476.     /**
  477.     * Searches users with given filters and returns
  478.     * all users found with their handle, passwd, auth_user_id
  479.     * lastlogin, is_active and the customFields if they are specified
  480.     *
  481.     * Untested: it most likely doesn't work.
  482.     *
  483.     * @access public
  484.     * @param   array   filters to apply to fetched data
  485.     * @param   array  custom fields you want to be returned. If not specified
  486.     *                  the basic set of fields is returned. The keys are the
  487.     *                  names and the values
  488.     * @param   string  if not null 'ORDER BY $order' will be appended to the query
  489.     * @param   boolean will return an associative array with the auth_user_id
  490.     *                   as the key by using DB::getAssoc() instead of DB::getAll()
  491.     * @return mixed error object or array
  492.     */
  493.     function searchUsers($filters = array()$customFields = array()$order = null,
  494.                         $rekey = false)
  495.     {
  496.         if (is_object($this->auth&& is_object($this->perm)) {
  497.             $search $this->auth->getUsers($filters$customFields$order$rekey);
  498.  
  499.             if (LiveUser::isError($search)) {
  500.                 return $search;
  501.             }
  502.  
  503.             return $search;
  504.         }
  505.         return LiveUser::raiseError(LIVEUSER_ERRORnullnull,
  506.                     'Perm or Auth container couldn\t be started.');
  507.     }
  508.  
  509.     /**
  510.     * Finds and gets userinfo by his userID, customFields can
  511.     *  also be gotten
  512.     *
  513.     * Untested: it most likely doesn't work.
  514.     *
  515.     * @access public
  516.     * @param mixed User ID
  517.     * @param   array  custom fields you want to be returned. If not specified
  518.     *                  the basic set of fields is returned. The keys are the
  519.     *                  names and the values
  520.     * @return mixed Array with userinfo if found else error object
  521.     */
  522.     function getUser($userId$customFields = array())
  523.     {
  524.         if (is_object($this->auth&& is_object($this->perm)) {
  525.             if (is_array($this->auth->authTableCols['user_id'])) {
  526.                 $user_auth_id $this->auth->authTableCols['user_id']['name'];
  527.                 $type $this->auth->authTableCols['user_id']['type'];
  528.             else {
  529.                 $user_auth_id $this->auth->authTableCols['user_id'];
  530.                 $type '';
  531.             }
  532.  
  533.             $filters = array($user_auth_id =>
  534.                     array('op' => '=''value' => $userId'cond' => '''type' => $type)
  535.             );
  536.  
  537.             $search $this->auth->getUsers($filters$customFields);
  538.  
  539.             if (LiveUser::isError($search)) {
  540.                 return $search;
  541.             }
  542.  
  543.             return $search;
  544.         }
  545.         return LiveUser::raiseError(LIVEUSER_ERRORnullnull,
  546.                     'Perm or Auth container couldn\t be started.');
  547.     }
  548. }

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