Author: as
Date: Wed Aug 8 16:41:51 2007
New Revision: 5850
Log:
- Implemented feature request #10971: Added the possibility to fetch extra
data during authentication for Database, LDAP, OpenID and TypeKey filters.
Modified:
trunk/AuthenticationDatabaseTiein/ChangeLog
trunk/AuthenticationDatabaseTiein/docs/tutorial.txt
trunk/AuthenticationDatabaseTiein/src/filters/database/database_filter.php
trunk/AuthenticationDatabaseTiein/tests/filters/database/database_test.php
Modified: trunk/AuthenticationDatabaseTiein/ChangeLog
==============================================================================
--- trunk/AuthenticationDatabaseTiein/ChangeLog [iso-8859-1] (original)
+++ trunk/AuthenticationDatabaseTiein/ChangeLog [iso-8859-1] Wed Aug 8
16:41:51 2007
@@ -3,6 +3,8 @@
- Implemented feature request #10998: Added a Database backend for OpenID
authentication.
+- Implemented feature request #10971: Added the possibility to fetch extra
+ data during authentication for Database, LDAP, OpenID and TypeKey filters.
1.0 - Monday 02 July 2007
Modified: trunk/AuthenticationDatabaseTiein/docs/tutorial.txt
==============================================================================
--- trunk/AuthenticationDatabaseTiein/docs/tutorial.txt [iso-8859-1] (original)
+++ trunk/AuthenticationDatabaseTiein/docs/tutorial.txt [iso-8859-1] Wed Aug 8
16:41:51 2007
@@ -97,6 +97,28 @@
content.
+Fetch extra data during Database authentication
+```````````````````````````````````````````````
+
+Any value from the table which holds the users can be fetched. The exact column
+names must be specified. Example: ::
+
+ // $filter is an ezcAuthenticationDatabaseFilter object
+ $filter->registerFetchData( array( 'name', 'country' ) );
+
+After the authentication process is finished (after run()), retrieve the extra
+data: ::
+
+ // $filter is an ezcAuthenticationDatabaseFilter object
+ $data = $filter->fetchData();
+
+For the previous example, the $data array will be something like this: ::
+
+ array( 'name' => array( 'John Doe' ),
+ 'country' => array( 'US' )
+ );
+
+
OpenID
------
Modified:
trunk/AuthenticationDatabaseTiein/src/filters/database/database_filter.php
==============================================================================
--- trunk/AuthenticationDatabaseTiein/src/filters/database/database_filter.php
[iso-8859-1] (original)
+++ trunk/AuthenticationDatabaseTiein/src/filters/database/database_filter.php
[iso-8859-1] Wed Aug 8 16:41:51 2007
@@ -43,6 +43,23 @@
* }
* </code>
*
+ * Extra data can be fetched from the database during the authentication
process,
+ * by registering the data to be fetched before calling run(). Example:
+ * <code>
+ * // $filter is an ezcAuthenticationDatabaseFilter object
+ * $filter->registerFetchData( array( 'name', 'country' ) );
+ *
+ * // after run()
+ * $data = $filter->fetchData();
+ * </code>
+ *
+ * The $data array will be something like:
+ * <code>
+ * array( 'name' => array( 'John Doe' ),
+ * 'country' => array( 'US' )
+ * );
+ * </code>
+ *
* @property ezcAuthenticationDatabaseInfo $database
* Structure which holds a database instance, table name and fields
* which are used for authentication.
@@ -51,7 +68,7 @@
* @version //autogen//
* @mainclass
*/
-class ezcAuthenticationDatabaseFilter extends ezcAuthenticationFilter
+class ezcAuthenticationDatabaseFilter extends ezcAuthenticationFilter
implements ezcAuthenticationDataFetch
{
/**
* Username is not found in the database.
@@ -62,6 +79,33 @@
* Password is incorrect.
*/
const STATUS_PASSWORD_INCORRECT = 2;
+
+ /**
+ * Holds the attributes which will be requested during the authentication
+ * process.
+ *
+ * Usually it has this structure:
+ * <code>
+ * array( 'fullname', 'gender', 'country', 'language' );
+ * </code>
+ *
+ * @var array(string)
+ */
+ protected $requestedData = array();
+
+ /**
+ * Holds the extra data fetched during the authentication process.
+ *
+ * Usually it has this structure:
+ * <code>
+ * array( 'name' => array( 'John Doe' ),
+ * 'country' => array( 'US' )
+ * );
+ * </code>
+ *
+ * @var array(string=>mixed)
+ */
+ protected $data = array();
/**
* Holds the properties of this class.
@@ -196,7 +240,62 @@
return self::STATUS_PASSWORD_INCORRECT;
}
+ if ( count( $this->requestedData ) > 0 )
+ {
+ // fetch extra data from the database
+ $query = new ezcQuerySelect( $db->instance );
+ $e = $query->expr;
+ $query->select( implode( ',', $this->requestedData ) )
+ ->from( $db->instance->quoteIdentifier( $db->table ) )
+ ->where( $e->lAnd(
+ $e->eq( $db->instance->quoteIdentifier( $db->fields[0]
), $query->bindValue( $credentials->id ) ),
+ $e->eq( $db->instance->quoteIdentifier( $db->fields[1]
), $query->bindValue( $credentials->password ) )
+ ) );
+ $rows = $query->prepare();
+ $rows->execute();
+ $data = $rows->fetchAll();
+ $data = $data[0];
+
+ foreach ( $this->requestedData as $attribute )
+ {
+ $this->data[$attribute] = array( $data[$attribute] );
+ }
+ }
+
return self::STATUS_OK;
+ }
+
+ /**
+ * Registers the extra data which will be fetched by the filter during the
+ * authentication process.
+ *
+ * The input $data should be an array of attributes, for example:
+ * <code>
+ * array( 'name', 'country' );
+ * </code>
+ *
+ * @param array(string) $data The extra data to fetch during authentication
+ */
+ public function registerFetchData( array $data = array() )
+ {
+ $this->requestedData = $data;
+ }
+
+ /**
+ * Returns the extra data which was fetched during the authentication
process.
+ *
+ * Example of returned array:
+ * <code>
+ * array( 'name' => array( 'John Doe' ),
+ * 'country' => array( 'US' )
+ * );
+ * </code>
+ *
+ * @return array(string=>mixed)
+ */
+ public function fetchData()
+ {
+ return $this->data;
}
}
?>
Modified:
trunk/AuthenticationDatabaseTiein/tests/filters/database/database_test.php
==============================================================================
--- trunk/AuthenticationDatabaseTiein/tests/filters/database/database_test.php
[iso-8859-1] (original)
+++ trunk/AuthenticationDatabaseTiein/tests/filters/database/database_test.php
[iso-8859-1] Wed Aug 8 16:41:51 2007
@@ -21,7 +21,8 @@
public static $fieldId = 'uniqueid';
public static $fieldUser = 'username';
public static $fieldPassword = 'pass';
-
+ public static $fieldName = 'name';
+ public static $fieldCountry = 'country';
public static function suite()
{
@@ -43,6 +44,8 @@
self::$fieldId => new
ezcDbSchemaField( 'integer', false, true, null, true ),
self::$fieldUser => new
ezcDbSchemaField( 'text', 32, true ),
self::$fieldPassword => new
ezcDbSchemaField( 'text', 64, true ),
+ self::$fieldName => new ezcDbSchemaField(
'text', 64, true ),
+ self::$fieldCountry => new
ezcDbSchemaField( 'text', 32, true )
),
array (
self::$fieldUser => new ezcDbSchemaIndex(
array ( self::$fieldUser => new ezcDbSchemaIndexField() ), false, false ),
@@ -67,7 +70,9 @@
$query->insertInto( $this->db->quoteIdentifier( self::$table ) )
->set( $this->db->quoteIdentifier( self::$fieldId ),
$query->bindValue( '1' ) )
->set( $this->db->quoteIdentifier( self::$fieldUser ),
$query->bindValue( 'jan.modaal' ) )
- ->set( $this->db->quoteIdentifier( self::$fieldPassword ),
$query->bindValue( sha1( 'qwerty' ) ) );
+ ->set( $this->db->quoteIdentifier( self::$fieldPassword ),
$query->bindValue( sha1( 'qwerty' ) ) )
+ ->set( $this->db->quoteIdentifier( self::$fieldName ),
$query->bindValue( 'Jan Modaal' ) )
+ ->set( $this->db->quoteIdentifier( self::$fieldCountry ),
$query->bindValue( 'NL' ) );
$stmt = $query->prepare();
$stmt->execute();
@@ -75,7 +80,9 @@
$query->insertInto( $this->db->quoteIdentifier( self::$table ) )
->set( $this->db->quoteIdentifier( self::$fieldId ),
$query->bindValue( '2' ) )
->set( $this->db->quoteIdentifier( self::$fieldUser ),
$query->bindValue( 'john.doe' ) )
- ->set( $this->db->quoteIdentifier( self::$fieldPassword ),
$query->bindValue( crypt( 'foobar', 'jo' ) ) );
+ ->set( $this->db->quoteIdentifier( self::$fieldPassword ),
$query->bindValue( crypt( 'foobar', 'jo' ) ) )
+ ->set( $this->db->quoteIdentifier( self::$fieldName ),
$query->bindValue( 'John Doe' ) )
+ ->set( $this->db->quoteIdentifier( self::$fieldCountry ),
$query->bindValue( 'US' ) );
$stmt = $query->prepare();
$stmt->execute();
@@ -83,7 +90,9 @@
$query->insertInto( $this->db->quoteIdentifier( self::$table ) )
->set( $this->db->quoteIdentifier( self::$fieldId ),
$query->bindValue( '3' ) )
->set( $this->db->quoteIdentifier( self::$fieldUser ),
$query->bindValue( 'zhang.san' ) )
- ->set( $this->db->quoteIdentifier( self::$fieldPassword ),
$query->bindValue( md5( 'asdfgh' ) ) );
+ ->set( $this->db->quoteIdentifier( self::$fieldPassword ),
$query->bindValue( md5( 'asdfgh' ) ) )
+ ->set( $this->db->quoteIdentifier( self::$fieldName ),
$query->bindValue( 'Zhang San' ) )
+ ->set( $this->db->quoteIdentifier( self::$fieldCountry ),
$query->bindValue( 'CN' ) );
$stmt = $query->prepare();
$stmt->execute();
@@ -91,7 +100,10 @@
$query->insertInto( $this->db->quoteIdentifier( self::$table ) )
->set( $this->db->quoteIdentifier( self::$fieldId ),
$query->bindValue( '4' ) )
->set( $this->db->quoteIdentifier( self::$fieldUser ),
$query->bindValue( 'hans.mustermann' ) )
- ->set( $this->db->quoteIdentifier( self::$fieldPassword ),
$query->bindValue( 'abcdef' ) );
+ ->set( $this->db->quoteIdentifier( self::$fieldPassword ),
$query->bindValue( 'abcdef' ) )
+ ->set( $this->db->quoteIdentifier( self::$fieldName ),
$query->bindValue( 'Hans Mustermann' ) )
+ ->set( $this->db->quoteIdentifier( self::$fieldCountry ),
$query->bindValue( 'DE' ) );
+
$stmt = $query->prepare();
$stmt->execute();
@@ -195,6 +207,24 @@
$authentication = new ezcAuthentication( $credentials );
$authentication->addFilter( new ezcAuthenticationDatabaseFilter(
$database ) );
$this->assertEquals( false, $authentication->run() );
+ }
+
+ public function testDatabaseFetchData()
+ {
+ $credentials = new ezcAuthenticationPasswordCredentials( 'john.doe',
'joB9EZ4O1cXDk' );
+ $database = new ezcAuthenticationDatabaseInfo( $this->db,
self::$table, array( self::$fieldUser, self::$fieldPassword ) );
+ $authentication = new ezcAuthentication( $credentials );
+
+ $filter = new ezcAuthenticationDatabaseFilter( $database );
+ $filter->registerFetchData( array( 'name', 'country' ) );
+
+ $authentication->addFilter( $filter );
+ $this->assertEquals( true, $authentication->run() );
+
+ $expected = array( 'name' => array( 'John Doe' ),
+ 'country' => array( 'US' )
+ );
+ $this->assertEquals( $expected, $filter->fetchData() );
}
public function testDatabaseInfo()
--
svn-components mailing list
[email protected]
http://lists.ez.no/mailman/listinfo/svn-components