DirectAuthenticate.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. App::uses('BaseAuthenticate', 'Controller/Component/Auth');
  3. /**
  4. * An authentication adapter for AuthComponent to directly log in a user by username, id or
  5. * any other distinct identification.
  6. *
  7. * Inside a controller(/component):
  8. *
  9. * $this->request->data = array('User' => array('id' => $userId));
  10. * $this->Auth->authenticate = array('Tools.Direct' => array('contain' => array('Role.id'), 'fields'=>array('username' => 'id')));
  11. * $result = $this->Auth->login();
  12. *
  13. * This has several advantages over using Auth->login($data) directly:
  14. * - You keep it dry, especially when using contain ($data would have to have the exact same data).
  15. * - No overhead - retrieving the data prior to the login is not necessary. It's short and easy.
  16. * - You keep it centralized, only one single mechanism to login (using your Authentication adapters
  17. * and its common _findUser() method). It also respects the scope and contain settings specified
  18. * in your AppController just as any other adapter.
  19. *
  20. * Possible configs: see BaseAuthenticate.
  21. *
  22. * @author Mark Scherer
  23. * @license MIT
  24. * @cakephp 2.x (>= 2.3)
  25. * 2012-11-05 ms
  26. */
  27. class DirectAuthenticate extends BaseAuthenticate {
  28. /**
  29. * Authenticates the identity contained in a request. Will use the `settings.userModel`, and `settings.fields`
  30. * to find POST data that is used to find a matching record in the `settings.userModel`. Will return false if
  31. * there is no post data, username is missing, of if the scope conditions have not been met.
  32. *
  33. * @param CakeRequest $request The request that contains login information.
  34. * @param CakeResponse $response Unused response object.
  35. * @return mixed. False on login failure. An array of User data on success.
  36. */
  37. public function authenticate(CakeRequest $request, CakeResponse $response) {
  38. $userModel = $this->settings['userModel'];
  39. list($plugin, $model) = pluginSplit($userModel);
  40. $fields = $this->settings['fields'];
  41. if (!$this->_checkFields($request, $model, $fields)) {
  42. return false;
  43. }
  44. $conditions = array(
  45. $model . '.' . $fields['username'] => $request->data[$model][$fields['username']]
  46. );
  47. return $this->_findUser($conditions);
  48. }
  49. /**
  50. * Checks the fields to ensure they are supplied.
  51. *
  52. * @param CakeRequest $request The request that contains login information.
  53. * @param string $model The model used for login verification.
  54. * @param array $fields The fields to be checked.
  55. * @return boolean False if the fields have not been supplied. True if they exist.
  56. */
  57. protected function _checkFields(CakeRequest $request, $model, $fields) {
  58. if (empty($request->data[$model])) {
  59. return false;
  60. }
  61. if (empty($request->data[$model][$fields['username']])) {
  62. return false;
  63. }
  64. return true;
  65. }
  66. /**
  67. * Find a user record using the standard options.
  68. *
  69. * The $conditions parameter can be a (string)username or an array containing conditions for Model::find('first').
  70. *
  71. * @param array $conditions An array of find conditions.
  72. * @return Mixed Either false on failure, or an array of user data.
  73. */
  74. protected function _findUser($conditions, $password = null) {
  75. $userModel = $this->settings['userModel'];
  76. list($plugin, $model) = pluginSplit($userModel);
  77. $fields = $this->settings['fields'];
  78. $user = parent::_findUser($conditions);
  79. if (isset($user[$fields['password']])) {
  80. unset($user[$fields['password']]);
  81. }
  82. return $user;
  83. }
  84. }