2016-02-23 59 views
3

在我的應用程序中,我有兩種類型的用戶 - 管理員和老師。在我AuthController我有這樣的:訪問控制到不同的用戶 - Laravel 5.2

public function getLogin() { 
    return view('auth.login'); 
} 

public function postLogin(\Illuminate\Http\Request $request) { 
    if(Auth::attempt([ 
     'username' => $request->input('username'), 
     'password' => $request->input('password'), 
     'type' => 'admin' 
    ])) { 
     return redirect ('admin'); 
    } 

    if(Auth::attempt([ 
     'username' => $request->input('username'), 
     'password' => $request->input('password'), 
     'type' => 'teacher' 
    ])) { 
     return redirect ('educator/account'); 
    } 


    return redirect('login')->with('message', [ 
     'type' => 'danger', 
     'message' => 'Грешно потребителско име или парола!' 
    ]); 
} 

public function getLogout() { 
    Auth::logout(); 

    return redirect('login'); 
} 

但是當我登錄的類型的教師用戶如果我去http://localhost/school_system/public/admin我自動轉到管理面板,而不要求用戶名和密碼。我想如果我想從教師帳戶進入管理面板,這發生在詢問用戶名和密碼時,我該如何做到這一點?

我的路線:

Route::group(['middleware' => ['auth'] 
], function() { 
    Route::group([ 
     'prefix' => 'admin', 
     'namespace' => 'Admin' 
    ], function() { 
     Route::controller('student', 'StudentsController'); 
     Route::controller('profile', 'ProfilesController'); 
     Route::controller('class', 'ClassesController'); 
     Route::controller('subjects', 'SubjectsController'); 
     Route::controller('teacher', 'TeachersController'); 
     Route::controller('marktype', 'MarkTypeController'); 
     Route::controller('rules', 'RuleController'); 
     Route::get('{slug?}', '[email protected]'); 


    }); 
}); 
    Route::group(['middleware' => ['auth'] 
     ], function() { 
      Route::group([ 
       'prefix' => 'educator', 
       'namespace' => 'Educator' 
     ], function() { 
       Route::controller('account', 'AccountController'); 
       Route::get('{slug?}', '[email protected]'); 
      }); 

     }); 

謝謝^^

+0

[Laravel 5個簡單的ACL經理(https://gist.github.com/drawmyattention/8cb599ee5dc0af5f4246) – Muhammet

+0

你必須要爲特定的用戶角色和途徑一些中間件你必須做出路由組對於每個角色和定義這些中間件 –

+0

但在這裏我的用戶在表本身中獲得它的類型,如下所示: 'Schema :: create('users',function(Blueprint $ table){table-> increments('id 「); $ table-> string('username'); $ table-> string('password'); $ table-> string('first_name',20); $ table-> string('last_name',20); $ table-> integer('class_id') - > unsigned() - > nullable(); $ table-> enum('type',['admin','educator','student']); $ table-> rememberToken(); $ table-> timestamps();' –

回答

2

您可以創建一個 「adminmiddleware」:代碼php artisan make:middleware AdminMiddleware 例如,你可以使用

if ($request->user()->type != 'admin') 
{ 
    return redirect('home'); 
} 

,並在路線:

Route::group(['middleware' => ['admin']... 

成品;)

+0

但我沒有班級管理員? –

+0

做成了,謝謝! –