Apache Zeta Components Manual :: File Source for database_filter.php

Source for file database_filter.php

Documentation is available at database_filter.php

  1. <?php
  2. /**
  3.  * File containing the ezcAuthenticationDatabaseFilter class.
  4.  *
  5.  * Licensed to the Apache Software Foundation (ASF) under one
  6.  * or more contributor license agreements.  See the NOTICE file
  7.  * distributed with this work for additional information
  8.  * regarding copyright ownership.  The ASF licenses this file
  9.  * to you under the Apache License, Version 2.0 (the
  10.  * "License"); you may not use this file except in compliance
  11.  * with the License.  You may obtain a copy of the License at
  12.  * 
  13.  *   http://www.apache.org/licenses/LICENSE-2.0
  14.  * 
  15.  * Unless required by applicable law or agreed to in writing,
  16.  * software distributed under the License is distributed on an
  17.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18.  * KIND, either express or implied.  See the License for the
  19.  * specific language governing permissions and limitations
  20.  * under the License.
  21.  *
  22.  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  23.  * @filesource
  24.  * @package AuthenticationDatabaseTiein
  25.  * @version //autogentag//
  26.  */
  27.  
  28. /**
  29.  * Filter to authenticate against a database.
  30.  *
  31.  * The database instance to use is specified using a ezcAuthenticationDatabaseInfo
  32.  * structure. Table name and field names are specified in the same structure.
  33.  *
  34.  * Example:
  35.  * <code>
  36.  * $credentials = new ezcAuthenticationPasswordCredentials( 'jan.modaal', 'b1b3773a05c0ed0176787a4f1574ff0075f7521e' );
  37.  * $database = new ezcAuthenticationDatabaseInfo( ezcDbInstance::get(), 'users', array( 'user', 'password' ) );
  38.  * $authentication = new ezcAuthentication( $credentials );
  39.  * $authentication->addFilter( new ezcAuthenticationDatabaseFilter( $database ) );
  40.  * if ( !$authentication->run() )
  41.  * {
  42.  *     // authentication did not succeed, so inform the user
  43.  *     $status = $authentication->getStatus();
  44.  *     $err = array(
  45.  *              'ezcAuthenticationDatabaseFilter' => array(
  46.  *                  ezcAuthenticationDatabaseFilter::STATUS_USERNAME_INCORRECT => 'Incorrect username',
  47.  *                  ezcAuthenticationDatabaseFilter::STATUS_PASSWORD_INCORRECT => 'Incorrect password'
  48.  *                  )
  49.  *              );
  50.  *     foreach ( $status as $line )
  51.  *     {
  52.  *         list( $key, $value ) = each( $line );
  53.  *         echo $err[$key][$value] . "\n";
  54.  *     }
  55.  * }
  56.  * else
  57.  * {
  58.  *     // authentication succeeded, so allow the user to see his content
  59.  * }
  60.  * </code>
  61.  *
  62.  * Extra data can be fetched from the database during the authentication process,
  63.  * by registering the data to be fetched before calling run(). Example:
  64.  * <code>
  65.  * // $filter is an ezcAuthenticationDatabaseFilter object
  66.  * $filter->registerFetchData( array( 'name', 'country' ) );
  67.  *
  68.  * // after run()
  69.  * $data = $filter->fetchData();
  70.  * </code>
  71.  *
  72.  * The $data array will be something like:
  73.  * <code>
  74.  * array( 'name' => array( 'John Doe' ),
  75.  *        'country' => array( 'US' )
  76.  *      );
  77.  * </code>
  78.  *
  79.  * @property ezcAuthenticationDatabaseInfo $database 
  80.  *            Structure which holds a database instance, table name and fields
  81.  *            which are used for authentication.
  82.  *
  83.  * @package AuthenticationDatabaseTiein
  84.  * @version //autogentag//
  85.  * @mainclass
  86.  */
  87. {
  88.     /**
  89.      * Username is not found in the database.
  90.      */
  91.     const STATUS_USERNAME_INCORRECT = 1;
  92.  
  93.     /**
  94.      * Password is incorrect.
  95.      */
  96.     const STATUS_PASSWORD_INCORRECT = 2;
  97.  
  98.     /**
  99.      * Holds the attributes which will be requested during the authentication
  100.      * process.
  101.      *
  102.      * Usually it has this structure:
  103.      * <code>
  104.      * array( 'fullname', 'gender', 'country', 'language' );
  105.      * </code>
  106.      *
  107.      * @var array(string) 
  108.      */
  109.     protected $requestedData = array();
  110.  
  111.     /**
  112.      * Holds the extra data fetched during the authentication process.
  113.      *
  114.      * Usually it has this structure:
  115.      * <code>
  116.      * array( 'name' => array( 'John Doe' ),
  117.      *        'country' => array( 'US' )
  118.      *      );
  119.      * </code>
  120.      *
  121.      * @var array(string=>mixed) 
  122.      */
  123.     protected $data = array();
  124.  
  125.     /**
  126.      * Holds the properties of this class.
  127.      *
  128.      * @var array(string=>mixed) 
  129.      */
  130.     private $properties array();
  131.  
  132.     /**
  133.      * Creates a new object of this class.
  134.      *
  135.      * @param ezcAuthenticationDatabaseInfo $database Database to use in authentication
  136.      * @param ezcAuthenticationDatabaseOptions $options Options for this class
  137.      */
  138.     public function __constructezcAuthenticationDatabaseInfo $databaseezcAuthenticationDatabaseOptions $options null )
  139.     {
  140.         $this->options = $options === null new ezcAuthenticationDatabaseOptions($options;
  141.         $this->database $database;
  142.     }
  143.  
  144.     /**
  145.      * Sets the property $name to $value.
  146.      *
  147.      * @throws ezcBasePropertyNotFoundException
  148.      *          if the property $name does not exist
  149.      * @throws ezcBaseValueException
  150.      *          if $value is not correct for the property $name
  151.      * @param string $name The name of the property to set
  152.      * @param mixed $value The new value of the property
  153.      * @ignore
  154.      */
  155.     public function __set$name$value )
  156.     {
  157.         switch $name )
  158.         {
  159.             case 'database':
  160.                 if $value instanceof ezcAuthenticationDatabaseInfo )
  161.                 {
  162.                     $this->properties[$name$value;
  163.                 }
  164.                 else
  165.                 {
  166.                     throw new ezcBaseValueException$name$value'ezcAuthenticationDatabaseInfo' );
  167.                 }
  168.                 break;
  169.  
  170.             default:
  171.                 throw new ezcBasePropertyNotFoundException$name );
  172.         }
  173.     }
  174.  
  175.     /**
  176.      * Returns the value of the property $name.
  177.      *
  178.      * @throws ezcBasePropertyNotFoundException
  179.      *          if the property $name does not exist
  180.      * @param string $name The name of the property for which to return the value
  181.      * @return mixed 
  182.      * @ignore
  183.      */
  184.     public function __get$name )
  185.     {
  186.         switch $name )
  187.         {
  188.             case 'database':
  189.                 return $this->properties[$name];
  190.  
  191.             default:
  192.                 throw new ezcBasePropertyNotFoundException$name );
  193.         }
  194.     }
  195.  
  196.     /**
  197.      * Returns true if the property $name is set, otherwise false.
  198.      *
  199.      * @param string $name The name of the property to test if it is set
  200.      * @return bool 
  201.      * @ignore
  202.      */
  203.     public function __isset$name )
  204.     {
  205.         switch $name )
  206.         {
  207.             case 'database':
  208.                 return isset$this->properties[$name);
  209.  
  210.             default:
  211.                 return false;
  212.         }
  213.     }
  214.  
  215.     /**
  216.      * Runs the filter and returns a status code when finished.
  217.      *
  218.      * @param ezcAuthenticationPasswordCredentials $credentials Authentication credentials
  219.      * @return int 
  220.      */
  221.     public function run$credentials )
  222.     {
  223.         $db $this->database;
  224.  
  225.         // see if username exists
  226.         $query new ezcQuerySelect$db->instance );
  227.         $e $query->expr;
  228.         $query->select'COUNT( ' $db->instance->quoteIdentifier$db->fields[0' )' )
  229.               ->from$db->instance->quoteIdentifier$db->table ) )
  230.               ->where(
  231.                   $e->eq$db->instance->quoteIdentifier$db->fields[0)$query->bindValue$credentials->id ) )
  232.                      );
  233.         $rows $query->prepare();
  234.         $rows->execute();
  235.         $count = (int)$rows->fetchColumn);
  236.         if $count === )
  237.         {
  238.             return self::STATUS_USERNAME_INCORRECT;
  239.         }
  240.         $rows->closeCursor();
  241.  
  242.         // see if username has the specified password
  243.         $query new ezcQuerySelect$db->instance );
  244.         $e $query->expr;
  245.         $query->select'COUNT( ' $db->instance->quoteIdentifier$db->fields[0'  )' )
  246.               ->from$db->instance->quoteIdentifier$db->table ) )
  247.               ->where$e->lAnd(
  248.                   $e->eq$db->instance->quoteIdentifier$db->fields[0)$query->bindValue$credentials->id ) ),
  249.                   $e->eq$db->instance->quoteIdentifier$db->fields[1)$query->bindValue$credentials->password ) )
  250.                      ) );
  251.         $rows $query->prepare();
  252.         $rows->execute();
  253.         $count = (int)$rows->fetchColumn);
  254.         if $count === )
  255.         {
  256.             return self::STATUS_PASSWORD_INCORRECT;
  257.         }
  258.         $rows->closeCursor();
  259.  
  260.         if count$this->requestedData )
  261.         {
  262.             // fetch extra data from the database
  263.             $query new ezcQuerySelect$db->instance );
  264.             $e $query->expr;
  265.             $params array();
  266.             foreach $this->requestedData as $param )
  267.             {
  268.                 $params[$db->instance->quoteIdentifier$param );
  269.             }
  270.             $query->selectimplode', '$params ) )
  271.                   ->from$db->instance->quoteIdentifier$db->table ) )
  272.                   ->where$e->lAnd(
  273.                       $e->eq$db->instance->quoteIdentifier$db->fields[0)$query->bindValue$credentials->id ) ),
  274.                       $e->eq$db->instance->quoteIdentifier$db->fields[1)$query->bindValue$credentials->password ) )
  275.                          ) );
  276.             $rows $query->prepare();
  277.             $rows->execute();
  278.             $data $rows->fetchAll();
  279.             $data $data[0];
  280.  
  281.             foreach $this->requestedData as $attribute )
  282.             {
  283.                 $this->data[$attributearray$data[$attribute);
  284.             }
  285.         }
  286.  
  287.         return self::STATUS_OK;
  288.     }
  289.  
  290.     /**
  291.      * Registers the extra data which will be fetched by the filter during the
  292.      * authentication process.
  293.      *
  294.      * The input $data should be an array of attributes, for example:
  295.      * <code>
  296.      * array( 'name', 'country' );
  297.      * </code>
  298.      *
  299.      * @param array(string) $data The extra data to fetch during authentication
  300.      */
  301.     public function registerFetchDataarray $data array() )
  302.     {
  303.         $this->requestedData = $data;
  304.     }
  305.  
  306.     /**
  307.      * Returns the extra data which was fetched during the authentication process.
  308.      *
  309.      * Example of returned array:
  310.      * <code>
  311.      * array( 'name' => array( 'John Doe' ),
  312.      *        'country' => array( 'US' )
  313.      *      );
  314.      * </code>
  315.      *
  316.      * @return array(string=>mixed) 
  317.      */
  318.     public function fetchData()
  319.     {
  320.         return $this->data;
  321.     }
  322. }
  323. ?>
Documentation generated by phpDocumentor 1.4.3