Yii还记得我的功能吗?

1cklez4t  于 2022-11-09  发布在  其他
关注(0)|答案(2)|浏览(119)

你好,我是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;
}
kd3sttzy

kd3sttzy1#

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模型类中,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);
z9ju0rcb

z9ju0rcb2#

给你
/**LoginForm类。 LoginForm是用于保存 * 用户登录窗体数据的数据结构。它由“SiteController”的“登录”操作使用。*/ class 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;
}

}

相关问题