2016-09-05 9 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; 
    } 

} 

2番目のファイル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