BaseAuthenticate.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * PHP 5
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  12. * @link http://cakephp.org CakePHP(tm) Project
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. App::uses('Security', 'Utility');
  16. App::uses('Hash', 'Utility');
  17. /**
  18. * Base Authentication class with common methods and properties.
  19. *
  20. * @package Cake.Controller.Component.Auth
  21. */
  22. abstract class BaseAuthenticate {
  23. /**
  24. * Settings for this object.
  25. *
  26. * - `fields` The fields to use to identify a user by.
  27. * - `userModel` The model name of the User, defaults to User.
  28. * - `scope` Additional conditions to use when looking up and authenticating users,
  29. * i.e. `array('User.is_active' => 1).`
  30. * - `recursive` The value of the recursive key passed to find(). Defaults to 0.
  31. * - `contain` Extra models to contain and store in session.
  32. *
  33. * @var array
  34. */
  35. public $settings = array(
  36. 'fields' => array(
  37. 'username' => 'username',
  38. 'password' => 'password'
  39. ),
  40. 'userModel' => 'User',
  41. 'scope' => array(),
  42. 'recursive' => 0,
  43. 'contain' => null,
  44. );
  45. /**
  46. * A Component collection, used to get more components.
  47. *
  48. * @var ComponentCollection
  49. */
  50. protected $_Collection;
  51. /**
  52. * Constructor
  53. *
  54. * @param ComponentCollection $collection The Component collection used on this request.
  55. * @param array $settings Array of settings to use.
  56. */
  57. public function __construct(ComponentCollection $collection, $settings) {
  58. $this->_Collection = $collection;
  59. $this->settings = Hash::merge($this->settings, $settings);
  60. }
  61. /**
  62. * Find a user record using the standard options.
  63. *
  64. * The $conditions parameter can be a (string)username or an array containing conditions for Model::find('first'). If
  65. * the password field is not included in the conditions the password will be returned.
  66. *
  67. * @param Mixed $conditions The username/identifier, or an array of find conditions.
  68. * @param Mixed $password The password, only use if passing as $conditions = 'username'.
  69. * @return Mixed Either false on failure, or an array of user data.
  70. */
  71. protected function _findUser($conditions, $password = null) {
  72. $userModel = $this->settings['userModel'];
  73. list(, $model) = pluginSplit($userModel);
  74. $fields = $this->settings['fields'];
  75. if (!is_array($conditions)) {
  76. if (!$password) {
  77. return false;
  78. }
  79. $username = $conditions;
  80. $conditions = array(
  81. $model . '.' . $fields['username'] => $username,
  82. $model . '.' . $fields['password'] => $this->_password($password),
  83. );
  84. }
  85. if (!empty($this->settings['scope'])) {
  86. $conditions = array_merge($conditions, $this->settings['scope']);
  87. }
  88. $result = ClassRegistry::init($userModel)->find('first', array(
  89. 'conditions' => $conditions,
  90. 'recursive' => $this->settings['recursive'],
  91. 'contain' => $this->settings['contain'],
  92. ));
  93. if (empty($result) || empty($result[$model])) {
  94. return false;
  95. }
  96. $user = $result[$model];
  97. if (
  98. isset($conditions[$model . '.' . $fields['password']]) ||
  99. isset($conditions[$fields['password']])
  100. ) {
  101. unset($user[$fields['password']]);
  102. }
  103. unset($result[$model]);
  104. return array_merge($user, $result);
  105. }
  106. /**
  107. * Hash the plain text password so that it matches the hashed/encrypted password
  108. * in the datasource.
  109. *
  110. * @param string $password The plain text password.
  111. * @return string The hashed form of the password.
  112. */
  113. protected function _password($password) {
  114. return Security::hash($password, null, true);
  115. }
  116. /**
  117. * Authenticate a user based on the request information.
  118. *
  119. * @param CakeRequest $request Request to get authentication information from.
  120. * @param CakeResponse $response A response object that can have headers added.
  121. * @return mixed Either false on failure, or an array of user data on success.
  122. */
  123. abstract public function authenticate(CakeRequest $request, CakeResponse $response);
  124. /**
  125. * Allows you to hook into AuthComponent::logout(),
  126. * and implement specialized logout behavior.
  127. *
  128. * All attached authentication objects will have this method
  129. * called when a user logs out.
  130. *
  131. * @param array $user The user about to be logged out.
  132. * @return void
  133. */
  134. public function logout($user) {
  135. }
  136. /**
  137. * Get a user based on information in the request. Primarily used by stateless authentication
  138. * systems like basic and digest auth.
  139. *
  140. * @param CakeRequest $request Request object.
  141. * @return mixed Either false or an array of user information
  142. */
  143. public function getUser($request) {
  144. return false;
  145. }
  146. }