Path

ez components / documentation / api reference / 2007.1 / authentication


eZ Components 2007.1

AuthenticationDatabaseTiein

[ Tutorial ] [ Class tree ] [ Element index ] [ ChangeLog ] [ Credits ]

Introduction

Description

The purpose of the Authentication component is to provide support for different means of identification and authentication of users using different providers and protocols.

AuthenticationDatabaseTiein provides a Database filter for the Authentication component by using the Database component.

Class overview

An overview of the most important classes in the Authentication component.

Base classes

ezcAuthentication
Main class of Authentication. It is a container for authentication filters, which will be run in sequence. The method run() returns true or false depending on the success of the authentication filters.
ezcAuthenticationCredentials
Structure which holds user credentials. Types are id credentials (ezcAuthenticationIdCredentials) and id + password credentials (ezcAuthenticationPasswordCredentials).

Authentication filters

ezcAuthenticationDatabaseFilter
Filter to authenticate against a database. Uses a database instance provided by the Database component (via the ezcDbInstance::get() function).

Authentication filters

Database

The following example shows how to authenticate against a database.

  1. <?php
  2. require_once 'tutorial_autoload.php';
  3. 
  4. $credentials = new ezcAuthenticationPasswordCredentials'jan.modaal''b1b3773a05c0ed0176787a4f1574ff0075f7521e' );
  5. $database = new ezcAuthenticationDatabaseInfoezcDbInstance::get(), 'users', array( 'user''password' ) );
  6. $authentication = new ezcAuthentication$credentials );
  7. $authentication->addFilter( new ezcAuthenticationDatabaseFilter$database ) );
  8. if ( !$authentication->run() )
  9. {
 10.     // authentication did not succeed, so inform the user
 11.     $status $authentication->getStatus();
 12.     $err = array(
 13.              'ezcAuthenticationDatabaseFilter=> array(
 14.                  ezcAuthenticationDatabaseFilter::STATUS_USERNAME_INCORRECT => 'Incorrect username',
 15.                  ezcAuthenticationDatabaseFilter::STATUS_PASSWORD_INCORRECT => 'Incorrect password'
 16.                  )
 17.              );
 18.     foreach ( $status as $line )
 19.     {
 20.         list( $key$value ) = each$line );
 21.         echo $err[$key][$value] . "\n";
 22.     }
 23. }
 24. else
 25. {
 26.     // authentication succeeded, so allow the user to see his content
 27. }
 28. ?>

First, a credentials object is created with username jan.modaal and password 'b1b3773a05c0ed0176787a4f1574ff0075f7521e' (sha1() hash).

An authentication object is created using the credentials object, and a Database filter is added to it. The $database structure specifies the database instance (ezcDbInstance::get()), the table name ('users') and the username and password fields in the table ('user', 'password').

After running the authentication (line 8), if the username and the password do not pass through the Database filter, then the credentials are incorrect and the user must be informed. The getStatus() method is used for this. The values in the status returned must be cycled through and for each value a response is created for the user ("Username incorrect", "Password incorrect").

If run() returned true (line 24) then the user is logged-in and he can see his content.

Securing applications

Securing applications - A guide to improve the security of online applications. It is not exhaustive, but it provides solutions against common attacks.

Last updated: Thu, 01 Nov 2007