2013-02-09 2 views
0

YII 응용 프로그램의 모든 페이지에서 인증을 시행해야합니다. 이렇게하려면 내가 http://www.heirbaut.nl/2010/02/23/forcing-a-yii-application-to-authenticate/에서 가져온 다음 코드로 SiteController 클래스를 확장했습니다Yii 강제 인증이 홈페이지/정적 페이지에서만 작동하는 이유는 무엇입니까?

/** 
* @return array action filters 
*/ 
public function filters(){ 
    return array(
     'accessControl', // perform access control for CRUD operations 
    ); 
} 

/** 
* Specifies the access control rules. 
* This method is used by the 'accessControl' filter. 
* @return array access control rules 
*/ 
public function accessRules(){ 
    return array(
     array('allow', // allow all users to perform 'login' 
      'actions'=>array('login'), 
      'users'=>array('*'), 
     ), 
     array('allow', // allow authenticated user to perform any action 
      'users'=>array('@'), 
     ), 
     array('deny', // deny all users 
      'users'=>array('*'), 
     ), 
    ); 
} 

이 단지가의 index.php URL에 대한, 로그인 폼에 unauthenticaed 사용자에 대한 모든 요청을 리디렉션되어 있는지 않습니다. 그러나 index.php?r=person 그리고 결과적으로 응용 프로그램의 주 메뉴는이 제한을 무시하고 인증에 관계없이 표시됩니다.

+2

마 당신은'PersonController'를 가지고 있습니까? – topher

+0

예. 모든 단일 컨트롤러가 동일한 코드를 참조해야합니까? 당신이 말한다면 물론 상속받을 새로운 수업을 만들어야합니다. 하지만 그것은 많은 보일러 플레이트 코드처럼 보입니다. 이 문제를 Yii의 사슬보다 높게 수정할 수는 없나요? – aelgoa

답변

0

모든 컨트롤러는 해당 코드를 참조해야합니다. 옵션이 CController를 확장 자신의 컨트롤러를 만들고 MyController을 확장하고 추가 규칙을 추가 accessRules()를 재정의해야 컨트롤러 클래스에

class MyController extends CController{ 
    /** 
    * @return array action filters 
    */ 
    public function filters(){ 
     return array(
      'accessControl', // perform access control for CRUD operations 
     ); 
    } 

    /** 
    * Specifies the access control rules. 
    * This method is used by the 'accessControl' filter. 
    * @return array access control rules 
    */ 
    public function accessRules(){ 
     return array(
      array('allow', // allow authenticated user to perform any action 
       'users'=>array('@'), 
      ), 
      array('deny', // deny all users 
       'users'=>array('*'), 
      ), 
     ); 
    } 
} 

그런 다음 protected/components 폴더에 배치하는 것입니다

public class SiteController extends MyController{ 

    ... 

    public function accessRules(){ 
     $rules=parent::accessRules(); 
     array_unshift($rules,array(
      'allow', // allow all users to perform 'login' 
      'actions'=>array('login'), 
      'users'=>array('*'), 
     )); 
     return $rules; 
    } 

    ... 
} 
+0

감사합니다. 이것은 내 가정을 확인하고 잘 작동합니다! – aelgoa

관련 문제