2012-07-31 124 views
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; 
} 
} 

我们遇到的问题是,当我们在主页上使用有效的证书进行登录时,会加载一个新的登录表单,而不会指示用户已被认证。我们也看到语法错误,说明可变$模型在登录表单中是未定义的。

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

我该如何纠正上述问题?

回答

2

首先,我注意到几件事情,你通过$model作为类型UserObject而不是LoginForm Object。首先改变这一点。其次,因为user被作为model(不知道什么是您的用户模型)

isset($_POST['LoginForm'])将始终false又一次

会转到登录表单