成功登录后Yii :: app() - > user-> id给出空结果和" UserIdentity.email"未定义

时间:2015-05-25 03:16:48

标签: yii login yii-components

成功登录后的Yii Yii :: app() - > user-> id给出空结果和Property" UserIdentity.email"当我在UserIdentity身份验证功能中打印$ this->电子邮件时未定义消息,但是通过登录表单发送电子邮件在$ this->用户名中可用 以下是我的控制器功能

public function actionLogin() {
    $model = new LoginForm();
    if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
        echo CActiveForm::validate($model);
        Yii::app()->end();
    }
    if (isset($_POST['LoginForm'])) {
        $model->attributes = $_POST['LoginForm'];
        if ($model->validate() && $model->login()) {
            echo 'success';
            exit;
        } else {
            echo 'failure';
            exit;
        }
    }
    $this->render('login', array('model' => $model));
}

这是我的查看文件

 <?php
            $form = $this->beginWidget('CActiveForm', array(
                'id' => 'login-form',
                'enableAjaxValidation' => true,
                'enableClientValidation' => true,
                'clientOptions' => array(
                    'validateOnSubmit' => true,
                ),
                'htmlOptions' => array(
                    'id' => 'form-contact'
                ),
            ));
            echo CHtml::errorSummary($model);
            ?>

            <div id="form-left">
                <label for="text-email">Email(<span style="color: red;">*</span>)</label><br />
                <?php echo $form->textField($model, 'email', array('class' => 'input')); ?><br />
                <label for="text-password">Password (<span style="color: red;">*</span>)</label><br />
                <?php echo $form->passwordField($model, 'password', array('class' => 'input')); ?><br />
                <br>
                <?php echo CHtml::submitButton('Login', array('class' => 'button', 'style' => 'margin-bottom:50px;')); ?>
            </div>
            <?php $this->endWidget(); ?>

以下是登录表单代码

<?php

class LoginForm extends CFormModel {

public $email;
public $password;
private $_identity;

public function rules() {
    return array(
        array('password, email', 'required'),
        array('password', 'authenticate'),
    );
}

public function attributeLabels() {
    return array(
        'email' => Yii::t('labels', 'e-mail'),
        'password' => Yii::t('labels', 'Password'),
    );
}



public function authenticate($attribute,$params)
{
    if(!$this->hasErrors())  // we only want to authenticate when no input errors
    {
        $identity=new UserIdentity($this->email,$this->password);
        $identity->authenticate();
        switch($identity->errorCode)
        {
            case UserIdentity::ERROR_NONE:
                Yii::app()->user->login($identity);
                break;
            case UserIdentity::ERROR_USERNAME_INVALID:
                $this->addError('email','Email address is incorrect.');
                break;
            default: // UserIdentity::ERROR_PASSWORD_INVALID
                $this->addError('password','Password is incorrect.');
                break;
        }
    }
}

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)
        return true;
    else
        return false;
}
}

此处是我的用户身份类

<?php

class UserIdentity extends CUserIdentity {

private $_id;

public function authenticate() {
    $user = User::model()->findByAttributes(array(
        'email' => $this->username,
    ));
    if ($user === null) { // No user found!
        $this->errorCode = self::ERROR_USERNAME_INVALID;
    } else if ($user->password !== md5($this->password)) {
        $this->errorCode = self::ERROR_PASSWORD_INVALID;
    } else {
        $this->errorCode = self::ERROR_NONE;
        $this->setState('userid', $user->id);
        $this->_id = $user->id;
    }
    return !$this->errorCode;
}

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

}

1 个答案:

答案 0 :(得分:0)

问题是您的userIdentity类中的$this->setState('userid', $user->id)。这应该转换为$this->setState('id', $user->id)。要访问用户电子邮件,您需要添加$this->setState('email', $user->username)。所以你的UserIdentity类应该是这样的:

class UserIdentity extends CUserIdentity {

    private $_id;

    public function authenticate() {
         $user = User::model()->findByAttributes(array(
         'email' => $this->username,
         ));
         if ($user === null) { // No user found!
            $this->errorCode = self::ERROR_USERNAME_INVALID;
         } 
         else if ($user->password !== md5($this->password)) {
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
         } 
         else {
            $this->errorCode = self::ERROR_NONE;
            $this->setState('id', $user->id);
            $this->setState('email', $user->username);
            $this->_id = $user->id;
         }
         return !$this->errorCode;
    }

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

}