Simple Db Adapter Zend_Auth using Zend_Db instance

1 message Options
Embed this post
Permalink
unknownman

Simple Db Adapter Zend_Auth using Zend_Db instance

Reply Threaded More More options
Print post
Permalink
This is a simple Db Adapter for Authentication using Zend_Auth_Adapter_Interface that manage authentication using Zend_Db instance

<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to [hidden email] so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Auth
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */

// loading Zend_Auth_Adapter_Interface class
require_once 'Zend/Auth/Adapter/Interface.php';

/**
 * This class is an adapter for db type authentication service
 *
 * This class get username , password, db handler and db schematic
 * and then authenticate using Zend_Db plugin.<br/>
 * -example
 * $db = new Zend_Db('PDO_MYSQL', $params);
 * requrie_once 'Zend/Widget/Auth/Adapter/Db.php';
 * $auth = new Zend_Widget_Auth_Adapter_Db('username', '******');
 * $auth->setDb($db, array(
 *                             'tbl'=>'TableName',
 *                             'user'=>'userField_name',
 *                          'pass'=>'passField_name',
 *                             'uid'=>'uidField_name'
 *                             ));
 * $status = $auth->authenticate();
 * if(!$status->isValid())
 *     echo 'Error in Authentication!!!';
 * else
 *     echo 'Authenticate successfully!';
 *
 * @category   Zend
 * @package    Zend_Auth
 * @license    http://framework.zend.com/license/new-bsd      New BSD License
 */
class Zend_Widget_Auth_Adapter_Db implements Zend_Auth_Adapter_Interface
{
    /**
     * username
     *
     * @var string
     */
    protected    $username;
    /**
     * password
     *
     * @var string
     */
    protected     $password;
    /**
     * an instance to Zend_Db plugin
     *
     * @var Zend_Db
     */
    protected     $dbHandler;
    /**
     * Schematic of user and password table
     *
     * @var array
     */
    protected     $dbSchema;
   
    /**
     * Constructor
     *
     * Sets username and password for authentication
     *
     * @return void
     */
    public function __construct($username, $password)
    {
        $this->useranme = $username;
        $this->password = $password;
    }

    /**
     * Performs an authentication attempt
     *
     * @return     Zend_Auth_Result                an instance of Zend_Auth_Result
     */
    public function authenticate()
    {
        $select = $this->dbHandler->select();
        // check existing of user in database
        $select->from(
                      $this->dbSchema['tbl'],
                      array(
                              $this->dbSchema['uid'],
                              $this->dbSchema['user']
                           )
                     );
        $select->where($this->dbSchema['user'].' = ?', $this->username);
        $select->where($this->dbSchema['pass'].' = ?', $this->password);
        $result = $select->query()->fetchAll();
        // set Zend_Auth_Result values
        if(!count($result)) {
            return new Zend_Auth_Result(false, null, 'There is no match for indicated username and password');
        } else {
            return new Zend_Auth_Result(
                                        true,
                                        $result[0][$this->dbSchema['user']],
                                        'Authentication Successed'
                                        );
        }
    }
   
    /**
     * set database for using
     *
     * This method is used to db interface for transaction
     * You must passed your default db handler to it. this
     * db handler must provided by Zend_Db plugin
     *
     * @param     Zend_Db     an interface to Zend Db Adapter
     * @param     array         schematic of database
     * @return     void
     */
    public function setDb(Zend_Db $dbHandler, $dbSchema) {
        $this->dbHandler    = $dbHandler;
        $this->dbSchema      = $dbSchema;
    }
}

__________________________________

Best Regards
Ali Masoudi