BaseAuthenticate.php 5.1 KB

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