yii2登入流程

來源:互聯網
上載者:User

標籤:

今天研究了下yii2 的登陸流程

 

在資料庫中建立user表

CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT ‘自增ID‘,
`username` varchar(255) NOT NULL COMMENT ‘使用者名稱‘,
`auth_key` varchar(32) NOT NULL COMMENT ‘自動登入key‘,
`password_hash` varchar(255) NOT NULL COMMENT ‘加密密碼‘,
`password_reset_token` varchar(255) DEFAULT NULL COMMENT ‘重設密碼token‘,
`email` varchar(255) NOT NULL COMMENT ‘郵箱‘,
`role` smallint(6) NOT NULL DEFAULT ‘10‘ COMMENT ‘角色等級‘,
`status` smallint(6) NOT NULL DEFAULT ‘10‘ COMMENT ‘狀態‘,
`created_at` int(11) NOT NULL COMMENT ‘建立時間‘,
`updated_at` int(11) NOT NULL COMMENT ‘更新時間‘,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COMMENT=‘使用者表‘

 

然後跟著測試發現跳轉流程大概是這樣的

1、SiteController

 public function actionLogin()    {        if (!\Yii::$app->user->isGuest) {            return $this->goHome();        }                $model = new LoginForm();        if ($model->load(Yii::$app->request->post()) && $model->login()) {//加在post資料,並驗證登陸            return $this->goBack();        } else {            $this->layout = "login.php";            return $this->render(‘login‘, [                ‘model‘ => $model,            ]);        }    }

2、跟進到$model->login,也就是common/modules/LoginForm.php檔案中的login函數

public function login()    {        if ($this->validate()) {//表單驗證通過            return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);        } else {            return false;        }    }

3、這個表單驗證,主要是對loginFrom中的rules規則進行驗證

public function rules(){        return [            // username and password are both required            [[‘username‘, ‘password‘], ‘required‘],            // rememberMe must be a boolean value            [‘rememberMe‘, ‘boolean‘],            // password is validated by validatePassword()            [‘password‘, ‘validatePassword‘],        ];}

4、其中,password需要去驗證validatePassword函數,這個函數用於判斷密碼的格式是否正確

public function validatePassword($attribute, $params){        if (!$this->hasErrors()) {            $user = $this->getUser();            if (!$user || !$user->validatePassword($this->password)) {                $this->addError($attribute, \Yii::t(‘login‘, ‘Incorrect username or password.‘));            }        }}

5、這裡需要注意的是,$this->getUser();的時候,已經獲得了user對象,其中包含了該使用者的password_hash這個變數。

這裡面的!$user->validatePassword($this->password)是去判斷了common/modules/user.php中的函數
public function validatePassword($password){        return Yii::$app->security->validatePassword($password, $this->password_hash);//其中$password,是使用者輸入的密碼,而$this->password_hash是資料庫中儲存的使用者password_hash
}

 

使用者密碼正確後,回到2、執行

return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);

這步驟是執行使用者登入操作,並儲存cookie
其中
Yii::$app->user是在\yii\console\Application|\yii\web\user.php
public function login(IdentityInterface $identity, $duration = 0){        if ($this->beforeLogin($identity, false, $duration)) {            $this->switchIdentity($identity, $duration);

        /************* 

         switchIdentity函數用於
         1、設定session的有效期間

          2、如果cookie的有效期間大於0並且允許自動登入,那麼就把使用者的認證資訊儲存到cookie中

          3、如果允許自動登入,刪除cookie資訊。這個是用於退出的時候調用的。退出的時候傳遞進來的$identity為null

       ************/            $id = $identity->getId();            $ip = Yii::$app->getRequest()->getUserIP();            if ($this->enableSession) {                $log = "User ‘$id‘ logged in from $ip with duration $duration.";            } else {                $log = "User ‘$id‘ logged in from $ip. Session not enabled.";            }            Yii::info($log, __METHOD__);            $this->afterLogin($identity, false, $duration);        }        return !$this->getIsGuest();}

 

以上內容參考了 http://www.kuitao8.com/20150518/3747.shtml

yii2登入流程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.