您好我是yii的新手,以下是我的UserIdentiy功能请让我知道如何添加记住我的功能
public function authenticate()
{
$users = array();
if ($this->usertype == "registration")
{
$users = Login::model()->findByAttributes(array('email' => $this->username));
$users = $users->attributes;
}
if (empty($users)) $this->errorCode = self::ERROR_USERNAME_INVALID;
elseif (!empty($users['password']) && $users['password'] !== md5($this->password))
$this->errorCode = self::ERROR_PASSWORD_INVALID;
elseif (!empty($users['status']) && $users['status'] !== 1)
$this->errorCode = self::STATUS_NOT_ACTIVE;
else
{
$this->_id = $users->id;
$this->errorCode = self::ERROR_NONE;
}
return !$this->errorCode;
}
答案 0 :(得分:3)
在protected\config\main.php
配置数组中,该数组中存在component
索引。在user
数组内部具有关联索引值'allowAutoLogin'
必须具有布尔值true
所以看起来应该是这样的
'components' => array(
'user' => array(
// enable cookie-based authentication
'allowAutoLogin' => true,
),
...
并且您必须使用以下属性以及下面给出的登录方法,您可以轻松地记住我。
class LoginForm extends CFormModel
{
public $username;
public $password;
public $rememberMe;
private $_identity;
登录方法在Login模型类中应该是这样的
public function login()
{
if($this->_identity===null)
{
$this->_identity=new UserIdentity($this->username, $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;
}
这是记住功能的核心代码
Yii::app()->user->login($this->_identity,$duration);
答案 1 :(得分:0)
这里
/ ** * LoginForm类。 * LoginForm是用于保留的数据结构 *用户登录表单数据。它由“ SiteController”的“登录”操作使用。 * / LoginFormUser类扩展了CFormModel {
public $Username;
public $password;
public $rememberMe;
private $_identity;
// public $verifyCode;
public $verifyCode;
/**
* Declares the validation rules.
* The rules state that Username and password are required,
* and password needs to be authenticated.
*/
public function rules() {
return array(
// Username and password are required
array('Username, password', 'required'),
// rememberMe needs to be a boolean
array('rememberMe', 'boolean'),
// password needs to be authenticated
array('password', 'authenticate', 'skipOnError' => true),
// array('verifyCode', 'CaptchaExtendedValidator', 'allowEmpty'=>!CCaptcha::checkRequirements()),
// array('verifyCode', 'required'),
// array('verifyCode', 'application.extensions.yiiReCaptcha.ReCaptchaValidator'),
);
}
/**
* Declares attribute labels.
*/
public function attributeLabels() {
return array(
'rememberMe' => 'Remember me next time',
'Username' => 'User name',
'password' => 'Password',
// 'verifyCode'=> 'verify Code',
);
}
/**
* Authenticates the password.
* This is the 'authenticate' validator as declared in rules().
*/
public function authenticate($attribute,$params)
{
if(!$this->hasErrors())
{
$this->_identity=new UserIdentity($this->Username,$this->password);
$dataAuthenticate = $this->_identity->authenticate();
if($dataAuthenticate == 1)
$this->addError('Username','username is invalid');
elseif($dataAuthenticate == 2)
$this->addError('password','password is invalid');
elseif($dataAuthenticate === 'lock')
$this->addError('Username', 'Your account has been locked for violating the policy');
elseif($dataAuthenticate == 3)
$this->addError('Username', 'Your account have been locked login in 15 minutes!');
}
}
/**
* Logs in the user using the given Username and password in the model.
* @return boolean whether login is successful
*/
public function login() {
if ($this->_identity === null) {
$this->_identity = new UserIdentity($this->Username, $this->password);
$this->_identity->authenticate();
}
if ($this->_identity->errorCode === UserIdentity::ERROR_NONE) {
$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
// if($this->rememberMe == true){
// $duration=3600*24*30; // 30 days
// }else {
// $duration = 0;
// }
Yii::app()->user->login($this->_identity,$duration);
// $get_cookie_first = Yii::app()->request->cookies['loginCookie']->value;
// $cookie = new CHttpCookie('loginCookie', $get_cookie_first);
// $cookie->expire = time() + $duration;
// Yii::app()->request->cookies['loginCookie'] = $cookie;
return true;
}
else
return false;
}
}