在yii中新增一個(gè)用戶驗(yàn)證的方法詳解
更新時(shí)間:2013年06月20日 10:47:06 作者:
本篇文章是對(duì)在yii中新增一個(gè)用戶驗(yàn)證的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
1.為什么要新增一個(gè)用戶驗(yàn)證:
因?yàn)槲乙獙⒕W(wǎng)站后臺(tái)和前臺(tái)做在同一個(gè)yii的應(yīng)用中.但是前臺(tái)也包含有會(huì)員的管理中心.而這兩個(gè)用戶驗(yàn)證是完全不同的,所以需要兩個(gè)不同登陸頁(yè)面,要將用戶信息保存在不同的cookie或session中.所以需要在一個(gè)應(yīng)用中增加一個(gè)用戶驗(yàn)證
2.yii的用戶驗(yàn)證:
在自定義用戶驗(yàn)證前,我們首先要弄清楚yii的驗(yàn)證和授權(quán)方式.
為了驗(yàn)證一個(gè)用戶,我們需要定義一個(gè)有驗(yàn)證邏輯的驗(yàn)證類.在yii中這個(gè)類需要實(shí)現(xiàn)IUserIdentity接口,不同的類就可以實(shí)現(xiàn)不同的驗(yàn)證方 法.網(wǎng)站登陸一般需要驗(yàn)證的就是用戶名和密碼,yii提供了CUserIdentity類,這個(gè)類一般用于驗(yàn)證用戶名和密碼的類.繼承后我們需要重寫(xiě)其中 的authenticate()方法來(lái)實(shí)現(xiàn)我們自己的驗(yàn)證方法.具體代碼如下:
Php代碼
class UserIdentity extends CUserIdentity
{
private $_id;
public function authenticate()
{
$record=User::model()->findByAttributes(array('username'=>$this->username));
if($record===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($record->password!==md5($this->password))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
$this->_id=$record->id;
$this->setState('title', $record->title);
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getId()
{
return $this->_id;
}
}
在用戶登陸時(shí)則調(diào)用如下代碼:
Php代碼
// 使用提供的用戶名和密碼登錄用戶
$identity=new UserIdentity($username,$password);
if($identity->authenticate())
Yii::app()->user->login($identity);
else
echo $identity->errorMessage;
用戶退出時(shí),則調(diào)用如下代碼:
Php代碼
// 注銷當(dāng)前用戶
Yii::app()->user->logout();
其中的user是yii的一個(gè)components.需要在protected/config/main.php中定義
Php代碼
'user'=>array(
// enable cookie-based authentication
'allowAutoLogin'=>true,
'loginUrl' => array('site/login'),
),
這里我們沒(méi)有指定user的類名.因?yàn)樵趛ii中默認(rèn)user為CWebUser類的實(shí)例.
我 們現(xiàn)在已經(jīng)實(shí)現(xiàn)了用戶的登陸驗(yàn)證和退出.但是現(xiàn)在無(wú)論是否登陸,用戶都能訪問(wèn)所有的action,所以下一步我們要對(duì)用戶訪問(wèn)進(jìn)行授權(quán).在yii里是通過(guò) Access Control Filter即訪問(wèn)控制過(guò)濾器來(lái)實(shí)現(xiàn)用戶授權(quán)的.我們看一下一個(gè)簡(jiǎn)單的帶有訪問(wèn)控制的Controller:
Php代碼
class AdminDefaultController extends CController
{
public function filters()
{
return array('accessControl');
}
public function accessRules()
{
return array(
array(
'allow',
'users' => array('@'),
),
array(
'deny',
'users' => array('*')
),
);
}
}
我們?cè)趂ilters方法中設(shè)置具體的filter.我們可以看到在filters方法返回的array里有accessControl參數(shù),在CController類中有一個(gè)filterAccessControl方法:
Php代碼
public function filterAccessControl($filterChain)
{
$filter=new CAccessControlFilter;
$filter->setRules($this->accessRules());
$filter->filter($filterChain);
}
在里面新建了一個(gè)CAccessControlFilter實(shí)例,并且在setRules時(shí)傳入了accessRules()方法返回的參數(shù).
$filter->filter($filterChain)則是繼續(xù)調(diào)用其它filter.
而所有具體的授權(quán)規(guī)則則是定義在accessRules中:
Php代碼
public function accessRules()
{
return array(
array('deny',
'actions'=>array('create', 'edit'),
'users'=>array('?'),
),
array('allow',
'actions'=>array('delete'),
'roles'=>array('admin'),
),
array('deny',
'actions'=>array('delete'),
'users'=>array('*'),
),
);
}
具體規(guī)則參見(jiàn)yii的手冊(cè).
3.新增一個(gè)驗(yàn)證體系:
首先我們從CWebUser繼承一個(gè)CAdminUser:
Php代碼
class CAdminWebUser extends CWebUser
{
public $loginUrl = array('admin/admin/login');
}
我們需要把他放置到components中
如果是全局應(yīng)用則通過(guò)protected/config/main.php的components小節(jié):
Php代碼
'user'=>array(
// enable cookie-based authentication
'class' => 'CAdminUser',
'allowAutoLogin'=>true,
'loginUrl' => array('site/login'),
),
如果是在modules中則在模塊類的init方法中添加如下代碼:
Php代碼
$this->setComponents(array(
'adminUser' => array(
'class' => 'CAdminWebUser',
'allowAutoLogin' => false,
)
));
最后調(diào)用方式
Php代碼
//全局應(yīng)用
Yii::app()->getComponent('adminUser');
//在模塊中
Yii::app()->controller->module->getComponent('adminUser');
但僅僅這樣還不夠,我們還需要修改Controller的filter,我們需要自定義一個(gè)filter,來(lái)實(shí)現(xiàn)另一個(gè)用戶的驗(yàn)證和授權(quán)
第一步自定義一個(gè)filter:
Php代碼
class CAdminAccessControlFilter extends CAccessControlFilter
{
protected function preFilter($filterChain)
{
$app=Yii::app();
$request=$app->getRequest();
$user = Yii::app()->controller->module->getComponent('adminUser');
$verb=$request->getRequestType();
$ip=$request->getUserHostAddress();
foreach($this->getRules() as $rule)
{
if(($allow=$rule->isUserAllowed($user,$filterChain->controller,$filterChain->action,$ip,$verb))>0) // allowed
break;
else if($allow<0) // denied
{
$this->accessDenied($user);
return false;
}
}
return true;
}
}
再重寫(xiě)CController類的filterAccessController方法
Php代碼
public function filterAccessControl($filterChain)
{
$filter = new CAdminAccessControlFilter();
$filter->setRules($this->accessRules());
$filter->filter($filterChain);
}
//在這里我們使用自定義的filter類替換了原來(lái)的filter
OK,到這里我們就可以在此Controller的accessRules()中指定adminUser的授權(quán)了
因?yàn)槲乙獙⒕W(wǎng)站后臺(tái)和前臺(tái)做在同一個(gè)yii的應(yīng)用中.但是前臺(tái)也包含有會(huì)員的管理中心.而這兩個(gè)用戶驗(yàn)證是完全不同的,所以需要兩個(gè)不同登陸頁(yè)面,要將用戶信息保存在不同的cookie或session中.所以需要在一個(gè)應(yīng)用中增加一個(gè)用戶驗(yàn)證
2.yii的用戶驗(yàn)證:
在自定義用戶驗(yàn)證前,我們首先要弄清楚yii的驗(yàn)證和授權(quán)方式.
為了驗(yàn)證一個(gè)用戶,我們需要定義一個(gè)有驗(yàn)證邏輯的驗(yàn)證類.在yii中這個(gè)類需要實(shí)現(xiàn)IUserIdentity接口,不同的類就可以實(shí)現(xiàn)不同的驗(yàn)證方 法.網(wǎng)站登陸一般需要驗(yàn)證的就是用戶名和密碼,yii提供了CUserIdentity類,這個(gè)類一般用于驗(yàn)證用戶名和密碼的類.繼承后我們需要重寫(xiě)其中 的authenticate()方法來(lái)實(shí)現(xiàn)我們自己的驗(yàn)證方法.具體代碼如下:
Php代碼
復(fù)制代碼 代碼如下:
class UserIdentity extends CUserIdentity
{
private $_id;
public function authenticate()
{
$record=User::model()->findByAttributes(array('username'=>$this->username));
if($record===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($record->password!==md5($this->password))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
$this->_id=$record->id;
$this->setState('title', $record->title);
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getId()
{
return $this->_id;
}
}
在用戶登陸時(shí)則調(diào)用如下代碼:
Php代碼
復(fù)制代碼 代碼如下:
// 使用提供的用戶名和密碼登錄用戶
$identity=new UserIdentity($username,$password);
if($identity->authenticate())
Yii::app()->user->login($identity);
else
echo $identity->errorMessage;
用戶退出時(shí),則調(diào)用如下代碼:
Php代碼
復(fù)制代碼 代碼如下:
// 注銷當(dāng)前用戶
Yii::app()->user->logout();
其中的user是yii的一個(gè)components.需要在protected/config/main.php中定義
Php代碼
復(fù)制代碼 代碼如下:
'user'=>array(
// enable cookie-based authentication
'allowAutoLogin'=>true,
'loginUrl' => array('site/login'),
),
這里我們沒(méi)有指定user的類名.因?yàn)樵趛ii中默認(rèn)user為CWebUser類的實(shí)例.
我 們現(xiàn)在已經(jīng)實(shí)現(xiàn)了用戶的登陸驗(yàn)證和退出.但是現(xiàn)在無(wú)論是否登陸,用戶都能訪問(wèn)所有的action,所以下一步我們要對(duì)用戶訪問(wèn)進(jìn)行授權(quán).在yii里是通過(guò) Access Control Filter即訪問(wèn)控制過(guò)濾器來(lái)實(shí)現(xiàn)用戶授權(quán)的.我們看一下一個(gè)簡(jiǎn)單的帶有訪問(wèn)控制的Controller:
Php代碼
復(fù)制代碼 代碼如下:
class AdminDefaultController extends CController
{
public function filters()
{
return array('accessControl');
}
public function accessRules()
{
return array(
array(
'allow',
'users' => array('@'),
),
array(
'deny',
'users' => array('*')
),
);
}
}
我們?cè)趂ilters方法中設(shè)置具體的filter.我們可以看到在filters方法返回的array里有accessControl參數(shù),在CController類中有一個(gè)filterAccessControl方法:
Php代碼
復(fù)制代碼 代碼如下:
public function filterAccessControl($filterChain)
{
$filter=new CAccessControlFilter;
$filter->setRules($this->accessRules());
$filter->filter($filterChain);
}
在里面新建了一個(gè)CAccessControlFilter實(shí)例,并且在setRules時(shí)傳入了accessRules()方法返回的參數(shù).
$filter->filter($filterChain)則是繼續(xù)調(diào)用其它filter.
而所有具體的授權(quán)規(guī)則則是定義在accessRules中:
Php代碼
復(fù)制代碼 代碼如下:
public function accessRules()
{
return array(
array('deny',
'actions'=>array('create', 'edit'),
'users'=>array('?'),
),
array('allow',
'actions'=>array('delete'),
'roles'=>array('admin'),
),
array('deny',
'actions'=>array('delete'),
'users'=>array('*'),
),
);
}
具體規(guī)則參見(jiàn)yii的手冊(cè).
3.新增一個(gè)驗(yàn)證體系:
首先我們從CWebUser繼承一個(gè)CAdminUser:
Php代碼
復(fù)制代碼 代碼如下:
class CAdminWebUser extends CWebUser
{
public $loginUrl = array('admin/admin/login');
}
我們需要把他放置到components中
如果是全局應(yīng)用則通過(guò)protected/config/main.php的components小節(jié):
Php代碼
復(fù)制代碼 代碼如下:
'user'=>array(
// enable cookie-based authentication
'class' => 'CAdminUser',
'allowAutoLogin'=>true,
'loginUrl' => array('site/login'),
),
如果是在modules中則在模塊類的init方法中添加如下代碼:
Php代碼
復(fù)制代碼 代碼如下:
$this->setComponents(array(
'adminUser' => array(
'class' => 'CAdminWebUser',
'allowAutoLogin' => false,
)
));
最后調(diào)用方式
Php代碼
復(fù)制代碼 代碼如下:
//全局應(yīng)用
Yii::app()->getComponent('adminUser');
//在模塊中
Yii::app()->controller->module->getComponent('adminUser');
但僅僅這樣還不夠,我們還需要修改Controller的filter,我們需要自定義一個(gè)filter,來(lái)實(shí)現(xiàn)另一個(gè)用戶的驗(yàn)證和授權(quán)
第一步自定義一個(gè)filter:
Php代碼
復(fù)制代碼 代碼如下:
class CAdminAccessControlFilter extends CAccessControlFilter
{
protected function preFilter($filterChain)
{
$app=Yii::app();
$request=$app->getRequest();
$user = Yii::app()->controller->module->getComponent('adminUser');
$verb=$request->getRequestType();
$ip=$request->getUserHostAddress();
foreach($this->getRules() as $rule)
{
if(($allow=$rule->isUserAllowed($user,$filterChain->controller,$filterChain->action,$ip,$verb))>0) // allowed
break;
else if($allow<0) // denied
{
$this->accessDenied($user);
return false;
}
}
return true;
}
}
再重寫(xiě)CController類的filterAccessController方法
Php代碼
復(fù)制代碼 代碼如下:
public function filterAccessControl($filterChain)
{
$filter = new CAdminAccessControlFilter();
$filter->setRules($this->accessRules());
$filter->filter($filterChain);
}
//在這里我們使用自定義的filter類替換了原來(lái)的filter
OK,到這里我們就可以在此Controller的accessRules()中指定adminUser的授權(quán)了
您可能感興趣的文章:
- Yii基于CActiveForm的Ajax數(shù)據(jù)驗(yàn)證用法示例
- Yii框架數(shù)據(jù)模型的驗(yàn)證規(guī)則rules()被執(zhí)行的方法
- Yii數(shù)據(jù)模型中rules類驗(yàn)證器用法分析
- yii2中的rules 自定義驗(yàn)證規(guī)則詳解
- PHP YII框架開(kāi)發(fā)小技巧之模型(models)中rules自定義驗(yàn)證規(guī)則
- Yii使用Captcha驗(yàn)證碼的方法
- yii2 modal彈窗之ActiveForm ajax表單異步驗(yàn)證
- PHP Yii框架之表單驗(yàn)證規(guī)則大全
- Yii2驗(yàn)證器(Validator)用法分析
- 詳解Yii2 rules 的驗(yàn)證規(guī)則
- Yii2框架數(shù)據(jù)驗(yàn)證操作實(shí)例詳解
相關(guān)文章
用PHP調(diào)用Oracle存儲(chǔ)過(guò)程的方法
php程序訪問(wèn)數(shù)據(jù)庫(kù),完全可以使用存儲(chǔ)過(guò)程,有人認(rèn)為使用存儲(chǔ)過(guò)程便于維護(hù)。不過(guò)仁者見(jiàn)仁,智者見(jiàn)智,在這個(gè)問(wèn)題上,偶認(rèn)為使用存儲(chǔ)過(guò)程意味著必須要dba和開(kāi)發(fā)人員更緊密配合,如果其中一方更變,則顯然難以維護(hù)。2008-09-09php對(duì)字符串中的特殊符號(hào)進(jìn)行過(guò)濾的方法
有時(shí)候我們會(huì)遇到過(guò)濾字符串中特殊字符的問(wèn)題,本文提供了三個(gè)處理特殊字符串的方法,文中通過(guò)代碼示例介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2023-11-11解析php擴(kuò)展php_curl.dll不加載的解決方法
本篇文章是對(duì)php擴(kuò)展php_curl.dll不加載的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06讓PHP以ROOT權(quán)限執(zhí)行系統(tǒng)命令的方法
這種問(wèn)題我想大家可能都遇到過(guò),網(wǎng)友提供的解決方法也很多。我也只是結(jié)合自己系統(tǒng)的需求并結(jié)合網(wǎng)友的解決方案來(lái)總結(jié)的一種方法2011-02-02PHP5中虛函數(shù)的實(shí)現(xiàn)方法分享
學(xué)過(guò)C++的人都應(yīng)該知道C++中有個(gè)虛函數(shù)的概念。而在php5中如何實(shí)現(xiàn)這個(gè)虛函數(shù)呢?2011-04-04CodeIgniter php mvc框架 中國(guó)網(wǎng)站
CodeIgniter 是一個(gè)小巧但功能強(qiáng)大的 PHP 框架,作為一個(gè)簡(jiǎn)單而“優(yōu)雅”的工具包,它可以為 PHP 程序員建立功能完善的 Web 應(yīng)用程序。如果你是一個(gè)使用共享主機(jī),并且為客戶所要求的期限而煩惱的開(kāi)發(fā)人員,如果你已經(jīng)厭倦了那些傻大笨粗的框架2008-05-05Sorting Array Values in PHP(數(shù)組排序)
有時(shí)候,你可能需要對(duì)數(shù)組內(nèi)的值進(jìn)行排序,那么就可以參考下面的文章。2011-09-09