FormAuthenticate.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. namespace Cake\Controller\Component\Auth;
  17. use Cake\Controller\ComponentRegistry;
  18. use Cake\Network\Request;
  19. use Cake\Network\Response;
  20. /**
  21. * An authentication adapter for AuthComponent. Provides the ability to authenticate using POST
  22. * data. Can be used by configuring AuthComponent to use it via the AuthComponent::$authenticate setting.
  23. *
  24. * {{{
  25. * $this->Auth->authenticate = array(
  26. * 'Form' => array(
  27. * 'scope' => array('User.active' => 1)
  28. * )
  29. * )
  30. * }}}
  31. *
  32. * When configuring FormAuthenticate you can pass in settings to which fields, model and additional conditions
  33. * are used. See FormAuthenticate::$settings for more information.
  34. *
  35. * @since 2.0
  36. * @see AuthComponent::$authenticate
  37. */
  38. class FormAuthenticate extends BaseAuthenticate {
  39. /**
  40. * Checks the fields to ensure they are supplied.
  41. *
  42. * @param Cake\Network\Request $request The request that contains login information.
  43. * @param string $model The model used for login verification.
  44. * @param array $fields The fields to be checked.
  45. * @return boolean False if the fields have not been supplied. True if they exist.
  46. */
  47. protected function _checkFields(Request $request, $model, $fields) {
  48. if (empty($request->data[$model])) {
  49. return false;
  50. }
  51. foreach (array($fields['username'], $fields['password']) as $field) {
  52. $value = $request->data($model . '.' . $field);
  53. if (empty($value) || !is_string($value)) {
  54. return false;
  55. }
  56. }
  57. return true;
  58. }
  59. /**
  60. * Authenticates the identity contained in a request. Will use the `settings.userModel`, and `settings.fields`
  61. * to find POST data that is used to find a matching record in the `settings.userModel`. Will return false if
  62. * there is no post data, either username or password is missing, or if the scope conditions have not been met.
  63. *
  64. * @param Cake\Network\Request $request The request that contains login information.
  65. * @param Cake\Network\Response $response Unused response object.
  66. * @return mixed False on login failure. An array of User data on success.
  67. */
  68. public function authenticate(Request $request, Response $response) {
  69. $userModel = $this->settings['userModel'];
  70. list(, $model) = pluginSplit($userModel);
  71. $fields = $this->settings['fields'];
  72. if (!$this->_checkFields($request, $model, $fields)) {
  73. return false;
  74. }
  75. return $this->_findUser(
  76. $request->data[$model][$fields['username']],
  77. $request->data[$model][$fields['password']]
  78. );
  79. }
  80. }