自从我发布这个问题以来,我一直在尽可能多地进行研究,最终得到了一个令人满意的解决方案。我不确定这是否是最干净的方式,但它对我有用。如果其他人可以从中受益,以下是我采取的步骤:
1) 我在app\User.php 模型中添加了以下代码:
# company / user relationship
public function company() {
return $this->belongsTo('App\Company', 'comp_id');
}
# determine if company is active
public function activeCompany() {
$comp_stat = $this->company()->first(['status'])->toArray();
return ($comp_stat >= 1) ? true : false;
}
然后我修改了app\Http\Middleware\Authenticate.php中的handle方法如下:
public function handle($request, Closure $next, $guard = null) {
if (Auth::guard($guard)->guest()) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('login');
}
}
/* begin modified section */
if (Auth::check()) {
# logout if company is inactive
if (!auth()->user()->activeCompany()) {
Auth::logout();
return redirect()->guest('login')->with('comp-status-error', true);
}
# logout if user is inactive
if (auth()->user()->status != 1) {
Auth::logout();
return redirect()->guest('login')->with('user-status-error', true);
}
}
/* end modified section */
return $next($request);
}
因此,从技术上讲,用户在公司和用户状态检查之前已经通过身份验证,这就是您必须手动调用 Auth::logout() 的原因。这也是为什么它让我感觉有点“脏”的原因,但同样,我想不出任何其他方法,所以我不得不做有效的事情!如果他们看到更好的方法来实现这一点,我鼓励任何人发表评论。