2011-01-21 23:09:41 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PHP 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2011-05-29 21:31:39 +00:00
|
|
|
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2011-01-21 23:09:41 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2011-05-29 21:31:39 +00:00
|
|
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2011-01-21 23:09:41 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
2011-03-05 22:10:42 +00:00
|
|
|
|
|
|
|
App::uses('Security', 'Utility');
|
2011-01-21 23:09:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Base Authentication class with common methods and properties.
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Controller.Component.Auth
|
2011-01-21 23:09:41 +00:00
|
|
|
*/
|
|
|
|
abstract class BaseAuthenticate {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Settings for this object.
|
|
|
|
*
|
|
|
|
* - `fields` The fields to use to identify a user by.
|
|
|
|
* - `userModel` The model name of the User, defaults to User.
|
|
|
|
* - `scope` Additional conditions to use when looking up and authenticating users,
|
|
|
|
* i.e. `array('User.is_active' => 1).`
|
2011-10-19 15:54:08 +00:00
|
|
|
* - `recursive` The value of the recursive key passed to find(). Defaults to 0.
|
2011-01-21 23:09:41 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $settings = array(
|
|
|
|
'fields' => array(
|
|
|
|
'username' => 'username',
|
|
|
|
'password' => 'password'
|
|
|
|
),
|
|
|
|
'userModel' => 'User',
|
2011-10-19 15:54:08 +00:00
|
|
|
'scope' => array(),
|
|
|
|
'recursive' => 0
|
2011-01-21 23:09:41 +00:00
|
|
|
);
|
|
|
|
|
2011-02-18 04:17:07 +00:00
|
|
|
/**
|
|
|
|
* A Component collection, used to get more components.
|
|
|
|
*
|
|
|
|
* @var ComponentCollection
|
|
|
|
*/
|
|
|
|
protected $_Collection;
|
|
|
|
|
2011-01-21 23:09:41 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2011-02-18 04:17:07 +00:00
|
|
|
* @param ComponentCollection $collection The Component collection used on this request.
|
2011-01-21 23:09:41 +00:00
|
|
|
* @param array $settings Array of settings to use.
|
|
|
|
*/
|
2011-02-18 04:17:07 +00:00
|
|
|
public function __construct(ComponentCollection $collection, $settings) {
|
|
|
|
$this->_Collection = $collection;
|
2011-01-21 23:09:41 +00:00
|
|
|
$this->settings = Set::merge($this->settings, $settings);
|
|
|
|
}
|
|
|
|
|
2011-01-22 01:52:38 +00:00
|
|
|
/**
|
|
|
|
* Find a user record using the standard options.
|
|
|
|
*
|
|
|
|
* @param string $username The username/identifier.
|
|
|
|
* @param string $password The unhashed password.
|
|
|
|
* @return Mixed Either false on failure, or an array of user data.
|
|
|
|
*/
|
|
|
|
protected function _findUser($username, $password) {
|
|
|
|
$userModel = $this->settings['userModel'];
|
|
|
|
list($plugin, $model) = pluginSplit($userModel);
|
|
|
|
$fields = $this->settings['fields'];
|
|
|
|
|
|
|
|
$conditions = array(
|
|
|
|
$model . '.' . $fields['username'] => $username,
|
2011-09-21 11:38:22 +00:00
|
|
|
$model . '.' . $fields['password'] => $this->_password($password),
|
2011-01-22 01:52:38 +00:00
|
|
|
);
|
|
|
|
if (!empty($this->settings['scope'])) {
|
|
|
|
$conditions = array_merge($conditions, $this->settings['scope']);
|
|
|
|
}
|
|
|
|
$result = ClassRegistry::init($userModel)->find('first', array(
|
|
|
|
'conditions' => $conditions,
|
2011-10-19 15:54:08 +00:00
|
|
|
'recursive' => $this->settings['recursive']
|
2011-01-22 01:52:38 +00:00
|
|
|
));
|
|
|
|
if (empty($result) || empty($result[$model])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
unset($result[$model][$fields['password']]);
|
|
|
|
return $result[$model];
|
2011-09-21 11:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-12-02 05:58:09 +00:00
|
|
|
* Hash the plain text password so that it matches the hashed/encrypted password
|
2011-09-21 11:38:22 +00:00
|
|
|
* in the datasource.
|
|
|
|
*
|
|
|
|
* @param string $password The plain text password.
|
|
|
|
* @return string The hashed form of the password.
|
|
|
|
*/
|
|
|
|
protected function _password($password) {
|
|
|
|
return Security::hash($password, null, true);
|
2011-01-22 01:52:38 +00:00
|
|
|
}
|
|
|
|
|
2011-01-21 23:09:41 +00:00
|
|
|
/**
|
|
|
|
* Authenticate a user based on the request information.
|
|
|
|
*
|
|
|
|
* @param CakeRequest $request Request to get authentication information from.
|
2011-01-22 00:56:23 +00:00
|
|
|
* @param CakeResponse $response A response object that can have headers added.
|
2011-01-21 23:09:41 +00:00
|
|
|
* @return mixed Either false on failure, or an array of user data on success.
|
|
|
|
*/
|
2011-01-22 00:56:23 +00:00
|
|
|
abstract public function authenticate(CakeRequest $request, CakeResponse $response);
|
2011-02-05 23:41:00 +00:00
|
|
|
|
2011-07-03 16:53:21 +00:00
|
|
|
/**
|
2011-08-16 03:55:08 +00:00
|
|
|
* Allows you to hook into AuthComponent::logout(),
|
2011-12-02 05:58:09 +00:00
|
|
|
* and implement specialized logout behavior.
|
2011-08-16 03:55:08 +00:00
|
|
|
*
|
|
|
|
* All attached authentication objects will have this method
|
2011-07-03 16:53:21 +00:00
|
|
|
* called when a user logs out.
|
|
|
|
*
|
|
|
|
* @param array $user The user about to be logged out.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function logout($user) { }
|
|
|
|
|
2011-02-05 23:41:00 +00:00
|
|
|
/**
|
|
|
|
* Get a user based on information in the request. Primarily used by stateless authentication
|
|
|
|
* systems like basic and digest auth.
|
|
|
|
*
|
|
|
|
* @param CakeRequest $request Request object.
|
|
|
|
* @return mixed Either false or an array of user information
|
|
|
|
*/
|
|
|
|
public function getUser($request) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-03 16:53:21 +00:00
|
|
|
}
|