PageRenderTime 55ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Controller/Component/Auth/BaseAuthenticate.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 134 lines | 46 code | 11 blank | 77 comment | 3 complexity | f7209bb23ec4ab3aae10384a147fc22d MD5 | raw file
  1. <?php
  2. /**
  3. * PHP 5
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright 2005-2011, 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-2011, 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.Controller.Component.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'] => $this->_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. * Hash the plain text password so that it matches the hashed/encrypted password
  86. * in the datasource.
  87. *
  88. * @param string $password The plain text password.
  89. * @return string The hashed form of the password.
  90. */
  91. protected function _password($password) {
  92. return Security::hash($password, null, true);
  93. }
  94. /**
  95. * Authenticate a user based on the request information.
  96. *
  97. * @param CakeRequest $request Request to get authentication information from.
  98. * @param CakeResponse $response A response object that can have headers added.
  99. * @return mixed Either false on failure, or an array of user data on success.
  100. */
  101. abstract public function authenticate(CakeRequest $request, CakeResponse $response);
  102. /**
  103. * Allows you to hook into AuthComponent::logout(),
  104. * and implement specialized logout behavior.
  105. *
  106. * All attached authentication objects will have this method
  107. * called when a user logs out.
  108. *
  109. * @param array $user The user about to be logged out.
  110. * @return void
  111. */
  112. public function logout($user) { }
  113. /**
  114. * Get a user based on information in the request. Primarily used by stateless authentication
  115. * systems like basic and digest auth.
  116. *
  117. * @param CakeRequest $request Request object.
  118. * @return mixed Either false or an array of user information
  119. */
  120. public function getUser($request) {
  121. return false;
  122. }
  123. }