PageRenderTime 57ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/efdac/e-forest_platform
PHP | 513 lines | 225 code | 63 blank | 225 comment | 28 complexity | 7a89ac5eeb1193f2bc7579a5d8115b95 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 22458 2010-06-18 22:47:46Z ralph $
  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 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 If null, default database adapter assumed
  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 = null, $tableName = null, $identityColumn = null,
  112. $credentialColumn = null, $credentialTreatment = null)
  113. {
  114. $this->_setDbAdapter($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. * _setDbAdapter() - set the database adapter to be used for quering
  130. *
  131. * @param Zend_Db_Adapter_Abstract
  132. * @throws Zend_Auth_Adapter_Exception
  133. * @return Zend_Auth_Adapter_DbTable
  134. */
  135. protected function _setDbAdapter(Zend_Db_Adapter_Abstract $zendDb = null)
  136. {
  137. $this->_zendDb = $zendDb;
  138. /**
  139. * If no adapter is specified, fetch default database adapter.
  140. */
  141. if(null === $this->_zendDb) {
  142. require_once 'Zend/Db/Table/Abstract.php';
  143. $this->_zendDb = Zend_Db_Table_Abstract::getDefaultAdapter();
  144. if (null === $this->_zendDb) {
  145. require_once 'Zend/Auth/Adapter/Exception.php';
  146. throw new Zend_Auth_Adapter_Exception('No database adapter present');
  147. }
  148. }
  149. return $this;
  150. }
  151. /**
  152. * setTableName() - set the table name to be used in the select query
  153. *
  154. * @param string $tableName
  155. * @return Zend_Auth_Adapter_DbTable Provides a fluent interface
  156. */
  157. public function setTableName($tableName)
  158. {
  159. $this->_tableName = $tableName;
  160. return $this;
  161. }
  162. /**
  163. * setIdentityColumn() - set the column name to be used as the identity column
  164. *
  165. * @param string $identityColumn
  166. * @return Zend_Auth_Adapter_DbTable Provides a fluent interface
  167. */
  168. public function setIdentityColumn($identityColumn)
  169. {
  170. $this->_identityColumn = $identityColumn;
  171. return $this;
  172. }
  173. /**
  174. * setCredentialColumn() - set the column name to be used as the credential column
  175. *
  176. * @param string $credentialColumn
  177. * @return Zend_Auth_Adapter_DbTable Provides a fluent interface
  178. */
  179. public function setCredentialColumn($credentialColumn)
  180. {
  181. $this->_credentialColumn = $credentialColumn;
  182. return $this;
  183. }
  184. /**
  185. * setCredentialTreatment() - allows the developer to pass a parameterized string that is
  186. * used to transform or treat the input credential data.
  187. *
  188. * In many cases, passwords and other sensitive data are encrypted, hashed, encoded,
  189. * obscured, or otherwise treated through some function or algorithm. By specifying a
  190. * parameterized treatment string with this method, a developer may apply arbitrary SQL
  191. * upon input credential data.
  192. *
  193. * Examples:
  194. *
  195. * 'PASSWORD(?)'
  196. * 'MD5(?)'
  197. *
  198. * @param string $treatment
  199. * @return Zend_Auth_Adapter_DbTable Provides a fluent interface
  200. */
  201. public function setCredentialTreatment($treatment)
  202. {
  203. $this->_credentialTreatment = $treatment;
  204. return $this;
  205. }
  206. /**
  207. * setIdentity() - set the value to be used as the identity
  208. *
  209. * @param string $value
  210. * @return Zend_Auth_Adapter_DbTable Provides a fluent interface
  211. */
  212. public function setIdentity($value)
  213. {
  214. $this->_identity = $value;
  215. return $this;
  216. }
  217. /**
  218. * setCredential() - set the credential value to be used, optionally can specify a treatment
  219. * to be used, should be supplied in parameterized form, such as 'MD5(?)' or 'PASSWORD(?)'
  220. *
  221. * @param string $credential
  222. * @return Zend_Auth_Adapter_DbTable Provides a fluent interface
  223. */
  224. public function setCredential($credential)
  225. {
  226. $this->_credential = $credential;
  227. return $this;
  228. }
  229. /**
  230. * getDbSelect() - Return the preauthentication Db Select object for userland select query modification
  231. *
  232. * @return Zend_Db_Select
  233. */
  234. public function getDbSelect()
  235. {
  236. if ($this->_dbSelect == null) {
  237. $this->_dbSelect = $this->_zendDb->select();
  238. }
  239. return $this->_dbSelect;
  240. }
  241. /**
  242. * getResultRowObject() - Returns the result row as a stdClass object
  243. *
  244. * @param string|array $returnColumns
  245. * @param string|array $omitColumns
  246. * @return stdClass|boolean
  247. */
  248. public function getResultRowObject($returnColumns = null, $omitColumns = null)
  249. {
  250. if (!$this->_resultRow) {
  251. return false;
  252. }
  253. $returnObject = new stdClass();
  254. if (null !== $returnColumns) {
  255. $availableColumns = array_keys($this->_resultRow);
  256. foreach ( (array) $returnColumns as $returnColumn) {
  257. if (in_array($returnColumn, $availableColumns)) {
  258. $returnObject->{$returnColumn} = $this->_resultRow[$returnColumn];
  259. }
  260. }
  261. return $returnObject;
  262. } elseif (null !== $omitColumns) {
  263. $omitColumns = (array) $omitColumns;
  264. foreach ($this->_resultRow as $resultColumn => $resultValue) {
  265. if (!in_array($resultColumn, $omitColumns)) {
  266. $returnObject->{$resultColumn} = $resultValue;
  267. }
  268. }
  269. return $returnObject;
  270. } else {
  271. foreach ($this->_resultRow as $resultColumn => $resultValue) {
  272. $returnObject->{$resultColumn} = $resultValue;
  273. }
  274. return $returnObject;
  275. }
  276. }
  277. /**
  278. * authenticate() - defined by Zend_Auth_Adapter_Interface. This method is called to
  279. * attempt an authentication. Previous to this call, this adapter would have already
  280. * been configured with all necessary information to successfully connect to a database
  281. * table and attempt to find a record matching the provided identity.
  282. *
  283. * @throws Zend_Auth_Adapter_Exception if answering the authentication query is impossible
  284. * @return Zend_Auth_Result
  285. */
  286. public function authenticate()
  287. {
  288. $this->_authenticateSetup();
  289. $dbSelect = $this->_authenticateCreateSelect();
  290. $resultIdentities = $this->_authenticateQuerySelect($dbSelect);
  291. if ( ($authResult = $this->_authenticateValidateResultSet($resultIdentities)) instanceof Zend_Auth_Result) {
  292. return $authResult;
  293. }
  294. $authResult = $this->_authenticateValidateResult(array_shift($resultIdentities));
  295. return $authResult;
  296. }
  297. /**
  298. * _authenticateSetup() - This method abstracts the steps involved with
  299. * making sure that this adapter was indeed setup properly with all
  300. * required pieces of information.
  301. *
  302. * @throws Zend_Auth_Adapter_Exception - in the event that setup was not done properly
  303. * @return true
  304. */
  305. protected function _authenticateSetup()
  306. {
  307. $exception = null;
  308. if ($this->_tableName == '') {
  309. $exception = 'A table must be supplied for the Zend_Auth_Adapter_DbTable authentication adapter.';
  310. } elseif ($this->_identityColumn == '') {
  311. $exception = 'An identity column must be supplied for the Zend_Auth_Adapter_DbTable authentication adapter.';
  312. } elseif ($this->_credentialColumn == '') {
  313. $exception = 'A credential column must be supplied for the Zend_Auth_Adapter_DbTable authentication adapter.';
  314. } elseif ($this->_identity == '') {
  315. $exception = 'A value for the identity was not provided prior to authentication with Zend_Auth_Adapter_DbTable.';
  316. } elseif ($this->_credential === null) {
  317. $exception = 'A credential value was not provided prior to authentication with Zend_Auth_Adapter_DbTable.';
  318. }
  319. if (null !== $exception) {
  320. /**
  321. * @see Zend_Auth_Adapter_Exception
  322. */
  323. require_once 'Zend/Auth/Adapter/Exception.php';
  324. throw new Zend_Auth_Adapter_Exception($exception);
  325. }
  326. $this->_authenticateResultInfo = array(
  327. 'code' => Zend_Auth_Result::FAILURE,
  328. 'identity' => $this->_identity,
  329. 'messages' => array()
  330. );
  331. return true;
  332. }
  333. /**
  334. * _authenticateCreateSelect() - This method creates a Zend_Db_Select object that
  335. * is completely configured to be queried against the database.
  336. *
  337. * @return Zend_Db_Select
  338. */
  339. protected function _authenticateCreateSelect()
  340. {
  341. // build credential expression
  342. if (empty($this->_credentialTreatment) || (strpos($this->_credentialTreatment, '?') === false)) {
  343. $this->_credentialTreatment = '?';
  344. }
  345. $credentialExpression = new Zend_Db_Expr(
  346. '(CASE WHEN ' .
  347. $this->_zendDb->quoteInto(
  348. $this->_zendDb->quoteIdentifier($this->_credentialColumn, true)
  349. . ' = ' . $this->_credentialTreatment, $this->_credential
  350. )
  351. . ' THEN 1 ELSE 0 END) AS '
  352. . $this->_zendDb->quoteIdentifier(
  353. $this->_zendDb->foldCase('zend_auth_credential_match')
  354. )
  355. );
  356. // get select
  357. $dbSelect = clone $this->getDbSelect();
  358. $dbSelect->from($this->_tableName, array('*', $credentialExpression))
  359. ->where($this->_zendDb->quoteIdentifier($this->_identityColumn, true) . ' = ?', $this->_identity);
  360. return $dbSelect;
  361. }
  362. /**
  363. * _authenticateQuerySelect() - This method accepts a Zend_Db_Select object and
  364. * performs a query against the database with that object.
  365. *
  366. * @param Zend_Db_Select $dbSelect
  367. * @throws Zend_Auth_Adapter_Exception - when an invalid select
  368. * object is encountered
  369. * @return array
  370. */
  371. protected function _authenticateQuerySelect(Zend_Db_Select $dbSelect)
  372. {
  373. try {
  374. if ($this->_zendDb->getFetchMode() != Zend_DB::FETCH_ASSOC) {
  375. $origDbFetchMode = $this->_zendDb->getFetchMode();
  376. $this->_zendDb->setFetchMode(Zend_DB::FETCH_ASSOC);
  377. }
  378. $resultIdentities = $this->_zendDb->fetchAll($dbSelect->__toString());
  379. if (isset($origDbFetchMode)) {
  380. $this->_zendDb->setFetchMode($origDbFetchMode);
  381. unset($origDbFetchMode);
  382. }
  383. } catch (Exception $e) {
  384. /**
  385. * @see Zend_Auth_Adapter_Exception
  386. */
  387. require_once 'Zend/Auth/Adapter/Exception.php';
  388. throw new Zend_Auth_Adapter_Exception('The supplied parameters to Zend_Auth_Adapter_DbTable failed to '
  389. . 'produce a valid sql statement, please check table and column names '
  390. . 'for validity.', 0, $e);
  391. }
  392. return $resultIdentities;
  393. }
  394. /**
  395. * _authenticateValidateResultSet() - This method attempts to make
  396. * certain that only one record was returned in the resultset
  397. *
  398. * @param array $resultIdentities
  399. * @return true|Zend_Auth_Result
  400. */
  401. protected function _authenticateValidateResultSet(array $resultIdentities)
  402. {
  403. if (count($resultIdentities) < 1) {
  404. $this->_authenticateResultInfo['code'] = Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND;
  405. $this->_authenticateResultInfo['messages'][] = 'A record with the supplied identity could not be found.';
  406. return $this->_authenticateCreateAuthResult();
  407. } elseif (count($resultIdentities) > 1) {
  408. $this->_authenticateResultInfo['code'] = Zend_Auth_Result::FAILURE_IDENTITY_AMBIGUOUS;
  409. $this->_authenticateResultInfo['messages'][] = 'More than one record matches the supplied identity.';
  410. return $this->_authenticateCreateAuthResult();
  411. }
  412. return true;
  413. }
  414. /**
  415. * _authenticateValidateResult() - This method attempts to validate that
  416. * the record in the resultset is indeed a record that matched the
  417. * identity provided to this adapter.
  418. *
  419. * @param array $resultIdentity
  420. * @return Zend_Auth_Result
  421. */
  422. protected function _authenticateValidateResult($resultIdentity)
  423. {
  424. $zendAuthCredentialMatchColumn = $this->_zendDb->foldCase('zend_auth_credential_match');
  425. if ($resultIdentity[$zendAuthCredentialMatchColumn] != '1') {
  426. $this->_authenticateResultInfo['code'] = Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID;
  427. $this->_authenticateResultInfo['messages'][] = 'Supplied credential is invalid.';
  428. return $this->_authenticateCreateAuthResult();
  429. }
  430. unset($resultIdentity[$zendAuthCredentialMatchColumn]);
  431. $this->_resultRow = $resultIdentity;
  432. $this->_authenticateResultInfo['code'] = Zend_Auth_Result::SUCCESS;
  433. $this->_authenticateResultInfo['messages'][] = 'Authentication successful.';
  434. return $this->_authenticateCreateAuthResult();
  435. }
  436. /**
  437. * _authenticateCreateAuthResult() - Creates a Zend_Auth_Result object from
  438. * the information that has been collected during the authenticate() attempt.
  439. *
  440. * @return Zend_Auth_Result
  441. */
  442. protected function _authenticateCreateAuthResult()
  443. {
  444. return new Zend_Auth_Result(
  445. $this->_authenticateResultInfo['code'],
  446. $this->_authenticateResultInfo['identity'],
  447. $this->_authenticateResultInfo['messages']
  448. );
  449. }
  450. }