您好,登錄后才能下訂單哦!
ACF是一種通過yii\filters\AccessControl類來實(shí)現(xiàn)的簡單授權(quán)
一般在控制器中我們調(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接口
一般來說,從數(shù)據(jù)庫查找數(shù)據(jù),只需要繼承AR類即可,但是,我們這個(gè)是用戶登錄模型,核心是驗(yàn)證,所以自然需要實(shí)現(xiàn)核心的驗(yàn)證功能,就像LoginForm模型提到的validatePassword一樣,實(shí)際的驗(yàn)證邏輯是在當(dāng)前的User模型完成的。一般來說,實(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、對用到控制器類做配置
/**
- {@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'],
],
],
];
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。