DirectAuthenticate.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. * @author Mark Scherer
  21. * @licente MIT
  22. * @cakephp 2.x (>= 2.3)
  23. * 2012-11-05 ms
  24. */
  25. class DirectAuthenticate extends BaseAuthenticate {
  26. /**
  27. * Authenticates the identity contained in a request. Will use the `settings.userModel`, and `settings.fields`
  28. * to find POST data that is used to find a matching record in the `settings.userModel`. Will return false if
  29. * there is no post data, username is missing, of if the scope conditions have not been met.
  30. *
  31. * @param CakeRequest $request The request that contains login information.
  32. * @param CakeResponse $response Unused response object.
  33. * @return mixed. False on login failure. An array of User data on success.
  34. */
  35. public function authenticate(CakeRequest $request, CakeResponse $response) {
  36. $userModel = $this->settings['userModel'];
  37. list($plugin, $model) = pluginSplit($userModel);
  38. $fields = $this->settings['fields'];
  39. if (!$this->_checkFields($request, $model, $fields)) {
  40. return false;
  41. }
  42. $conditions = array(
  43. $model . '.' . $fields['username'] => $request->data[$model][$fields['username']]
  44. );
  45. return $this->_findUser($conditions);
  46. }
  47. /**
  48. * Checks the fields to ensure they are supplied.
  49. *
  50. * @param CakeRequest $request The request that contains login information.
  51. * @param string $model The model used for login verification.
  52. * @param array $fields The fields to be checked.
  53. * @return boolean False if the fields have not been supplied. True if they exist.
  54. */
  55. protected function _checkFields(CakeRequest $request, $model, $fields) {
  56. if (empty($request->data[$model])) {
  57. return false;
  58. }
  59. if (empty($request->data[$model][$fields['username']])) {
  60. return false;
  61. }
  62. return true;
  63. }
  64. /**
  65. * Find a user record using the standard options.
  66. *
  67. * The $conditions parameter can be a (string)username or an array containing conditions for Model::find('first').
  68. *
  69. * @param array $conditions An array of find conditions.
  70. * @return Mixed Either false on failure, or an array of user data.
  71. */
  72. protected function _findUser($conditions) {
  73. $userModel = $this->settings['userModel'];
  74. list($plugin, $model) = pluginSplit($userModel);
  75. $fields = $this->settings['fields'];
  76. $user = parent::_findUser($conditions);
  77. if (isset($user[$fields['password']])) {
  78. unset($user[$fields['password']]);
  79. }
  80. return $user;
  81. }
  82. }