2016-09-05 198 views
0

我遵循這篇文章中的步驟http://www.yiiframework.com/doc/guide/1.1/en/topics.auth如何在我的網站上創建登錄和註冊系統,但我不明白我應該在哪裏放這段代碼,在哪個文件中?Yii身份驗證和授權

$identity=new UserIdentity($username,$password); 
if($identity->authenticate()) 
    Yii::app()->user->login($identity); 
else 
    echo $identity->errorMessage; 
...... 
// Logout the current user 
Yii::app()->user->logout(); 
+0

如果你安裝了先進的模板,它配置了完整的登錄/註冊/密碼恢復系統。 –

+0

我創建了簡單的博客 – rick1

+0

@ rick1,你應該看看Yii2。 Fabrizio的評論是關於Yii2的高級模板。 – topher

回答

0

首先,你應該創建一個這樣的LoginForm。

<?php 

/** 
* LoginForm class. 
* LoginForm is the data structure for keeping 
* user login form data. It is used by the 'login' action of 'SiteController'. 
*/ 
class LoginForm extends CFormModel { 

    public $username; 
    public $password; 
    public $rememberMe; 
    public $qrcode; 
    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', 
      'rememberMe' => Yii::t('default', 'Remember me next time'), 
      'username' => Yii::t('default', 'Username'), 
      'password' => Yii::t('default', 'Password'), 
     ); 
    } 

    /** 
    * 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', Yii::t('default', 'Incorrect username or password')); 
     } 
    } 

    /** 
    * 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); 
      // Yii::app()->user->setState("password", $this->password); 
      //$_SESSION['password'] = $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; 
    } 

} 

第二個創建文件UserIdentity是這樣的。

<?php 

/**  * UserIdentity represents the data needed to identity a user. 
* * It contains the authentication method that checks if the provided 
* * data can identity the user. 
*/ 
class UserIdentity extends CUserIdentity { 

    private $_id; 
    public $user; 
    public $usertype; 

    public function authenticate() { 

      $user = User::model()->find('LOWER(username)=? or easiio_id=?', array(strtolower($this->username), $this->username)); 

     if ($user === null) { 
      $this->errorCode = self::ERROR_USERNAME_INVALID; 
     } else { 
      //date_default_timezone_set("America/Los_angeles"); 
      $this->_id = $user->id; 
      $this->usertype = $user->status; 
      $this->user = $user; 
      $this->username = $user->username; 
      $this->setState("user", $user); 
      $this->setState('username', $user->username); 
      $this->setState('password', $user->password); 
      $this->setState('org', $user->org_id); 

      $user->saveAttributes(array(
       'lastlogin' => date("Y-m-d H:i:s", time()), 
      )); 
      $this->errorCode = self::ERROR_NONE; 
     } 
     return $this->errorCode == self::ERROR_NONE; 
    } 

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

} 

第三次登錄

+0

默認情況下有用戶登錄表單和用戶身份,但我不能使用我創建的用戶名和密碼 – rick1

+0

登錄,您可以自己創建用戶身份和登錄表單,並檢查功能驗證。 – EricWang

+0

我在登錄表單和userIdentity中具有與您在此處編寫的代碼相同的代碼。當我登錄時我有空白屏幕 – rick1