2014-09-02 3 views
0

사용자가 속한 부서에만 액세스 할 수 있도록 경로를 보호하려면 어떻게해야합니까? 내 현재 필터 :필터를 사용하여 laravel의 경로 보호

 

    Route::filter('department', function ($route, $request) { 
     // Check to see if the current user belongs to the department: 
     if (!Request::isMethod('post')) 
     { 
     if($request->segment(2) != "create") 
     { 
      if (!Auth::user()->canAccessDepartment($request->segment(2))) { 
       // The user shouldn't be allowed to access the department! Redirect them 
       return Redirect::to('/')->with('notice', 'Error!');; 
      } 
     } 
    } 
    }); 

는 그리고이

public function canAccessDepartment($department_id) { 
     $user = Confide::user(); 

     if ($user->departments()->where('department_id', $department_id)->count() < 1) 
     { 
      return false; 
     } 
     else{ return true; } 
    } 

답변

0

내가이 데이터베이스/모델 수준에서 수행해야한다고 생각하는 사용자 모델에 나의 방법이다. 비교해야 할 데이터가 데이터베이스에 있기 때문에 데이터베이스 수준에서이 트랜잭션을 수행하는 것이 좋습니다.

1

코드에서 필터는 모든 경로에 적용된 다음 일치하는 메소드/액션이 있는지 확인합니다. 필자가 선호하는 것은 필요할 때만 필터를 적용하는 것입니다. 그래서

[경고 - 테스트되지 않은 코드는 다음]

Route::resource('department', 'DepartmentController', 
       array('except' => array('create','store', 'update', 'destroy'))); 

Route::resource('department','DepartmentController',array('only'=>array('create','store', 'update', 'destroy'),'before'=>'departmentFilter')); 


Route::filter('department', function ($route, $request) { 
    // should this be Confide::user() ? 
    if (!Auth::user()->canAccessDepartment($request->segment(2))) { 
     // The user shouldn't be allowed to access the department! Redirect them 
     return Redirect::to('/')->with('notice', 'Error!'); 
    } 
}); 
내가 액세스 할 때이 작동
+0

특정 부서 도메인/부서/2,하지만 난 도메인/부서 (모든 부서를 나열 것이다) 내가 얻을에 액세스하려고하면 "/"로 리디렉션되는 "error" – SuperManSL

+0

도메인/부서에 액세스하면 컨트롤러에서 index() 함수를 호출 할 것입니다. 거기에있는 코드는 어떻게 생겼습니까? –

+0

만 해당 \t \t 돌아 가기보기 :: make ('department.index'); – SuperManSL

관련 문제