Login handler

A login handler handles authentication of a user. The login handler is provided with a login id and a password, and returns a valid eZ publish user for the system to
use. This information can be used to authenticate against an external system.

An eZ publish installation can use multiple loginhandlers. The loginhandler to be used is specified in the site.ini[LoginHandler].LoginHandler array. eZ publish will try to authenticate the users with all the loginhandlers in this array. The first one that returns a valid eZ publish user will be used.

The main parts of an login handler is

  • Settings
  • Implementation of the actual login handler

Example code

extension/myloginext/settings/site.ini.append.php

 

Explanation:

We first specify that we have an extension myloginext that contains an login-handler. Then we clear the loginhandler array, and set it to use our login-handler. Be warned that this disables the standard loginhandler.

extension/myloginext/login_handler/ezmyloginuser.php

<?php
include_once( "kernel/classes/datatypes/ezuser/ezusersetting.php" );
include_once( "kernel/classes/datatypes/ezuser/ezuser.php" );
include_once( 'lib/ezutils/classes/ezini.php' );

class eZMyLoginUser extends eZUser
{
    /*!
     Constructor
    */
    function eZMyLoginUser()
    {
    }

    /*!
    \static
     Logs in the user if applied username and password is
     valid. The userID is returned if succesful, false if not.
    */
    function &loginUser( $login, $password, $authenticationMatch = false )
    {
        $userID = 14;

        $user = eZUser::fetch( $userID );
        eZUser::updateLastVisit( $userID );
        eZUser::setCurrentlyLoggedInUser( $user, $userID );

        return $user;
    }
}

?>

Explanation:

Our PHP-class must extend from the eZUser class and implement the loginUser function. Notice that the loginhandler class must be called "eZ" + name from setting + "User", in this case "eZMyLoginUser". What this extension does is to fetch the user with id 14 (the default admin user), update the timestamp for last visit, set the currently logged in user to this user, and return the user.

Use this extension only as an example, since it logs in all users (regardless of password and username) as the administrator user.

Extensions containing login handlers

Troubleshooting

If eZ Publish doesn't seem to want to load your login handler, then you might want to check out some useful tips to get it to work at the Troubleshooting extensions page.