/standard/tags/release-0.9.0/library/Zend/Auth/Adapter/DbTable.php
https://github.com/bhaumik25/zend-framework · PHP · 316 lines · 124 code · 40 blank · 152 comment · 15 complexity · 58c50931aacc40025e02145212145a62 MD5 · raw file
- <?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 license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Auth
- * @subpackage Zend_Auth_Adapter
- * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- /**
- * @see Zend_Auth_Adapter_Interface
- */
- require_once 'Zend/Auth/Adapter/Interface.php';
- /**
- * @see Zend_Db_Adapter_Abstract
- */
- require_once 'Zend/Db/Adapter/Abstract.php';
- /**
- * @category Zend
- * @package Zend_Auth
- * @subpackage Zend_Auth_Adapter
- * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
- class Zend_Auth_Adapter_DbTable implements Zend_Auth_Adapter_Interface
- {
- /**
- * Database Connection
- *
- * @var Zend_Db_Adapter_Abstract
- */
- protected $_zendDb = null;
- /**
- * $_tableName - the table name to check
- *
- * @var string
- */
- protected $_tableName = null;
- /**
- * $_identityColumn - the column to use as the identity
- *
- * @var string
- */
- protected $_identityColumn = null;
- /**
- * $_credentialColumns - columns to be used as the credentials
- *
- * @var string
- */
- protected $_credentialColumn = null;
- /**
- * $_identity - Identity value
- *
- * @var string
- */
- protected $_identity = null;
- /**
- * $_credential - Credential values
- *
- * @var string
- */
- protected $_credential = null;
- /**
- * $_credentialTreatment - Treatment applied to the credential, such as MD5() or PASSWORD()
- *
- * @var string
- */
- protected $_credentialTreatment = null;
- /**
- * $_resultRow - Results of database authentication query
- *
- * @var array
- */
- protected $_resultRow = null;
- /**
- * __construct() - Sets configuration options
- *
- * @param Zend_Db_Adapter_Abstract $zendDb
- * @param string $tableName
- * @param string $identityColumn
- * @param string $credentialColumn
- * @param string $credentialTreatment
- * @return void
- */
- public function __construct(Zend_Db_Adapter_Abstract $zendDb, $tableName = null, $identityColumn = null,
- $credentialColumn = null, $credentialTreatment = null)
- {
- $this->_zendDb = $zendDb;
- if (null !== $tableName) {
- $this->setTableName($tableName);
- }
- if (null !== $identityColumn) {
- $this->setIdentityColumn($identityColumn);
- }
- if (null !== $credentialColumn) {
- $this->setCredentialColumn($credentialColumn);
- }
- if (null !== $credentialTreatment) {
- $this->setCredentialTreatment($credentialTreatment);
- }
- }
- /**
- * setTableName() - set the table name to be used in the select query
- *
- * @param string $tableName
- * @return Zend_Auth_Adapter_DbTable
- */
- public function setTableName($tableName)
- {
- $this->_tableName = $tableName;
- return $this;
- }
- /**
- * setIdentityColumn() - set the column name to be used as the identity column
- *
- * @param string $identityColumn
- * @return Zend_Auth_Adapter_DbTable
- */
- public function setIdentityColumn($identityColumn)
- {
- $this->_identityColumn = $identityColumn;
- return $this;
- }
- /**
- * setCredentialColumn() - set the column name to be used as the credential column
- *
- * @param string $credentialColumn
- * @return Zend_Auth_Adapter_DbTable
- */
- public function setCredentialColumn($credentialColumn)
- {
- $this->_credentialColumn = $credentialColumn;
- return $this;
- }
- /**
- * setCredentialTreatment() - allows the developer to pass a parameterized string that is
- * used to transform or treat the input credential data
- *
- * In many cases, passwords and other sensitive data are encrypted, hashed, encoded,
- * obscured, or otherwise treated through some function or algorithm. By specifying a
- * parameterized treatment string with this method, a developer may apply arbitrary SQL
- * upon input credential data.
- *
- * Examples:
- *
- * 'PASSWORD(?)'
- * 'MD5(?)'
- *
- * @param string $treatment
- * @return Zend_Auth_Adapter_DbTable
- */
- public function setCredentialTreatment($treatment)
- {
- $this->_credentialTreatment = $treatment;
- return $this;
- }
- /**
- * setIdentity() - set the value to be used as the identity
- *
- * @param string $value
- * @return Zend_Auth_Adapter_DbTable
- */
- public function setIdentity($value)
- {
- $this->_identity = $value;
- return $this;
- }
- /**
- * setCredential() - set the credential value to be used, optionally can specify a treatment
- * to be used, should be supplied in parameterized form, such as 'MD5(?)' or 'PASSWORD(?)'
- *
- * @param string $credential
- * @return Zend_Auth_Adapter_DbTable
- */
- public function setCredential($credential)
- {
- $this->_credential = $credential;
- return $this;
- }
- /**
- * getResultRow() - returns the result row
- *
- * @return array
- */
- public function getResultRow()
- {
- return $this->_resultRow;
- }
- /**
- * authenticate() - defined by Zend_Auth_Adapter_Interface.
- *
- * @throws Zend_Auth_Adapter_Exception if answering the authentication query is impossible
- * @return Zend_Auth_Result
- */
- public function authenticate()
- {
- $exception = null;
- if ($this->_tableName == '') {
- $exception = 'A table must be supplied authentication adapter.';
- } elseif ($this->_identityColumn == '') {
- $exception = 'A table column must be supplied for the identity.';
- } elseif ($this->_identity == '') {
- $exception = 'A value for the identity must be provided to authenticate.';
- } elseif ($this->_credentialColumn == '') {
- $exception = 'A credential column must be supplied to autheticate against.';
- } elseif ($this->_credential === null) {
- $exception = 'A credential value must be provided to authenticate.';
- }
- if (null !== $exception) {
- /**
- * @see Zend_Auth_Adapter_Exception
- */
- require_once 'Zend/Auth/Adapter/Exception.php';
- throw new Zend_Auth_Adapter_Exception($exception);
- }
- // create result array
- $authResult = array(
- 'isValid' => false,
- 'identity' => $this->_identity,
- 'messages' => array()
- );
- // build credential expression
- if (empty($this->_credentialTreatment) || (strpos($this->_credentialTreatment, "?") === false)) {
- $this->_credentialTreatment = '?';
- }
- $credentialExpression = new Zend_Db_Expr(
- $this->_zendDb->quoteInto(
- $this->_zendDb->quoteIdentifier($this->_credentialColumn)
- . ' = ' . $this->_credentialTreatment, $this->_credential
- )
- . ' AS zend_auth_credential_match'
- );
- // get select
- $dbSelect = $this->_zendDb->select();
- $dbSelect->from($this->_tableName, array('*', $credentialExpression))
- ->where($this->_zendDb->quoteIdentifier($this->_identityColumn) . ' = ?', $this->_identity);
- // query for the identity
- try {
- $resultIdentities = $this->_zendDb->fetchAll($dbSelect->__toString());
- } catch (Exception $e) {
- /**
- * @see Zend_Auth_Adapter_Exception
- */
- require_once 'Zend/Auth/Adapter/Exception.php';
- throw new Zend_Auth_Adapter_Exception($e->getMessage());
- }
- if (count($resultIdentities) < 1) {
- $authResult['messages'][] = 'A record with the supplied identity could not be found.';
- return new Zend_Auth_Result($authResult['isValid'], $authResult['identity'], $authResult['messages']);
- } elseif (count($resultIdentities) > 1) {
- $authResult['messages'][] = 'More than one record matches the supplied identity.';
- return new Zend_Auth_Result($authResult['isValid'], $authResult['identity'], $authResult['messages']);
- }
- $resultIdentity = $resultIdentities[0];
- if ($resultIdentity['zend_auth_credential_match'] != '1') {
- $authResult['messages'][] = 'Supplied credential is invalid.';
- return new Zend_Auth_Result($authResult['isValid'], $authResult['identity'], $authResult['messages']);
- }
- unset($resultIdentity['zend_auth_credential_match']);
- $this->_resultRow = $resultIdentity;
- $authResult['isValid'] = true;
- return new Zend_Auth_Result($authResult['isValid'], $authResult['identity'], $authResult['messages']);
- }
- }