FormAuthenticate.php 2.9 KB

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