| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://cakephp.org CakePHP(tm) Project
- * @since 0.10.0
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Controller\Component;
- use Cake\Controller\Component;
- use Cake\Controller\ComponentRegistry;
- use Cake\Controller\Controller;
- use Cake\Core\App;
- use Cake\Core\Configure;
- use Cake\Error;
- use Cake\Event\Event;
- use Cake\Network\Request;
- use Cake\Network\Response;
- use Cake\Network\Session;
- use Cake\Routing\Router;
- use Cake\Utility\Debugger;
- use Cake\Utility\Hash;
- use Cake\Utility\Security;
- /**
- * Authentication control component class
- *
- * Binds access control with user authentication and session management.
- *
- * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html
- */
- class AuthComponent extends Component {
- /**
- * Constant for 'all'
- *
- * @var string
- */
- const ALL = 'all';
- /**
- * Default config
- *
- * - `authenticate` - An array of authentication objects to use for authenticating users.
- * You can configure multiple adapters and they will be checked sequentially
- * when users are identified.
- *
- * {{{
- * $this->Auth->config('authenticate', [
- * 'Form' => [
- * 'userModel' => 'Users.Users'
- * ]
- * ]);
- * }}}
- *
- * Using the class name without 'Authenticate' as the key, you can pass in an
- * array of config for each authentication object. Additionally you can define
- * config that should be set to all authentications objects using the 'all' key:
- *
- * {{{
- * $this->Auth->config('authenticate', [
- * AuthComponent::ALL => [
- * 'userModel' => 'Users.Users',
- * 'scope' => ['Users.active' => 1]
- * ],
- * 'Form',
- * 'Basic'
- * ]);
- * }}}
- *
- * - `authorize` - An array of authorization objects to use for authorizing users.
- * You can configure multiple adapters and they will be checked sequentially
- * when authorization checks are done.
- *
- * {{{
- * $this->Auth->config('authorize', [
- * 'Crud' => [
- * 'actionPath' => 'controllers/'
- * ]
- * ]);
- * }}}
- *
- * Using the class name without 'Authorize' as the key, you can pass in an array
- * of config for each authorization object. Additionally you can define config
- * that should be set to all authorization objects using the AuthComponent::ALL key:
- *
- * {{{
- * $this->Auth->config('authorize', [
- * AuthComponent::ALL => [
- * 'actionPath' => 'controllers/'
- * ],
- * 'Crud',
- * 'CustomAuth'
- * ]);
- * }}}
- *
- * - `ajaxLogin` - The name of an optional view element to render when an Ajax
- * request is made with an invalid or expired session.
- *
- * - `flash` - Settings to use when Auth needs to do a flash message with
- * SessionComponent::setFlash(). Available keys are:
- *
- * - `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 []
- *
- * - `loginAction` - A URL (defined as a string or array) to the controller action
- * that handles logins. Defaults to `/users/login`.
- *
- * - `loginRedirect` - 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, redirectUrl() method will return the URL specified in `loginRedirect`.
- *
- * - `logoutRedirect` - The default action to redirect to after the user is logged out.
- * While AuthComponent does not handle post-logout redirection, a redirect URL
- * will be returned from `AuthComponent::logout()`. Defaults to `loginAction`.
- *
- * - `authError` - Error to display when user attempts to access an object or
- * action to which they do not have access.
- *
- * - `unauthorizedRedirect` - Controls handling of unauthorized access.
- *
- * - For default value `true` unauthorized user is redirected to the referrer URL
- * or `$loginRedirect` or '/'.
- * - If set to a string or array the value is used as a URL to redirect to.
- * - If set to false a `ForbiddenException` exception is thrown instead of redirecting.
- *
- * @var array
- */
- protected $_defaultConfig = [
- 'authenticate' => null,
- 'authorize' => null,
- 'ajaxLogin' => null,
- 'flash' => null,
- 'loginAction' => null,
- 'loginRedirect' => null,
- 'logoutRedirect' => null,
- 'authError' => null,
- 'unauthorizedRedirect' => true
- ];
- /**
- * Other components utilized by AuthComponent
- *
- * @var array
- */
- public $components = array('Session', 'RequestHandler');
- /**
- * Objects that will be used for authentication checks.
- *
- * @var array
- */
- protected $_authenticateObjects = array();
- /**
- * Objects that will be used for authorization checks.
- *
- * @var array
- */
- protected $_authorizeObjects = array();
- /**
- * 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.
- *
- * @var string
- */
- public static $sessionKey = 'Auth.User';
- /**
- * The current user, used for stateless authentication when
- * sessions are not available.
- *
- * @var array
- */
- protected static $_user = array();
- /**
- * Controller actions for which user validation is not required.
- *
- * @var array
- * @see AuthComponent::allow()
- */
- public $allowedActions = array();
- /**
- * Request object
- *
- * @var \Cake\Network\Request
- */
- public $request;
- /**
- * Response object
- *
- * @var \Cake\Network\Response
- */
- public $response;
- /**
- * Method list for bound controller.
- *
- * @var array
- */
- protected $_methods = array();
- /**
- * Initializes AuthComponent for use in the controller.
- *
- * @param Event $event The initialize event.
- * @return void
- */
- public function initialize(Event $event) {
- $controller = $event->subject();
- $this->request = $controller->request;
- $this->response = $controller->response;
- $this->_methods = $controller->methods;
- if (Configure::read('debug')) {
- Debugger::checkSecurityKeys();
- }
- }
- /**
- * Main execution method. Handles redirecting of invalid users, and processing
- * of login form data.
- *
- * @param Event $event The startup event.
- * @return bool
- */
- public function startup(Event $event) {
- $controller = $event->subject();
- $methods = array_flip(array_map('strtolower', $controller->methods));
- $action = strtolower($controller->request->params['action']);
- if (!isset($methods[$action])) {
- return true;
- }
- $this->_setDefaults();
- if ($this->_isAllowed($controller)) {
- return true;
- }
- if (!$this->_getUser()) {
- return $this->_unauthenticated($controller);
- }
- if ($this->_isLoginAction($controller) ||
- empty($this->_config['authorize']) ||
- $this->isAuthorized($this->user())
- ) {
- return true;
- }
- return $this->_unauthorized($controller);
- }
- /**
- * Checks whether current action is accessible without authentication.
- *
- * @param Controller $controller A reference to the instantiating controller object
- * @return bool True if action is accessible without authentication else false
- */
- protected function _isAllowed(Controller $controller) {
- $action = strtolower($controller->request->params['action']);
- if (in_array($action, array_map('strtolower', $this->allowedActions))) {
- return true;
- }
- return false;
- }
- /**
- * 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 config
- * `ajaxLogin` is specified that element is rendered else a 403 http status code
- * is returned.
- *
- * @param Controller $controller A reference to the controller object.
- * @return bool True if current action is login action else false.
- */
- protected function _unauthenticated(Controller $controller) {
- 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)) {
- if (empty($controller->request->data) &&
- !$this->Session->check('Auth.redirect') &&
- $this->request->env('HTTP_REFERER')
- ) {
- $this->Session->write('Auth.redirect', $controller->referer(null, true));
- }
- return true;
- }
- if (!$controller->request->is('ajax')) {
- $this->flash($this->_config['authError']);
- $this->Session->write('Auth.redirect', $controller->request->here(false));
- $controller->redirect($this->_config['loginAction']);
- return false;
- }
- if (!empty($this->_config['ajaxLogin'])) {
- $controller->response->statusCode(403);
- $controller->viewPath = 'Element';
- echo $controller->render($this->_config['ajaxLogin'], $this->RequestHandler->ajaxLayout);
- $controller->response->stop();
- return false;
- }
- $controller->redirect(null, 403);
- return false;
- }
- /**
- * Normalizes config `loginAction` and checks if current request URL is same as login action.
- *
- * @param Controller $controller A reference to the controller object.
- * @return bool True if current action is login action else false.
- */
- protected function _isLoginAction(Controller $controller) {
- $url = '';
- if (isset($controller->request->url)) {
- $url = $controller->request->url;
- }
- $url = Router::normalize($url);
- $loginAction = Router::normalize($this->_config['loginAction']);
- return $loginAction === $url;
- }
- /**
- * Handle unauthorized access attempt
- *
- * @param Controller $controller A reference to the controller object
- * @return bool Returns false
- * @throws \Cake\Error\ForbiddenException
- */
- protected function _unauthorized(Controller $controller) {
- if ($this->_config['unauthorizedRedirect'] === false) {
- throw new Error\ForbiddenException($this->_config['authError']);
- }
- $this->flash($this->_config['authError']);
- if ($this->_config['unauthorizedRedirect'] === true) {
- $default = '/';
- if (!empty($this->_config['loginRedirect'])) {
- $default = $this->_config['loginRedirect'];
- }
- $url = $controller->referer($default, true);
- } else {
- $url = $this->_config['unauthorizedRedirect'];
- }
- $controller->redirect($url, null, true);
- return false;
- }
- /**
- * Sets defaults for configs.
- *
- * @return void
- */
- protected function _setDefaults() {
- $defaults = [
- 'authenticate' => ['Form'],
- 'flash' => [
- 'element' => 'default',
- 'key' => 'auth',
- 'params' => []
- ],
- 'loginAction' => [
- 'controller' => 'users',
- 'action' => 'login',
- 'plugin' => null
- ],
- 'logoutRedirect' => $this->_config['loginAction'],
- 'authError' => __d('cake', 'You are not authorized to access that location.')
- ];
- $config = $this->config();
- foreach ($config as $key => $value) {
- if ($value !== null) {
- unset($defaults[$key]);
- }
- }
- $this->config($defaults);
- }
- /**
- * Check if the provided user is authorized for the request.
- *
- * Uses the configured Authorization adapters to check whether or not a user is authorized.
- * Each adapter will be checked in sequence, if any of them return true, then the user will
- * be authorized for the request.
- *
- * @param array $user The user to check the authorization of. If empty the user in the session will be used.
- * @param \Cake\Network\Request $request The request to authenticate for. If empty, the current request will be used.
- * @return bool True if $user is authorized, otherwise false
- */
- public function isAuthorized($user = null, Request $request = null) {
- if (empty($user) && !$this->user()) {
- return false;
- }
- if (empty($user)) {
- $user = $this->user();
- }
- if (empty($request)) {
- $request = $this->request;
- }
- if (empty($this->_authorizeObjects)) {
- $this->constructAuthorize();
- }
- foreach ($this->_authorizeObjects as $authorizer) {
- if ($authorizer->authorize($user, $request) === true) {
- return true;
- }
- }
- return false;
- }
- /**
- * Loads the authorization objects configured.
- *
- * @return mixed Either null when authorize is empty, or the loaded authorization objects.
- * @throws \Cake\Error\Exception
- */
- public function constructAuthorize() {
- if (empty($this->_config['authorize'])) {
- return;
- }
- $this->_authorizeObjects = array();
- $authorize = Hash::normalize((array)$this->_config['authorize']);
- $global = array();
- if (isset($authorize[AuthComponent::ALL])) {
- $global = $authorize[AuthComponent::ALL];
- unset($authorize[AuthComponent::ALL]);
- }
- foreach ($authorize as $class => $config) {
- $className = App::classname($class, 'Controller/Component/Auth', 'Authorize');
- if (!class_exists($className)) {
- throw new Error\Exception(sprintf('Authorization adapter "%s" was not found.', $class));
- }
- if (!method_exists($className, 'authorize')) {
- throw new Error\Exception('Authorization objects must implement an authorize() method.');
- }
- $config = array_merge($global, (array)$config);
- $this->_authorizeObjects[] = new $className($this->_registry, $config);
- }
- return $this->_authorizeObjects;
- }
- /**
- * Takes a list of actions in the current controller for which authentication is not required, or
- * no parameters to allow all actions.
- *
- * You can use allow with either an array, or var args.
- *
- * `$this->Auth->allow(array('edit', 'add'));` or
- * `$this->Auth->allow('edit', 'add');` or
- * `$this->Auth->allow();` to allow all actions
- *
- * @param string|array $action,... Controller action name or array of actions
- * @return void
- * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#making-actions-public
- */
- public function allow($action = null) {
- $args = func_get_args();
- if (empty($args) || $action === null) {
- $this->allowedActions = $this->_methods;
- return;
- }
- if (isset($args[0]) && is_array($args[0])) {
- $args = $args[0];
- }
- $this->allowedActions = array_merge($this->allowedActions, $args);
- }
- /**
- * Removes items from the list of allowed/no authentication required actions.
- *
- * You can use deny with either an array, or var args.
- *
- * `$this->Auth->deny(array('edit', 'add'));` or
- * `$this->Auth->deny('edit', 'add');` or
- * `$this->Auth->deny();` to remove all items from the allowed list
- *
- * @param string|array $action,... Controller action name or array of actions
- * @return void
- * @see AuthComponent::allow()
- * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#making-actions-require-authorization
- */
- public function deny($action = null) {
- $args = func_get_args();
- if (empty($args) || $action === null) {
- $this->allowedActions = array();
- 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]);
- }
- }
- $this->allowedActions = array_values($this->allowedActions);
- }
- /**
- * Maps action names to CRUD operations.
- *
- * Used for controller-based authentication. Make sure
- * to configure the authorize property before calling this method. As it delegates $map to all the
- * attached authorize objects.
- *
- * @param array $map Actions to map
- * @return void
- * @see BaseAuthorize::mapActions()
- * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#mapping-actions-when-using-crudauthorize
- */
- public function mapActions(array $map = array()) {
- if (empty($this->_authorizeObjects)) {
- $this->constructAuthorize();
- }
- foreach ($this->_authorizeObjects as $auth) {
- $auth->mapActions($map);
- }
- }
- /**
- * 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
- * specified, the request will be used to identify a user. If the identification was successful,
- * 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.
- *
- * @param array $user Either an array of user data, or null to identify a user using the current request.
- * @return bool True on login success, false on failure
- * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#identifying-users-and-logging-them-in
- */
- public function login($user = null) {
- $this->_setDefaults();
- if (empty($user)) {
- $user = $this->identify($this->request, $this->response);
- }
- if ($user) {
- $this->Session->renew();
- $this->Session->write(static::$sessionKey, $user);
- }
- return (bool)$this->user();
- }
- /**
- * Log a user out.
- *
- * Returns the logout action to redirect to. Triggers the logout() method of
- * 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.
- *
- * @return string Normalized config `logoutRedirect`
- * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#logging-users-out
- */
- public function logout() {
- $this->_setDefaults();
- if (empty($this->_authenticateObjects)) {
- $this->constructAuthenticate();
- }
- $user = (array)$this->user();
- foreach ($this->_authenticateObjects as $auth) {
- $auth->logout($user);
- }
- $this->Session->delete(static::$sessionKey);
- $this->Session->delete('Auth.redirect');
- $this->Session->renew();
- return Router::normalize($this->_config['logoutRedirect']);
- }
- /**
- * Get the current user.
- *
- * Will prefer the static user cache over sessions. The static user
- * cache is primarily used for stateless authentication. For stateful authentication,
- * cookies + sessions will be used.
- *
- * @param string $key field to retrieve. Leave null to get entire User record
- * @return mixed User record. or null if no user is logged in.
- * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#accessing-the-logged-in-user
- */
- public static function user($key = null) {
- if (!empty(static::$_user)) {
- $user = static::$_user;
- } elseif (static::$sessionKey && Session::check(static::$sessionKey)) {
- $user = Session::read(static::$sessionKey);
- } else {
- return null;
- }
- if ($key === null) {
- return $user;
- }
- return Hash::get($user, $key);
- }
- /**
- * Similar to AuthComponent::user() except if the session user cannot be found, connected authentication
- * objects will have their getUser() methods called. This lets stateless authentication methods function correctly.
- *
- * @return bool true if a user can be found, false if one cannot.
- */
- protected function _getUser() {
- $user = $this->user();
- if ($user) {
- $this->Session->delete('Auth.redirect');
- return true;
- }
- if (empty($this->_authenticateObjects)) {
- $this->constructAuthenticate();
- }
- foreach ($this->_authenticateObjects as $auth) {
- $result = $auth->getUser($this->request);
- if (!empty($result) && is_array($result)) {
- static::$_user = $result;
- return true;
- }
- }
- return false;
- }
- /**
- * Get the URL a user should be redirected to upon login.
- *
- * Pass a URL in to set the destination a user should be redirected to upon
- * logging in.
- *
- * If no parameter is passed, gets the authentication redirect URL. The URL
- * returned is as per following rules:
- *
- * - Returns the normalized URL from session Auth.redirect value if it is
- * present and for the same domain the current app is running on.
- * - If there is no session value and there is a config `loginRedirect`, the
- * `loginRedirect` value is returned.
- * - If there is no session and no `loginRedirect`, / is returned.
- *
- * @param string|array $url Optional URL to write as the login redirect URL.
- * @return string Redirect URL
- */
- public function redirectUrl($url = null) {
- if ($url !== null) {
- $redir = $url;
- $this->Session->write('Auth.redirect', $redir);
- } elseif ($this->Session->check('Auth.redirect')) {
- $redir = $this->Session->read('Auth.redirect');
- $this->Session->delete('Auth.redirect');
- if (Router::normalize($redir) == Router::normalize($this->_config['loginAction'])) {
- $redir = $this->_config['loginRedirect'];
- }
- } elseif ($this->_config['loginRedirect']) {
- $redir = $this->_config['loginRedirect'];
- } else {
- $redir = '/';
- }
- if (is_array($redir)) {
- return Router::url($redir + array('base' => false));
- }
- return $redir;
- }
- /**
- * Use the configured authentication adapters, and attempt to identify the user
- * by credentials contained in $request.
- *
- * @param \Cake\Network\Request $request The request that contains authentication data.
- * @param \Cake\Network\Response $response The response
- * @return array User record data, or false, if the user could not be identified.
- */
- public function identify(Request $request, Response $response) {
- if (empty($this->_authenticateObjects)) {
- $this->constructAuthenticate();
- }
- foreach ($this->_authenticateObjects as $auth) {
- $result = $auth->authenticate($request, $response);
- if (!empty($result) && is_array($result)) {
- return $result;
- }
- }
- return false;
- }
- /**
- * Loads the configured authentication objects.
- *
- * @return mixed either null on empty authenticate value, or an array of loaded objects.
- * @throws \Cake\Error\Exception
- */
- public function constructAuthenticate() {
- if (empty($this->_config['authenticate'])) {
- return;
- }
- $this->_authenticateObjects = array();
- $authenticate = Hash::normalize((array)$this->_config['authenticate']);
- $global = array();
- if (isset($authenticate[AuthComponent::ALL])) {
- $global = $authenticate[AuthComponent::ALL];
- unset($authenticate[AuthComponent::ALL]);
- }
- foreach ($authenticate as $class => $config) {
- $className = App::classname($class, 'Controller/Component/Auth', 'Authenticate');
- if (!class_exists($className)) {
- throw new Error\Exception(sprintf('Authentication adapter "%s" was not found.', $class));
- }
- if (!method_exists($className, 'authenticate')) {
- throw new Error\Exception('Authentication objects must implement an authenticate() method.');
- }
- $config = array_merge($global, (array)$config);
- $this->_authenticateObjects[] = new $className($this->_registry, $config);
- }
- return $this->_authenticateObjects;
- }
- /**
- * Set a flash message. Uses the Session component, and values from `flash` config.
- *
- * @param string $message The message to set.
- * @return void
- */
- public function flash($message) {
- if ($message === false) {
- return;
- }
- $flashConfig = $this->_config['flash'];
- $this->Session->setFlash(
- $message,
- $flashConfig['element'],
- $flashConfig['params'],
- $flashConfig['key']
- );
- }
- }
|