zoukankan      html  css  js  c++  java
  • YII2框架阅读随笔

    今天阅读的是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; } }
  • 相关阅读:
    A、B、C、D四个字母,能组成多少个互不相同且无重复三位组合
    一对老耗子,每个月都生一对小耗子。小耗子长3个月,第四个开始变成老耗子开始生! 假如都不死,那么请问24个月后有多少只耗子?
    猜数字大小游戏,用户输入一个数字,如果大了就显示大了,如果小了就显示小了, 如果对了就提示正确(补充难度,只有5次机会,限制数字的范围在百位以内)
    输出100-300中的任意两个数相同的三位数(不能三个数都相同)
    打印出A到Z的所有字符,使用char和int转换
    EmployeeMapper.xml例子,学习佟刚老师的myBatis课程,记录下的EmployeeMapper.xml,注释详细
    log4j.xml 精选的log4j.xml文档,比较详细,网上的版本很多,这个版本相对而言比较完整
    Win-Lin双系统重装Windows找回Linux启动
    素材下载网站
    Android系统备忘1
  • 原文地址:https://www.cnblogs.com/taokai/p/5381854.html
Copyright © 2011-2022 走看看