/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

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Auth
  17. * @subpackage Zend_Auth_Adapter
  18. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Auth_Adapter_Interface
  24. */
  25. require_once 'Zend/Auth/Adapter/Interface.php';
  26. /**
  27. * @see Zend_Db_Adapter_Abstract
  28. */
  29. require_once 'Zend/Db/Adapter/Abstract.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Auth
  33. * @subpackage Zend_Auth_Adapter
  34. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Auth_Adapter_DbTable implements Zend_Auth_Adapter_Interface
  38. {
  39. /**
  40. * Database Connection
  41. *
  42. * @var Zend_Db_Adapter_Abstract
  43. */
  44. protected $_zendDb = null;
  45. /**
  46. * $_tableName - the table name to check
  47. *
  48. * @var string
  49. */
  50. protected $_tableName = null;
  51. /**
  52. * $_identityColumn - the column to use as the identity
  53. *
  54. * @var string
  55. */
  56. protected $_identityColumn = null;
  57. /**
  58. * $_credentialColumns - columns to be used as the credentials
  59. *
  60. * @var string
  61. */
  62. protected $_credentialColumn = null;
  63. /**
  64. * $_identity - Identity value
  65. *
  66. * @var string
  67. */
  68. protected $_identity = null;
  69. /**
  70. * $_credential - Credential values
  71. *
  72. * @var string
  73. */
  74. protected $_credential = null;
  75. /**
  76. * $_credentialTreatment - Treatment applied to the credential, such as MD5() or PASSWORD()
  77. *
  78. * @var string
  79. */
  80. protected $_credentialTreatment = null;
  81. /**
  82. * $_resultRow - Results of database authentication query
  83. *
  84. * @var array
  85. */
  86. protected $_resultRow = null;
  87. /**
  88. * __construct() - Sets configuration options
  89. *
  90. * @param Zend_Db_Adapter_Abstract $zendDb
  91. * @param string $tableName
  92. * @param string $identityColumn
  93. * @param string $credentialColumn
  94. * @param string $credentialTreatment
  95. * @return void
  96. */
  97. public function __construct(Zend_Db_Adapter_Abstract $zendDb, $tableName = null, $identityColumn = null,
  98. $credentialColumn = null, $credentialTreatment = null)
  99. {
  100. $this->_zendDb = $zendDb;
  101. if (null !== $tableName) {
  102. $this->setTableName($tableName);
  103. }
  104. if (null !== $identityColumn) {
  105. $this->setIdentityColumn($identityColumn);
  106. }
  107. if (null !== $credentialColumn) {
  108. $this->setCredentialColumn($credentialColumn);
  109. }
  110. if (null !== $credentialTreatment) {
  111. $this->setCredentialTreatment($credentialTreatment);
  112. }
  113. }
  114. /**
  115. * setTableName() - set the table name to be used in the select query
  116. *
  117. * @param string $tableName
  118. * @return Zend_Auth_Adapter_DbTable
  119. */
  120. public function setTableName($tableName)
  121. {
  122. $this->_tableName = $tableName;
  123. return $this;
  124. }
  125. /**
  126. * setIdentityColumn() - set the column name to be used as the identity column
  127. *
  128. * @param string $identityColumn
  129. * @return Zend_Auth_Adapter_DbTable
  130. */
  131. public function setIdentityColumn($identityColumn)
  132. {
  133. $this->_identityColumn = $identityColumn;
  134. return $this;
  135. }
  136. /**
  137. * setCredentialColumn() - set the column name to be used as the credential column
  138. *
  139. * @param string $credentialColumn
  140. * @return Zend_Auth_Adapter_DbTable
  141. */
  142. public function setCredentialColumn($credentialColumn)
  143. {
  144. $this->_credentialColumn = $credentialColumn;
  145. return $this;
  146. }
  147. /**
  148. * setCredentialTreatment() - allows the developer to pass a parameterized string that is
  149. * used to transform or treat the input credential data
  150. *
  151. * In many cases, passwords and other sensitive data are encrypted, hashed, encoded,
  152. * obscured, or otherwise treated through some function or algorithm. By specifying a
  153. * parameterized treatment string with this method, a developer may apply arbitrary SQL
  154. * upon input credential data.
  155. *
  156. * Examples:
  157. *
  158. * 'PASSWORD(?)'
  159. * 'MD5(?)'
  160. *
  161. * @param string $treatment
  162. * @return Zend_Auth_Adapter_DbTable
  163. */
  164. public function setCredentialTreatment($treatment)
  165. {
  166. $this->_credentialTreatment = $treatment;
  167. return $this;
  168. }
  169. /**
  170. * setIdentity() - set the value to be used as the identity
  171. *
  172. * @param string $value
  173. * @return Zend_Auth_Adapter_DbTable
  174. */
  175. public function setIdentity($value)
  176. {
  177. $this->_identity = $value;
  178. return $this;
  179. }
  180. /**
  181. * setCredential() - set the credential value to be used, optionally can specify a treatment
  182. * to be used, should be supplied in parameterized form, such as 'MD5(?)' or 'PASSWORD(?)'
  183. *
  184. * @param string $credential
  185. * @return Zend_Auth_Adapter_DbTable
  186. */
  187. public function setCredential($credential)
  188. {
  189. $this->_credential = $credential;
  190. return $this;
  191. }
  192. /**
  193. * getResultRow() - returns the result row
  194. *
  195. * @return array
  196. */
  197. public function getResultRow()
  198. {
  199. return $this->_resultRow;
  200. }
  201. /**
  202. * authenticate() - defined by Zend_Auth_Adapter_Interface.
  203. *
  204. * @throws Zend_Auth_Adapter_Exception if answering the authentication query is impossible
  205. * @return Zend_Auth_Result
  206. */
  207. public function authenticate()
  208. {
  209. $exception = null;
  210. if ($this->_tableName == '') {
  211. $exception = 'A table must be supplied authentication adapter.';
  212. } elseif ($this->_identityColumn == '') {
  213. $exception = 'A table column must be supplied for the identity.';
  214. } elseif ($this->_identity == '') {
  215. $exception = 'A value for the identity must be provided to authenticate.';
  216. } elseif ($this->_credentialColumn == '') {
  217. $exception = 'A credential column must be supplied to autheticate against.';
  218. } elseif ($this->_credential === null) {
  219. $exception = 'A credential value must be provided to authenticate.';
  220. }
  221. if (null !== $exception) {
  222. /**
  223. * @see Zend_Auth_Adapter_Exception
  224. */
  225. require_once 'Zend/Auth/Adapter/Exception.php';
  226. throw new Zend_Auth_Adapter_Exception($exception);
  227. }
  228. // create result array
  229. $authResult = array(
  230. 'isValid' => false,
  231. 'identity' => $this->_identity,
  232. 'messages' => array()
  233. );
  234. // build credential expression
  235. if (empty($this->_credentialTreatment) || (strpos($this->_credentialTreatment, "?") === false)) {
  236. $this->_credentialTreatment = '?';
  237. }
  238. $credentialExpression = new Zend_Db_Expr(
  239. $this->_zendDb->quoteInto(
  240. $this->_zendDb->quoteIdentifier($this->_credentialColumn)
  241. . ' = ' . $this->_credentialTreatment, $this->_credential
  242. )
  243. . ' AS zend_auth_credential_match'
  244. );
  245. // get select
  246. $dbSelect = $this->_zendDb->select();
  247. $dbSelect->from($this->_tableName, array('*', $credentialExpression))
  248. ->where($this->_zendDb->quoteIdentifier($this->_identityColumn) . ' = ?', $this->_identity);
  249. // query for the identity
  250. try {
  251. $resultIdentities = $this->_zendDb->fetchAll($dbSelect->__toString());
  252. } catch (Exception $e) {
  253. /**
  254. * @see Zend_Auth_Adapter_Exception
  255. */
  256. require_once 'Zend/Auth/Adapter/Exception.php';
  257. throw new Zend_Auth_Adapter_Exception($e->getMessage());
  258. }
  259. if (count($resultIdentities) < 1) {
  260. $authResult['messages'][] = 'A record with the supplied identity could not be found.';
  261. return new Zend_Auth_Result($authResult['isValid'], $authResult['identity'], $authResult['messages']);
  262. } elseif (count($resultIdentities) > 1) {
  263. $authResult['messages'][] = 'More than one record matches the supplied identity.';
  264. return new Zend_Auth_Result($authResult['isValid'], $authResult['identity'], $authResult['messages']);
  265. }
  266. $resultIdentity = $resultIdentities[0];
  267. if ($resultIdentity['zend_auth_credential_match'] != '1') {
  268. $authResult['messages'][] = 'Supplied credential is invalid.';
  269. return new Zend_Auth_Result($authResult['isValid'], $authResult['identity'], $authResult['messages']);
  270. }
  271. unset($resultIdentity['zend_auth_credential_match']);
  272. $this->_resultRow = $resultIdentity;
  273. $authResult['isValid'] = true;
  274. return new Zend_Auth_Result($authResult['isValid'], $authResult['identity'], $authResult['messages']);
  275. }
  276. }