FormAuthenticate.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * PHP 5
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  12. * @link http://cakephp.org CakePHP(tm) Project
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. App::uses('BaseAuthenticate', 'Controller/Component/Auth');
  16. /**
  17. * An authentication adapter for AuthComponent. Provides the ability to authenticate using POST
  18. * data. Can be used by configuring AuthComponent to use it via the AuthComponent::$authenticate setting.
  19. *
  20. * {{{
  21. * $this->Auth->authenticate = array(
  22. * 'Form' => array(
  23. * 'scope' => array('User.active' => 1)
  24. * )
  25. * )
  26. * }}}
  27. *
  28. * When configuring FormAuthenticate you can pass in settings to which fields, model and additional conditions
  29. * are used. See FormAuthenticate::$settings for more information.
  30. *
  31. * @package Cake.Controller.Component.Auth
  32. * @since 2.0
  33. * @see AuthComponent::$authenticate
  34. */
  35. class FormAuthenticate extends BaseAuthenticate {
  36. /**
  37. * Checks the fields to ensure they are supplied.
  38. *
  39. * @param CakeRequest $request The request that contains login information.
  40. * @param string $model The model used for login verification.
  41. * @param array $fields The fields to be checked.
  42. * @return boolean False if the fields have not been supplied. True if they exist.
  43. */
  44. protected function _checkFields(CakeRequest $request, $model, $fields) {
  45. if (empty($request->data[$model])) {
  46. return false;
  47. }
  48. if (
  49. empty($request->data[$model][$fields['username']]) ||
  50. empty($request->data[$model][$fields['password']])
  51. ) {
  52. return false;
  53. }
  54. return true;
  55. }
  56. /**
  57. * Authenticates the identity contained in a request. Will use the `settings.userModel`, and `settings.fields`
  58. * to find POST data that is used to find a matching record in the `settings.userModel`. Will return false if
  59. * there is no post data, either username or password is missing, of if the scope conditions have not been met.
  60. *
  61. * @param CakeRequest $request The request that contains login information.
  62. * @param CakeResponse $response Unused response object.
  63. * @return mixed False on login failure. An array of User data on success.
  64. */
  65. public function authenticate(CakeRequest $request, CakeResponse $response) {
  66. $userModel = $this->settings['userModel'];
  67. list(, $model) = pluginSplit($userModel);
  68. $fields = $this->settings['fields'];
  69. if (!$this->_checkFields($request, $model, $fields)) {
  70. return false;
  71. }
  72. return $this->_findUser(
  73. $request->data[$model][$fields['username']],
  74. $request->data[$model][$fields['password']]
  75. );
  76. }
  77. }