2007-01-12 17:49:02 +00:00
|
|
|
<?php
|
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Authentication component
|
|
|
|
*
|
|
|
|
* Manages user logins and permissions.
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2007-02-02 10:39:45 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
|
|
|
|
* Copyright 2005-2007, Cake Software Foundation, Inc.
|
2007-01-12 17:49:02 +00:00
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
2007-02-02 10:39:45 +00:00
|
|
|
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
|
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
2007-01-12 17:49:02 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.controller.components
|
2007-02-02 10:39:45 +00:00
|
|
|
* @since CakePHP(tm) v 0.10.0.1076
|
2007-01-12 17:49:02 +00:00
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
|
*/
|
|
|
|
|
|
|
|
uses('set', 'security');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Authentication control component class
|
|
|
|
*
|
|
|
|
* Binds access control with user authentication and session management.
|
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.controller.components
|
|
|
|
*/
|
|
|
|
class AuthComponent extends Object {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Other components utilized by AuthComponent
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $components = array('Session', 'Acl', 'RequestHandler');
|
|
|
|
/**
|
|
|
|
* The name of an optional view element to render when an Ajax request is made
|
|
|
|
* with an invalid or expired session
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $ajaxLogin = null;
|
|
|
|
/**
|
|
|
|
* The name of the model that represents users which will be authenticated. If
|
|
|
|
* this value is unspecified, AuthComponent will look for one of the following:
|
|
|
|
* 'User', 'Person', 'Contact', 'Member', 'Customer', 'Account', 'Client',
|
|
|
|
* 'Employee', 'Staff', or 'Friend', in that order.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $userModel = null;
|
|
|
|
/**
|
|
|
|
* Additional query conditions to use when looking up and authenticating users,
|
|
|
|
* i.e. array('User.is_active' => 1).
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $userScope = array();
|
|
|
|
/**
|
|
|
|
* Allows you to specify non-default login name and password fields used in
|
|
|
|
* $userModel, i.e. array('username' => 'login_name', 'password' => 'passwd').
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $fields = array('username' => 'username', 'password' => 'password');
|
|
|
|
/**
|
|
|
|
* The session key name where the record of the current user is stored. If
|
|
|
|
* unspecified, it will be "Auth.{$userModel name}".
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $sessionKey = null;
|
|
|
|
/**
|
|
|
|
* If using action-based access control, this defines how the paths to action
|
|
|
|
* ACO nodes is computed. If, for example, all controller nodes are nested
|
|
|
|
* under an ACO node named 'Controllers', $actionPath should be set to
|
|
|
|
* "Controllers/:controller/:action".
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $actionPath = ':controller/:action';
|
|
|
|
/**
|
|
|
|
* A URL (defined as a string or array) to the controller action that handles
|
|
|
|
* logins.
|
|
|
|
*
|
|
|
|
* @var mixed
|
|
|
|
* @access public
|
|
|
|
*/
|
2007-01-22 08:08:34 +00:00
|
|
|
var $loginAction = null;
|
2007-01-12 17:49:02 +00:00
|
|
|
/**
|
|
|
|
* Normally, if a user is redirected to the $loginAction page, the location they
|
|
|
|
* were redirected from will be stored in the session so that they can be
|
|
|
|
* redirected back after a successful login. If this session value is not
|
|
|
|
* set, the user will be redirected to the page specified in $loginRedirect.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $loginRedirect = null;
|
|
|
|
/**
|
|
|
|
* Holds a reference to the model object specified by $userModel.
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $_model = null;
|
|
|
|
/**
|
|
|
|
* The type of automatic ACL validation to perform, where 'actions' validates
|
|
|
|
* the controller action of the current request, 'objects' validates against
|
|
|
|
* model objects accessed, and null prevents automatic validation.
|
|
|
|
*
|
2007-01-22 08:08:34 +00:00
|
|
|
* @var string
|
2007-01-12 17:49:02 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2007-01-22 08:08:34 +00:00
|
|
|
var $validate = 'actions';
|
2007-01-16 12:51:56 +00:00
|
|
|
/**
|
|
|
|
* Error to display when user login fails. For security purposes, only one error is used for all
|
|
|
|
* login failures, so as not to expose information on why the login failed.
|
|
|
|
*
|
2007-01-22 08:08:34 +00:00
|
|
|
* @var string
|
2007-01-16 12:51:56 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $loginError = 'Login failed. Invalid username or password.';
|
2007-01-23 15:22:23 +00:00
|
|
|
/**
|
|
|
|
* Maintains current user login state.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $_loggedIn = false;
|
2007-01-24 02:28:16 +00:00
|
|
|
/**
|
|
|
|
* Determines whether AuthComponent will automatically redirect and exit if login is successful.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $autoRedirect = true;
|
2007-01-22 08:08:34 +00:00
|
|
|
/**
|
|
|
|
* Controller actions for which user validation is not required.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
* @see AuthComponent::allow()
|
|
|
|
*/
|
|
|
|
var $allowedActions = array();
|
2007-01-12 17:49:02 +00:00
|
|
|
/**
|
|
|
|
* Main execution method. Handles redirecting of invalid users, and processing
|
|
|
|
* of login form data.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param object $controller A reference to the instantiating controller object
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function startup(&$controller) {
|
|
|
|
|
2007-01-22 08:08:34 +00:00
|
|
|
if (low($controller->name) == 'app' || (low($controller->name) == 'tests' && DEBUG > 0)) {
|
2007-01-12 17:49:02 +00:00
|
|
|
return;
|
|
|
|
}
|
2007-01-22 08:08:34 +00:00
|
|
|
|
2007-01-23 15:22:23 +00:00
|
|
|
// Hash incoming passwords
|
|
|
|
if (isset($controller->data[$this->userModel])) {
|
|
|
|
if (isset($controller->data[$this->userModel][$this->fields['username']]) && isset($controller->data[$this->userModel][$this->fields['password']])) {
|
|
|
|
$model =& $this->getUserModel();
|
|
|
|
$controller->data[$this->userModel][$this->fields['password']] = Security::hash($controller->data[$this->userModel][$this->fields['password']]);
|
|
|
|
}
|
|
|
|
}
|
2007-01-22 08:08:34 +00:00
|
|
|
|
2007-01-24 02:28:16 +00:00
|
|
|
if (!$this->_setDefaults($controller)) {
|
2007-01-23 15:22:23 +00:00
|
|
|
return;
|
2007-01-12 17:49:02 +00:00
|
|
|
}
|
|
|
|
|
2007-01-16 12:51:56 +00:00
|
|
|
if (!isset($controller->params['url']['url'])) {
|
|
|
|
$url = '';
|
|
|
|
} else {
|
|
|
|
$url = $controller->params['url']['url'];
|
|
|
|
}
|
|
|
|
|
2007-01-22 08:08:34 +00:00
|
|
|
if ($this->_normalizeURL($this->loginAction) == $this->_normalizeURL($url)) {
|
2007-01-12 17:49:02 +00:00
|
|
|
// We're already at the login action
|
2007-01-22 08:08:34 +00:00
|
|
|
if (empty($controller->data) || !isset($controller->data[$this->userModel])) {
|
2007-01-12 17:49:02 +00:00
|
|
|
return;
|
|
|
|
}
|
2007-01-22 08:08:34 +00:00
|
|
|
$data = array(
|
2007-02-05 20:19:34 +00:00
|
|
|
$this->userModel . '.' . $this->fields['username'] => '= ' . $controller->data[$this->userModel][$this->fields['username']],
|
|
|
|
$this->userModel . '.' . $this->fields['password'] => '= ' . $controller->data[$this->userModel][$this->fields['password']]
|
2007-01-22 08:08:34 +00:00
|
|
|
);
|
2007-01-23 15:22:23 +00:00
|
|
|
if ($this->login($data)) {
|
|
|
|
$controller->redirect($this->redirect(), null, true);
|
2007-01-22 08:08:34 +00:00
|
|
|
} else {
|
2007-01-23 15:22:23 +00:00
|
|
|
$this->Session->setFlash($this->loginError, 'default', array(), 'Auth.login');
|
2007-02-05 20:19:34 +00:00
|
|
|
unset($controller->data[$this->userModel][$this->fields['password']]);
|
2007-01-12 17:49:02 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else {
|
2007-01-22 08:08:34 +00:00
|
|
|
if (!$this->Session->check($this->sessionKey)) {
|
2007-01-12 17:49:02 +00:00
|
|
|
if (!$this->RequestHandler->isAjax()) {
|
2007-01-16 12:51:56 +00:00
|
|
|
$this->Session->write('Auth.redirect', $url);
|
2007-01-12 17:49:02 +00:00
|
|
|
$controller->redirect('/' . $this->loginAction, null, true);
|
|
|
|
} elseif ($this->ajaxLogin != null) {
|
|
|
|
$this->viewPath = 'elements';
|
|
|
|
$this->render($this->ajaxLogin, 'ajax');
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-22 08:08:34 +00:00
|
|
|
switch ($this->validate) {
|
2007-01-12 17:49:02 +00:00
|
|
|
case 'actions':
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 'objects':
|
|
|
|
|
|
|
|
break;
|
2007-01-23 15:22:23 +00:00
|
|
|
case null:
|
|
|
|
case false:
|
2007-02-05 20:19:34 +00:00
|
|
|
return;
|
2007-01-23 15:22:23 +00:00
|
|
|
break;
|
2007-01-12 17:49:02 +00:00
|
|
|
default:
|
|
|
|
trigger_error(__('Auth::startup() - $type is set to an incorrect value. Should be "actions", "objects", or null.'), E_USER_WARNING);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Attempts to introspect the correct values for object properties including
|
|
|
|
* $userModel and $sessionKey.
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @param object $controller A reference to the instantiating controller object
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function _setDefaults(&$controller) {
|
2007-01-24 07:26:52 +00:00
|
|
|
$this->data = $controller->data;
|
|
|
|
|
2007-01-24 02:28:16 +00:00
|
|
|
if ($this->allowedActions == array('*') || in_array($controller->action, $this->allowedActions)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-01-12 17:49:02 +00:00
|
|
|
if (empty($this->userModel)) {
|
|
|
|
$classes = array_values(array_intersect(
|
|
|
|
array_map('strtolower', get_declared_classes()),
|
|
|
|
array('user', 'person', 'contact', 'member', 'customer', 'account', 'client', 'employee', 'staff', 'friend')
|
|
|
|
));
|
|
|
|
|
|
|
|
if (!empty($classes)) {
|
|
|
|
$this->userModel = ucwords($classes[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-24 02:28:16 +00:00
|
|
|
if (empty($this->userModel)) {
|
2007-02-05 20:19:34 +00:00
|
|
|
trigger_error(__('Could not find $userModel. Please set AuthComponent::$userModel in beforeFilter().'), E_USER_WARNING);
|
2007-01-24 02:28:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-01-22 08:08:34 +00:00
|
|
|
if (empty($this->loginAction)) {
|
|
|
|
$this->loginAction = Inflector::underscore(Inflector::pluralize($this->userModel)) . '/login';
|
|
|
|
}
|
|
|
|
|
2007-01-24 07:26:52 +00:00
|
|
|
if (empty($this->sessionKey)) {
|
2007-01-13 23:05:26 +00:00
|
|
|
$this->sessionKey = 'Auth.' . $this->userModel;
|
2007-01-12 17:49:02 +00:00
|
|
|
}
|
2007-01-24 02:28:16 +00:00
|
|
|
return true;
|
2007-01-12 17:49:02 +00:00
|
|
|
}
|
2007-01-22 08:08:34 +00:00
|
|
|
/**
|
2007-01-24 04:38:39 +00:00
|
|
|
* Takes a list of actions in the current controller for which authentication is not required, or
|
2007-01-22 08:08:34 +00:00
|
|
|
* no parameters to allow all actions.
|
|
|
|
*
|
|
|
|
* @access public
|
2007-01-23 15:22:23 +00:00
|
|
|
* @param string $action Controller action name
|
|
|
|
* @param string $action Controller action name
|
|
|
|
* @param string ... etc.
|
2007-01-22 08:08:34 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function allow() {
|
|
|
|
$args = func_get_args();
|
|
|
|
if (empty($args)) {
|
|
|
|
$this->allowedActions = array('*');
|
|
|
|
} else {
|
|
|
|
$this->allowedActions = $args;
|
|
|
|
}
|
|
|
|
}
|
2007-01-23 15:22:23 +00:00
|
|
|
/**
|
2007-01-24 04:38:39 +00:00
|
|
|
* Manually log-in a user with the given parameter data. The $data provided can be any data
|
|
|
|
* structure used to identify a user in AuthComponent::identify(). If $data is empty or not
|
|
|
|
* specified, POST data from Controller::$data will be used automatically.
|
|
|
|
*
|
|
|
|
* After (if) login is successful, the user record is written to the session key specified in
|
|
|
|
* AuthComponent::$sessionKey.
|
2007-01-23 15:22:23 +00:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param mixed $data User object
|
|
|
|
* @return boolean True on login success, false on failure
|
|
|
|
*/
|
|
|
|
function login($data = null) {
|
|
|
|
$this->_loggedIn = false;
|
|
|
|
|
|
|
|
if (empty($data)) {
|
|
|
|
$data = $this->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($user = $this->identify($data)) {
|
|
|
|
$this->Session->write($this->sessionKey, $user);
|
|
|
|
$this->_loggedIn = true;
|
|
|
|
}
|
|
|
|
return $this->_loggedIn;
|
|
|
|
}
|
2007-01-24 02:28:16 +00:00
|
|
|
/**
|
|
|
|
* Logs a user out, and returns the login action to redirect to.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param mixed $url Optional URL to redirect the user to after logout
|
|
|
|
* @return string AuthComponent::$loginAction
|
2007-01-24 04:38:39 +00:00
|
|
|
* @see AuthComponent::$loginAction
|
2007-01-24 02:28:16 +00:00
|
|
|
*/
|
|
|
|
function logout() {
|
2007-01-24 07:26:52 +00:00
|
|
|
$this->Session->del($this->sessionKey);
|
|
|
|
$this->Session->del('Auth.redirect');
|
2007-01-24 02:28:16 +00:00
|
|
|
$this->_loggedIn = false;
|
|
|
|
return $this->_normalizeURL($this->loginAction);
|
|
|
|
}
|
2007-01-23 16:27:34 +00:00
|
|
|
/**
|
|
|
|
* Get the current user from the session.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return array User record, or null if no user is logged in.
|
|
|
|
*/
|
2007-01-24 07:26:52 +00:00
|
|
|
function user($key = null) {
|
2007-01-23 16:27:34 +00:00
|
|
|
if (!$this->Session->check($this->sessionKey)) {
|
|
|
|
return null;
|
|
|
|
}
|
2007-01-24 07:26:52 +00:00
|
|
|
if ($key == null) {
|
|
|
|
return array($this->userModel => $this->Session->read($this->sessionKey));
|
|
|
|
} else {
|
|
|
|
$user = $this->Session->read($this->sessionKey);
|
|
|
|
|
|
|
|
if (isset($user[$key])) {
|
|
|
|
return $user[$key];
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2007-01-23 16:27:34 +00:00
|
|
|
}
|
2007-01-23 15:22:23 +00:00
|
|
|
/**
|
|
|
|
* Gets the authentication redirect URL
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return string Redirect URL
|
|
|
|
*/
|
|
|
|
function redirect() {
|
|
|
|
if ($this->Session->check('Auth.redirect')) {
|
|
|
|
$redir = $this->Session->read('Auth.redirect');
|
|
|
|
$this->Session->delete('Auth.redirect');
|
|
|
|
} else {
|
|
|
|
$redir = $this->loginRedirect;
|
|
|
|
}
|
2007-01-24 02:28:16 +00:00
|
|
|
return $this->_normalizeURL('/' . $redir);
|
2007-01-23 15:22:23 +00:00
|
|
|
}
|
2007-01-12 17:49:02 +00:00
|
|
|
/**
|
|
|
|
* Validates a user against an abstract object.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param mixed $object The object to validate the user against.
|
|
|
|
* @param mixed $user Optional. The identity of the user to be validated.
|
|
|
|
* Uses the current user session if none specified. For
|
|
|
|
* valid forms of identifying users, see
|
|
|
|
* AuthComponent::identify().
|
|
|
|
* @see AuthComponent::identify()
|
|
|
|
* @return boolean True if the user validates, false otherwise.
|
|
|
|
*/
|
|
|
|
function validate($object, $user = null) {
|
|
|
|
$user = $this->identify($user);
|
|
|
|
if ($user == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
pr($user);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Validates a user against a controller action.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $action Optional. The controller/action path to validate the
|
|
|
|
* user against. The current request action is used if
|
|
|
|
* none is specified.
|
|
|
|
* @param mixed $user Optional. The identity of the user to be validated.
|
|
|
|
* Uses the current user session if none specified. For
|
|
|
|
* valid forms of identifying users, see
|
|
|
|
* AuthComponent::identify().
|
|
|
|
* @see AuthComponent::validate()
|
|
|
|
* @return boolean True if the user validates, false otherwise.
|
|
|
|
*/
|
|
|
|
function validateAction($action = null, $user = null) {
|
|
|
|
$path = r(
|
|
|
|
array(':controller', ':action'),
|
|
|
|
array($this->params['controller'], $this->params['action']),
|
|
|
|
$path
|
|
|
|
);
|
|
|
|
return $this->validate($path, $user);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Returns a reference to the model object specified by $userModel, and attempts
|
|
|
|
* to load it if it is not found.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return object A reference to a model object.
|
|
|
|
*/
|
|
|
|
function &getUserModel() {
|
|
|
|
if (!ClassRegistry::isKeySet($this->userModel)) {
|
|
|
|
if (!loadModel($this->userModel)) {
|
|
|
|
trigger_error(__('Auth::getUserModel() - $userModel is not set or could not be found') . $this->userModel, E_USER_WARNING);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (PHP5) {
|
|
|
|
$user = ClassRegistry::getObject($this->userModel);
|
|
|
|
} else {
|
|
|
|
$user =& ClassRegistry::getObject($this->userModel);
|
|
|
|
}
|
|
|
|
if (empty($user)) {
|
2007-01-22 08:08:34 +00:00
|
|
|
trigger_error(__('Auth::getUserModel() - $userModel is not set or could not be found ') . $this->userModel, E_USER_WARNING);
|
2007-01-12 17:49:02 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Identifies a user based on specific criteria.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param mixed $user Optional. The identity of the user to be validated.
|
|
|
|
* Uses the current user session if none specified.
|
|
|
|
* @return array User record data, or null, if the user could not be identified.
|
|
|
|
*/
|
|
|
|
function identify($user = null) {
|
|
|
|
if ($user == null) {
|
|
|
|
$model =& $this->getUserModel();
|
2007-01-23 15:22:23 +00:00
|
|
|
} else if (is_object($user) && is_a($user, 'Model')) {
|
|
|
|
if (!$user->exists()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$user = $user->read();
|
|
|
|
$user = $user[$this->userModel];
|
2007-01-12 17:49:02 +00:00
|
|
|
} else if (is_array($user) && isset($user[$this->userModel])) {
|
|
|
|
$user = $user[$this->userModel];
|
2007-01-23 15:22:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($user) && (isset($user[$this->fields['username']]) || isset($user[$this->userModel . '.' . $this->fields['username']]))) {
|
|
|
|
if (isset($user[$this->fields['username']])) {
|
|
|
|
$find = array(
|
|
|
|
$this->fields['username'] => $user[$this->fields['username']],
|
|
|
|
$this->fields['password'] => $user[$this->fields['password']]
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$find = array(
|
|
|
|
$this->fields['username'] => $user[$this->userModel . '.' . $this->fields['username']],
|
|
|
|
$this->fields['password'] => $user[$this->userModel . '.' . $this->fields['password']]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2007-01-12 17:49:02 +00:00
|
|
|
$model =& $this->getUserModel();
|
2007-01-23 15:22:23 +00:00
|
|
|
$data = $model->find(am($find, $this->userScope), null, null, -1);
|
2007-01-12 17:49:02 +00:00
|
|
|
|
|
|
|
if (empty($data) || empty($data[$this->userModel])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $data[$this->userModel];
|
|
|
|
} else if (is_numeric($user)) {
|
|
|
|
// Assume it's a user's ID
|
|
|
|
$model =& $this->getUserModel();
|
|
|
|
$data = $model->find(array($this->userModel . $model->primaryKey => $user));
|
|
|
|
|
|
|
|
if (empty($data) || empty($data[$this->userModel])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $data[$this->userModel];
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Allows setting of multiple properties of AuthComponent in a single line of code.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param array $properties An associative array containing AuthComponent
|
|
|
|
* properties and corresponding values.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function set($properties = array()) {
|
|
|
|
if (is_array($properties) && !empty($properties)) {
|
|
|
|
$vars = get_object_vars($this);
|
|
|
|
foreach ($properties as $key => $val) {
|
|
|
|
if (array_key_exists($key, $vars)) {
|
|
|
|
$this->{$key} = $val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-01-23 15:22:23 +00:00
|
|
|
/**
|
|
|
|
* Component shutdown. If user is logged in, wipe out redirect.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param object $controller
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function shutdown(&$controller) {
|
|
|
|
if ($this->_loggedIn) {
|
|
|
|
$this->Session->del('Auth.redirect');
|
|
|
|
}
|
|
|
|
}
|
2007-01-22 08:08:34 +00:00
|
|
|
/**
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function _normalizeURL($url = '/') {
|
|
|
|
if (is_array($url)) {
|
|
|
|
$url = Router::url($url);
|
|
|
|
$paths = Router::getPaths();
|
|
|
|
$url = r($paths['base'], '', $url);
|
|
|
|
}
|
2007-01-24 02:28:16 +00:00
|
|
|
$url = '/' . $url . '/';
|
|
|
|
|
|
|
|
while (strpos($url, '//') !== false) {
|
|
|
|
$url = r('//', '/', $url);
|
|
|
|
}
|
|
|
|
return $url;
|
2007-01-22 08:08:34 +00:00
|
|
|
}
|
2007-01-12 17:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|