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

    今天继续阅读vendor/yiisoft/yii2/web/User.php
      protected function loginByCookie() //用户的日志。
        {
            $value = Yii::$app->getRequest()->getCookies()->getValue($this->identityCookie['name']); //根据登录的Cookie中name字段取得用户的日志信息。
            if ($value === null) {
                return;            //如果$value的值为空,则返回。
            }
    
            $data = json_decode($value, true); //用json_decode的方式对$value的值做处理,带上后缀的参数true.处理后的值用$data表示。
            if (count($data) !== 3 || !isset($data[0], $data[1], $data[2])) { //如果$data里面的值得数量不等于3或者没有设置$data[0],$data[1],$data[2]里面的值,返回。
                return;
            }
    
            list ($id, $authKey, $duration) = $data; //把$data中0,1,2下标的值分别赋给$id,$authKey,$duration.
            /* @var $class IdentityInterface */
            $class = $this->identityClass;
            $identity = $class::findIdentity($id); //$identity由静态类方法调用,参数是$id。
            if ($identity === null) {  //如果$identity的值全等于null,则返回。
                return;
            } elseif (!$identity instanceof IdentityInterface) { //如果$identity不符合IdentityInterface类中的运算,则抛出一个新的提示:必须返回一个对象。
                throw new InvalidValueException("$class::findIdentity() must return an object implementing IdentityInterface.");
            }
    
            if ($identity->validateAuthKey($authKey)) {
                if ($this->beforeLogin($identity, true, $duration)) { //如果$this->beforeLogin($identity, true, $duration)有值,即$event->isValid有值,则走下面这一步。
                    $this->switchIdentity($identity, $this->autoRenewCookie ? $duration : 0); //三元运算符,如果$this->autoRenewCookie存在,则第二个参数为$duration,如果不存在,第二个参数为0.
    $ip = Yii::$app->getRequest()->getUserIP(); //静态调用YII类,值赋值给$ip;
                    Yii::info("User '$id' logged in from $ip via cookie.", __METHOD__);//信息内容为User '$id' logged in from $ip via cookie。
                    $this->afterLogin($identity, true, $duration);
                }
            } else {
                Yii::warning("Invalid auth key attempted for user '$id': $authKey", __METHOD__);//如果$identity->validateAuthKey($authKey)没值,返回错误信息。
            }
        }
    
     public function logout($destroySession = true)   //用户退出默认$destroySession为真。
        {
            $identity = $this->getIdentity();
            if ($identity !== null && $this->beforeLogout($identity)) {//如果这两个条件同时成立,那么就把$id,$ip的值得到。
                $this->switchIdentity(null);
                $id = $identity->getId();
                $ip = Yii::$app->getRequest()->getUserIP();
                Yii::info("User '$id' logged out from $ip.", __METHOD__);//信息内容,这个id的用户注销。
                if ($destroySession && $this->enableSession) {
                    Yii::$app->getSession()->destroy();  //如果$destroySession为真并且$this->enableSession为真,调用静态方法销毁数据,达到退出的目的。
                }
                $this->afterLogout($identity);
            }
    
            return $this->getIsGuest(); //调用getIsGuest方法,使$this->getIdentity()的值为null.
        }
    
    
          public function getIsGuest()
        {
            return $this->getIdentity() === null;//使$this-<getIdentity()的值全等于null.
        }
    
          public function getId()
        {
            $identity = $this->getIdentity();
    
            return $identity !== null ? $identity->getId() : null; //返回 $identity,如果$identity不为null,返回$identity->getId();如果为null,返回null.
        }
    
          public function getReturnUrl($defaultUrl = null)//返回的网址,成功登陆后重定向的url.
        {
            $url = Yii::$app->getSession()->get($this->returnUrlParam, $defaultUrl);//url赋值。参数为$this->returnUrlParam,$defaultUrl.
            if (is_array($url)) {
                if (isset($url[0])) { //如果$url为数组并且$url[0]下标存在的话,返回一个重定向的url.
                    return Yii::$app->getUrlManager()->createUrl($url);
                } else {
                    $url = null;  
                }
            }
    
            return $url === null ? Yii::$app->getHomeUrl() : $url;//三元运算符。
        }
    
         public function setReturnUrl($url)
        {
            Yii::$app->getSession()->set($this->returnUrlParam, $url);
        }
    
        public function loginRequired($checkAjax = true) //将用户重定向浏览器到登录页面。
        {
            $request = Yii::$app->getRequest();  //$request的值为访问静态类得到的值。
            if ($this->enableSession && (!$checkAjax || !$request->getIsAjax())) {
                $this->setReturnUrl($request->getUrl());
            }
            if ($this->loginUrl !== null) { //如果不为null,$loginUrl=$this->loginUrl强制转换为数组形式。
                $loginUrl = (array) $this->loginUrl;
                if ($loginUrl[0] !== Yii::$app->requestedRoute) {
                    return Yii::$app->getResponse()->redirect($this->loginUrl);
                }
            }
            throw new ForbiddenHttpException(Yii::t('yii', 'Login Required'));//抛出提示。
        }
  • 相关阅读:
    108. Convert Sorted Array to Binary Search Tree
    How to check if one path is a child of another path?
    Why there is two completely different version of Reverse for List and IEnumerable?
    在Jenkins中集成Sonarqube
    如何查看sonarqube的版本 how to check the version of sonarqube
    Queue
    BFS广度优先 vs DFS深度优先 for Binary Tree
    Depth-first search and Breadth-first search 深度优先搜索和广度优先搜索
    102. Binary Tree Level Order Traversal 广度优先遍历
    How do I check if a type is a subtype OR the type of an object?
  • 原文地址:https://www.cnblogs.com/taokai/p/5384800.html
Copyright © 2011-2022 走看看