我使用entrust來管理Laravel 5.3中的基於角色的權限,並自然地爲不同的用戶類型使用手動/自定義登錄。 Auth::attempt()
有可能處理外部表關係嗎?基本上,我想要做這樣的事情:可以Laravel Auth :: attempt()處理關係嗎?
if(Auth::attempt(['email' => $request->email, 'password' => $request->password, hasRole('teacher') => true])) {
return redirect()->intended('teacher/dashboard');
}
但我不斷收到錯誤hasRole()
是未定義功能。我已經包含了use Zizaco\Entrust\Traits\EntrustUserTrait;
和use EntrustUserTrait;
,我認爲這會讓我可以訪問該功能,但顯然不是。
我很欣賞hasRole()
是一個用戶對象的方法,並在檢查時我沒有用戶對象,但我不能做嘗試成功後的角色檢查,因爲那麼用戶已經已登錄,如果角色錯誤,我需要將它們註銷,因爲它們的憑據是正確的,但不是角色;這似乎緩慢而不務實。
我應該如何處理這個問題?有沒有替代Auth::attempt
,我可以使用,不會登錄用戶,但檢查用戶存在與指定的輸入,然後運行角色檢查,然後使用另一個函數來啓動一個經過身份驗證的會話?
如果有用的LoginController我是在這裏:
<?php
namespace App\Http\Controllers\Teacher;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Validator;
use Zizaco\Entrust\Traits\EntrustUserTrait;
class LoginController extends Controller {
use EntrustUserTrait;
public function showLoginForm() {
return view('teacher/login');
}
public function authenticate(Request $request) {
Validator::make($request->all(), [
'email' => 'required|email',
'password' => 'required',
])->validate();
if(Auth::attempt(['email' => $request->email, 'password' => $request->password, hasRole('teacher') => true])) {
return redirect()->intended('teacher/dashboard');
}
return redirect('admin/login')->with('invalid', 'Invalid username or password');
}
}
任何幫助或方向將不勝感激。謝謝。
這將記錄有人在誰不一定是教師,但可以在不同的登錄門戶 –