Ste*_*n-v 4 php authentication if-statement laravel laravel-3
这是非常标准的登录功能和验证,可以很好地工作.但我还想检查用户是否处于活动状态.我在users表中设置了一个列,其中'active'设置为0或1.
public function post_login()
{
$input = Input::all();
$rules = array(
'email' => 'required|email',
'password' => 'required',
);
$validation = Validator::make($input, $rules);
if ($validation->fails())
{
return Redirect::to_route('login_user')
->with_errors($validation->errors)->with_input();
}
$credentials = array(
'username' => $input['email'],
'password' => $input['password'],
);
if (Auth::attempt($credentials))
{
// Set remember me cookie if the user checks the box
$remember = Input::get('remember');
if ( !empty($remember) )
{
Auth::login(Auth::user()->id, true);
}
return Redirect::home();
} else {
return Redirect::to_route('login_user')
->with('login_errors', true);
}
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试过这样的事了:
$is_active = Auth::user()->active;
if (!$is_active == 1)
{
echo "Account not activated";
}
Run Code Online (Sandbox Code Playgroud)
但是这只能在"auth attempt"if语句中使用,并且此时用户凭据(电子邮件和通行证)已经过验证.因此,即使此时用户帐户处于活动状态,他们也已登录.
我需要一种方法来返回验证,让他们知道他们仍然需要激活他们的帐户,并检查他们的帐户是否在他们的电子邮件和通行证被检查的同时设置.
小智 8
过滤器是要走的路.解决这个问题简单易行,请参阅下面的示例.
Route::filter('auth', function()
{
if (Auth::guest())
{
if (Request::ajax())
{
return Response::make('Unauthorized', 401);
}
else
{
return Redirect::guest('login');
}
}
else
{
// If the user is not active any more, immidiately log out.
if(Auth::check() && !Auth::user()->active)
{
Auth::logout();
return Redirect::to('/');
}
}
});
Run Code Online (Sandbox Code Playgroud)