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

Source for file MDB.php

Documentation is available at MDB.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: MDB.php,v 1.32 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 'MDB.php';
  52.  
  53. /**
  54.  * MDB container for permission handling
  55.  *
  56.  * This is a PEAR::MDB backend driver for the LiveUser class.
  57.  * A PEAR::MDB 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::MDB connection object must be
  63.  *   passed to the constructor.
  64.  *   Example: array('dsn' => 'mysql://user:pass@host/db_name')
  65.  *              OR
  66.  *            &$conn (PEAR::MDB connection object)
  67.  *
  68.  * @category authentication
  69.  * @package LiveUser
  70.  * @author  Lukas Smith <smith@pooteeweet.org>
  71.  * @author  Bjoern Kraus <krausbn@php.net>
  72.  * @copyright 2002-2006 Markus Wolff
  73.  * @license http://www.gnu.org/licenses/lgpl.txt
  74.  * @version Release: @package_version@
  75.  * @link http://pear.php.net/LiveUser
  76.  */
  77. {
  78.     /**
  79.      * Database connection functions
  80.      *
  81.      * @var    object 
  82.      * @access private
  83.      */
  84.     var $function 'connect';
  85.  
  86.     /**
  87.      * Initialize the storage container
  88.      *
  89.      * @param array Array with the storage configuration
  90.      * @return bool true on success, false on failure.
  91.      *
  92.      * @access public
  93.      */
  94.     function init($storageConf)
  95.     {
  96.         parent::init($storageConf);
  97.  
  98.         if (!MDB::isConnection($this->dbc&& !is_null($this->dsn)) {
  99.             $this->options['optimize''portability';
  100.             if ($this->function == 'singleton'{
  101.                 $dbc =MDB::singleton($this->dsn$this->options);
  102.             else {
  103.                 $dbc =MDB::connect($this->dsn$this->options);
  104.             }
  105.             if (PEAR::isError($dbc)) {
  106.                 $this->stack->push(LIVEUSER_ERROR_INIT_ERROR'error',
  107.                     array('container' => 'could not connect: '.$dbc->getMessage(),
  108.                     'debug' => $dbc->getUserInfo()));
  109.                 return false;
  110.             }
  111.             $this->dbc =$dbc;
  112.         }
  113.  
  114.         if (!MDB::isConnection($this->dbc)) {
  115.             $this->stack->push(LIVEUSER_ERROR_INIT_ERROR'error',
  116.                 array('container' => 'storage layer configuration missing'));
  117.             return false;
  118.         }
  119.  
  120.         return true;
  121.     }
  122.  
  123.     /**
  124.      * map an auth user to a perm user
  125.      *
  126.      * @param int $auth_user_id 
  127.      * @param string $containerName 
  128.      * @return array requested data or false on failure
  129.      *
  130.      * @access public
  131.      */
  132.     function mapUser($auth_user_id$containerName)
  133.     {
  134.         $query '
  135.             SELECT
  136.                 ' $this->alias['perm_user_id'' AS perm_user_id,
  137.                 ' $this->alias['perm_type''    AS perm_type
  138.             FROM
  139.                 '.$this->prefix.$this->alias['perm_users'].'
  140.             WHERE
  141.                 ' $this->alias['auth_user_id'' = '.
  142.                     $this->dbc->getValue($this->fields['auth_user_id']$auth_user_id).'
  143.             AND
  144.                 ' $this->alias['auth_container_name'' = '.
  145.                     $this->dbc->getValue($this->fields['auth_container_name']$containerName);
  146.  
  147.         $types = array(
  148.             $this->fields['perm_user_id'],
  149.             $this->fields['perm_type']
  150.         );
  151.         $result $this->dbc->queryRow($query$typesMDB_FETCHMODE_ASSOC);
  152.  
  153.         if (PEAR::isError($result)) {
  154.             $this->stack->push(LIVEUSER_ERROR'exception'array(),
  155.                 'error in query' $result->getMessage('-' $result->getUserInfo());
  156.             return false;
  157.         }
  158.  
  159.         return $result;
  160.     }
  161.  
  162.     /**
  163.      * Reads all rights of current user into a
  164.      * two-dimensional associative array, having the
  165.      * area names as the key of the 1st dimension.
  166.      * Group rights and invididual rights are being merged
  167.      * in the process.
  168.      *
  169.      * @param int perm user id
  170.      * @return array requested data or false on failure
  171.      *
  172.      * @access public
  173.      */
  174.     function readUserRights($perm_user_id)
  175.     {
  176.         $query '
  177.             SELECT
  178.                 ' $this->alias['right_id'',
  179.                 ' $this->alias['right_level''
  180.             FROM
  181.                 '.$this->prefix.$this->alias['userrights'].'
  182.             WHERE
  183.                 ' $this->alias['perm_user_id'' = '.
  184.                     $this->dbc->getValue($this->fields['perm_user_id']$perm_user_id);
  185.  
  186.         $types = array(
  187.             $this->fields['right_id'],
  188.             $this->fields['right_level']
  189.         );
  190.         $result $this->dbc->queryAll($query$typesMDB_FETCHMODE_ORDEREDtrue);
  191.  
  192.         if (PEAR::isError($result)) {
  193.             $this->stack->push(LIVEUSER_ERROR'exception'array(),
  194.                 'error in query' $result->getMessage('-' $result->getUserInfo());
  195.             return false;
  196.         }
  197.  
  198.         return $result;
  199.     }
  200.  
  201.     /**
  202.      * read the areas in which a user is an area admin
  203.      *
  204.      * @param int perm user id
  205.      * @return array requested data or false on failure
  206.      *
  207.      * @access public
  208.      */
  209.     function readAreaAdminAreas($perm_user_id)
  210.     {
  211.         // get all areas in which the user is area admin
  212.         $query '
  213.             SELECT
  214.                 R.' $this->alias['right_id'' AS right_id,
  215.                 '.LIVEUSER_MAX_LEVEL.'             AS right_level
  216.             FROM
  217.                 '.$this->prefix.$this->alias['area_admin_areas'].' AAA,
  218.                 '.$this->prefix.$this->alias['rights'].' R
  219.             WHERE
  220.                 AAA.area_id = R.area_id
  221.             AND
  222.                 AAA.' $this->alias['perm_user_id'' = '.
  223.                     $this->dbc->getValue($this->fields['perm_user_id']$perm_user_id);
  224.  
  225.         $types = array(
  226.             $this->fields['right_id'],
  227.             $this->fields['right_level']
  228.         );
  229.         $result $this->dbc->queryAll($query$typesMDB_FETCHMODE_ORDEREDtrue);
  230.  
  231.         if (PEAR::isError($result)) {
  232.             $this->stack->push(LIVEUSER_ERROR'exception'array(),
  233.                 'error in query' $result->getMessage('-' $result->getUserInfo());
  234.             return false;
  235.         }
  236.  
  237.         return $result;
  238.     }
  239.  
  240.     /**
  241.      * Reads all the group ids in that the user is also a member of
  242.      * (all groups that are subgroups of these are also added recursively)
  243.      *
  244.      * @param int perm user id
  245.      * @return array requested data or false on failure
  246.      *
  247.      * @see    readRights()
  248.      * @access public
  249.      */
  250.     function readGroups($perm_user_id)
  251.     {
  252.         $query '
  253.             SELECT
  254.                 GU.' $this->alias['group_id''
  255.             FROM
  256.                 '.$this->prefix.$this->alias['groupusers'].' GU,
  257.                 '.$this->prefix.$this->alias['groups'].' G
  258.             WHERE
  259.                 GU.' $this->alias['group_id'' = G. ' $this->alias['group_id''
  260.             AND
  261.                 GU.' $this->alias['perm_user_id'' = '.
  262.                     $this->dbc->getValue($this->fields['perm_user_id']$perm_user_id);
  263.  
  264.         if (array_key_exists('is_active'$this->tables['groups']['fields'])) {
  265.             $query .= ' AND
  266.                 G.' $this->alias['is_active''=' .
  267.                     $this->dbc->getValue($this->fields['is_active']true);
  268.         }
  269.  
  270.         $result $this->dbc->queryCol($query$this->fields['group_id']);
  271.  
  272.         if (PEAR::isError($result)) {
  273.             $this->stack->push(LIVEUSER_ERROR'exception'array(),
  274.                 'error in query' $result->getMessage('-' $result->getUserInfo());
  275.             return false;
  276.         }
  277.  
  278.         return $result;
  279.     }
  280.  
  281.     /**
  282.      * Reads the group rights
  283.      * and put them in the array
  284.      *
  285.      * right => 1
  286.      *
  287.      * @param int group ids
  288.      * @return array requested data or false on failure
  289.      *
  290.      * @access public
  291.      */
  292.     function readGroupRights($group_ids)
  293.     {
  294.         $query '
  295.             SELECT
  296.                 GR.' $this->alias['right_id'',
  297.                 MAX(GR.' $this->alias['right_level'')
  298.             FROM
  299.                 '.$this->prefix.$this->alias['grouprights'].' GR
  300.             WHERE
  301.                 GR.' $this->alias['group_id'' IN('.
  302.                     implode(', '$group_ids).')
  303.             GROUP BY
  304.                 GR.' $this->alias['right_id''';
  305.  
  306.         $types = array(
  307.             $this->fields['right_id'],
  308.             $this->fields['right_level']
  309.         );
  310.         $result $this->dbc->queryAll($query$typesMDB_FETCHMODE_ORDEREDtrue);
  311.  
  312.         if (PEAR::isError($result)) {
  313.             $this->stack->push(LIVEUSER_ERROR'exception'array(),
  314.                 'error in query' $result->getMessage('-' $result->getUserInfo());
  315.             return false;
  316.         }
  317.  
  318.         return $result;
  319.     }
  320.  
  321.     /**
  322.      * Read the sub groups of the new groups that are not part of the group ids
  323.      *
  324.      * @param array group ids
  325.      * @param array new group ids
  326.      * @return array requested data or false on failure
  327.      *
  328.      * @access public
  329.      */
  330.     function readSubGroups($group_ids$newGroupIds)
  331.     {
  332.         $query '
  333.             SELECT
  334.                 DISTINCT SG.' $this->alias['subgroup_id''
  335.             FROM
  336.                 '.$this->prefix.$this->alias['groups'].' G,
  337.                 '.$this->prefix.$this->alias['group_subgroups'].' SG
  338.             WHERE
  339.                 SG.' $this->alias['subgroup_id'' = G.' .
  340.                     $this->alias['group_id''
  341.             AND
  342.                 SG.' $this->alias['group_id'' IN ('.
  343.                     implode(', '$newGroupIds).')
  344.             AND
  345.                 SG.' $this->alias['subgroup_id'' NOT IN ('.
  346.                     implode(', '$group_ids).')';
  347.  
  348.         if (array_key_exists('is_active'$this->tables['groups']['fields'])) {
  349.             $query .= ' AND
  350.                 G.' $this->alias['is_active''=' .
  351.                     $this->dbc->getValue($this->fields['is_active']true);
  352.         }
  353.  
  354.         $result $this->dbc->queryCol($query$this->fields['group_id']);
  355.  
  356.         if (PEAR::isError($result)) {
  357.             $this->stack->push(LIVEUSER_ERROR'exception'array(),
  358.                 'error in query' $result->getMessage('-' $result->getUserInfo());
  359.             return false;
  360.         }
  361.  
  362.         return $result;
  363.     }
  364.  
  365.     /**
  366.      * Read out the rights from the userrights or grouprights table
  367.      * that imply other rights along with their level
  368.      *
  369.      * @param array right ids
  370.      * @param string name of the table
  371.      * @return array requested data or false on failure
  372.      *
  373.      * @access public
  374.      */
  375.     function readImplyingRights($rightIds$table)
  376.     {
  377.         $query '
  378.             SELECT
  379.             DISTINCT
  380.                 TR.' $this->alias['right_level'',
  381.                 TR.' $this->alias['right_id''
  382.             FROM
  383.                 '.$this->prefix.$this->alias['rights'].' R,
  384.                 '.$this->prefix.$this->alias[$table.'rights'].' TR
  385.             WHERE
  386.                 TR.' $this->alias['right_id'' = R.' $this->alias['right_id''
  387.             AND
  388.                 R.' $this->alias['right_id'' IN ('.
  389.                     implode(', 'array_keys($rightIds)).')
  390.             AND
  391.                 R.' $this->alias['has_implied''='.
  392.                     $this->dbc->getValue($this->fields['has_implied']true);
  393.  
  394.         $types = array(
  395.             $this->fields['right_level'],
  396.             $this->fields['right_id'],
  397.         );
  398.         $result $this->dbc->queryAll($query$typesMDB_FETCHMODE_ORDEREDtruefalsetrue);
  399.  
  400.         if (PEAR::isError($result)) {
  401.             $this->stack->push(LIVEUSER_ERROR'exception'array(),
  402.                 'error in query' $result->getMessage('-' $result->getUserInfo());
  403.             return false;
  404.         }
  405.  
  406.         return $result;
  407.     }
  408.  
  409.     /**
  410.     * Read out the implied rights with a given level from the implied_rights table
  411.     *
  412.     * @param array current right ids
  413.     * @param string current level
  414.      * @return array requested data or false on failure
  415.     *
  416.     * @access public
  417.     */
  418.     function readImpliedRights($currentRights$currentLevel)
  419.     {
  420.         $query '
  421.             SELECT
  422.                 RI.' $this->alias['implied_right_id'' AS right_id,
  423.                 '.$currentLevel.'                           AS right_level,
  424.                 R.' $this->alias['has_implied''       AS has_implied
  425.             FROM
  426.                 '.$this->prefix.$this->alias['rights'].' R,
  427.                 '.$this->prefix.$this->alias['right_implied'].' RI
  428.             WHERE
  429.                 RI.' $this->alias['implied_right_id'' = R.' $this->alias['right_id''
  430.             AND
  431.                 RI.' $this->alias['right_id'' IN ('.
  432.                     implode(', '$currentRights).')';
  433.  
  434.         $types = array(
  435.             $this->fields['right_id'],
  436.             $this->fields['right_level'],
  437.             $this->fields['has_implied']
  438.         );
  439.         $result $this->dbc->queryAll($query$typesMDB_FETCHMODE_ASSOC);
  440.  
  441.         if (PEAR::isError($result)) {
  442.             $this->stack->push(LIVEUSER_ERROR'exception'array(),
  443.                 'error in query' $result->getMessage('-' $result->getUserInfo());
  444.             return false;
  445.         }
  446.  
  447.         return $result;
  448.     }
  449. }
  450. ?>

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