ACF是一種通過(guò)yii\filters\AccessControl類來(lái)實(shí)現(xiàn)的簡(jiǎn)單授權(quán)
創(chuàng)新互聯(lián)建站成立于2013年,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元丁青做網(wǎng)站,已為上家服務(wù),為丁青各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108
一般在控制器中我們調(diào)用如下:
打開backend\controller\SiteController.php 我們看到這樣一段代碼
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['login', 'error'],
'allow' => true,
],
[
'actions' => ['logout', 'index'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
幾個(gè)必須到配置:
1、配置驗(yàn)證類User
'user' => [
'identityClass' => 'common\models\Usermember',
'enableAutoLogin' => true,
'identityCookie' => ['name' => '_identity-frontend', 'httpOnly' => true],
'loginUrl'=>'/public/login'
],
2、繼承IdentityInterface接口
一般來(lái)說(shuō),從數(shù)據(jù)庫(kù)查找數(shù)據(jù),只需要繼承AR類即可,但是,我們這個(gè)是用戶登錄模型,核心是驗(yàn)證,所以自然需要實(shí)現(xiàn)核心的驗(yàn)證功能,就像LoginForm模型提到的validatePassword一樣,實(shí)際的驗(yàn)證邏輯是在當(dāng)前的User模型完成的。一般來(lái)說(shuō),實(shí)現(xiàn)IdentityInterface接口,需要實(shí)現(xiàn)以下方法:
public static function findIdentity($id); //①
public static function findIdentityByAccessToken($token, $type = null); //② public function getId(); //③ public function getAuthKey(); //④ public function validateAuthKey($authKey); //⑤
3、登錄到login()做相關(guān)調(diào)用驗(yàn)證:
public function login()
{
if ($this->validate()) {
if($this->rememberMe)
{
$this->_user->generateAuthKey();//③
}
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 36002430 : 0);
}
return false;
}
4、對(duì)用到控制器類做配置
/**
- {@inheritdoc}
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['userhome', 'signup'],
'rules' => [
[
'actions' => ['','signup'],
'allow' => true,
'roles' => ['?'],
],
[
'actions' => ['userhome'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}