PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

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