PageRenderTime 41ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

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

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