BaseAuthenticate.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * PHP 5
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright 2005-2010, 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-2010, 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. /**
  17. * Base Authentication class with common methods and properties.
  18. *
  19. * @package cake.libs.controller.components.auth
  20. */
  21. abstract class BaseAuthenticate {
  22. /**
  23. * Settings for this object.
  24. *
  25. * - `fields` The fields to use to identify a user by.
  26. * - `userModel` The model name of the User, defaults to User.
  27. * - `scope` Additional conditions to use when looking up and authenticating users,
  28. * i.e. `array('User.is_active' => 1).`
  29. *
  30. * @var array
  31. */
  32. public $settings = array(
  33. 'fields' => array(
  34. 'username' => 'username',
  35. 'password' => 'password'
  36. ),
  37. 'userModel' => 'User',
  38. 'scope' => array()
  39. );
  40. /**
  41. * A Component collection, used to get more components.
  42. *
  43. * @var ComponentCollection
  44. */
  45. protected $_Collection;
  46. /**
  47. * Constructor
  48. *
  49. * @param ComponentCollection $collection The Component collection used on this request.
  50. * @param array $settings Array of settings to use.
  51. */
  52. public function __construct(ComponentCollection $collection, $settings) {
  53. $this->_Collection = $collection;
  54. $this->settings = Set::merge($this->settings, $settings);
  55. }
  56. /**
  57. * Find a user record using the standard options.
  58. *
  59. * @param string $username The username/identifier.
  60. * @param string $password The unhashed password.
  61. * @return Mixed Either false on failure, or an array of user data.
  62. */
  63. protected function _findUser($username, $password) {
  64. $userModel = $this->settings['userModel'];
  65. list($plugin, $model) = pluginSplit($userModel);
  66. $fields = $this->settings['fields'];
  67. $conditions = array(
  68. $model . '.' . $fields['username'] => $username,
  69. $model . '.' . $fields['password'] => AuthComponent::password($password),
  70. );
  71. if (!empty($this->settings['scope'])) {
  72. $conditions = array_merge($conditions, $this->settings['scope']);
  73. }
  74. $result = ClassRegistry::init($userModel)->find('first', array(
  75. 'conditions' => $conditions,
  76. 'recursive' => 0
  77. ));
  78. if (empty($result) || empty($result[$model])) {
  79. return false;
  80. }
  81. unset($result[$model][$fields['password']]);
  82. return $result[$model];
  83. }
  84. /**
  85. * Authenticate a user based on the request information.
  86. *
  87. * @param CakeRequest $request Request to get authentication information from.
  88. * @param CakeResponse $response A response object that can have headers added.
  89. * @return mixed Either false on failure, or an array of user data on success.
  90. */
  91. abstract public function authenticate(CakeRequest $request, CakeResponse $response);
  92. /**
  93. * Get a user based on information in the request. Primarily used by stateless authentication
  94. * systems like basic and digest auth.
  95. *
  96. * @param CakeRequest $request Request object.
  97. * @return mixed Either false or an array of user information
  98. */
  99. public function getUser($request) {
  100. return false;
  101. }
  102. }