mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Adding automatic password hashing for user logins and creates in AuthComponent
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4321 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
dcca6e711c
commit
6c670400ca
1 changed files with 98 additions and 23 deletions
|
@ -141,6 +141,13 @@ class AuthComponent extends Object {
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
var $loginError = 'Login failed. Invalid username or password.';
|
var $loginError = 'Login failed. Invalid username or password.';
|
||||||
|
/**
|
||||||
|
* Maintains current user login state.
|
||||||
|
*
|
||||||
|
* @var boolean
|
||||||
|
* @access private
|
||||||
|
*/
|
||||||
|
var $_loggedIn = false;
|
||||||
/**
|
/**
|
||||||
* Controller actions for which user validation is not required.
|
* Controller actions for which user validation is not required.
|
||||||
*
|
*
|
||||||
|
@ -163,14 +170,21 @@ class AuthComponent extends Object {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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']]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->_setDefaults($controller);
|
||||||
|
|
||||||
if ($this->allowedActions == array('*') || in_array($controller->action, $this->allowedActions)) {
|
if ($this->allowedActions == array('*') || in_array($controller->action, $this->allowedActions)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->_setDefaults($controller);
|
|
||||||
|
|
||||||
if (empty($this->userModel)) {
|
if (empty($this->userModel)) {
|
||||||
return false;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isset($controller->params['url']['url'])) {
|
if (!isset($controller->params['url']['url'])) {
|
||||||
|
@ -188,20 +202,13 @@ class AuthComponent extends Object {
|
||||||
|
|
||||||
$data = array(
|
$data = array(
|
||||||
$this->userModel . '.' . $this->fields['username'] => $controller->data[$this->userModel][$this->fields['username']],
|
$this->userModel . '.' . $this->fields['username'] => $controller->data[$this->userModel][$this->fields['username']],
|
||||||
$this->userModel . '.' . $this->fields['password'] => Security::hash($controller->data[$this->userModel][$this->fields['password']])
|
$this->userModel . '.' . $this->fields['password'] => $controller->data[$this->userModel][$this->fields['password']]
|
||||||
);
|
);
|
||||||
|
|
||||||
if ($user = $this->identify($data)) {
|
if ($this->login($data)) {
|
||||||
$this->Session->write($this->sessionKey, $user);
|
$controller->redirect($this->redirect(), null, true);
|
||||||
if ($this->Session->check('Auth.redirect')) {
|
|
||||||
$redir = $this->Session->read('Auth.redirect');
|
|
||||||
$this->Session->delete('Auth.redirect');
|
|
||||||
} else {
|
|
||||||
$redir = $this->loginRedirect;
|
|
||||||
}
|
|
||||||
$controller->redirect('/' . $redir, null, true);
|
|
||||||
} else {
|
} else {
|
||||||
$this->Session->setFlash(__($this->loginError), 'default', array(), 'Auth.login');
|
$this->Session->setFlash($this->loginError, 'default', array(), 'Auth.login');
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -213,6 +220,7 @@ class AuthComponent extends Object {
|
||||||
$this->Session->write('Auth.redirect', $url);
|
$this->Session->write('Auth.redirect', $url);
|
||||||
$controller->redirect('/' . $this->loginAction, null, true);
|
$controller->redirect('/' . $this->loginAction, null, true);
|
||||||
} elseif ($this->ajaxLogin != null) {
|
} elseif ($this->ajaxLogin != null) {
|
||||||
|
$this->_loggedIn = true;
|
||||||
$this->viewPath = 'elements';
|
$this->viewPath = 'elements';
|
||||||
$this->render($this->ajaxLogin, 'ajax');
|
$this->render($this->ajaxLogin, 'ajax');
|
||||||
exit();
|
exit();
|
||||||
|
@ -232,8 +240,9 @@ class AuthComponent extends Object {
|
||||||
case 'objects':
|
case 'objects':
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case null: break;
|
case null:
|
||||||
case false: break;
|
case false:
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
trigger_error(__('Auth::startup() - $type is set to an incorrect value. Should be "actions", "objects", or null.'), E_USER_WARNING);
|
trigger_error(__('Auth::startup() - $type is set to an incorrect value. Should be "actions", "objects", or null.'), E_USER_WARNING);
|
||||||
break;
|
break;
|
||||||
|
@ -266,15 +275,16 @@ class AuthComponent extends Object {
|
||||||
if (empty($this->sessionKey) && !empty($this->userModel)) {
|
if (empty($this->sessionKey) && !empty($this->userModel)) {
|
||||||
$this->sessionKey = 'Auth.' . $this->userModel;
|
$this->sessionKey = 'Auth.' . $this->userModel;
|
||||||
}
|
}
|
||||||
|
$this->data = $controller->data;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Takes a list of actions in the current controller for which validation is not required, or
|
* Takes a list of actions in the current controller for which validation is not required, or
|
||||||
* no parameters to allow all actions.
|
* no parameters to allow all actions.
|
||||||
*
|
*
|
||||||
* @access public
|
* @access public
|
||||||
* @param string $action
|
* @param string $action Controller action name
|
||||||
* @param string $action
|
* @param string $action Controller action name
|
||||||
* @param string ...
|
* @param string ... etc.
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
function allow() {
|
function allow() {
|
||||||
|
@ -285,6 +295,41 @@ class AuthComponent extends Object {
|
||||||
$this->allowedActions = $args;
|
$this->allowedActions = $args;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Manually log-in a user with the given parameter data.
|
||||||
|
*
|
||||||
|
* @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;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
return $redir;
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Validates a user against an abstract object.
|
* Validates a user against an abstract object.
|
||||||
*
|
*
|
||||||
|
@ -362,13 +407,31 @@ class AuthComponent extends Object {
|
||||||
function identify($user = null) {
|
function identify($user = null) {
|
||||||
if ($user == null) {
|
if ($user == null) {
|
||||||
$model =& $this->getUserModel();
|
$model =& $this->getUserModel();
|
||||||
} else if (is_object($user) && is_a($user, 'model')) {
|
} else if (is_object($user) && is_a($user, 'Model')) {
|
||||||
|
if (!$user->exists()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
$user = $user->read();
|
||||||
|
$user = $user[$this->userModel];
|
||||||
} else if (is_array($user) && isset($user[$this->userModel])) {
|
} else if (is_array($user) && isset($user[$this->userModel])) {
|
||||||
$user = $user[$this->userModel];
|
$user = $user[$this->userModel];
|
||||||
} else if (is_array($user) && (isset($user[$this->fields['username']]) || isset($user[$this->userModel . '.' . $this->fields['username']]))) {
|
}
|
||||||
|
|
||||||
|
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']]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
$model =& $this->getUserModel();
|
$model =& $this->getUserModel();
|
||||||
$data = $model->find($user, null, null, -1);
|
$data = $model->find(am($find, $this->userScope), null, null, -1);
|
||||||
|
|
||||||
if (empty($data) || empty($data[$this->userModel])) {
|
if (empty($data) || empty($data[$this->userModel])) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -405,6 +468,18 @@ class AuthComponent extends Object {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* 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');
|
||||||
|
}
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* @access private
|
* @access private
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Add table
Reference in a new issue