2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Authentication component
|
|
|
|
*
|
|
|
|
* Manages user logins and permissions.
|
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2008-05-30 11:40:08 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Controller.Component
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 0.10.0.1076
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
|
2011-01-28 06:36:30 +00:00
|
|
|
App::uses('Component', 'Controller');
|
2010-12-04 07:04:30 +00:00
|
|
|
App::uses('Router', 'Routing');
|
2010-12-15 05:50:02 +00:00
|
|
|
App::uses('Security', 'Utility');
|
2010-12-04 07:04:30 +00:00
|
|
|
App::uses('Debugger', 'Utility');
|
2012-03-11 01:57:18 +00:00
|
|
|
App::uses('Hash', 'Utility');
|
2011-02-22 02:58:30 +00:00
|
|
|
App::uses('CakeSession', 'Model/Datasource');
|
2011-03-05 22:24:42 +00:00
|
|
|
App::uses('BaseAuthorize', 'Controller/Component/Auth');
|
|
|
|
App::uses('BaseAuthenticate', 'Controller/Component/Auth');
|
2014-11-05 18:46:45 +00:00
|
|
|
App::uses('CakeEvent', 'Event');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Authentication control component class
|
|
|
|
*
|
|
|
|
* Binds access control with user authentication and session management.
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Controller.Component
|
2011-10-15 15:13:26 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-07-04 19:12:42 +00:00
|
|
|
class AuthComponent extends Component {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-11-28 22:30:47 +00:00
|
|
|
/**
|
|
|
|
* Constant for 'all'
|
2014-02-07 14:45:00 +00:00
|
|
|
*
|
|
|
|
* @var string
|
2012-11-28 22:30:47 +00:00
|
|
|
*/
|
2011-01-22 00:56:23 +00:00
|
|
|
const ALL = 'all';
|
2011-01-21 22:55:04 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Other components utilized by AuthComponent
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2015-06-24 12:46:48 +00:00
|
|
|
public $components = array('Session', 'Flash', 'RequestHandler');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* An array of authentication objects to use for authenticating users. You can configure
|
2011-01-03 23:18:42 +00:00
|
|
|
* multiple adapters and they will be checked sequentially when users are identified.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-01-21 22:55:04 +00:00
|
|
|
* $this->Auth->authenticate = array(
|
|
|
|
* 'Form' => array(
|
|
|
|
* 'userModel' => 'Users.User'
|
|
|
|
* )
|
|
|
|
* );
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-01-21 22:55:04 +00:00
|
|
|
*
|
|
|
|
* Using the class name without 'Authenticate' as the key, you can pass in an array of settings for each
|
2012-12-22 22:48:15 +00:00
|
|
|
* authentication object. Additionally you can define settings that should be set to all authentications objects
|
2011-01-25 03:13:39 +00:00
|
|
|
* using the 'all' key:
|
2011-01-21 22:55:04 +00:00
|
|
|
*
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-01-21 22:55:04 +00:00
|
|
|
* $this->Auth->authenticate = array(
|
2011-01-25 03:13:39 +00:00
|
|
|
* 'all' => array(
|
2011-01-21 22:55:04 +00:00
|
|
|
* 'userModel' => 'Users.User',
|
|
|
|
* 'scope' => array('User.active' => 1)
|
|
|
|
* ),
|
|
|
|
* 'Form',
|
|
|
|
* 'Basic'
|
|
|
|
* );
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-01-21 22:55:04 +00:00
|
|
|
*
|
2011-01-25 03:13:39 +00:00
|
|
|
* You can also use AuthComponent::ALL instead of the string 'all'.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-01-21 22:55:04 +00:00
|
|
|
* @var array
|
2011-10-15 15:13:26 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-01-03 23:18:42 +00:00
|
|
|
public $authenticate = array('Form');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-01-03 23:18:42 +00:00
|
|
|
* Objects that will be used for authentication checks.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-01-03 23:18:42 +00:00
|
|
|
* @var array
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-01-03 23:18:42 +00:00
|
|
|
protected $_authenticateObjects = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* An array of authorization objects to use for authorizing users. You can configure
|
2011-01-03 23:18:42 +00:00
|
|
|
* multiple adapters and they will be checked sequentially when authorization checks are done.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-01-21 22:55:04 +00:00
|
|
|
* $this->Auth->authorize = array(
|
|
|
|
* 'Crud' => array(
|
|
|
|
* 'actionPath' => 'controllers/'
|
|
|
|
* )
|
|
|
|
* );
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-01-21 22:55:04 +00:00
|
|
|
*
|
|
|
|
* Using the class name without 'Authorize' as the key, you can pass in an array of settings for each
|
2012-12-22 22:48:15 +00:00
|
|
|
* authorization object. Additionally you can define settings that should be set to all authorization objects
|
2011-01-25 03:13:39 +00:00
|
|
|
* using the 'all' key:
|
2011-01-21 22:55:04 +00:00
|
|
|
*
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-01-21 22:55:04 +00:00
|
|
|
* $this->Auth->authorize = array(
|
2011-01-25 03:13:39 +00:00
|
|
|
* 'all' => array(
|
2011-01-21 22:55:04 +00:00
|
|
|
* 'actionPath' => 'controllers/'
|
|
|
|
* ),
|
|
|
|
* 'Crud',
|
|
|
|
* 'CustomAuth'
|
|
|
|
* );
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-01-21 22:55:04 +00:00
|
|
|
*
|
2011-01-25 03:13:39 +00:00
|
|
|
* You can also use AuthComponent::ALL instead of the string 'all'
|
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* @var mixed
|
2012-04-27 02:49:18 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#authorization
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $authorize = false;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-08-13 15:18:46 +00:00
|
|
|
/**
|
2011-01-03 21:48:16 +00:00
|
|
|
* Objects that will be used for authorization checks.
|
2009-08-13 15:18:46 +00:00
|
|
|
*
|
2011-01-03 21:48:16 +00:00
|
|
|
* @var array
|
2009-08-13 15:18:46 +00:00
|
|
|
*/
|
2011-01-03 21:48:16 +00:00
|
|
|
protected $_authorizeObjects = array();
|
2009-08-13 15:18:46 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* The name of an optional view element to render when an Ajax request is made
|
|
|
|
* with an invalid or expired session
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $ajaxLogin = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-01-19 21:17:17 +00:00
|
|
|
* Settings to use when Auth needs to do a flash message with SessionComponent::setFlash().
|
|
|
|
* Available keys are:
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-01-19 21:17:17 +00:00
|
|
|
* - `element` - The element to use, defaults to 'default'.
|
|
|
|
* - `key` - The key to use, defaults to 'auth'
|
|
|
|
* - `params` - The array of additional params to use, defaults to array()
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-01-19 21:17:17 +00:00
|
|
|
public $flash = array(
|
|
|
|
'element' => 'default',
|
|
|
|
'key' => 'auth',
|
|
|
|
'params' => array()
|
|
|
|
);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-03-03 20:42:52 +00:00
|
|
|
* The session key name where the record of the current user is stored. Default
|
|
|
|
* key is "Auth.User". If you are using only stateless authenticators set this
|
|
|
|
* to false to ensure session is not started.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-01-21 21:55:02 +00:00
|
|
|
public static $sessionKey = 'Auth.User';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-10-23 16:54:51 +00:00
|
|
|
/**
|
|
|
|
* The current user, used for stateless authentication when
|
|
|
|
* sessions are not available.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $_user = array();
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-10-22 04:09:34 +00:00
|
|
|
* A URL (defined as a string or array) to the controller action that handles
|
2013-09-01 02:58:10 +00:00
|
|
|
* logins. Defaults to `/users/login`.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var mixed
|
|
|
|
*/
|
2011-01-19 21:53:58 +00:00
|
|
|
public $loginAction = array(
|
|
|
|
'controller' => 'users',
|
|
|
|
'action' => 'login',
|
|
|
|
'plugin' => null
|
|
|
|
);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +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
|
2012-12-22 22:48:15 +00:00
|
|
|
* redirected back after a successful login. If this session value is not
|
2013-10-08 03:17:58 +00:00
|
|
|
* set, redirectUrl() method will return the URL specified in $loginRedirect.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var mixed
|
2011-10-15 15:13:26 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#AuthComponent::$loginRedirect
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $loginRedirect = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* The default action to redirect to after the user is logged out. While AuthComponent does
|
2008-05-30 11:40:08 +00:00
|
|
|
* not handle post-logout redirection, a redirect URL will be returned from AuthComponent::logout().
|
|
|
|
* Defaults to AuthComponent::$loginAction.
|
|
|
|
*
|
|
|
|
* @var mixed
|
|
|
|
* @see AuthComponent::$loginAction
|
|
|
|
* @see AuthComponent::logout()
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $logoutRedirect = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Error to display when user attempts to access an object or action to which they do not have
|
2011-12-02 05:58:09 +00:00
|
|
|
* access.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var string|bool
|
2011-10-15 15:13:26 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#AuthComponent::$authError
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $authError = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-10-02 14:57:41 +00:00
|
|
|
/**
|
2012-12-30 10:13:11 +00:00
|
|
|
* Controls handling of unauthorized access.
|
2013-04-29 09:05:17 +00:00
|
|
|
* - For default value `true` unauthorized user is redirected to the referrer URL
|
2012-12-30 10:13:11 +00:00
|
|
|
* or AuthComponent::$loginRedirect or '/'.
|
2013-10-22 04:09:34 +00:00
|
|
|
* - If set to a string or array the value is used as a URL to redirect to.
|
2012-12-30 10:13:11 +00:00
|
|
|
* - If set to false a ForbiddenException exception is thrown instead of redirecting.
|
2012-10-02 14:57:41 +00:00
|
|
|
*
|
2012-12-30 10:13:11 +00:00
|
|
|
* @var mixed
|
2012-10-02 14:57:41 +00:00
|
|
|
*/
|
|
|
|
public $unauthorizedRedirect = true;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Controller actions for which user validation is not required.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @see AuthComponent::allow()
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $allowedActions = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-05-15 04:55:15 +00:00
|
|
|
/**
|
|
|
|
* Request object
|
|
|
|
*
|
|
|
|
* @var CakeRequest
|
|
|
|
*/
|
|
|
|
public $request;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-01-22 00:56:23 +00:00
|
|
|
* Response object
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-01-22 00:56:23 +00:00
|
|
|
* @var CakeResponse
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-01-22 00:56:23 +00:00
|
|
|
public $response;
|
2010-07-31 19:41:28 +00:00
|
|
|
|
2008-10-15 02:52:19 +00:00
|
|
|
/**
|
2013-09-01 02:58:10 +00:00
|
|
|
* Method list for bound controller.
|
2008-10-15 02:52:19 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 06:36:12 +00:00
|
|
|
protected $_methods = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-09-01 02:58:10 +00:00
|
|
|
* Initializes AuthComponent for use in the controller.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param Controller $controller A reference to the instantiating controller object
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:06:25 +00:00
|
|
|
public function initialize(Controller $controller) {
|
2010-05-15 04:55:15 +00:00
|
|
|
$this->request = $controller->request;
|
2011-01-22 00:56:23 +00:00
|
|
|
$this->response = $controller->response;
|
2008-11-11 16:34:05 +00:00
|
|
|
$this->_methods = $controller->methods;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-07 02:46:08 +00:00
|
|
|
if (Configure::read('debug') > 0) {
|
2010-01-15 21:56:26 +00:00
|
|
|
Debugger::checkSecurityKeys();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Main execution method. Handles redirecting of invalid users, and processing
|
2008-05-30 11:40:08 +00:00
|
|
|
* of login form data.
|
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param Controller $controller A reference to the instantiating controller object
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:06:25 +00:00
|
|
|
public function startup(Controller $controller) {
|
2011-11-28 05:22:47 +00:00
|
|
|
$methods = array_flip(array_map('strtolower', $controller->methods));
|
|
|
|
$action = strtolower($controller->request->params['action']);
|
2010-07-28 02:32:08 +00:00
|
|
|
|
2009-02-06 23:23:12 +00:00
|
|
|
$isMissingAction = (
|
|
|
|
$controller->scaffold === false &&
|
2009-07-03 00:20:54 +00:00
|
|
|
!isset($methods[$action])
|
2009-02-06 23:23:12 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($isMissingAction) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-08-20 05:39:30 +00:00
|
|
|
if (!$this->_setDefaults()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-01-21 21:22:29 +00:00
|
|
|
|
2013-02-10 08:19:07 +00:00
|
|
|
if ($this->_isAllowed($controller)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->_getUser()) {
|
|
|
|
return $this->_unauthenticated($controller);
|
|
|
|
}
|
|
|
|
|
2013-09-24 08:16:31 +00:00
|
|
|
if ($this->_isLoginAction($controller) ||
|
|
|
|
empty($this->authorize) ||
|
|
|
|
$this->isAuthorized($this->user())
|
|
|
|
) {
|
2013-02-10 08:19:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_unauthorized($controller);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether current action is accessible without authentication.
|
|
|
|
*
|
|
|
|
* @param Controller $controller A reference to the instantiating controller object
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool True if action is accessible without authentication else false
|
2013-02-10 08:19:07 +00:00
|
|
|
*/
|
|
|
|
protected function _isAllowed(Controller $controller) {
|
|
|
|
$action = strtolower($controller->request->params['action']);
|
2013-03-03 20:42:52 +00:00
|
|
|
if (in_array($action, array_map('strtolower', $this->allowedActions))) {
|
2011-02-17 12:44:41 +00:00
|
|
|
return true;
|
2012-09-14 00:50:24 +00:00
|
|
|
}
|
2013-02-10 08:19:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-09-14 00:50:24 +00:00
|
|
|
|
2013-02-10 08:19:07 +00:00
|
|
|
/**
|
2013-03-03 20:42:52 +00:00
|
|
|
* Handles unauthenticated access attempt. First the `unathenticated()` method
|
|
|
|
* of the last authenticator in the chain will be called. The authenticator can
|
|
|
|
* handle sending response or redirection as appropriate and return `true` to
|
|
|
|
* indicate no furthur action is necessary. If authenticator returns null this
|
|
|
|
* method redirects user to login action. If it's an ajax request and
|
|
|
|
* $ajaxLogin is specified that element is rendered else a 403 http status code
|
|
|
|
* is returned.
|
2013-02-10 08:19:07 +00:00
|
|
|
*
|
2013-03-03 20:42:52 +00:00
|
|
|
* @param Controller $controller A reference to the controller object.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool True if current action is login action else false.
|
2013-02-10 08:19:07 +00:00
|
|
|
*/
|
|
|
|
protected function _unauthenticated(Controller $controller) {
|
2013-03-03 20:42:52 +00:00
|
|
|
if (empty($this->_authenticateObjects)) {
|
|
|
|
$this->constructAuthenticate();
|
|
|
|
}
|
|
|
|
$auth = $this->_authenticateObjects[count($this->_authenticateObjects) - 1];
|
|
|
|
if ($auth->unauthenticated($this->request, $this->response)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->_isLoginAction($controller)) {
|
2013-09-24 08:16:31 +00:00
|
|
|
if (empty($controller->request->data)) {
|
|
|
|
if (!$this->Session->check('Auth.redirect') && env('HTTP_REFERER')) {
|
|
|
|
$this->Session->write('Auth.redirect', $controller->referer(null, true));
|
|
|
|
}
|
|
|
|
}
|
2013-03-03 20:42:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-02-10 08:19:07 +00:00
|
|
|
if (!$controller->request->is('ajax')) {
|
|
|
|
$this->flash($this->authError);
|
2013-07-26 18:00:32 +00:00
|
|
|
$this->Session->write('Auth.redirect', $controller->request->here(false));
|
2013-02-10 08:19:07 +00:00
|
|
|
$controller->redirect($this->loginAction);
|
|
|
|
return false;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2013-02-10 08:19:07 +00:00
|
|
|
if (!empty($this->ajaxLogin)) {
|
2014-02-05 15:05:02 +00:00
|
|
|
$controller->response->statusCode(403);
|
2013-02-10 08:19:07 +00:00
|
|
|
$controller->viewPath = 'Elements';
|
2014-07-22 11:21:42 +00:00
|
|
|
$response = $controller->render($this->ajaxLogin, $this->RequestHandler->ajaxLayout);
|
|
|
|
$response->send();
|
2013-02-10 08:19:07 +00:00
|
|
|
$this->_stop();
|
|
|
|
return false;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2015-07-16 00:32:58 +00:00
|
|
|
$controller->response->statusCode(403);
|
|
|
|
$controller->response->send();
|
|
|
|
$this->_stop();
|
2013-02-10 08:19:07 +00:00
|
|
|
return false;
|
2012-10-02 14:57:41 +00:00
|
|
|
}
|
|
|
|
|
2013-03-03 20:42:52 +00:00
|
|
|
/**
|
2013-10-08 03:17:58 +00:00
|
|
|
* Normalizes $loginAction and checks if current request URL is same as login action.
|
2013-03-03 20:42:52 +00:00
|
|
|
*
|
|
|
|
* @param Controller $controller A reference to the controller object.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool True if current action is login action else false.
|
2013-03-03 20:42:52 +00:00
|
|
|
*/
|
|
|
|
protected function _isLoginAction(Controller $controller) {
|
|
|
|
$url = '';
|
|
|
|
if (isset($controller->request->url)) {
|
|
|
|
$url = $controller->request->url;
|
|
|
|
}
|
|
|
|
$url = Router::normalize($url);
|
|
|
|
$loginAction = Router::normalize($this->loginAction);
|
|
|
|
|
2013-09-24 08:16:31 +00:00
|
|
|
return $loginAction === $url;
|
2013-03-03 20:42:52 +00:00
|
|
|
}
|
|
|
|
|
2012-10-02 14:57:41 +00:00
|
|
|
/**
|
|
|
|
* Handle unauthorized access attempt
|
|
|
|
*
|
|
|
|
* @param Controller $controller A reference to the controller object
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool Returns false
|
2012-10-02 14:57:41 +00:00
|
|
|
* @throws ForbiddenException
|
2013-03-03 20:42:52 +00:00
|
|
|
* @see AuthComponent::$unauthorizedRedirect
|
2012-10-02 14:57:41 +00:00
|
|
|
*/
|
|
|
|
protected function _unauthorized(Controller $controller) {
|
2012-12-30 10:13:11 +00:00
|
|
|
if ($this->unauthorizedRedirect === false) {
|
2012-10-02 14:57:41 +00:00
|
|
|
throw new ForbiddenException($this->authError);
|
|
|
|
}
|
|
|
|
|
2011-01-19 21:17:17 +00:00
|
|
|
$this->flash($this->authError);
|
2012-12-30 10:13:11 +00:00
|
|
|
if ($this->unauthorizedRedirect === true) {
|
|
|
|
$default = '/';
|
|
|
|
if (!empty($this->loginRedirect)) {
|
|
|
|
$default = $this->loginRedirect;
|
|
|
|
}
|
|
|
|
$url = $controller->referer($default, true);
|
|
|
|
} else {
|
|
|
|
$url = $this->unauthorizedRedirect;
|
2012-03-28 05:51:47 +00:00
|
|
|
}
|
2015-12-08 17:21:51 +00:00
|
|
|
$controller->redirect($url);
|
2008-05-31 12:36:38 +00:00
|
|
|
return false;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-11-17 04:17:48 +00:00
|
|
|
* Attempts to introspect the correct values for object properties.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool True
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-08-20 05:39:30 +00:00
|
|
|
protected function _setDefaults() {
|
2008-05-31 12:36:38 +00:00
|
|
|
$defaults = array(
|
|
|
|
'logoutRedirect' => $this->loginAction,
|
2011-10-23 18:19:14 +00:00
|
|
|
'authError' => __d('cake', 'You are not authorized to access that location.')
|
2008-05-31 12:36:38 +00:00
|
|
|
);
|
|
|
|
foreach ($defaults as $key => $value) {
|
2013-06-16 04:48:47 +00:00
|
|
|
if (!isset($this->{$key}) || $this->{$key} === true) {
|
2008-05-31 12:36:38 +00:00
|
|
|
$this->{$key} = $value;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-02-27 02:43:53 +00:00
|
|
|
* Check if the provided user is authorized for the request.
|
|
|
|
*
|
2011-01-03 21:48:16 +00:00
|
|
|
* Uses the configured Authorization adapters to check whether or not a user is authorized.
|
2011-03-19 17:07:05 +00:00
|
|
|
* Each adapter will be checked in sequence, if any of them return true, then the user will
|
2011-01-03 21:48:16 +00:00
|
|
|
* be authorized for the request.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param array $user The user to check the authorization of. If empty the user in the session will be used.
|
2012-12-22 22:48:15 +00:00
|
|
|
* @param CakeRequest $request The request to authenticate for. If empty, the current request will be used.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool True if $user is authorized, otherwise false
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2013-02-09 12:39:11 +00:00
|
|
|
public function isAuthorized($user = null, CakeRequest $request = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (empty($user) && !$this->user()) {
|
|
|
|
return false;
|
2012-09-14 00:50:24 +00:00
|
|
|
}
|
|
|
|
if (empty($user)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$user = $this->user();
|
|
|
|
}
|
2011-01-03 23:18:42 +00:00
|
|
|
if (empty($request)) {
|
|
|
|
$request = $this->request;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-01-03 23:18:42 +00:00
|
|
|
if (empty($this->_authorizeObjects)) {
|
2011-01-09 05:33:06 +00:00
|
|
|
$this->constructAuthorize();
|
2011-01-03 23:18:42 +00:00
|
|
|
}
|
2011-01-03 21:48:16 +00:00
|
|
|
foreach ($this->_authorizeObjects as $authorizer) {
|
2011-01-03 23:18:42 +00:00
|
|
|
if ($authorizer->authorize($user, $request) === true) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
2011-01-03 21:48:16 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-01-03 21:48:16 +00:00
|
|
|
return false;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-01-03 21:48:16 +00:00
|
|
|
* Loads the authorization objects configured.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-01-03 21:48:16 +00:00
|
|
|
* @return mixed Either null when authorize is empty, or the loaded authorization objects.
|
2011-07-31 20:55:52 +00:00
|
|
|
* @throws CakeException
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-01-09 05:33:06 +00:00
|
|
|
public function constructAuthorize() {
|
2011-01-03 21:48:16 +00:00
|
|
|
if (empty($this->authorize)) {
|
2015-09-25 15:11:20 +00:00
|
|
|
return null;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-01-03 23:18:42 +00:00
|
|
|
$this->_authorizeObjects = array();
|
2012-03-11 01:57:18 +00:00
|
|
|
$config = Hash::normalize((array)$this->authorize);
|
2011-01-21 22:55:04 +00:00
|
|
|
$global = array();
|
|
|
|
if (isset($config[AuthComponent::ALL])) {
|
|
|
|
$global = $config[AuthComponent::ALL];
|
|
|
|
unset($config[AuthComponent::ALL]);
|
|
|
|
}
|
|
|
|
foreach ($config as $class => $settings) {
|
2011-03-05 22:10:42 +00:00
|
|
|
list($plugin, $class) = pluginSplit($class, true);
|
2011-01-03 21:48:16 +00:00
|
|
|
$className = $class . 'Authorize';
|
2011-03-05 22:10:42 +00:00
|
|
|
App::uses($className, $plugin . 'Controller/Component/Auth');
|
|
|
|
if (!class_exists($className)) {
|
2011-03-20 15:35:43 +00:00
|
|
|
throw new CakeException(__d('cake_dev', 'Authorization adapter "%s" was not found.', $class));
|
2011-01-03 21:48:16 +00:00
|
|
|
}
|
|
|
|
if (!method_exists($className, 'authorize')) {
|
2013-08-20 22:05:53 +00:00
|
|
|
throw new CakeException(__d('cake_dev', 'Authorization objects must implement an %s method.', 'authorize()'));
|
2011-01-03 21:48:16 +00:00
|
|
|
}
|
2011-01-21 22:55:04 +00:00
|
|
|
$settings = array_merge($global, (array)$settings);
|
2011-02-18 04:17:07 +00:00
|
|
|
$this->_authorizeObjects[] = new $className($this->_Collection, $settings);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-01-03 21:48:16 +00:00
|
|
|
return $this->_authorizeObjects;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Takes a list of actions in the current controller for which authentication is not required, or
|
|
|
|
* no parameters to allow all actions.
|
|
|
|
*
|
2011-01-21 02:34:39 +00:00
|
|
|
* You can use allow with either an array, or var args.
|
|
|
|
*
|
2011-03-19 17:07:05 +00:00
|
|
|
* `$this->Auth->allow(array('edit', 'add'));` or
|
2011-11-01 20:25:59 +00:00
|
|
|
* `$this->Auth->allow('edit', 'add');` or
|
|
|
|
* `$this->Auth->allow();` to allow all actions
|
2011-01-21 02:34:39 +00:00
|
|
|
*
|
2014-05-31 21:36:05 +00:00
|
|
|
* @param string|array $action Controller action name or array of actions
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2011-10-15 15:13:26 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#making-actions-public
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-08-31 01:12:57 +00:00
|
|
|
public function allow($action = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$args = func_get_args();
|
2012-01-11 01:32:12 +00:00
|
|
|
if (empty($args) || $action === null) {
|
2008-10-15 02:52:19 +00:00
|
|
|
$this->allowedActions = $this->_methods;
|
2012-09-14 00:50:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (isset($args[0]) && is_array($args[0])) {
|
|
|
|
$args = $args[0];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-09-14 00:50:24 +00:00
|
|
|
$this->allowedActions = array_merge($this->allowedActions, $args);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-01-21 02:34:39 +00:00
|
|
|
* Removes items from the list of allowed/no authentication required actions.
|
|
|
|
*
|
|
|
|
* You can use deny with either an array, or var args.
|
|
|
|
*
|
2011-03-19 17:07:05 +00:00
|
|
|
* `$this->Auth->deny(array('edit', 'add'));` or
|
2011-10-29 15:54:35 +00:00
|
|
|
* `$this->Auth->deny('edit', 'add');` or
|
|
|
|
* `$this->Auth->deny();` to remove all items from the allowed list
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-05-31 21:36:05 +00:00
|
|
|
* @param string|array $action Controller action name or array of actions
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
* @see AuthComponent::allow()
|
2011-10-15 15:13:26 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#making-actions-require-authorization
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-08-31 01:12:57 +00:00
|
|
|
public function deny($action = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$args = func_get_args();
|
2012-01-11 01:32:12 +00:00
|
|
|
if (empty($args) || $action === null) {
|
2011-10-27 00:07:17 +00:00
|
|
|
$this->allowedActions = array();
|
2012-09-14 00:50:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (isset($args[0]) && is_array($args[0])) {
|
|
|
|
$args = $args[0];
|
|
|
|
}
|
|
|
|
foreach ($args as $arg) {
|
|
|
|
$i = array_search($arg, $this->allowedActions);
|
|
|
|
if (is_int($i)) {
|
|
|
|
unset($this->allowedActions[$i]);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-14 00:50:24 +00:00
|
|
|
$this->allowedActions = array_values($this->allowedActions);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-02-27 02:43:53 +00:00
|
|
|
* Maps action names to CRUD operations.
|
|
|
|
*
|
|
|
|
* Used for controller-based authentication. Make sure
|
2011-01-05 04:12:37 +00:00
|
|
|
* to configure the authorize property before calling this method. As it delegates $map to all the
|
|
|
|
* attached authorize objects.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param array $map Actions to map
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2011-09-29 03:24:21 +00:00
|
|
|
* @see BaseAuthorize::mapActions()
|
2011-10-15 15:13:26 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#mapping-actions-when-using-crudauthorize
|
2014-09-01 13:52:26 +00:00
|
|
|
* @deprecated 3.0.0 Map actions using `actionMap` config key on authorize objects instead
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function mapActions($map = array()) {
|
2011-01-05 04:12:37 +00:00
|
|
|
if (empty($this->_authorizeObjects)) {
|
2011-01-09 05:33:06 +00:00
|
|
|
$this->constructAuthorize();
|
2011-01-05 04:12:37 +00:00
|
|
|
}
|
2014-08-27 21:06:54 +00:00
|
|
|
$mappedActions = array();
|
2011-01-05 04:12:37 +00:00
|
|
|
foreach ($this->_authorizeObjects as $auth) {
|
2014-08-27 21:06:54 +00:00
|
|
|
$mappedActions = Hash::merge($mappedActions, $auth->mapActions($map));
|
|
|
|
}
|
|
|
|
if (empty($map)) {
|
|
|
|
return $mappedActions;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-02-27 02:43:53 +00:00
|
|
|
* Log a user in.
|
|
|
|
*
|
|
|
|
* If a $user is provided that data will be stored as the logged in user. If `$user` is empty or not
|
2011-01-21 02:22:54 +00:00
|
|
|
* specified, the request will be used to identify a user. If the identification was successful,
|
2011-09-25 02:35:21 +00:00
|
|
|
* the user record is written to the session key specified in AuthComponent::$sessionKey. Logging in
|
|
|
|
* will also change the session id in order to help mitigate session replays.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param array $user Either an array of user data, or null to identify a user using the current request.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool True on login success, false on failure
|
2011-10-15 15:13:26 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#identifying-users-and-logging-them-in
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-01-05 04:27:16 +00:00
|
|
|
public function login($user = null) {
|
2011-08-20 05:39:30 +00:00
|
|
|
$this->_setDefaults();
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-01-05 04:27:16 +00:00
|
|
|
if (empty($user)) {
|
2011-01-22 00:56:23 +00:00
|
|
|
$user = $this->identify($this->request, $this->response);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-01-05 04:27:16 +00:00
|
|
|
if ($user) {
|
2011-09-25 02:35:21 +00:00
|
|
|
$this->Session->renew();
|
2015-07-21 08:22:53 +00:00
|
|
|
$this->Session->write(static::$sessionKey, $user);
|
2014-11-05 17:34:25 +00:00
|
|
|
$event = new CakeEvent('Auth.afterIdentify', $this, array('user' => $user));
|
|
|
|
$this->_Collection->getController()->getEventManager()->dispatch($event);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2014-09-14 16:15:08 +00:00
|
|
|
return (bool)$this->user();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-03-14 07:12:21 +00:00
|
|
|
* Log a user out.
|
2013-02-27 02:43:53 +00:00
|
|
|
*
|
2013-09-01 02:58:10 +00:00
|
|
|
* Returns the logout action to redirect to. Triggers the logout() method of
|
2013-02-27 02:43:53 +00:00
|
|
|
* all the authenticate objects, so they can perform custom logout logic.
|
|
|
|
* AuthComponent will remove the session data, so there is no need to do that
|
|
|
|
* in an authentication object. Logging out will also renew the session id.
|
|
|
|
* This helps mitigate issues with session replays.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-30 20:48:37 +00:00
|
|
|
* @return string AuthComponent::$logoutRedirect
|
|
|
|
* @see AuthComponent::$logoutRedirect
|
2011-10-15 15:13:26 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#logging-users-out
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function logout() {
|
2011-08-20 05:39:30 +00:00
|
|
|
$this->_setDefaults();
|
2011-07-03 16:53:21 +00:00
|
|
|
if (empty($this->_authenticateObjects)) {
|
|
|
|
$this->constructAuthenticate();
|
|
|
|
}
|
|
|
|
$user = $this->user();
|
|
|
|
foreach ($this->_authenticateObjects as $auth) {
|
|
|
|
$auth->logout($user);
|
|
|
|
}
|
2015-07-21 08:22:53 +00:00
|
|
|
$this->Session->delete(static::$sessionKey);
|
2009-09-11 10:58:23 +00:00
|
|
|
$this->Session->delete('Auth.redirect');
|
2011-09-25 02:35:21 +00:00
|
|
|
$this->Session->renew();
|
2008-05-30 11:40:08 +00:00
|
|
|
return Router::normalize($this->logoutRedirect);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-10-23 16:54:51 +00:00
|
|
|
* Get the current user.
|
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* Will prefer the static user cache over sessions. The static user
|
|
|
|
* cache is primarily used for stateless authentication. For stateful authentication,
|
2011-10-23 16:54:51 +00:00
|
|
|
* cookies + sessions will be used.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* @param string $key field to retrieve. Leave null to get entire User record
|
2014-11-05 12:03:27 +00:00
|
|
|
* @return array|null User record. or null if no user is logged in.
|
2011-10-15 15:13:26 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#accessing-the-logged-in-user
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-01-21 21:55:02 +00:00
|
|
|
public static function user($key = null) {
|
2015-07-21 08:22:53 +00:00
|
|
|
if (!empty(static::$_user)) {
|
|
|
|
$user = static::$_user;
|
|
|
|
} elseif (static::$sessionKey && CakeSession::check(static::$sessionKey)) {
|
|
|
|
$user = CakeSession::read(static::$sessionKey);
|
2013-03-03 20:42:52 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
2011-10-23 16:54:51 +00:00
|
|
|
}
|
|
|
|
if ($key === null) {
|
|
|
|
return $user;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-07-25 19:09:22 +00:00
|
|
|
return Hash::get($user, $key);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-02-05 23:41:00 +00:00
|
|
|
* Similar to AuthComponent::user() except if the session user cannot be found, connected authentication
|
2012-12-22 22:48:15 +00:00
|
|
|
* objects will have their getUser() methods called. This lets stateless authentication methods function correctly.
|
2011-02-05 23:41:00 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool true if a user can be found, false if one cannot.
|
2011-02-05 23:41:00 +00:00
|
|
|
*/
|
|
|
|
protected function _getUser() {
|
2013-04-22 20:05:04 +00:00
|
|
|
$user = $this->user();
|
|
|
|
if ($user) {
|
|
|
|
$this->Session->delete('Auth.redirect');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-02-05 23:41:00 +00:00
|
|
|
if (empty($this->_authenticateObjects)) {
|
|
|
|
$this->constructAuthenticate();
|
|
|
|
}
|
|
|
|
foreach ($this->_authenticateObjects as $auth) {
|
|
|
|
$result = $auth->getUser($this->request);
|
|
|
|
if (!empty($result) && is_array($result)) {
|
2015-07-21 08:22:53 +00:00
|
|
|
static::$_user = $result;
|
2011-02-05 23:41:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2013-03-03 20:42:52 +00:00
|
|
|
|
2011-02-05 23:41:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-27 15:52:11 +00:00
|
|
|
/**
|
2013-09-01 02:58:10 +00:00
|
|
|
* Backwards compatible alias for AuthComponent::redirectUrl().
|
2013-01-27 15:52:11 +00:00
|
|
|
*
|
|
|
|
* @param string|array $url Optional URL to write as the login redirect URL.
|
|
|
|
* @return string Redirect URL
|
2014-09-02 15:03:22 +00:00
|
|
|
* @deprecated 3.0.0 Since 2.3.0, use AuthComponent::redirectUrl() instead
|
2013-01-27 15:52:11 +00:00
|
|
|
*/
|
|
|
|
public function redirect($url = null) {
|
|
|
|
return $this->redirectUrl($url);
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-06-21 14:49:31 +00:00
|
|
|
* Get the URL a user should be redirected to upon login.
|
2013-02-27 02:43:53 +00:00
|
|
|
*
|
2013-10-22 04:09:34 +00:00
|
|
|
* Pass a URL in to set the destination a user should be redirected to upon
|
2013-03-26 03:29:18 +00:00
|
|
|
* logging in.
|
|
|
|
*
|
2013-04-29 09:05:17 +00:00
|
|
|
* If no parameter is passed, gets the authentication redirect URL. The URL
|
2013-03-26 03:29:18 +00:00
|
|
|
* returned is as per following rules:
|
|
|
|
*
|
2013-06-30 03:24:26 +00:00
|
|
|
* - Returns the normalized URL from session Auth.redirect value if it is
|
|
|
|
* present and for the same domain the current app is running on.
|
2013-03-26 03:29:18 +00:00
|
|
|
* - If there is no session value and there is a $loginRedirect, the $loginRedirect
|
|
|
|
* value is returned.
|
|
|
|
* - If there is no session and no $loginRedirect, / is returned.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $url Optional URL to write as the login redirect URL.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string Redirect URL
|
|
|
|
*/
|
2013-01-27 15:52:11 +00:00
|
|
|
public function redirectUrl($url = null) {
|
2013-08-16 18:12:49 +00:00
|
|
|
if ($url !== null) {
|
2008-12-24 05:40:05 +00:00
|
|
|
$redir = $url;
|
|
|
|
$this->Session->write('Auth.redirect', $redir);
|
2008-12-17 07:30:55 +00:00
|
|
|
} elseif ($this->Session->check('Auth.redirect')) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$redir = $this->Session->read('Auth.redirect');
|
|
|
|
$this->Session->delete('Auth.redirect');
|
|
|
|
|
2014-04-29 10:05:47 +00:00
|
|
|
if (Router::normalize($redir) === Router::normalize($this->loginAction)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$redir = $this->loginRedirect;
|
|
|
|
}
|
2013-03-26 03:29:18 +00:00
|
|
|
} elseif ($this->loginRedirect) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$redir = $this->loginRedirect;
|
2013-03-26 03:29:18 +00:00
|
|
|
} else {
|
|
|
|
$redir = '/';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2013-07-13 01:40:38 +00:00
|
|
|
if (is_array($redir)) {
|
2013-07-26 10:05:45 +00:00
|
|
|
return Router::url($redir + array('base' => false));
|
2013-07-13 01:40:38 +00:00
|
|
|
}
|
|
|
|
return $redir;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-01-03 23:18:42 +00:00
|
|
|
* Use the configured authentication adapters, and attempt to identify the user
|
|
|
|
* by credentials contained in $request.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-01-03 23:18:42 +00:00
|
|
|
* @param CakeRequest $request The request that contains authentication data.
|
2011-07-30 20:48:37 +00:00
|
|
|
* @param CakeResponse $response The response
|
2011-01-03 23:18:42 +00:00
|
|
|
* @return array User record data, or false, if the user could not be identified.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-01-22 00:56:23 +00:00
|
|
|
public function identify(CakeRequest $request, CakeResponse $response) {
|
2011-01-03 23:18:42 +00:00
|
|
|
if (empty($this->_authenticateObjects)) {
|
2011-01-09 05:33:06 +00:00
|
|
|
$this->constructAuthenticate();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-01-03 23:18:42 +00:00
|
|
|
foreach ($this->_authenticateObjects as $auth) {
|
2011-01-22 00:56:23 +00:00
|
|
|
$result = $auth->authenticate($request, $response);
|
2011-01-03 23:18:42 +00:00
|
|
|
if (!empty($result) && is_array($result)) {
|
|
|
|
return $result;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2011-01-03 23:18:42 +00:00
|
|
|
return false;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-09-01 02:58:10 +00:00
|
|
|
* Loads the configured authentication objects.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2015-09-25 15:11:20 +00:00
|
|
|
* @return mixed Either null on empty authenticate value, or an array of loaded objects.
|
2011-07-31 20:55:52 +00:00
|
|
|
* @throws CakeException
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-01-09 05:33:06 +00:00
|
|
|
public function constructAuthenticate() {
|
2011-01-03 23:18:42 +00:00
|
|
|
if (empty($this->authenticate)) {
|
2015-09-25 15:11:20 +00:00
|
|
|
return null;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-01-03 23:18:42 +00:00
|
|
|
$this->_authenticateObjects = array();
|
2012-03-11 01:57:18 +00:00
|
|
|
$config = Hash::normalize((array)$this->authenticate);
|
2011-01-21 22:55:04 +00:00
|
|
|
$global = array();
|
|
|
|
if (isset($config[AuthComponent::ALL])) {
|
|
|
|
$global = $config[AuthComponent::ALL];
|
|
|
|
unset($config[AuthComponent::ALL]);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-01-21 22:55:04 +00:00
|
|
|
foreach ($config as $class => $settings) {
|
2014-05-06 14:57:51 +00:00
|
|
|
if (!empty($settings['className'])) {
|
|
|
|
$class = $settings['className'];
|
|
|
|
unset($settings['className']);
|
|
|
|
}
|
2011-03-05 22:10:42 +00:00
|
|
|
list($plugin, $class) = pluginSplit($class, true);
|
2011-01-03 23:18:42 +00:00
|
|
|
$className = $class . 'Authenticate';
|
2011-03-05 22:10:42 +00:00
|
|
|
App::uses($className, $plugin . 'Controller/Component/Auth');
|
|
|
|
if (!class_exists($className)) {
|
2011-03-20 15:35:43 +00:00
|
|
|
throw new CakeException(__d('cake_dev', 'Authentication adapter "%s" was not found.', $class));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-01-03 23:18:42 +00:00
|
|
|
if (!method_exists($className, 'authenticate')) {
|
2013-08-20 22:05:53 +00:00
|
|
|
throw new CakeException(__d('cake_dev', 'Authentication objects must implement an %s method.', 'authenticate()'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-02-02 02:24:46 +00:00
|
|
|
$settings = array_merge($global, (array)$settings);
|
2014-11-05 23:03:26 +00:00
|
|
|
$auth = new $className($this->_Collection, $settings);
|
|
|
|
$this->_Collection->getController()->getEventManager()->attach($auth);
|
|
|
|
$this->_authenticateObjects[] = $auth;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-01-03 23:18:42 +00:00
|
|
|
return $this->_authenticateObjects;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Hash a password with the application's salt value (as defined with Configure::write('Security.salt');
|
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* This method is intended as a convenience wrapper for Security::hash(). If you want to use
|
2011-09-21 11:38:22 +00:00
|
|
|
* a hashing/encryption system not supported by that method, do not use this method.
|
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $password Password to hash
|
|
|
|
* @return string Hashed password
|
2014-09-02 15:03:22 +00:00
|
|
|
* @deprecated 3.0.0 Since 2.4. Use Security::hash() directly or a password hasher object.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-01-30 21:06:01 +00:00
|
|
|
public static function password($password) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return Security::hash($password, null, true);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-06-10 04:33:49 +00:00
|
|
|
/**
|
2011-05-10 01:54:59 +00:00
|
|
|
* Check whether or not the current user has data in the session, and is considered logged in.
|
2010-06-10 04:33:49 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool true if the user is logged in, false otherwise
|
2014-09-02 15:03:22 +00:00
|
|
|
* @deprecated 3.0.0 Since 2.5. Use AuthComponent::user() directly.
|
2010-06-10 04:33:49 +00:00
|
|
|
*/
|
2011-05-10 01:54:59 +00:00
|
|
|
public function loggedIn() {
|
2013-09-17 13:15:25 +00:00
|
|
|
return (bool)$this->user();
|
2010-06-10 04:33:49 +00:00
|
|
|
}
|
2011-01-19 21:17:17 +00:00
|
|
|
|
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Set a flash message. Uses the Session component, and values from AuthComponent::$flash.
|
2011-01-19 21:17:17 +00:00
|
|
|
*
|
|
|
|
* @param string $message The message to set.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function flash($message) {
|
2013-06-16 04:48:47 +00:00
|
|
|
if ($message === false) {
|
|
|
|
return;
|
|
|
|
}
|
2015-06-24 12:46:48 +00:00
|
|
|
$this->Flash->set($message, $this->flash);
|
2011-01-19 21:17:17 +00:00
|
|
|
}
|
2012-03-04 00:27:46 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|