3

我有一个标准的登录表单,如下所示:

<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
    'validateOnSubmit'=>true,
),
)); ?>

<p class="note">Fields with <span class="required">*</span> are required.</p>

<div class="row">
    <?php echo $form->labelEx($model,'username'); ?>
    <?php echo $form->textField($model,'username'); ?>
    <?php echo $form->error($model,'username'); ?>  
</div>

<div class="row">
    <?php echo $form->labelEx($model,'password'); ?>
    <?php echo $form->passwordField($model,'password'); ?>
    <?php echo $form->error($model,'password'); ?>

</div>

<div class="row rememberMe">
    <?php echo $form->checkBox($model,'rememberMe'); ?>
    <?php echo $form->label($model,'rememberMe'); ?>
    <?php echo $form->error($model,'rememberMe'); ?>
</div>

<div class="row buttons">
    <?php echo CHtml::submitButton('Login'); ?>
</div>

<?php $this->endWidget(); ?>

我的 UserIdentity 类是:

public function authenticate()
    {
//                $username = $this->username;
//                $password = $this->password;

            $user = Users::model()->findbyAttributes(array($username=>$this->username));
            if($user === NULL){
                    $this->errorCode=self::ERROR_UNKNOWN_IDENTITY;


            }else if ($user->password !== md5($this->password)){    
//                        $this->username = $user->username;
//                       sess('SESS_USER_INFO', $user->attributes);
//                        $this->errorCode=self::ERROR_NONE;
        //invalid password
        $this->errorCode=self::ERROR_PASSWORD_INVALID;
            }
            else {
                $this->errorCode=self::ERROR_NONE;
            }
            return !$this->errorCode;
    }
}

我的 UserController.php actionLogin 如下:

public function actionLogin()
{
    $model= new Users();
//      Yii::app()->sessiion[username]= Yii::app()->model($username);
//      Yii:app()->session[password] = Yii::app()->model($password);
    // if it is ajax validation request
    if(isset($_POST['ajax']))
    {
        echo CActiveForm::validate($model);
        Yii::app()->end();
    }
//                print_r($_POST['Users']);
    // collect user input data
    $form =  new LoginForm;
//      if(isset($_POST['Users']))
//      {
//          $model->attributes=$_POST['Users'];     

        // validate user input and redirect to the previous page if valid
//          if($model->login())
//              $this->redirect(Yii::app()->user->getReturnUrl('index'));
//      }
    // display the login form
//      $this->render('login',array('model'=>$model));
    if(isset($_POST['LoginForm'])){
    $form->attributes=$_POST['LoginForm'];
    if($form->validate() && $form->login()) $this->redirect(Yii::app()->user->returnUrl);
    }
    $this->render('login',array('model'=>$model));
}

LoginForm 模型如下:

class LoginForm extends CFormModel
{
public $username;
public $password;
public $rememberMe;

private $_identity;

/**
 * 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'),
    );
}

/**
 * Declares attribute labels.
 */
public function attributeLabels()
{
    return array(
        'rememberMe'=>'Remember me next time',
    );
}

/**
 * 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);
//          if(!$this->_identity->authenticate())
//              $this->addError('password','Incorrect username or password.');
        $identity = new UserIdentity($this->username, $this->password);
        $identity->authenticate();
        switch($identity->errorCode)
        {
            case UserIdentity::ERROR_NONE:
                $duration=$this->rememberMe ? 3600*24*30 : 0;  //30 days
                Yii::app()->user->login($identity,$duration);
                break;
            case UserIdentity::ERROR_USERNAME_INVALID:
                $this->addError('username', 'Username is incorrect.');
                break;
            default; // UserIdentity:ERROR_USERNAME_INVALID
                $this->addError('password','Password is incorrect.');
                break;
        }           
    }
}

/**
 * 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
        Yii::app()->user->login($this->_identity,$duration);
        return true;
    }
    else
        return false;
}
}

我们遇到的问题是,当我们在主页上使用有效凭据登录时,会加载一个新的登录表单,而不会指示用户已通过身份验证。我们还看到语法错误,指出登录表单中的可用 $model 未定义。

更新:我们想使用 /views/users/login 并将其连接到我们的 UserController,而不是使用 /views/site/login。

我该如何纠正上述问题?

4

1 回答 1

2

首先,我注意到你传递的一些东西是$model类型User Object而不是LoginForm Object. 首先改变它。其次,因为user发送为model(不确定您的用户模型中有什么)

isset($_POST['LoginForm'])将永远false并且会再次登录表单

于 2012-07-31T19:27:42.717 回答