2013-11-14 5 views
3

데이터베이스에 문의하지 않고 yii에서 지정된 사용자 이름으로 사용자를 인증하도록 강제 설정해야합니까?Yii : 강제 인증

내 앱에서 로그인 할 때 api을 사용하고 api이 기록되지 않을 때까지 app을 사용할 수 없습니다. User::model()

public function authenticate() 
{ 
    if (strpos($this->username,"@")) { 
     $user=User::model()->findByAttributes(array('email'=>$this->username)); 
    } else { 
     $user=User::model()->findByAttributes(array('username'=>$this->username)); 
    } 
    if($user===null) 
     if (strpos($this->username,"@")) { 
      $this->errorCode=self::ERROR_EMAIL_INVALID; 
     } else { 
      $this->errorCode=self::ERROR_USERNAME_INVALID; 
     } 
    else if(Yii::app()->getModule('user')->encrypting($this->password)!==$user->password) 
     $this->errorCode=self::ERROR_PASSWORD_INVALID; 
    else if($user->active==0&&Yii::app()->getModule('user')->loginNotActiv==false) 
     $this->errorCode=self::ERROR_STATUS_NOTACTIV; 
    else if($user->active==-1) 
     $this->errorCode=self::ERROR_STATUS_BAN; 
    else { 
     $this->_id=$user->id; 
     $this->errorCode=self::ERROR_NONE; 
     $user->saveState($this); 
    } 
    return !$this->errorCode; 
} 

답변

3

다음 코드는 yiic가 생성하는 기본 UserIdentity.php 표시를 사용하려고 할 때이 api

Beacause은 우리가, 그래서 인증 프로세스 균열을 user 모델이 없습니다. 그것은 당신이 원하는 하드 코딩 된 사용자와 암호를 사용합니다.

<?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 
{ 
    /** 
    * Authenticates a user. 
    * The example implementation makes sure if the username and password 
    * are both 'demo'. 
    * In practical applications, this should be changed to authenticate 
    * against some persistent user identity storage (e.g. database). 
    * @return boolean whether authentication succeeds. 
    */ 
    public function authenticate() 
    { 
     $users=array(
      // username => password 
      'demo'=>'demo', 
      'admin'=>'admin', 
     ); 
     if(!isset($users[$this->username])) 
      $this->errorCode=self::ERROR_USERNAME_INVALID; 
     elseif($users[$this->username]!==$this->password) 
      $this->errorCode=self::ERROR_PASSWORD_INVALID; 
     else 
      $this->errorCode=self::ERROR_NONE; 
     return !$this->errorCode; 
    } 
}