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

Source for file PDO.php

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

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