- 设置控制器,登录页面方法,退出方法 admin/user/login, logout..
/**
*登录页面
*/
public function actionAdminLogin()
{
$model = new User;
// if it is ajax validation request
/* if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}*/
// collect user input data
if(isset($_POST['User']))
{
$model->attributes=$_POST['User'];
// validate user input and redirect to the previous page if valid
if($model->validate() && $model->Adminlogin())
$this->redirect(Yii::app()->user->returnUrl);
}
// display the login form
//$this->render('login',array('model'=>$model));
$this->render('login',array(
'model'=>$model,
));
}
/**
* 退出
*/
public function actionAdminLogout()
{
Yii::app()->user->logout();
$this->redirect(Yii::app()->homeUrl."?r=admin");
}
- 设置模块,设置密码验证,登录函数,
/**
*验证密码
*parm $passord 用户输入的密码
*return 匹配成为返回为TRUE,否则为FALSE
*/
public function validatapassword($password)
{
return $this->encrytp($password)===$this->password;
}
/**
* 加密
*/
private function encrytp($password,$pre="yiiapp"){
return md5($pre.$password);
}
public function beforeSave()
{
if(parent::beforeSave()){
if($this->isNewRecord){
$this->password = $this->encrytp($this->password);
}
return true;
}else{
return false;
}
}
public $username;
public $password;
public $rememberMe;
public $verifyCode;
private $_identity;
/**
**/登录方法
public function Adminlogin()
{
if($this->_identity===null)
{
$this->_identity=new AdminIdentity($this->username,$this->password);
$this->_identity->authenticate();
}
if($this->_identity->errorCode===AdminIdentity::ERROR_NONE)
{
$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
Yii::app()->user->login($this->_identity,$duration);//echo "fas";
return true;
}
else
return false;
}
- 配置密码验证模块。复制protected/components/UserIdentily.php->AdminIdentily.php
class AdminIdentity extends CUserIdentity
{
private $_id;
public function authenticate()
{
$username = strToLower($this->username);
$user = User::model()->find('LOWER(username)=?',array($username));
if($user===null){
$this->errorCode=self::ERROR_USERNAME_INVALID;
}elseif(!$user->validatapassword($this->password)){
$this->errorCode=self::ERROR_PASSWORD_INVALID;
}else{
$this->_id = $user->id;
$this->username = $user->username;
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getid()
{
return $this->_id;
}
public function getPersistentStates()
{
return 'admin';
}
}
- 修改布局文件链接
array('label'=>'登录', 'url'=>array('/admin/user/adminlogin'), 'visible'=>!Yii::app()->user->getstate('manage')==='admin'),
array('label'=>'退出 ('.Yii::app()->user->name.')', 'url'=>array('/admin/user/adminlogout'), 'visible'=>Yii::app()->user->getstate('manage')==='admin')
- module文件配置登录页面,登录跳转页面
class AdminIdentity extends CUserIdentity
{
private $_id;
public function authenticate()
{
$username = strToLower($this->username);
$user = User::model()->find('LOWER(username)=?',array($username));
if($user===null){
$this->errorCode=self::ERROR_USERNAME_INVALID;
}elseif(!$user->validatapassword($this->password)){
$this->errorCode=self::ERROR_PASSWORD_INVALID;
}else{
$this->_id = $user->id;
$this->username = $user->username;
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getid()
{
return $this->_id;
}
public function getPersistentStates()
{
return array('manage'=>'admin');
}
}