DirectAuthenticate.php 3.4 KB

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