PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Auth/Adapter/DbTable.php

https://github.com/ostric/e-learning
PHP | 461 lines | 201 code | 56 blank | 204 comment | 24 complexity | 023d2e4d6e6fac50237a310597d35698 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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: DbTable.php 13202 2008-12-13 19:53:44Z sidhighwind $
  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. * @see Zend_Auth_Result
  32. */
  33. require_once 'Zend/Auth/Result.php';
  34. /**
  35. * @category Zend
  36. * @package Zend_Auth
  37. * @subpackage Zend_Auth_Adapter
  38. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. */
  41. class Zend_Auth_Adapter_DbTable implements Zend_Auth_Adapter_Interface
  42. {
  43. /**
  44. * Database Connection
  45. *
  46. * @var Zend_Db_Adapter_Abstract
  47. */
  48. protected $_zendDb = null;
  49. /**
  50. * $_tableName - the table name to check
  51. *
  52. * @var string
  53. */
  54. protected $_tableName = null;
  55. /**
  56. * $_identityColumn - the column to use as the identity
  57. *
  58. * @var string
  59. */
  60. protected $_identityColumn = null;
  61. /**
  62. * $_credentialColumns - columns to be used as the credentials
  63. *
  64. * @var string
  65. */
  66. protected $_credentialColumn = null;
  67. /**
  68. * $_identity - Identity value
  69. *
  70. * @var string
  71. */
  72. protected $_identity = null;
  73. /**
  74. * $_credential - Credential values
  75. *
  76. * @var string
  77. */
  78. protected $_credential = null;
  79. /**
  80. * $_credentialTreatment - Treatment applied to the credential, such as MD5() or PASSWORD()
  81. *
  82. * @var string
  83. */
  84. protected $_credentialTreatment = null;
  85. /**
  86. * $_authenticateResultInfo
  87. *
  88. * @var array
  89. */
  90. protected $_authenticateResultInfo = null;
  91. /**
  92. * $_resultRow - Results of database authentication query
  93. *
  94. * @var array
  95. */
  96. protected $_resultRow = null;
  97. /**
  98. * __construct() - Sets configuration options
  99. *
  100. * @param Zend_Db_Adapter_Abstract $zendDb
  101. * @param string $tableName
  102. * @param string $identityColumn
  103. * @param string $credentialColumn
  104. * @param string $credentialTreatment
  105. * @return void
  106. */
  107. public function __construct(Zend_Db_Adapter_Abstract $zendDb, $tableName = null, $identityColumn = null,
  108. $credentialColumn = null, $credentialTreatment = null)
  109. {
  110. $this->_zendDb = $zendDb;
  111. if (null !== $tableName) {
  112. $this->setTableName($tableName);
  113. }
  114. if (null !== $identityColumn) {
  115. $this->setIdentityColumn($identityColumn);
  116. }
  117. if (null !== $credentialColumn) {
  118. $this->setCredentialColumn($credentialColumn);
  119. }
  120. if (null !== $credentialTreatment) {
  121. $this->setCredentialTreatment($credentialTreatment);
  122. }
  123. }
  124. /**
  125. * setTableName() - set the table name to be used in the select query
  126. *
  127. * @param string $tableName
  128. * @return Zend_Auth_Adapter_DbTable Provides a fluent interface
  129. */
  130. public function setTableName($tableName)
  131. {
  132. $this->_tableName = $tableName;
  133. return $this;
  134. }
  135. /**
  136. * setIdentityColumn() - set the column name to be used as the identity column
  137. *
  138. * @param string $identityColumn
  139. * @return Zend_Auth_Adapter_DbTable Provides a fluent interface
  140. */
  141. public function setIdentityColumn($identityColumn)
  142. {
  143. $this->_identityColumn = $identityColumn;
  144. return $this;
  145. }
  146. /**
  147. * setCredentialColumn() - set the column name to be used as the credential column
  148. *
  149. * @param string $credentialColumn
  150. * @return Zend_Auth_Adapter_DbTable Provides a fluent interface
  151. */
  152. public function setCredentialColumn($credentialColumn)
  153. {
  154. $this->_credentialColumn = $credentialColumn;
  155. return $this;
  156. }
  157. /**
  158. * setCredentialTreatment() - allows the developer to pass a parameterized string that is
  159. * used to transform or treat the input credential data
  160. *
  161. * In many cases, passwords and other sensitive data are encrypted, hashed, encoded,
  162. * obscured, or otherwise treated through some function or algorithm. By specifying a
  163. * parameterized treatment string with this method, a developer may apply arbitrary SQL
  164. * upon input credential data.
  165. *
  166. * Examples:
  167. *
  168. * 'PASSWORD(?)'
  169. * 'MD5(?)'
  170. *
  171. * @param string $treatment
  172. * @return Zend_Auth_Adapter_DbTable Provides a fluent interface
  173. */
  174. public function setCredentialTreatment($treatment)
  175. {
  176. $this->_credentialTreatment = $treatment;
  177. return $this;
  178. }
  179. /**
  180. * setIdentity() - set the value to be used as the identity
  181. *
  182. * @param string $value
  183. * @return Zend_Auth_Adapter_DbTable Provides a fluent interface
  184. */
  185. public function setIdentity($value)
  186. {
  187. $this->_identity = $value;
  188. return $this;
  189. }
  190. /**
  191. * setCredential() - set the credential value to be used, optionally can specify a treatment
  192. * to be used, should be supplied in parameterized form, such as 'MD5(?)' or 'PASSWORD(?)'
  193. *
  194. * @param string $credential
  195. * @return Zend_Auth_Adapter_DbTable Provides a fluent interface
  196. */
  197. public function setCredential($credential)
  198. {
  199. $this->_credential = $credential;
  200. return $this;
  201. }
  202. /**
  203. * getResultRowObject() - Returns the result row as a stdClass object
  204. *
  205. * @param string|array $returnColumns
  206. * @param string|array $omitColumns
  207. * @return stdClass|boolean
  208. */
  209. public function getResultRowObject($returnColumns = null, $omitColumns = null)
  210. {
  211. if (!$this->_resultRow) {
  212. return false;
  213. }
  214. $returnObject = new stdClass();
  215. if (null !== $returnColumns) {
  216. $availableColumns = array_keys($this->_resultRow);
  217. foreach ( (array) $returnColumns as $returnColumn) {
  218. if (in_array($returnColumn, $availableColumns)) {
  219. $returnObject->{$returnColumn} = $this->_resultRow[$returnColumn];
  220. }
  221. }
  222. return $returnObject;
  223. } elseif (null !== $omitColumns) {
  224. $omitColumns = (array) $omitColumns;
  225. foreach ($this->_resultRow as $resultColumn => $resultValue) {
  226. if (!in_array($resultColumn, $omitColumns)) {
  227. $returnObject->{$resultColumn} = $resultValue;
  228. }
  229. }
  230. return $returnObject;
  231. } else {
  232. foreach ($this->_resultRow as $resultColumn => $resultValue) {
  233. $returnObject->{$resultColumn} = $resultValue;
  234. }
  235. return $returnObject;
  236. }
  237. }
  238. /**
  239. * authenticate() - defined by Zend_Auth_Adapter_Interface. This method is called to
  240. * attempt an authenication. Previous to this call, this adapter would have already
  241. * been configured with all nessissary information to successfully connect to a database
  242. * table and attempt to find a record matching the provided identity.
  243. *
  244. * @throws Zend_Auth_Adapter_Exception if answering the authentication query is impossible
  245. * @return Zend_Auth_Result
  246. */
  247. public function authenticate()
  248. {
  249. $this->_authenticateSetup();
  250. $dbSelect = $this->_authenticateCreateSelect();
  251. $resultIdentities = $this->_authenticateQuerySelect($dbSelect);
  252. if ( ($authResult = $this->_authenticateValidateResultset($resultIdentities)) instanceof Zend_Auth_Result) {
  253. return $authResult;
  254. }
  255. $authResult = $this->_authenticateValidateResult(array_shift($resultIdentities));
  256. return $authResult;
  257. }
  258. /**
  259. * _authenticateSetup() - This method abstracts the steps involved with making sure
  260. * that this adapter was indeed setup properly with all required peices of information.
  261. *
  262. * @throws Zend_Auth_Adapter_Exception - in the event that setup was not done properly
  263. * @return true
  264. */
  265. protected function _authenticateSetup()
  266. {
  267. $exception = null;
  268. if ($this->_tableName == '') {
  269. $exception = 'A table must be supplied for the Zend_Auth_Adapter_DbTable authentication adapter.';
  270. } elseif ($this->_identityColumn == '') {
  271. $exception = 'An identity column must be supplied for the Zend_Auth_Adapter_DbTable authentication adapter.';
  272. } elseif ($this->_credentialColumn == '') {
  273. $exception = 'A credential column must be supplied for the Zend_Auth_Adapter_DbTable authentication adapter.';
  274. } elseif ($this->_identity == '') {
  275. $exception = 'A value for the identity was not provided prior to authentication with Zend_Auth_Adapter_DbTable.';
  276. } elseif ($this->_credential === null) {
  277. $exception = 'A credential value was not provided prior to authentication with Zend_Auth_Adapter_DbTable.';
  278. }
  279. if (null !== $exception) {
  280. /**
  281. * @see Zend_Auth_Adapter_Exception
  282. */
  283. require_once 'Zend/Auth/Adapter/Exception.php';
  284. throw new Zend_Auth_Adapter_Exception($exception);
  285. }
  286. $this->_authenticateResultInfo = array(
  287. 'code' => Zend_Auth_Result::FAILURE,
  288. 'identity' => $this->_identity,
  289. 'messages' => array()
  290. );
  291. return true;
  292. }
  293. /**
  294. * _authenticateCreateSelect() - This method creates a Zend_Db_Select object that
  295. * is completely configured to be queried against the database.
  296. *
  297. * @return Zend_Db_Select
  298. */
  299. protected function _authenticateCreateSelect()
  300. {
  301. // build credential expression
  302. if (empty($this->_credentialTreatment) || (strpos($this->_credentialTreatment, '?') === false)) {
  303. $this->_credentialTreatment = '?';
  304. }
  305. $credentialExpression = new Zend_Db_Expr(
  306. '(CASE WHEN ' .
  307. $this->_zendDb->quoteInto(
  308. $this->_zendDb->quoteIdentifier($this->_credentialColumn, true)
  309. . ' = ' . $this->_credentialTreatment, $this->_credential
  310. )
  311. . ' THEN 1 ELSE 0 END) AS '
  312. . $this->_zendDb->quoteIdentifier('zend_auth_credential_match')
  313. );
  314. // get select
  315. $dbSelect = $this->_zendDb->select();
  316. $dbSelect->from($this->_tableName, array('*', $credentialExpression))
  317. ->where($this->_zendDb->quoteIdentifier($this->_identityColumn, true) . ' = ?', $this->_identity);
  318. return $dbSelect;
  319. }
  320. /**
  321. * _authenticateQuerySelect() - This method accepts a Zend_Db_Select object and
  322. * performs a query against the database with that object.
  323. *
  324. * @param Zend_Db_Select $dbSelect
  325. * @throws Zend_Auth_Adapter_Exception - when a invalid select object is encoutered
  326. * @return array
  327. */
  328. protected function _authenticateQuerySelect(Zend_Db_Select $dbSelect)
  329. {
  330. try {
  331. if ($this->_zendDb->getFetchMode() != Zend_DB::FETCH_ASSOC) {
  332. $origDbFetchMode = $this->_zendDb->getFetchMode();
  333. $this->_zendDb->setFetchMode(Zend_DB::FETCH_ASSOC);
  334. }
  335. $resultIdentities = $this->_zendDb->fetchAll($dbSelect->__toString());
  336. if (isset($origDbFetchMode)) {
  337. $this->_zendDb->setFetchMode($origDbFetchMode);
  338. unset($origDbFetchMode);
  339. }
  340. } catch (Exception $e) {
  341. /**
  342. * @see Zend_Auth_Adapter_Exception
  343. */
  344. require_once 'Zend/Auth/Adapter/Exception.php';
  345. throw new Zend_Auth_Adapter_Exception('The supplied parameters to Zend_Auth_Adapter_DbTable failed to '
  346. . 'produce a valid sql statement, please check table and column names '
  347. . 'for validity.');
  348. }
  349. return $resultIdentities;
  350. }
  351. /**
  352. * _authenticateValidateResultSet() - This method attempts to make certian that only one
  353. * record was returned in the result set
  354. *
  355. * @param array $resultIdentities
  356. * @return true|Zend_Auth_Result
  357. */
  358. protected function _authenticateValidateResultSet(array $resultIdentities)
  359. {
  360. if (count($resultIdentities) < 1) {
  361. $this->_authenticateResultInfo['code'] = Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND;
  362. $this->_authenticateResultInfo['messages'][] = 'A record with the supplied identity could not be found.';
  363. return $this->_authenticateCreateAuthResult();
  364. } elseif (count($resultIdentities) > 1) {
  365. $this->_authenticateResultInfo['code'] = Zend_Auth_Result::FAILURE_IDENTITY_AMBIGUOUS;
  366. $this->_authenticateResultInfo['messages'][] = 'More than one record matches the supplied identity.';
  367. return $this->_authenticateCreateAuthResult();
  368. }
  369. return true;
  370. }
  371. /**
  372. * _authenticateValidateResult() - This method attempts to validate that the record in the
  373. * result set is indeed a record that matched the identity provided to this adapter.
  374. *
  375. * @param array $resultIdentity
  376. * @return Zend_Auth_Result
  377. */
  378. protected function _authenticateValidateResult($resultIdentity)
  379. {
  380. if ($resultIdentity['zend_auth_credential_match'] != '1') {
  381. $this->_authenticateResultInfo['code'] = Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID;
  382. $this->_authenticateResultInfo['messages'][] = 'Supplied credential is invalid.';
  383. return $this->_authenticateCreateAuthResult();
  384. }
  385. unset($resultIdentity['zend_auth_credential_match']);
  386. $this->_resultRow = $resultIdentity;
  387. $this->_authenticateResultInfo['code'] = Zend_Auth_Result::SUCCESS;
  388. $this->_authenticateResultInfo['messages'][] = 'Authentication successful.';
  389. return $this->_authenticateCreateAuthResult();
  390. }
  391. /**
  392. * _authenticateCreateAuthResult() - This method creates a Zend_Auth_Result object
  393. * from the information that has been collected during the authenticate() attempt.
  394. *
  395. * @return Zend_Auth_Result
  396. */
  397. protected function _authenticateCreateAuthResult()
  398. {
  399. return new Zend_Auth_Result(
  400. $this->_authenticateResultInfo['code'],
  401. $this->_authenticateResultInfo['identity'],
  402. $this->_authenticateResultInfo['messages']
  403. );
  404. }
  405. }