yii 从数据库获取用户名、密码

修改 protected/components/UserIdentity.php

class UserIdentity extends CUserIdentity
{

private $_id;
public function authenticate()
{
$username=strtolower($this->username);//将用户输入的用户名变为小写(防止因大小写重名)
$user=User::model()->find('LOWER(username)=?',array($username))
//'LOWER(username)此处的username 为数据库user表里的字段
if($user==null){
$this->errorCode=self::ERROR_USERNAME_INVALID;
}else{
if(!$user->validatePassword($this->password)){
//validatePassword 为模型user类里的方法

$this->errorCode=self::ERROR_PASSWORD_INVALID;

}else{

$this->_id=$user->id;
$this->username=$user->username;
$this->errorCode=self::ERROR_NONE;

}
}
return $this->errorCode===self::ERROR_NONE;
}

public function getId(){
return $this->_id;
}
}

在模型user类中(models/user.php)添加如下

public function validatePassword($password){
return $this->encypt($password)===$this->password;
}

public function encypt($pass){
return md5($pass);
}

你可能感兴趣的:(yii 从数据库获取用户名、密码)