2014-07-15 2 views

답변

1

Yii는 액션에 대한 액세스 제어를 제공합니다. 여기에 예가 나와 있습니다. 게스트 사용자가 이러한 작업에 액세스하려고하면 로그인 페이지로 리디렉션됩니다.

class ExampleController extends Controller { 
     /** 
     * @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 these actions 
        'actions' => array('create','delete'), // add the actions need authuentication 
        'users' => array('@'), 
       ),     
      ); 
     }  
    } 
+0

이것이 ExampleController에서 작동한다고 생각합니다. 나는 사용자가 임의의 URL을 입력하면 로그인 페이지로 리디렉션해야합니다. 승인 된 사용자 만 페이지에 액세스하거나 모든 작업을 수행 할 수 있습니다. – user2474272

+0

ok. 모든 컨트롤러는 // Controller를 상속받습니다 // 여기서이 두 함수를 작성하십시오. -이 내가 이미 시도 –

+0

을 작동하지만이 (> 사용자 -> isGuest() YII : 응용 프로그램)을 ($ 조치) \t { \t \t 경우이 코드를 \t 공공 기능 beforeAction를 사용 – user2474272

0

모든 컨트롤러에이 기능을 추가하십시오.

public function beforeAction($action) { 

if (Yii::app()->user->isGuest && Yii::app()->controller->action->id != "login") { 
    Yii::app()->user->loginRequired(); 
} 
//something code right here if user valid 
return true; 
} 

사이트 컨트롤러에만 Yii::app()->controller->action->id != "login"이 필요합니다. 다른 컨트롤러에서 제거하십시오.

관련 문제