简单的用户登录表单 - yii框架

时间:2014-01-15 11:12:17

标签: php yii widget yii-extensions yii-components

我正在尝试在我的网站上打开登录表单。我已经编写了代码,但有些代码无法正常工作。

问题是登录表单不会返回任何错误或消息,只会将我重定向到登录页面。

此外,由于某种原因,checklogin功能无效。

controller / main:

public function actionLogin()
  {
  $model = new LoginForm;
  $this->render('login',array('model'=>$model));
  }

模型/ LoginForm的:

    class LoginForm extends CFormModel
{
  public $email;
  public $password;

  private $_identity;

  public function rules()
  {
    return array(
      array('email, password', 'required', 'message' => 'error'),
      array('email', 'email', 'allowEmpty' => false, 'checkMX' => true, 'message' => 'error'),
      array('password', 'authenticate')
    );
  }

  public function authenticate($attribute,$params)
  {
    $this->_identity = Account::model()->checkLogin($this->email, $this->password);
    if(!$this->_identity)
            $this->addError('password', 'error');
  }

}

模特/帐号:

    public static function model()
  {
    return parent::model(__CLASS__);
  }

  public function tableName()
  {
    return 'table';
  }

  public function primaryKey()
  {
    return 'id';
  }

  public function checkLogin($email, md5($password))
  {
    $user = $this->findByAttributes(array('email' => $email, 'password' => $password));
    if($user===null)
    {
      return false;
    }

    return false;

views / main / login:

<?php $form=$this->beginWidget('CActiveForm', array('action' => Yii::app()->createUrl('login'))); ?>
    <table>
      <tr><?php echo $form->errorSummary($model); ?></tr>
      <tr> <?php echo $form->emailField($model,'email'); ?></tr>
      <tr><?php echo $form->passwordField($model,'password'); ?></tr>
      <tr><?php echo CHtml::submitButton('Login'); ?></tr>
    </table>
    <?php $this->endWidget(); ?>

2 个答案:

答案 0 :(得分:2)

要实施身份验证,您必须按照以下步骤操作:

首先采取行动:

public function actionLogin() {
    $model = new LoginForm();
    if (isset($_POST['LoginForm'])) {
        if (CActiveForm::validate($model) && $model->validate() && $model->login()) {
            // Authentication DONE
        } else {
            //TRY TO GET ERRORS
        }
    }
}

在你的模型中添加登录功能:

public function login() {
    /*
     * if identity property had no value, here we initialize 
     * identity property
     */
    if ($this->identity === null) {
        $this->identity = new UserIdentity($this->username, $this->password);
        //authenticating
        $this->identity->authenticate();
    } else {
        /*
         * if error code was NONE, it means user has been successfully
         * authenticated.
         */
        if ($this->identity->errorCode === UserIdentity::ERROR_NONE) {
            Yii::app()->user->login($this->identity);
            return true;
        }
    }
}

并在您的模型的身份验证方法中:

public function authenticate() {
    //if validation was done and we had no error while validating
    if (!$this->hasErrors()) {
        //new instance of identity class
        $this->identity = new UserIdentity($this->username, $this->password);
        if (!$this->identity->authenticate()) {
            $this->addError('password', Yii::t('app', 'Invalid Username or Password'));
        }
    }
}

然后你需要添加UserIdentity Class(把这个类放在你的组件目录中)

class UserIdentity extends CUserIdentity { 

private $_id;
private $_username;

public function authenticate() {
    $record = Account::model()->findByAttributes(array(
        'username' => $this->username
    ));

    if ($record === null) {
        //adds error to user
        $this->errorCode = self::ERROR_USERNAME_INVALID;
        //authentication failed
        return false;
    } else if (!CPasswordHelper::verifyPassword($this->password, $record->password)) {
        $this->errorCode = self::ERROR_PASSWORD_INVALID;
        return false;
    } else {
        /*
         * no error
         * user information[username and password are valid]
         */
        $this->errorCode = self::ERROR_NONE;

        //user's id whitch will be accessible through Yii::app()->user->id
        $this->_id = $record->id;
        //user's username whitch will be accessible through Yii::app()->user->name
        $this->_username = $record->username;
        //success
        return true;
    }
}

/**
 * Overriding CUserIdentity's getId() method
 * @access public
 * @return integer user id
 */
public function getId() {
    return $this->_id;
}

/**
 * Overriding CUserIdentity's getName() method
 * @access public
 * @return string username
 */
public function getName() {
    return $this->_username;
}

答案 1 :(得分:0)

更改checklogin函数,如下所示,然后再次尝试解决此问题。

public function checkLogin($email, md5($password))
  {
    $user = $this->model()->findByAttributes(array('email' => $email, 'password' => $password));
    if($user===null)
    {
      return false;
    }

    return false;
  }

如果您尝试单独实现登录功能,那么您将缺少使用依赖于CUserIdentity类的Yii::app()->login()来注册用户的身份验证详细信息的整个逻辑。

掌握此链接 - &gt; http://www.yiiframework.com/doc/guide/1.1/en/topics.auth并继续进行后验证。