DbTable.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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-2009 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. * @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-2009 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 authenication. Previous to this call, this adapter would have already
  257. * been configured with all nessissary 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 making sure
  276. * that this adapter was indeed setup properly with all required peices of information.
  277. *
  278. * @throws Zend_Auth_Adapter_Exception - in the event that setup was not done properly
  279. * @return true
  280. */
  281. protected function _authenticateSetup()
  282. {
  283. $exception = null;
  284. if ($this->_tableName == '') {
  285. $exception = 'A table must be supplied for the Zend_Auth_Adapter_DbTable authentication adapter.';
  286. } elseif ($this->_identityColumn == '') {
  287. $exception = 'An identity column must be supplied for the Zend_Auth_Adapter_DbTable authentication adapter.';
  288. } elseif ($this->_credentialColumn == '') {
  289. $exception = 'A credential column must be supplied for the Zend_Auth_Adapter_DbTable authentication adapter.';
  290. } elseif ($this->_identity == '') {
  291. $exception = 'A value for the identity was not provided prior to authentication with Zend_Auth_Adapter_DbTable.';
  292. } elseif ($this->_credential === null) {
  293. $exception = 'A credential value was not provided prior to authentication with Zend_Auth_Adapter_DbTable.';
  294. }
  295. if (null !== $exception) {
  296. /**
  297. * @see Zend_Auth_Adapter_Exception
  298. */
  299. require_once 'Zend/Auth/Adapter/Exception.php';
  300. throw new Zend_Auth_Adapter_Exception($exception);
  301. }
  302. $this->_authenticateResultInfo = array(
  303. 'code' => Zend_Auth_Result::FAILURE,
  304. 'identity' => $this->_identity,
  305. 'messages' => array()
  306. );
  307. return true;
  308. }
  309. /**
  310. * _authenticateCreateSelect() - This method creates a Zend_Db_Select object that
  311. * is completely configured to be queried against the database.
  312. *
  313. * @return Zend_Db_Select
  314. */
  315. protected function _authenticateCreateSelect()
  316. {
  317. // build credential expression
  318. if (empty($this->_credentialTreatment) || (strpos($this->_credentialTreatment, '?') === false)) {
  319. $this->_credentialTreatment = '?';
  320. }
  321. $credentialExpression = new Zend_Db_Expr(
  322. '(CASE WHEN ' .
  323. $this->_zendDb->quoteInto(
  324. $this->_zendDb->quoteIdentifier($this->_credentialColumn, true)
  325. . ' = ' . $this->_credentialTreatment, $this->_credential
  326. )
  327. . ' THEN 1 ELSE 0 END) AS '
  328. . $this->_zendDb->quoteIdentifier('zend_auth_credential_match')
  329. );
  330. // get select
  331. $dbSelect = clone $this->getDbSelect();
  332. $dbSelect->from($this->_tableName, array('*', $credentialExpression))
  333. ->where($this->_zendDb->quoteIdentifier($this->_identityColumn, true) . ' = ?', $this->_identity);
  334. return $dbSelect;
  335. }
  336. /**
  337. * _authenticateQuerySelect() - This method accepts a Zend_Db_Select object and
  338. * performs a query against the database with that object.
  339. *
  340. * @param Zend_Db_Select $dbSelect
  341. * @throws Zend_Auth_Adapter_Exception - when a invalid select object is encoutered
  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. require_once 'Zend/Auth/Adapter/Exception.php';
  361. throw new Zend_Auth_Adapter_Exception('The supplied parameters to Zend_Auth_Adapter_DbTable failed to '
  362. . 'produce a valid sql statement, please check table and column names '
  363. . 'for validity.');
  364. }
  365. return $resultIdentities;
  366. }
  367. /**
  368. * _authenticateValidateResultSet() - This method attempts to make certian that only one
  369. * record was returned in the result set
  370. *
  371. * @param array $resultIdentities
  372. * @return true|Zend_Auth_Result
  373. */
  374. protected function _authenticateValidateResultSet(array $resultIdentities)
  375. {
  376. if (count($resultIdentities) < 1) {
  377. $this->_authenticateResultInfo['code'] = Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND;
  378. $this->_authenticateResultInfo['messages'][] = 'A record with the supplied identity could not be found.';
  379. return $this->_authenticateCreateAuthResult();
  380. } elseif (count($resultIdentities) > 1) {
  381. $this->_authenticateResultInfo['code'] = Zend_Auth_Result::FAILURE_IDENTITY_AMBIGUOUS;
  382. $this->_authenticateResultInfo['messages'][] = 'More than one record matches the supplied identity.';
  383. return $this->_authenticateCreateAuthResult();
  384. }
  385. return true;
  386. }
  387. /**
  388. * _authenticateValidateResult() - This method attempts to validate that the record in the
  389. * result set is indeed a record that matched the identity provided to this adapter.
  390. *
  391. * @param array $resultIdentity
  392. * @return Zend_Auth_Result
  393. */
  394. protected function _authenticateValidateResult($resultIdentity)
  395. {
  396. if ($resultIdentity['zend_auth_credential_match'] != '1') {
  397. $this->_authenticateResultInfo['code'] = Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID;
  398. $this->_authenticateResultInfo['messages'][] = 'Supplied credential is invalid.';
  399. return $this->_authenticateCreateAuthResult();
  400. }
  401. unset($resultIdentity['zend_auth_credential_match']);
  402. $this->_resultRow = $resultIdentity;
  403. $this->_authenticateResultInfo['code'] = Zend_Auth_Result::SUCCESS;
  404. $this->_authenticateResultInfo['messages'][] = 'Authentication successful.';
  405. return $this->_authenticateCreateAuthResult();
  406. }
  407. /**
  408. * _authenticateCreateAuthResult() - This method creates a Zend_Auth_Result object
  409. * from the information that has been collected during the authenticate() attempt.
  410. *
  411. * @return Zend_Auth_Result
  412. */
  413. protected function _authenticateCreateAuthResult()
  414. {
  415. return new Zend_Auth_Result(
  416. $this->_authenticateResultInfo['code'],
  417. $this->_authenticateResultInfo['identity'],
  418. $this->_authenticateResultInfo['messages']
  419. );
  420. }
  421. }