2013-06-04 45 views
0

對於Laravel 3來說還是很新的,並且解決了一些問題。基於Laravel角色的頁面訪問 - 試圖獲取非對象的屬性 - Auth :: user()

我試圖設置一個基於角色的頁面訪問。用戶現在可以登錄,並且部分工作得很好,但我想限制訪問基於用戶角色管理如等

所以,我創建了一個過濾器,編輯器的某些頁面如下:

Route::filter('check_roles', function() { 

$current_url = URI::current(); //get current url excluding the domain. 
$current_page = URI::segment(2); //get current page which is stored in the second uri segment. Just a refresher: //the first uri segment is the controller, the second is the method, 
//third and up are the parameters that you wish to pass in 

$access = 0; 
$counter = 1; 


//excluded pages are the pages we don't want to execute this filter 
//since they should always be accessible for a logged in user 
$excluded_pages = array(
    'base' => array('login', 'user/authenticate'), 
    1 => array('user/profile', 'dashboard','dashboard/index','articles','articles/index', 'articles/create', 'articles/preview', 'articles/edit', 'user/profile', 'user/logout'), 
    2 => array('articles/publish','user/create', 'user/edit'), 
    3 => array('user/delete') 
); 

if (!in_array($current_url, $excluded_pages['base'])) { //if current page is not an excluded pages 
    if(Auth::user()->level < 4) { 
    do { 
     if (in_array($current_url, $excluded_pages[$counter])) { 
      $access=1; 

     } 
     $counter++; 

    } while ($counter < $user_level AND $counter < 4); 

    if ($access == 0) { //if user doesn't have access to the page that he's trying to access 

     //redirect the user to the homepage 
     return Redirect::to('dashboard') 
      ->with('error', 'You don\'t have permission to access the following page: ' . $current_url); 
    } 
    } 
} 

這是基於教程中,我發現https://gist.github.com/anchetaWern/4223764

我的想法被這取決於哪一個「級別」的用戶對象我會過濾網頁等

但是我得到的用戶訪問級別錯誤「試圖獲得財產非對象的」這涉及到這樣的代碼:

if(Auth::user()->level < 4) { 

測試驗證::用戶() - >在視圖中水平證實了用戶登錄誰能指教爲什麼在此routes.php文件不工作。作爲過濾器?

謝謝

回答

1

問題解決了 - 我用的是不正確的語法在我的劇本,我意識到,一旦我張貼在這裏。

if($user_level = Auth::user()->level < 4) { 

應該是:

if(Auth::user()->level < 4) { 

過濾器現在工作。不過,我正在尋找改進方法,因爲不確定這是最有效的方法!

謝謝

相關問題