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

Source for file MDB2.php

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

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