javalzbin
- javalzbin 2016-11-18 已签到连续签到270天,获得了20个金钱
- javalzbin 回答了问题 rbac具体能实现哪些厉害的功能?
1是普通,2是实名,3是高级。执行action的时候我判断这个字段的值
那如果,作为普通(1),有20个Controller,100个action,你是要在这100个action中每一个都判断,if(xx=1){}吗?然后我突然多了一个“4是vip”,你又一个一个action去判断吗?作为普通的,应要求,100个action有30个不能访问,又一个一个去修改?
在RBAC中,权限与角色相关联,用户通过成为适当角色的成员而得到这些角色的权限。这就极大地简化了权限的管理。
首先角色,普通、实名、高级 这就是角色
然后权限,你的action就是权限。rbac你可以这么理解,你用户表设置一个字段,然后关联到角色表(普通、实名、高级)的id,然后这个角色拥有那些权限,就是action集合,那么,你只需要在一个地方,获取他的角色,然后找到对应的权限集,判断当前这个访问是否在这个集合里面,如果包含在集合里面说明有权限访问,如果不在集合里面,说明没权限访问。你要操作的,只需要给这个角色,增加或者删除对应的action即可。
- javalzbin 2016-11-17 已签到连续签到269天,获得了20个金钱
- javalzbin 2016-11-16 已签到连续签到268天,获得了20个金钱
先说自动登录,cookie在哪里设置的,允许自动登录必须得启用enableAutoLogin,设置为true
'user' => [ 'identityClass' => 'common\models\User', 'enableAutoLogin' => true, ],
然后看登录,在你的LoginFrom中的login方法有这么一行代码:
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
也是真正执行登录操作的代码,他实际上调用的是\yii\web\User的login方法,这个类的实际路径在:盘符/项目名/vendor/yiisoft/yii2/web/User.php
public function login(IdentityInterface $identity, $duration = 0) { if ($this->beforeLogin($identity, false, $duration)) { $this->switchIdentity($identity, $duration); ............ ...其他代码.. ........... } ...... }
首先执行登录之前的操作,然后,关键是$this->switchIdentity($identity, $duration)这个方法:
public function switchIdentity($identity, $duration = 0) { ...... ...... if ($identity) { ...... ...... if ($duration > 0 && $this->enableAutoLogin) { $this->sendIdentityCookie($identity, $duration); } } elseif ($this->enableAutoLogin) { ...... } }
在$this->sendIdentityCookie($identity, $duration);这里把cookie设置进去
protected function sendIdentityCookie($identity, $duration) { $cookie = new Cookie($this->identityCookie); $cookie->value = json_encode([ $identity->getId(), $identity->getAuthKey(), $duration, ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); $cookie->expire = time() + $duration; Yii::$app->getResponse()->getCookies()->add($cookie); }
Yii::$app->getResponse()->getCookies()->add($cookie); 这里设置cookie
到这里,应该解决了cookie的设置的问题了吧。
然后说说自动登录的实现
在SiteController中的actionLogin()方法,他判断这个用户是不是登录用户,如果不是,则返回主页或者说登录页if (!\Yii::$app->user->isGuest) { return $this->goHome(); }
还是走到\yii\web\User这个类,调用了getIsGuest()方法,getIsGuest()方法调用getIdentity($autoRenew = true)方法
public function getIdentity($autoRenew = true) { if ($this->_identity === false) { if ($this->enableSession && $autoRenew) { $this->_identity = null; $this->renewAuthStatus(); } else { return null; } } return $this->_identity; }
是否开启自动登录($this->enableSession && $autoRenew),是,那么执行renewAuthStatus()方法:
protected function renewAuthStatus() { ...... ...前面代码是session有值的判断,不看他... ...... if ($this->enableAutoLogin) { if ($this->getIsGuest()) { $this->loginByCookie(); } elseif ($this->autoRenewCookie) { $this->renewIdentityCookie(); } } }
启用自动登录$this->enableAutoLogin为true,然后判断是否是访客$this->getIsGuest(),如果是,则执行登录,从cookie登录$this->loginByCookie();这就是cookie登录的具体实现
protected function loginByCookie() { //cookie取值 $value = Yii::$app->getRequest()->getCookies()->getValue($this->identityCookie['name']); if ($value === null) { return; } $data = json_decode($value, true); if (count($data) !== 3 || !isset($data[0], $data[1], $data[2])) { return; } list ($id, $authKey, $duration) = $data; /* @var $class IdentityInterface */ $class = $this->identityClass; $identity = $class::findIdentity($id); if ($identity === null) { return; } elseif (!$identity instanceof IdentityInterface) { throw new InvalidValueException("$class::findIdentity() must return an object implementing IdentityInterface."); } if ($identity->validateAuthKey($authKey)) { if ($this->beforeLogin($identity, true, $duration)) { $this->switchIdentity($identity, $this->autoRenewCookie ? $duration : 0); $ip = Yii::$app->getRequest()->getUserIP(); Yii::info("User '$id' logged in from $ip via cookie.", __METHOD__); $this->afterLogin($identity, true, $duration); } } else { Yii::warning("Invalid auth key attempted for user '$id': $authKey", __METHOD__); } }
$identity = $class::findIdentity($id);//根据cookie保存的记录id在数据库中找,这里调用的是在配置文件中main.php配置的
'user' => [ 'identityClass' => 'common\models\User', 'enableAutoLogin' => true, ],
common\models\User这个类的findIdentity($id)方法
public static function findIdentity($id) { return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]); }
if ($identity->validateAuthKey($authKey)) {} 则调用 common\models\User这个类的validateAuthKey($authKey)方法,实际上就是判断这个记录id的auth_key这个数据库的值是否跟保存到cookie中的一样,如果一样,返回true。
目的是,所以,如果管理员要修改一个帐号的密码,实际上出了修改他的密码password_hash之外,还需要修改auth_key,否则他如果以前登录过。cookie有值,一样可以正常登录。这点很重要然后就没啥好说的拉,就是正常的登录流程拉,个人建议,好好利用ide的debug功能,跟踪代码绝对杠杠的
@数字派 你还是debug跟踪一下代码吧,我表达的并没有看代码直接
先说自动登录,cookie在哪里设置的,允许自动登录必须得启用enableAutoLogin,设置为true
'user' => [ 'identityClass' => 'common\models\User', 'enableAutoLogin' => true, ],
然后看登录,在你的LoginFrom中的login方法有这么一行代码:
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
也是真正执行登录操作的代码,他实际上调用的是\yii\web\User的login方法,这个类的实际路径在:盘符/项目名/vendor/yiisoft/yii2/web/User.php
public function login(IdentityInterface $identity, $duration = 0) { if ($this->beforeLogin($identity, false, $duration)) { $this->switchIdentity($identity, $duration); ............ ...其他代码.. ........... } ...... }
首先执行登录之前的操作,然后,关键是$this->switchIdentity($identity, $duration)这个方法:
public function switchIdentity($identity, $duration = 0) { ...... ...... if ($identity) { ...... ...... if ($duration > 0 && $this->enableAutoLogin) { $this->sendIdentityCookie($identity, $duration); } } elseif ($this->enableAutoLogin) { ...... } }
在$this->sendIdentityCookie($identity, $duration);这里把cookie设置进去
protected function sendIdentityCookie($identity, $duration) { $cookie = new Cookie($this->identityCookie); $cookie->value = json_encode([ $identity->getId(), $identity->getAuthKey(), $duration, ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); $cookie->expire = time() + $duration; Yii::$app->getResponse()->getCookies()->add($cookie); }
Yii::$app->getResponse()->getCookies()->add($cookie); 这里设置cookie
到这里,应该解决了cookie的设置的问题了吧。
然后说说自动登录的实现
在SiteController中的actionLogin()方法,他判断这个用户是不是登录用户,如果不是,则返回主页或者说登录页if (!\Yii::$app->user->isGuest) { return $this->goHome(); }
还是走到\yii\web\User这个类,调用了getIsGuest()方法,getIsGuest()方法调用getIdentity($autoRenew = true)方法
public function getIdentity($autoRenew = true) { if ($this->_identity === false) { if ($this->enableSession && $autoRenew) { $this->_identity = null; $this->renewAuthStatus(); } else { return null; } } return $this->_identity; }
是否开启自动登录($this->enableSession && $autoRenew),是,那么执行renewAuthStatus()方法:
protected function renewAuthStatus() { ...... ...前面代码是session有值的判断,不看他... ...... if ($this->enableAutoLogin) { if ($this->getIsGuest()) { $this->loginByCookie(); } elseif ($this->autoRenewCookie) { $this->renewIdentityCookie(); } } }
启用自动登录$this->enableAutoLogin为true,然后判断是否是访客$this->getIsGuest(),如果是,则执行登录,从cookie登录$this->loginByCookie();这就是cookie登录的具体实现
protected function loginByCookie() { //cookie取值 $value = Yii::$app->getRequest()->getCookies()->getValue($this->identityCookie['name']); if ($value === null) { return; } $data = json_decode($value, true); if (count($data) !== 3 || !isset($data[0], $data[1], $data[2])) { return; } list ($id, $authKey, $duration) = $data; /* @var $class IdentityInterface */ $class = $this->identityClass; $identity = $class::findIdentity($id); if ($identity === null) { return; } elseif (!$identity instanceof IdentityInterface) { throw new InvalidValueException("$class::findIdentity() must return an object implementing IdentityInterface."); } if ($identity->validateAuthKey($authKey)) { if ($this->beforeLogin($identity, true, $duration)) { $this->switchIdentity($identity, $this->autoRenewCookie ? $duration : 0); $ip = Yii::$app->getRequest()->getUserIP(); Yii::info("User '$id' logged in from $ip via cookie.", __METHOD__); $this->afterLogin($identity, true, $duration); } } else { Yii::warning("Invalid auth key attempted for user '$id': $authKey", __METHOD__); } }
$identity = $class::findIdentity($id);//根据cookie保存的记录id在数据库中找,这里调用的是在配置文件中main.php配置的
'user' => [ 'identityClass' => 'common\models\User', 'enableAutoLogin' => true, ],
common\models\User这个类的findIdentity($id)方法
public static function findIdentity($id) { return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]); }
if ($identity->validateAuthKey($authKey)) {} 则调用 common\models\User这个类的validateAuthKey($authKey)方法,实际上就是判断这个记录id的auth_key这个数据库的值是否跟保存到cookie中的一样,如果一样,返回true。
目的是,所以,如果管理员要修改一个帐号的密码,实际上出了修改他的密码password_hash之外,还需要修改auth_key,否则他如果以前登录过。cookie有值,一样可以正常登录。这点很重要然后就没啥好说的拉,就是正常的登录流程拉,个人建议,好好利用ide的debug功能,跟踪代码绝对杠杠的
@数字派 session没有值,cookie有值,它会验证cookie的值,然后,他会把值放到session中去,下次他会从session中取值来判断,我建议你写一个父类,让你的Controller都继承这个父类,然后你在父类中重写beforeAction($action)方法,在这里去判断就好了,判断通过,beforeAction方法返回true,然后他自动会去执行你要执行的Controller的action,你需要在一个地方写一句代码,就可以拦截所有为登录和已经登录的请求了
先说自动登录,cookie在哪里设置的,允许自动登录必须得启用enableAutoLogin,设置为true
'user' => [ 'identityClass' => 'common\models\User', 'enableAutoLogin' => true, ],
然后看登录,在你的LoginFrom中的login方法有这么一行代码:
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
也是真正执行登录操作的代码,他实际上调用的是\yii\web\User的login方法,这个类的实际路径在:盘符/项目名/vendor/yiisoft/yii2/web/User.php
public function login(IdentityInterface $identity, $duration = 0) { if ($this->beforeLogin($identity, false, $duration)) { $this->switchIdentity($identity, $duration); ............ ...其他代码.. ........... } ...... }
首先执行登录之前的操作,然后,关键是$this->switchIdentity($identity, $duration)这个方法:
public function switchIdentity($identity, $duration = 0) { ...... ...... if ($identity) { ...... ...... if ($duration > 0 && $this->enableAutoLogin) { $this->sendIdentityCookie($identity, $duration); } } elseif ($this->enableAutoLogin) { ...... } }
在$this->sendIdentityCookie($identity, $duration);这里把cookie设置进去
protected function sendIdentityCookie($identity, $duration) { $cookie = new Cookie($this->identityCookie); $cookie->value = json_encode([ $identity->getId(), $identity->getAuthKey(), $duration, ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); $cookie->expire = time() + $duration; Yii::$app->getResponse()->getCookies()->add($cookie); }
Yii::$app->getResponse()->getCookies()->add($cookie); 这里设置cookie
到这里,应该解决了cookie的设置的问题了吧。
然后说说自动登录的实现
在SiteController中的actionLogin()方法,他判断这个用户是不是登录用户,如果不是,则返回主页或者说登录页if (!\Yii::$app->user->isGuest) { return $this->goHome(); }
还是走到\yii\web\User这个类,调用了getIsGuest()方法,getIsGuest()方法调用getIdentity($autoRenew = true)方法
public function getIdentity($autoRenew = true) { if ($this->_identity === false) { if ($this->enableSession && $autoRenew) { $this->_identity = null; $this->renewAuthStatus(); } else { return null; } } return $this->_identity; }
是否开启自动登录($this->enableSession && $autoRenew),是,那么执行renewAuthStatus()方法:
protected function renewAuthStatus() { ...... ...前面代码是session有值的判断,不看他... ...... if ($this->enableAutoLogin) { if ($this->getIsGuest()) { $this->loginByCookie(); } elseif ($this->autoRenewCookie) { $this->renewIdentityCookie(); } } }
启用自动登录$this->enableAutoLogin为true,然后判断是否是访客$this->getIsGuest(),如果是,则执行登录,从cookie登录$this->loginByCookie();这就是cookie登录的具体实现
protected function loginByCookie() { //cookie取值 $value = Yii::$app->getRequest()->getCookies()->getValue($this->identityCookie['name']); if ($value === null) { return; } $data = json_decode($value, true); if (count($data) !== 3 || !isset($data[0], $data[1], $data[2])) { return; } list ($id, $authKey, $duration) = $data; /* @var $class IdentityInterface */ $class = $this->identityClass; $identity = $class::findIdentity($id); if ($identity === null) { return; } elseif (!$identity instanceof IdentityInterface) { throw new InvalidValueException("$class::findIdentity() must return an object implementing IdentityInterface."); } if ($identity->validateAuthKey($authKey)) { if ($this->beforeLogin($identity, true, $duration)) { $this->switchIdentity($identity, $this->autoRenewCookie ? $duration : 0); $ip = Yii::$app->getRequest()->getUserIP(); Yii::info("User '$id' logged in from $ip via cookie.", __METHOD__); $this->afterLogin($identity, true, $duration); } } else { Yii::warning("Invalid auth key attempted for user '$id': $authKey", __METHOD__); } }
$identity = $class::findIdentity($id);//根据cookie保存的记录id在数据库中找,这里调用的是在配置文件中main.php配置的
'user' => [ 'identityClass' => 'common\models\User', 'enableAutoLogin' => true, ],
common\models\User这个类的findIdentity($id)方法
public static function findIdentity($id) { return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]); }
if ($identity->validateAuthKey($authKey)) {} 则调用 common\models\User这个类的validateAuthKey($authKey)方法,实际上就是判断这个记录id的auth_key这个数据库的值是否跟保存到cookie中的一样,如果一样,返回true。
目的是,所以,如果管理员要修改一个帐号的密码,实际上出了修改他的密码password_hash之外,还需要修改auth_key,否则他如果以前登录过。cookie有值,一样可以正常登录。这点很重要然后就没啥好说的拉,就是正常的登录流程拉,个人建议,好好利用ide的debug功能,跟踪代码绝对杠杠的
@数字派 你想做什么?自动登录不用你去改,你只需要在执行action之前,通过这句代码 if (!\Yii::$app->user->isGuest) {} 他就自动帮你做了,如果条件为true,表示没有登录,你就跳转到登录页面,如果为false,他就是已经登录的用户
先说自动登录,cookie在哪里设置的,允许自动登录必须得启用enableAutoLogin,设置为true
'user' => [ 'identityClass' => 'common\models\User', 'enableAutoLogin' => true, ],
然后看登录,在你的LoginFrom中的login方法有这么一行代码:
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
也是真正执行登录操作的代码,他实际上调用的是\yii\web\User的login方法,这个类的实际路径在:盘符/项目名/vendor/yiisoft/yii2/web/User.php
public function login(IdentityInterface $identity, $duration = 0) { if ($this->beforeLogin($identity, false, $duration)) { $this->switchIdentity($identity, $duration); ............ ...其他代码.. ........... } ...... }
首先执行登录之前的操作,然后,关键是$this->switchIdentity($identity, $duration)这个方法:
public function switchIdentity($identity, $duration = 0) { ...... ...... if ($identity) { ...... ...... if ($duration > 0 && $this->enableAutoLogin) { $this->sendIdentityCookie($identity, $duration); } } elseif ($this->enableAutoLogin) { ...... } }
在$this->sendIdentityCookie($identity, $duration);这里把cookie设置进去
protected function sendIdentityCookie($identity, $duration) { $cookie = new Cookie($this->identityCookie); $cookie->value = json_encode([ $identity->getId(), $identity->getAuthKey(), $duration, ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); $cookie->expire = time() + $duration; Yii::$app->getResponse()->getCookies()->add($cookie); }
Yii::$app->getResponse()->getCookies()->add($cookie); 这里设置cookie
到这里,应该解决了cookie的设置的问题了吧。
然后说说自动登录的实现
在SiteController中的actionLogin()方法,他判断这个用户是不是登录用户,如果不是,则返回主页或者说登录页if (!\Yii::$app->user->isGuest) { return $this->goHome(); }
还是走到\yii\web\User这个类,调用了getIsGuest()方法,getIsGuest()方法调用getIdentity($autoRenew = true)方法
public function getIdentity($autoRenew = true) { if ($this->_identity === false) { if ($this->enableSession && $autoRenew) { $this->_identity = null; $this->renewAuthStatus(); } else { return null; } } return $this->_identity; }
是否开启自动登录($this->enableSession && $autoRenew),是,那么执行renewAuthStatus()方法:
protected function renewAuthStatus() { ...... ...前面代码是session有值的判断,不看他... ...... if ($this->enableAutoLogin) { if ($this->getIsGuest()) { $this->loginByCookie(); } elseif ($this->autoRenewCookie) { $this->renewIdentityCookie(); } } }
启用自动登录$this->enableAutoLogin为true,然后判断是否是访客$this->getIsGuest(),如果是,则执行登录,从cookie登录$this->loginByCookie();这就是cookie登录的具体实现
protected function loginByCookie() { //cookie取值 $value = Yii::$app->getRequest()->getCookies()->getValue($this->identityCookie['name']); if ($value === null) { return; } $data = json_decode($value, true); if (count($data) !== 3 || !isset($data[0], $data[1], $data[2])) { return; } list ($id, $authKey, $duration) = $data; /* @var $class IdentityInterface */ $class = $this->identityClass; $identity = $class::findIdentity($id); if ($identity === null) { return; } elseif (!$identity instanceof IdentityInterface) { throw new InvalidValueException("$class::findIdentity() must return an object implementing IdentityInterface."); } if ($identity->validateAuthKey($authKey)) { if ($this->beforeLogin($identity, true, $duration)) { $this->switchIdentity($identity, $this->autoRenewCookie ? $duration : 0); $ip = Yii::$app->getRequest()->getUserIP(); Yii::info("User '$id' logged in from $ip via cookie.", __METHOD__); $this->afterLogin($identity, true, $duration); } } else { Yii::warning("Invalid auth key attempted for user '$id': $authKey", __METHOD__); } }
$identity = $class::findIdentity($id);//根据cookie保存的记录id在数据库中找,这里调用的是在配置文件中main.php配置的
'user' => [ 'identityClass' => 'common\models\User', 'enableAutoLogin' => true, ],
common\models\User这个类的findIdentity($id)方法
public static function findIdentity($id) { return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]); }
if ($identity->validateAuthKey($authKey)) {} 则调用 common\models\User这个类的validateAuthKey($authKey)方法,实际上就是判断这个记录id的auth_key这个数据库的值是否跟保存到cookie中的一样,如果一样,返回true。
目的是,所以,如果管理员要修改一个帐号的密码,实际上出了修改他的密码password_hash之外,还需要修改auth_key,否则他如果以前登录过。cookie有值,一样可以正常登录。这点很重要然后就没啥好说的拉,就是正常的登录流程拉,个人建议,好好利用ide的debug功能,跟踪代码绝对杠杠的
@数字派 不是这个意思,cookie除了保存记录的id值,还有保存authkey,那么通过cookie登录时,通过cookie取的记录的id
值,然后匹配cookie中的authkey是否和数据库查询出来的authkey相当,如果相等,就能自动登录,否则登录失败先说自动登录,cookie在哪里设置的,允许自动登录必须得启用enableAutoLogin,设置为true
'user' => [ 'identityClass' => 'common\models\User', 'enableAutoLogin' => true, ],
然后看登录,在你的LoginFrom中的login方法有这么一行代码:
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
也是真正执行登录操作的代码,他实际上调用的是\yii\web\User的login方法,这个类的实际路径在:盘符/项目名/vendor/yiisoft/yii2/web/User.php
public function login(IdentityInterface $identity, $duration = 0) { if ($this->beforeLogin($identity, false, $duration)) { $this->switchIdentity($identity, $duration); ............ ...其他代码.. ........... } ...... }
首先执行登录之前的操作,然后,关键是$this->switchIdentity($identity, $duration)这个方法:
public function switchIdentity($identity, $duration = 0) { ...... ...... if ($identity) { ...... ...... if ($duration > 0 && $this->enableAutoLogin) { $this->sendIdentityCookie($identity, $duration); } } elseif ($this->enableAutoLogin) { ...... } }
在$this->sendIdentityCookie($identity, $duration);这里把cookie设置进去
protected function sendIdentityCookie($identity, $duration) { $cookie = new Cookie($this->identityCookie); $cookie->value = json_encode([ $identity->getId(), $identity->getAuthKey(), $duration, ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); $cookie->expire = time() + $duration; Yii::$app->getResponse()->getCookies()->add($cookie); }
Yii::$app->getResponse()->getCookies()->add($cookie); 这里设置cookie
到这里,应该解决了cookie的设置的问题了吧。
然后说说自动登录的实现
在SiteController中的actionLogin()方法,他判断这个用户是不是登录用户,如果不是,则返回主页或者说登录页if (!\Yii::$app->user->isGuest) { return $this->goHome(); }
还是走到\yii\web\User这个类,调用了getIsGuest()方法,getIsGuest()方法调用getIdentity($autoRenew = true)方法
public function getIdentity($autoRenew = true) { if ($this->_identity === false) { if ($this->enableSession && $autoRenew) { $this->_identity = null; $this->renewAuthStatus(); } else { return null; } } return $this->_identity; }
是否开启自动登录($this->enableSession && $autoRenew),是,那么执行renewAuthStatus()方法:
protected function renewAuthStatus() { ...... ...前面代码是session有值的判断,不看他... ...... if ($this->enableAutoLogin) { if ($this->getIsGuest()) { $this->loginByCookie(); } elseif ($this->autoRenewCookie) { $this->renewIdentityCookie(); } } }
启用自动登录$this->enableAutoLogin为true,然后判断是否是访客$this->getIsGuest(),如果是,则执行登录,从cookie登录$this->loginByCookie();这就是cookie登录的具体实现
protected function loginByCookie() { //cookie取值 $value = Yii::$app->getRequest()->getCookies()->getValue($this->identityCookie['name']); if ($value === null) { return; } $data = json_decode($value, true); if (count($data) !== 3 || !isset($data[0], $data[1], $data[2])) { return; } list ($id, $authKey, $duration) = $data; /* @var $class IdentityInterface */ $class = $this->identityClass; $identity = $class::findIdentity($id); if ($identity === null) { return; } elseif (!$identity instanceof IdentityInterface) { throw new InvalidValueException("$class::findIdentity() must return an object implementing IdentityInterface."); } if ($identity->validateAuthKey($authKey)) { if ($this->beforeLogin($identity, true, $duration)) { $this->switchIdentity($identity, $this->autoRenewCookie ? $duration : 0); $ip = Yii::$app->getRequest()->getUserIP(); Yii::info("User '$id' logged in from $ip via cookie.", __METHOD__); $this->afterLogin($identity, true, $duration); } } else { Yii::warning("Invalid auth key attempted for user '$id': $authKey", __METHOD__); } }
$identity = $class::findIdentity($id);//根据cookie保存的记录id在数据库中找,这里调用的是在配置文件中main.php配置的
'user' => [ 'identityClass' => 'common\models\User', 'enableAutoLogin' => true, ],
common\models\User这个类的findIdentity($id)方法
public static function findIdentity($id) { return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]); }
if ($identity->validateAuthKey($authKey)) {} 则调用 common\models\User这个类的validateAuthKey($authKey)方法,实际上就是判断这个记录id的auth_key这个数据库的值是否跟保存到cookie中的一样,如果一样,返回true。
目的是,所以,如果管理员要修改一个帐号的密码,实际上出了修改他的密码password_hash之外,还需要修改auth_key,否则他如果以前登录过。cookie有值,一样可以正常登录。这点很重要然后就没啥好说的拉,就是正常的登录流程拉,个人建议,好好利用ide的debug功能,跟踪代码绝对杠杠的
- javalzbin 2016-11-15 已签到连续签到267天,获得了20个金钱