主要问题是用户在没有记住选项的情况下无法登录网站。我已经修改了登录功能来检查用户访问并重定向到他们的特定操作,但是如果用户在登录时没有选择记住我选项,它就不起作用了。这是我的登录操作代码..
if(isset($_POST['LoginForm']))
{
$model->attributes=$_POST['LoginForm'];
if($model->validate() && $model->login()){
$user_id = Yii::app()->user->id;
$record=Users::model()->findByPk($user_id);
if($record->masrole->name == 'Admin'){
$this->redirect(array('//users/admin'));}
if($record->masrole->name == 'Merchant'){ $this->redirect(array('//users/Description','user'=>$record->username));
}
}
}
这是我的配置文件。
'components'=>array(
'user'=>array(
'class' => 'WebUser',
'allowAutoLogin'=>true,
'autoRenewCookie' => true,
'identityCookie' => array('domain' => '.xxxxx.com'),
'loginUrl'=>'http://xxxxx.com/login',
),
'session' => array(
'class' => 'CDbHttpSession',
'cookieParams' => array('domain' => '.xxxxxx.com'),
'timeout' => 3600,
'connectionID' => 'db',
'sessionName' => 'session',
),
这是我的模型登录功能
public function login()
{
if($this->_identity===null)
{
$this->_identity=new UserIdentity($this->email,$this->password);
$this->_identity->authenticate();
}
if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
{
$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
Yii::app()->user->login($this->_identity,$duration);
return true;
}
else{
return false;
}
}
这是 WebUser.php
class WebUser extends CWebUser {
private $_model;
function isAdmin(){
$user = $this->loadUser(Yii::app()->user->id);
return intval($user->mas_role_id) == 1;
}
function isMerchant(){
$user = $this->loadUser(Yii::app()->user->id);
return intval($user->mas_role_id) == 2;
}
}
// Load user model.
protected function loadUser($id=null)
{
if($this->_model===null)
{
if($id!==null)
$this->_model=Users::model()->findByPk($id);
}
return $this->_model;
}
}
最后这是用户身份
class UserIdentity extends CUserIdentity
{
private $_id;
public function authenticate()
{
$ctiteria = new CDbCriteria;
$ctiteria->condition = "email = '".$this->username."' OR username = '".$this->username."'";
$record=Users::model()->find($ctiteria);
if($record===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if ($record->status != 1)
$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->email);
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getId()
{
return $this->_id;
}
}
function isAdmin(){
$user = $this->loadUser(Yii::app()->user->id);
return intval($user->mas_role_id) == 1;
}
function isMerchant(){
$user = $this->loadUser(Yii::app()->user->id);
return intval($user->mas_role_id) == 2;
}
protected function loadUser($id=null)
{
if($this->_model===null)
{
if($id!==null)
$this->_model=Users::model()->findByPk($id);
}
return $this->_model;
}
}