auth.php
来自「Cake Framwork , Excellent」· PHP 代码 · 共 825 行 · 第 1/2 页
PHP
825 行
<?php/* SVN FILE: $Id: auth.php 7118 2008-06-04 20:49:29Z gwoo $ *//** * Authentication component * * Manages user logins and permissions. * * PHP versions 4 and 5 * * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/> * Copyright 2005-2008, Cake Software Foundation, Inc. * 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 * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project * @package cake * @subpackage cake.cake.libs.controller.components * @since CakePHP(tm) v 0.10.0.1076 * @version $Revision: 7118 $ * @modifiedby $LastChangedBy: gwoo $ * @lastmodified $Date: 2008-06-04 13:49:29 -0700 (Wed, 04 Jun 2008) $ * @license http://www.opensource.org/licenses/mit-license.php The MIT License */App::import(array('Router', '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 {/** * Maintains current user login state. * * @var boolean * @access private */ var $_loggedIn = false;/** * Other components utilized by AuthComponent * * @var array * @access public */ var $components = array('Session', 'RequestHandler');/** * A reference to the object used for authentication * * @var object * @access public */ var $authenticate = null;/** * The name of the component to use for Authorization or set this to * 'controller' will validate against Controller::isAuthorized() * 'actions' will validate Controller::action against an AclComponent::check() * 'crud' will validate mapActions against an AclComponent::check() * array('model'=> 'name'); will validate mapActions against model $name::isAuthorize(user, controller, mapAction) * 'object' will validate Controller::action against object::isAuthorized(user, controller, action) * * @var mixed * @access public */ var $authorize = false;/** * 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. Defaults to 'User'. * * @var string * @access public */ var $userModel = 'User';/** * 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/". * * @var string * @access public */ var $actionPath = null;/** * A URL (defined as a string or array) to the controller action that handles * logins. * * @var mixed * @access public */ var $loginAction = null;/** * 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 mixed * @access public */ var $loginRedirect = null;/** * The 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 AuthComponent::$loginAction. * * @var mixed * @access public * @see AuthComponent::$loginAction * @see AuthComponent::logout() */ var $logoutRedirect = null;/** * The name of model or model object, or any other object has an isAuthorized method. * * @var string * @access public */ var $object = null;/** * 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. * * @var string * @access public */ var $loginError = null;/** * Error to display when user attempts to access an object or action to which they do not have * acccess. * * @var string * @access public */ var $authError = null;/** * Determines whether AuthComponent will automatically redirect and exit if login is successful. * * @var boolean * @access public */ var $autoRedirect = true;/** * Controller actions for which user validation is not required. * * @var array * @access public * @see AuthComponent::allow() */ var $allowedActions = array();/** * Maps actions to CRUD operations. Used for controller-based validation ($validate = 'controller'). * * @var array * @access public * @see AuthComponent::mapActions() */ var $actionMap = array( 'index' => 'read', 'add' => 'create', 'edit' => 'update', 'view' => 'read', 'remove' => 'delete' );/** * Form data from Controller::$data * * @var array * @access public */ var $data = array();/** * Parameter data from Controller::$params * * @var array * @access public */ var $params = array();/** * Initializes AuthComponent for use in the controller * * @param object $controller A reference to the instantiating controller object * @access public */ function initialize(&$controller) { $this->params = $controller->params; $crud = array('create', 'read', 'update', 'delete'); $this->actionMap = array_merge($this->actionMap, array_combine($crud, $crud)); $admin = Configure::read('Routing.admin'); if (!empty($admin)) { $this->actionMap = array_merge($this->actionMap, array( $admin . '_index' => 'read', $admin . '_add' => 'create', $admin . '_edit' => 'update', $admin . '_view' => 'read', $admin . '_remove' => 'delete', $admin . '_create' => 'create', $admin . '_read' => 'read', $admin . '_update' => 'update', $admin . '_delete' => 'delete' )); } if (Configure::read() > 0) { App::import('Debugger'); Debugger::checkSessionKey(); } }/** * Main execution method. Handles redirecting of invalid users, and processing * of login form data. * * @param object $controller A reference to the instantiating controller object * @access public */ function startup(&$controller) { if (strtolower($controller->name) == 'app' || (strtolower($controller->name) == 'tests' && Configure::read() > 0)) { return; } if (!$this->__setDefaults()) { return false; } $this->data = $controller->data = $this->hashPasswords($controller->data); $url = ''; if (is_array($this->loginAction)) { $params = $controller->params; $keys = array('pass', 'named', 'controller', 'action', 'plugin'); $url = array(); foreach($keys as $key) { if (!empty($params[$key])) { if (is_array($params[$key])) { foreach ($params[$key] as $name => $value) { $url[$name] = $value; } } else { $url[$key] = $params[$key]; } } } } elseif (isset($controller->params['url']['url'])) { $url = $controller->params['url']['url']; } $url = Router::normalize($url); $loginAction = Router::normalize($this->loginAction); if ($loginAction != $url && ($this->allowedActions == array('*') || in_array($controller->action, $this->allowedActions))) { return false; } if ($loginAction == $url) { if (empty($controller->data) || !isset($controller->data[$this->userModel])) { if (!$this->Session->check('Auth.redirect') && env('HTTP_REFERER')) { $this->Session->write('Auth.redirect', $controller->referer()); } return false; } $data = array( $this->userModel . '.' . $this->fields['username'] => $controller->data[$this->userModel][$this->fields['username']], $this->userModel . '.' . $this->fields['password'] => $controller->data[$this->userModel][$this->fields['password']] ); if ($this->login($data)) { if ($this->autoRedirect) { $controller->redirect($this->redirect(), null, true); } return true; } else { $this->Session->setFlash($this->loginError, 'default', array(), 'auth'); $controller->data[$this->userModel][$this->fields['password']] = null; } return false; } else { if (!$this->user()) { if (!$this->RequestHandler->isAjax()) { $this->Session->setFlash($this->authError, 'default', array(), 'auth'); $this->Session->write('Auth.redirect', $url); $controller->redirect($loginAction, null, true); return false; } elseif (!empty($this->ajaxLogin)) { $controller->viewPath = 'elements'; echo $controller->render($this->ajaxLogin, 'ajax'); $this->_stop(); return false; } } } if (!$this->authorize) { return true; } extract($this->__authType()); switch ($type) { case 'controller': $this->object =& $controller; break; case 'crud': case 'actions': if (isset($controller->Acl)) { $this->Acl =& $controller->Acl; } else { trigger_error(__('Could not find AclComponent. Please include Acl in Controller::$components.', true), E_USER_WARNING); } break; case 'model': if (!isset($object)) { if (isset($controller->{$controller->modelClass}) && is_object($controller->{$controller->modelClass})) { $object = $controller->modelClass; } elseif (!empty($controller->uses) && isset($controller->{$controller->uses[0]}) && is_object($controller->{$controller->uses[0]})) { $object = $controller->uses[0]; } } $type = array('model' => $object); break; } if ($this->isAuthorized($type)) { return true; } $this->Session->setFlash($this->authError, 'default', array(), 'auth'); $controller->redirect($controller->referer(), null, true); return false; }/** * Attempts to introspect the correct values for object properties including * $userModel and $sessionKey. * * @param object $controller A reference to the instantiating controller object * @access private */ function __setDefaults() { if (empty($this->userModel)) { trigger_error(__("Could not find \$userModel. Please set AuthComponent::\$userModel in beforeFilter().", true), E_USER_WARNING); return false; } $defaults = array( 'loginAction' => Router::normalize(array( 'controller'=> Inflector::underscore(Inflector::pluralize($this->userModel)), 'action' => 'login' )), 'sessionKey' => 'Auth.' . $this->userModel, 'logoutRedirect' => $this->loginAction, 'loginError' => __('Login failed. Invalid username or password.', true), 'authError' => __('You are not authorized to access that location.', true) ); foreach ($defaults as $key => $value) { if (empty($this->{$key})) { $this->{$key} = $value; } } return true; }/** * Determines whether the given user is authorized to perform an action. The type of authorization * used is based on the value of AuthComponent::$authorize or the passed $type param. * * Types: * 'controller' will validate against Controller::isAuthorized() if controller instance is passed in $object * 'actions' will validate Controller::action against an AclComponent::check() * 'crud' will validate mapActions against an AclComponent::check() * array('model'=> 'name'); will validate mapActions against model $name::isAuthorize(user, controller, mapAction) * 'object' will validate Controller::action against object::isAuthorized(user, controller, action) * * @param string $type Type of authorization * @param mixed $object object, model object, or model name * @param mixed $user The user to check the authorization of * @return boolean True if $user is authorized, otherwise false
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?