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

Source for file DB.php

Documentation is available at DB.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * A framework for authentication and authorization in PHP applications
  6.  *
  7.  * LiveUser is an authentication/permission framework designed
  8.  * to be flexible and easily extendable.
  9.  *
  10.  * Since it is impossible to have a
  11.  * "one size fits all" it takes a container
  12.  * approach which should enable it to
  13.  * be versatile enough to meet most needs.
  14.  *
  15.  * PHP version 4 and 5
  16.  *
  17.  * LICENSE: This library is free software; you can redistribute it and/or
  18.  * modify it under the terms of the GNU Lesser General Public
  19.  * License as published by the Free Software Foundation; either
  20.  * version 2.1 of the License, or (at your option) any later version.
  21.  *
  22.  * This library is distributed in the hope that it will be useful,
  23.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  25.  * Lesser General Public License for more details.
  26.  *
  27.  * You should have received a copy of the GNU Lesser General Public
  28.  * License along with this library; if not, write to the Free Software
  29.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  30.  * MA  02111-1307  USA
  31.  *
  32.  *
  33.  * @category authentication
  34.  * @package LiveUser
  35.  * @author  Markus Wolff <wolff@21st.de>
  36.  * @author  Helgi Þormar Þorbjörnsson <dufuz@php.net>
  37.  * @author  Lukas Smith <smith@pooteeweet.org>
  38.  * @author  Arnaud Limbourg <arnaud@php.net>
  39.  * @author  Pierre-Alain Joye <pajoye@php.net>
  40.  * @author  Bjoern Kraus <krausbn@php.net>
  41.  * @copyright 2002-2006 Markus Wolff
  42.  * @license http://www.gnu.org/licenses/lgpl.txt
  43.  * @version CVS: $Id: DB.php,v 1.33 2006/04/07 22:20:57 lsmith Exp $
  44.  * @link http://pear.php.net/LiveUser
  45.  */
  46.  
  47. /**
  48.  * Require parent class definition.
  49.  */
  50. require_once 'LiveUser/Perm/Storage/SQL.php';
  51. require_once 'DB.php';
  52.  
  53. /**
  54.  * DB container for permission handling
  55.  *
  56.  * This is a PEAR::DB backend driver for the LiveUser class.
  57.  * A PEAR::DB connection object can be passed to the constructor to reuse an
  58.  * existing connection. Alternatively, a DSN can be passed to open a new one.
  59.  *
  60.  * Requirements:
  61.  * - File "Liveuser.php" (contains the parent class "LiveUser")
  62.  * - Array of connection options or a PEAR::DB connection object must be
  63.  *   passed to the constructor.
  64.  *   Example: array('dsn' => 'mysql://user:pass@host/db_name')
  65.  *              OR
  66.  *            &$conn (PEAR::DB connection object)
  67.  *
  68.  * @category authentication
  69.  * @package LiveUser
  70.  * @author  Lukas Smith <smith@pooteeweet.org>
  71.  * @author  Bjoern Kraus <krausbn@php.net>
  72.  * @version $Id: DB.php,v 1.33 2006/04/07 22:20:57 lsmith Exp $
  73.  * @copyright 2002-2006 Markus Wolff
  74.  * @license http://www.gnu.org/licenses/lgpl.txt
  75.  * @version Release: @package_version@
  76.  * @link http://pear.php.net/LiveUser
  77.  */
  78. {
  79.     /**
  80.      * Initialize the storage container
  81.      *
  82.      * @param array Array with the storage configuration
  83.      * @return bool true on success, false on failure.
  84.      *
  85.      * @access public
  86.      */
  87.     function init(&$storageConf)
  88.     {
  89.         parent::init($storageConf);
  90.  
  91.         if (!is_a($this->dbc'db_common'&& !is_null($this->dsn)) {
  92.             $this->options['portability'= DB_PORTABILITY_ALL;
  93.             $dbc =DB::connect($this->dsn$this->options);
  94.             if (PEAR::isError($dbc)) {
  95.                 $this->stack->push(LIVEUSER_ERROR_INIT_ERROR'error',
  96.                     array('container' => 'could not connect: '.$dbc->getMessage(),
  97.                     'debug' => $dbc->getUserInfo()));
  98.                 return false;
  99.             }
  100.             $this->dbc =$dbc;
  101.         }
  102.  
  103.         if (!is_a($this->dbc'db_common')) {
  104.             $this->stack->push(LIVEUSER_ERROR_INIT_ERROR'error',
  105.                 array('container' => 'storage layer configuration missing'));
  106.             return false;
  107.         }
  108.  
  109.         return true;
  110.     }
  111.  
  112.     /**
  113.      * map an auth user to a perm user
  114.      *
  115.      * @param int $auth_user_id 
  116.      * @param string $containerName 
  117.      * @return array requested data or false on failure
  118.      *
  119.      * @access public
  120.      */
  121.     function mapUser($auth_user_id$containerName)
  122.     {
  123.         $query '
  124.             SELECT
  125.                 ' $this->alias['perm_user_id'' AS perm_user_id,
  126.                 ' $this->alias['perm_type''    AS perm_type
  127.             FROM
  128.                 '.$this->prefix.$this->alias['perm_users'].'
  129.             WHERE
  130.                 ' $this->alias['auth_user_id'' = '.
  131.                     $this->dbc->quoteSmart($auth_user_id).'
  132.             AND
  133.                 ' $this->alias['auth_container_name'' = '.
  134.                     $this->dbc->quoteSmart((string)$containerName);
  135.  
  136.         $result $this->dbc->getRow($querynullDB_FETCHMODE_ASSOC);
  137.  
  138.         if (PEAR::isError($result)) {
  139.             $this->stack->push(LIVEUSER_ERROR'exception'array(),
  140.                 'error in query' $result->getMessage('-' $result->getUserInfo());
  141.             return false;
  142.         }
  143.  
  144.         return $result;
  145.     }
  146.  
  147.     /**
  148.      * Reads all rights of current user into a
  149.      * two-dimensional associative array, having the
  150.      * area names as the key of the 1st dimension.
  151.      * Group rights and invididual rights are being merged
  152.      * in the process.
  153.      *
  154.      * @param int perm user id
  155.      * @return array requested data or false on failure
  156.      *
  157.      * @access public
  158.      */
  159.     function readUserRights($perm_user_id)
  160.     {
  161.         $query '
  162.             SELECT
  163.                 ' $this->alias['right_id'',
  164.                 ' $this->alias['right_level''
  165.             FROM
  166.                 '.$this->prefix.$this->alias['userrights'].'
  167.             WHERE
  168.                 ' $this->alias['perm_user_id'' = '.
  169.                     $this->dbc->quoteSmart($perm_user_id);
  170.  
  171.         $result $this->dbc->getAssoc($queryfalsenullDB_FETCHMODE_ORDERED);
  172.  
  173.         if (PEAR::isError($result)) {
  174.             $this->stack->push(LIVEUSER_ERROR'exception'array(),
  175.                 'error in query' $result->getMessage('-' $result->getUserInfo());
  176.             return false;
  177.         }
  178.  
  179.         return $result;
  180.     }
  181.  
  182.     /**
  183.      * read the areas in which a user is an area admin
  184.      *
  185.      * @param int perm user id
  186.      * @return array requested data or false on failure
  187.      *
  188.      * @access public
  189.      */
  190.     function readAreaAdminAreas($perm_user_id)
  191.     {
  192.         // get all areas in which the user is area admin
  193.         $query '
  194.             SELECT
  195.                 R.' $this->alias['right_id'' AS right_id,
  196.                 '.LIVEUSER_MAX_LEVEL.'             AS right_level
  197.             FROM
  198.                 '.$this->prefix.$this->alias['area_admin_areas'].' AAA,
  199.                 '.$this->prefix.$this->alias['rights'].' R
  200.             WHERE
  201.                 AAA.area_id = R.area_id
  202.             AND
  203.                 AAA.' $this->alias['perm_user_id'' = '.
  204.                     $this->dbc->quoteSmart($perm_user_id);
  205.  
  206.         $result $this->dbc->getAssoc($queryfalsenullDB_FETCHMODE_ORDERED);
  207.  
  208.         if (PEAR::isError($result)) {
  209.             $this->stack->push(LIVEUSER_ERROR'exception'array(),
  210.                 'error in query' $result->getMessage('-' $result->getUserInfo());
  211.             return false;
  212.         }
  213.  
  214.         return $result;
  215.     }
  216.  
  217.     /**
  218.      * Reads all the group ids in that the user is also a member of
  219.      * (all groups that are subgroups of these are also added recursively)
  220.      *
  221.      * @param int perm user id
  222.      * @return array requested data or false on failure
  223.      *
  224.      * @see    readRights()
  225.      * @access public
  226.      */
  227.     function readGroups($perm_user_id)
  228.     {
  229.         $query '
  230.             SELECT
  231.                 GU.' $this->alias['group_id''
  232.             FROM
  233.                 '.$this->prefix.$this->alias['groupusers'].' GU,
  234.                 '.$this->prefix.$this->alias['groups'].' G
  235.             WHERE
  236.                 GU.' $this->alias['group_id'' = G. ' $this->alias['group_id''
  237.             AND
  238.                 GU.' $this->alias['perm_user_id'' = '.
  239.                     $this->dbc->quoteSmart($perm_user_id);
  240.  
  241.         if (array_key_exists('is_active'$this->tables['groups']['fields'])) {
  242.             $query .= ' AND
  243.                 G.' $this->alias['is_active''=' .
  244.                     $this->dbc->quoteSmart(true);
  245.         }
  246.  
  247.         $result $this->dbc->getCol($query);
  248.  
  249.         if (PEAR::isError($result)) {
  250.             $this->stack->push(LIVEUSER_ERROR'exception'array(),
  251.                 'error in query' $result->getMessage('-' $result->getUserInfo());
  252.             return false;
  253.         }
  254.  
  255.         return $result;
  256.     }
  257.  
  258.     /**
  259.      * Reads the group rights
  260.      * and put them in the array
  261.      *
  262.      * right => 1
  263.      *
  264.      * @param int group ids
  265.      * @return array requested data or false on failure
  266.      *
  267.      * @access public
  268.      */
  269.     function readGroupRights($group_ids)
  270.     {
  271.         $query '
  272.             SELECT
  273.                 GR.' $this->alias['right_id'',
  274.                 MAX(GR.' $this->alias['right_level'')
  275.             FROM
  276.                 '.$this->prefix.$this->alias['grouprights'].' GR
  277.             WHERE
  278.                 GR.' $this->alias['group_id'' IN('.
  279.                     implode(', '$group_ids).')
  280.             GROUP BY
  281.                 GR.' $this->alias['right_id''';
  282.  
  283.         $result $this->dbc->getAssoc($queryfalsenullDB_FETCHMODE_ORDERED);
  284.  
  285.         if (PEAR::isError($result)) {
  286.             $this->stack->push(LIVEUSER_ERROR'exception'array(),
  287.                 'error in query' $result->getMessage('-' $result->getUserInfo());
  288.             return false;
  289.         }
  290.  
  291.         return $result;
  292.     }
  293.  
  294.     /**
  295.      * Read the sub groups of the new groups that are not part of the group ids
  296.      *
  297.      * @param array group ids
  298.      * @param array new group ids
  299.      * @return array requested data or false on failure
  300.      *
  301.      * @access public
  302.      */
  303.     function readSubGroups($group_ids$newGroupIds)
  304.     {
  305.         $query '
  306.             SELECT
  307.                 DISTINCT SG.' $this->alias['subgroup_id''
  308.             FROM
  309.                 '.$this->prefix.$this->alias['groups'].' G,
  310.                 '.$this->prefix.$this->alias['group_subgroups'].' SG
  311.             WHERE
  312.                 SG.' $this->alias['subgroup_id'' = G.' .
  313.                     $this->alias['group_id''
  314.             AND
  315.                 SG.' $this->alias['group_id'' IN ('.
  316.                     implode(', '$newGroupIds).')
  317.             AND
  318.                 SG.' $this->alias['subgroup_id'' NOT IN ('.
  319.                     implode(', '$group_ids).')';
  320.  
  321.         if (array_key_exists('is_active'$this->tables['groups']['fields'])) {
  322.             $query .= ' AND
  323.                 G.' $this->alias['is_active''=' .
  324.                     $this->dbc->quoteSmart(true);
  325.         }
  326.  
  327.         $result $this->dbc->getCol($query);
  328.  
  329.         if (PEAR::isError($result)) {
  330.             $this->stack->push(LIVEUSER_ERROR'exception'array(),
  331.                 'error in query' $result->getMessage('-' $result->getUserInfo());
  332.             return false;
  333.         }
  334.  
  335.         return $result;
  336.     }
  337.  
  338.     /**
  339.      * Read out the rights from the userrights or grouprights table
  340.      * that imply other rights along with their level
  341.      *
  342.      * @param array right ids
  343.      * @param string name of the table
  344.      * @return array requested data or false on failure
  345.      *
  346.      * @access public
  347.      */
  348.     function readImplyingRights($rightIds$table)
  349.     {
  350.         $query '
  351.             SELECT
  352.             DISTINCT
  353.                 TR.' $this->alias['right_level'',
  354.                 TR.' $this->alias['right_id''
  355.             FROM
  356.                 '.$this->prefix.$this->alias['rights'].' R,
  357.                 '.$this->prefix.$this->alias[$table.'rights'].' TR
  358.             WHERE
  359.                 TR.' $this->alias['right_id'' = R.' $this->alias['right_id''
  360.             AND
  361.                 R.' $this->alias['right_id'' IN ('.
  362.                     implode(', 'array_keys($rightIds)).')
  363.             AND
  364.                 R.' $this->alias['has_implied''='.
  365.                     $this->dbc->quoteSmart(true);
  366.  
  367.         $result $this->dbc->getAssoc($queryfalsenullDB_FETCHMODE_ORDEREDtrue);
  368.  
  369.         if (PEAR::isError($result)) {
  370.             $this->stack->push(LIVEUSER_ERROR'exception'array(),
  371.                 'error in query' $result->getMessage('-' $result->getUserInfo());
  372.             return false;
  373.         }
  374.  
  375.         return $result;
  376.     }
  377.  
  378.     /**
  379.     * Read out the implied rights with a given level from the implied_rights table
  380.     *
  381.     * @param array current right ids
  382.     * @param string current level
  383.      * @return array requested data or false on failure
  384.     *
  385.     * @access public
  386.     */
  387.     function readImpliedRights($currentRights$currentLevel)
  388.     {
  389.         $query '
  390.             SELECT
  391.                 RI.' $this->alias['implied_right_id'' AS right_id,
  392.                 '.$currentLevel.'                           AS right_level,
  393.                 R.' $this->alias['has_implied''       AS has_implied
  394.             FROM
  395.                 '.$this->prefix.$this->alias['rights'].' R,
  396.                 '.$this->prefix.$this->alias['right_implied'].' RI
  397.             WHERE
  398.                 RI.' $this->alias['implied_right_id'' = R.' $this->alias['right_id''
  399.             AND
  400.                 RI.' $this->alias['right_id'' IN ('.
  401.                     implode(', '$currentRights).')';
  402.  
  403.         $result $this->dbc->getAll($querynullDB_FETCHMODE_ASSOC);
  404.  
  405.         if (PEAR::isError($result)) {
  406.             $this->stack->push(LIVEUSER_ERROR'exception'array(),
  407.                 'error in query' $result->getMessage('-' $result->getUserInfo());
  408.             return false;
  409.         }
  410.  
  411.         return $result;
  412.     }
  413. }
  414. ?>

Documentation generated on Mon, 28 Jan 2008 03:30:13 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.