You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

486 lines
17 KiB

12 years ago
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\web;
use Yii;
12 years ago
use yii\base\Component;
12 years ago
use yii\base\InvalidConfigException;
12 years ago
/**
* User is the class for the "user" application component that manages the user authentication status.
*
* In particular, [[User::isGuest]] returns a value indicating whether the current user is a guest or not.
* Through methods [[login()]] and [[logout()]], you can change the user authentication status.
*
* User works with a class implementing the [[Identity]] interface. This class implements
* the actual user authentication logic and is often backed by a user database table.
*
12 years ago
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class User extends Component
{
12 years ago
const EVENT_BEFORE_LOGIN = 'beforeLogin';
const EVENT_AFTER_LOGIN = 'afterLogin';
const EVENT_BEFORE_LOGOUT = 'beforeLogout';
const EVENT_AFTER_LOGOUT = 'afterLogout';
/**
12 years ago
* @var string the class name of the [[identity]] object.
12 years ago
*/
public $identityClass;
12 years ago
/**
* @var boolean whether to enable cookie-based login. Defaults to false.
*/
public $enableAutoLogin = false;
12 years ago
/**
* @var string|array the URL for login when [[loginRequired()]] is called.
* If an array is given, [[UrlManager::createUrl()]] will be called to create the corresponding URL.
* The first element of the array should be the route to the login action, and the rest of
* the name-value pairs are GET parameters used to construct the login URL. For example,
*
* ~~~
* array('site/login', 'ref' => 1)
* ~~~
*
* If this property is null, a 403 HTTP exception will be raised when [[loginRequired()]] is called.
12 years ago
*/
public $loginUrl = array('site/login');
12 years ago
/**
* @var array the configuration of the identity cookie. This property is used only when [[enableAutoLogin]] is true.
* @see Cookie
12 years ago
*/
12 years ago
public $identityCookie = array('name' => '_identity', 'httpOnly' => true);
12 years ago
/**
* @var integer the number of seconds in which the user will be logged out automatically if he
* remains inactive. If this property is not set, the user will be logged out after
* the current session expires (c.f. [[Session::timeout]]).
12 years ago
*/
public $authTimeout;
/**
* @var boolean whether to automatically renew the identity cookie each time a page is requested.
12 years ago
* This property is effective only when [[enableAutoLogin]] is true.
12 years ago
* When this is false, the identity cookie will expire after the specified duration since the user
* is initially logged in. When this is true, the identity cookie will expire after the specified duration
* since the user visits the site the last time.
* @see enableAutoLogin
12 years ago
*/
12 years ago
public $autoRenewCookie = true;
12 years ago
/**
* @var string the session variable name used to store the value of [[id]].
*/
public $idVar = '__id';
/**
* @var string the session variable name used to store the value of expiration timestamp of the authenticated state.
* This is used when [[authTimeout]] is set.
*/
public $authTimeoutVar = '__expire';
/**
* @var string the session variable name used to store the value of [[returnUrl]].
*/
public $returnUrlVar = '__returnUrl';
12 years ago
private $_access = array();
12 years ago
/**
* Initializes the application component.
*/
public function init()
{
parent::init();
12 years ago
if ($this->identityClass === null) {
throw new InvalidConfigException('User::identityClass must be set.');
}
12 years ago
if ($this->enableAutoLogin && !isset($this->identityCookie['name'])) {
throw new InvalidConfigException('User::identityCookie must contain the "name" element.');
}
Yii::$app->getSession()->open();
12 years ago
$this->renewAuthStatus();
if ($this->enableAutoLogin) {
if ($this->getIsGuest()) {
$this->loginByCookie();
} elseif ($this->autoRenewCookie) {
$this->renewIdentityCookie();
}
}
}
private $_identity = false;
12 years ago
/**
* Returns the identity object associated with the currently logged user.
* @return Identity the identity object associated with the currently logged user.
* Null is returned if the user is not logged in (not authenticated).
* @see login
* @see logout
12 years ago
*/
public function getIdentity()
12 years ago
{
if ($this->_identity === false) {
$id = $this->getId();
if ($id === null) {
$this->_identity = null;
} else {
/** @var $class Identity */
$class = Yii::import($this->identityClass);
$this->_identity = $class::findIdentity($id);
}
12 years ago
}
return $this->_identity;
}
/**
* Sets the identity object.
* This method should be mainly be used by the User component or its child class
* to maintain the identity object.
*
* You should normally update the user identity via methods [[login()]], [[logout()]]
* or [[switchIdentity()]].
*
* @param Identity $identity the identity object associated with the currently logged user.
*/
public function setIdentity($identity)
{
$this->_identity = $identity;
12 years ago
}
/**
* Logs in a user.
*
12 years ago
* This method stores the necessary session information to keep track
* of the user identity information. If `$duration` is greater than 0
* and [[enableAutoLogin]] is true, it will also send out an identity
* cookie to support cookie-based login.
12 years ago
*
12 years ago
* @param Identity $identity the user identity (which should already be authenticated)
12 years ago
* @param integer $duration number of seconds that the user can remain in logged-in status.
* Defaults to 0, meaning login till the user closes the browser or the session is manually destroyed.
* If greater than 0 and [[enableAutoLogin]] is true, cookie-based login will be supported.
12 years ago
* @return boolean whether the user is logged in
*/
public function login($identity, $duration = 0)
{
12 years ago
if ($this->beforeLogin($identity, false)) {
$this->switchIdentity($identity, $duration);
12 years ago
$this->afterLogin($identity, false);
12 years ago
}
return !$this->getIsGuest();
}
/**
12 years ago
* Logs in a user by cookie.
*
* This method attempts to log in a user using the ID and authKey information
* provided by the given cookie.
12 years ago
*/
protected function loginByCookie()
{
$name = $this->identityCookie['name'];
$value = Yii::$app->getRequest()->getCookies()->getValue($name);
if ($value !== null) {
$data = json_decode($value, true);
if (count($data) === 3 && isset($data[0], $data[1], $data[2])) {
list ($id, $authKey, $duration) = $data;
/** @var $class Identity */
$class = $this->identityClass;
$identity = $class::findIdentity($id);
12 years ago
if ($identity !== null && $identity->validateAuthKey($authKey)) {
if ($this->beforeLogin($identity, true)) {
$this->switchIdentity($identity, $this->autoRenewCookie ? $duration : 0);
12 years ago
$this->afterLogin($identity, true);
}
12 years ago
} elseif ($identity !== null) {
Yii::warning("Invalid auth key attempted for user '$id': $authKey", __METHOD__);
12 years ago
}
}
}
}
/**
12 years ago
* Logs out the current user.
* This will remove authentication-related session data.
12 years ago
* If `$destroySession` is true, all session data will be removed.
* @param boolean $destroySession whether to destroy the whole session. Defaults to true.
12 years ago
*/
public function logout($destroySession = true)
{
$identity = $this->getIdentity();
12 years ago
if ($identity !== null && $this->beforeLogout($identity)) {
$this->switchIdentity(null);
12 years ago
if ($destroySession) {
12 years ago
Yii::$app->getSession()->destroy();
12 years ago
}
12 years ago
$this->afterLogout($identity);
12 years ago
}
}
/**
* Returns a value indicating whether the user is a guest (not authenticated).
12 years ago
* @return boolean whether the current user is a guest.
12 years ago
*/
public function getIsGuest()
{
return $this->getIdentity() === null;
12 years ago
}
/**
* Returns a value that uniquely represents the user.
12 years ago
* @return string|integer the unique identifier for the user. If null, it means the user is a guest.
12 years ago
*/
public function getId()
{
12 years ago
return Yii::$app->getSession()->get($this->idVar);
12 years ago
}
/**
* Returns the URL that the user should be redirected to after successful login.
* This property is usually used by the login action. If the login is successful,
* the action should read this property and use it to redirect the user browser.
12 years ago
* @param string|array $defaultUrl the default return URL in case it was not set previously.
* If this is null, it means [[Application::homeUrl]] will be redirected to.
* Please refer to [[\yii\helpers\Html::url()]] on acceptable URL formats.
12 years ago
* @return string the URL that the user should be redirected to after login.
* @see loginRequired
*/
public function getReturnUrl($defaultUrl = null)
{
12 years ago
$url = Yii::$app->getSession()->get($this->returnUrlVar, $defaultUrl);
return $url === null ? Yii::$app->getHomeUrl() : $url;
12 years ago
}
/**
12 years ago
* @param string|array $url the URL that the user should be redirected to after login.
* Please refer to [[\yii\helpers\Html::url()]] on acceptable URL formats.
12 years ago
*/
12 years ago
public function setReturnUrl($url)
12 years ago
{
12 years ago
Yii::$app->getSession()->set($this->returnUrlVar, $url);
12 years ago
}
/**
* Redirects the user browser to the login page.
* Before the redirection, the current URL (if it's not an AJAX url) will be
12 years ago
* kept as [[returnUrl]] so that the user browser may be redirected back
* to the current page after successful login. Make sure you set [[loginUrl]]
12 years ago
* so that the user browser can be redirected to the specified login URL after
* calling this method.
* After calling this method, the current request processing will be terminated.
*/
public function loginRequired()
{
12 years ago
$request = Yii::$app->getRequest();
12 years ago
if (!$request->getIsAjax()) {
12 years ago
$this->setReturnUrl($request->getUrl());
}
if ($this->loginUrl !== null) {
Yii::$app->getResponse()->redirect($this->loginUrl)->send();
exit();
12 years ago
} else {
throw new HttpException(403, Yii::t('yii', 'Login Required'));
12 years ago
}
}
/**
* This method is called before logging in a user.
12 years ago
* The default implementation will trigger the [[EVENT_BEFORE_LOGIN]] event.
* If you override this method, make sure you call the parent implementation
* so that the event is triggered.
* @param Identity $identity the user identity information
* @param boolean $cookieBased whether the login is cookie-based
* @return boolean whether the user should continue to be logged in
12 years ago
*/
12 years ago
protected function beforeLogin($identity, $cookieBased)
12 years ago
{
12 years ago
$event = new UserEvent(array(
'identity' => $identity,
12 years ago
'cookieBased' => $cookieBased,
12 years ago
));
$this->trigger(self::EVENT_BEFORE_LOGIN, $event);
return $event->isValid;
12 years ago
}
/**
* This method is called after the user is successfully logged in.
12 years ago
* The default implementation will trigger the [[EVENT_AFTER_LOGIN]] event.
* If you override this method, make sure you call the parent implementation
* so that the event is triggered.
* @param Identity $identity the user identity information
* @param boolean $cookieBased whether the login is cookie-based
12 years ago
*/
12 years ago
protected function afterLogin($identity, $cookieBased)
12 years ago
{
12 years ago
$this->trigger(self::EVENT_AFTER_LOGIN, new UserEvent(array(
'identity' => $identity,
12 years ago
'cookieBased' => $cookieBased,
12 years ago
)));
12 years ago
}
/**
12 years ago
* This method is invoked when calling [[logout()]] to log out a user.
* The default implementation will trigger the [[EVENT_BEFORE_LOGOUT]] event.
* If you override this method, make sure you call the parent implementation
* so that the event is triggered.
* @param Identity $identity the user identity information
* @return boolean whether the user should continue to be logged out
12 years ago
*/
12 years ago
protected function beforeLogout($identity)
12 years ago
{
12 years ago
$event = new UserEvent(array(
'identity' => $identity,
));
$this->trigger(self::EVENT_BEFORE_LOGOUT, $event);
return $event->isValid;
12 years ago
}
/**
12 years ago
* This method is invoked right after a user is logged out via [[logout()]].
* The default implementation will trigger the [[EVENT_AFTER_LOGOUT]] event.
* If you override this method, make sure you call the parent implementation
* so that the event is triggered.
* @param Identity $identity the user identity information
12 years ago
*/
12 years ago
protected function afterLogout($identity)
12 years ago
{
12 years ago
$this->trigger(self::EVENT_AFTER_LOGOUT, new UserEvent(array(
'identity' => $identity,
)));
12 years ago
}
/**
* Renews the identity cookie.
* This method will set the expiration time of the identity cookie to be the current time
* plus the originally specified cookie duration.
*/
12 years ago
protected function renewIdentityCookie()
12 years ago
{
12 years ago
$name = $this->identityCookie['name'];
$value = Yii::$app->getRequest()->getCookies()->getValue($name);
if ($value !== null) {
$data = json_decode($value, true);
if (is_array($data) && isset($data[2])) {
$cookie = new Cookie($this->identityCookie);
$cookie->value = $value;
$cookie->expire = time() + (int)$data[2];
Yii::$app->getResponse()->getCookies()->add($cookie);
12 years ago
}
}
}
/**
12 years ago
* Sends an identity cookie.
* This method is used when [[enableAutoLogin]] is true.
* It saves [[id]], [[Identity::getAuthKey()|auth key]], and the duration of cookie-based login
* information in the cookie.
12 years ago
* @param Identity $identity
12 years ago
* @param integer $duration number of seconds that the user can remain in logged-in status.
12 years ago
* @see loginByCookie
12 years ago
*/
12 years ago
protected function sendIdentityCookie($identity, $duration)
12 years ago
{
12 years ago
$cookie = new Cookie($this->identityCookie);
$cookie->value = json_encode(array(
$identity->getId(),
$identity->getAuthKey(),
12 years ago
$duration,
12 years ago
));
$cookie->expire = time() + $duration;
Yii::$app->getResponse()->getCookies()->add($cookie);
12 years ago
}
/**
* Switches to a new identity for the current user.
*
* This method will save necessary session information to keep track of the user authentication status.
* If `$duration` is provided, it will also send out appropriate identity cookie
* to support cookie-based login.
*
* This method is mainly called by [[login()]], [[logout()]] and [[loginByCookie()]]
* when the current user needs to be associated with the corresponding identity information.
*
12 years ago
* @param Identity $identity the identity information to be associated with the current user.
* If null, it means switching to be a guest.
* @param integer $duration number of seconds that the user can remain in logged-in status.
* This parameter is used only when `$identity` is not null.
12 years ago
*/
public function switchIdentity($identity, $duration = 0)
12 years ago
{
12 years ago
$session = Yii::$app->getSession();
if (!YII_ENV_TEST) {
$session->regenerateID(true);
}
$this->setIdentity($identity);
12 years ago
$session->remove($this->idVar);
$session->remove($this->authTimeoutVar);
12 years ago
if ($identity instanceof Identity) {
$session->set($this->idVar, $identity->getId());
12 years ago
if ($this->authTimeout !== null) {
$session->set($this->authTimeoutVar, time() + $this->authTimeout);
}
if ($duration > 0 && $this->enableAutoLogin) {
$this->sendIdentityCookie($identity, $duration);
12 years ago
}
} elseif ($this->enableAutoLogin) {
Yii::$app->getResponse()->getCookies()->remove(new Cookie($this->identityCookie));
12 years ago
}
}
/**
12 years ago
* Updates the authentication status according to [[authTimeout]].
* This method is called during [[init()]].
* It will update the user's authentication status if it has not outdated yet.
* Otherwise, it will logout the user.
12 years ago
*/
12 years ago
protected function renewAuthStatus()
12 years ago
{
if ($this->authTimeout !== null && !$this->getIsGuest()) {
12 years ago
$expire = Yii::$app->getSession()->get($this->authTimeoutVar);
12 years ago
if ($expire !== null && $expire < time()) {
$this->logout(false);
} else {
12 years ago
Yii::$app->getSession()->set($this->authTimeoutVar, time() + $this->authTimeout);
12 years ago
}
}
12 years ago
}
12 years ago
/**
* Performs access check for this user.
* @param string $operation the name of the operation that need access check.
* @param array $params name-value pairs that would be passed to business rules associated
* with the tasks and roles assigned to the user. A param with name 'userId' is added to
* this array, which holds the value of [[id]] when [[DbAuthManager]] or
* [[PhpAuthManager]] is used.
* @param boolean $allowCaching whether to allow caching the result of access check.
* When this parameter is true (default), if the access check of an operation was performed
* before, its result will be directly returned when calling this method to check the same
* operation. If this parameter is false, this method will always call
* [[AuthManager::checkAccess()]] to obtain the up-to-date access result. Note that this
* caching is effective only within the same request and only works when `$params = array()`.
* @return boolean whether the operations can be performed by this user.
12 years ago
*/
public function checkAccess($operation, $params = array(), $allowCaching = true)
12 years ago
{
$auth = Yii::$app->getAuthManager();
if ($auth === null) {
12 years ago
return false;
12 years ago
}
if ($allowCaching && empty($params) && isset($this->_access[$operation])) {
return $this->_access[$operation];
}
$access = $auth->checkAccess($this->getId(), $operation, $params);
if ($allowCaching && empty($params)) {
$this->_access[$operation] = $access;
}
return $access;
12 years ago
}
12 years ago
}