今天阅读的是vendor/yiisoft/yii2/web/User.php
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yiiweb; //命名空间
use Yii;
use yiiaseComponent;
use yiiaseInvalidConfigException;
use yiiaseInvalidValueException; //加载相关的类
class User extends Component //User 类继承Compoent类,是用于管理用户身份验证状态“用户”应用程序组件的类。
{
const EVENT_BEFORE_LOGIN = 'beforeLogin';
const EVENT_AFTER_LOGIN = 'afterLogin';
const EVENT_BEFORE_LOGOUT = 'beforeLogout';
const EVENT_AFTER_LOGOUT = 'afterLogout'; //const定义一些常量。
public $identityClass; //标识对象的类名。
public $enableAutoLogin = false; //是否启用基于cookie的登录。默认为false。请注意,如果[enableSession]是假此属性将被忽略。
public $enableSession = true; //否使用会话跨多个请求持续认证状态。
public $loginUrl = ['site/login']; //登录URL时loginRequired()被调用。第一个是控制器,第二个是方法,后面跟的是参数。
public $identityCookie = ['name' => '_identity', 'httpOnly' => true];//COOKIE的配置,仅当登录时是正确的。
public $authTimeout; //用户登录的状态,如果非活动状态,注销。
public $absoluteAuthTimeout;
public $autoRenewCookie = true;//每次请求页面时自动更新COOKIE。
public $idParam = '__id';//用来储存ID的值,以下同理。
public $authTimeoutParam = '__expire';
public $absoluteAuthTimeoutParam = '__absoluteExpire';
public $returnUrlParam = '__returnUrl';
private $_access = [];
public function init() //初始化程序应用组件。
{
parent::init();//调用init()方法。
if ($this->identityClass === null) { //如果此变量为空,那么会执行一个报错信息,identityClass必须要设置。
throw new InvalidConfigException('User::identityClass must be set.');
}
if ($this->enableAutoLogin && !isset($this->identityCookie['name'])) { //如果$this->enableAutoLogin为true并且$this->identityCookie['name']没设置,抛出一个信息,身份的Cookie中必须包含'name'元素。
throw new InvalidConfigException('User::identityCookie must contain the "name" element.');
}
}
private $_identity = false; //设置此变量为false;
public function getIdentity($autoRenew = true) //返回与当前登录的用户关联的标识对象
{
if ($this->_identity === false) { //如果此变量为false,$this->enableSession是真的并且$autoRenew为true。读取相应的标识对象数据。
if ($this->enableSession && $autoRenew) {
$this->_identity = null;
$this->renewAuthStatus();
} else { //否则,返回null.
return null;
}
}
return $this->_identity;
}
public function setIdentity($identity) //设置用户身份
{
if ($identity instanceof IdentityInterface) {//如果$identity满足IdentityInterface验证方法。那么使$this->_identity =$identity,$this->_access为数组。
$this->_identity = $identity;
$this->_access = [];
} elseif ($identity === null) { //如果$identity为空,设置$this->_identity =null.
$this->_identity = null;
} else {
throw new InvalidValueException('The identity object must implement IdentityInterface.');//都不满足的话,抛出一个信息,身份对象必须满足IdentityInterface.
}
public function login(IdentityInterface $identity, $duration = 0)//用户登录
{
if ($this->beforeLogin($identity, false, $duration)) { //如果调用beforeLogin方法返回值是true,那么走下面的程序。
$this->switchIdentity($identity, $duration);
$id = $identity->getId(); //$id等于 $identity->getId()该方法返回的值。
$ip = Yii::$app->getRequest()->getUserIP(); //ip为Yii::$app->getRequest()->getUserIP()返回的IP。
if ($this->enableSession) {
$log = "User '$id' logged in from $ip with duration $duration.";//如果$this->enableSession是真的话,存储在Cookie中的信息将会在$duration后到期。
} else {
$log = "User '$id' logged in from $ip. Session not enabled.";//如果是假的话,$duration将会在这种没有意思的情况下忽略。
}
Yii::info($log, __METHOD__);
$this->afterLogin($identity, false, $duration);
}
return !$this->getIsGuest();
}
public function loginByAccessToken($token, $type = null)//给用户返回登录的制定令牌。
{
/* @var $class IdentityInterface */
$class = $this->identityClass;
$identity = $class::findIdentityByAccessToken($token, $type);
if ($identity && $this->login($identity)) {//登录成功,返回身份用户。失败则返回null.
return $identity;
} else {
return null;
}
}