Php 在laravel 5.3中添加登录的附加条件
我正在尝试使用明显的电子邮件和密码以及数据库中的ban_状态设置为0来验证用户 我查看了最新的laravel文档,并在AuthenticateUsers.php中尝试了这种方式Php 在laravel 5.3中添加登录的附加条件,php,laravel,Php,Laravel,我正在尝试使用明显的电子邮件和密码以及数据库中的ban_状态设置为0来验证用户 我查看了最新的laravel文档,并在AuthenticateUsers.php中尝试了这种方式 protected function validateLogin(Request $request) { $this->validate($request, [ $this->username() => 'required', 'password' => 'required
protected function validateLogin(Request $request)
{
$this->validate($request, [
$this->username() => 'required', 'password' => 'required', 'ban_status' => '0',
]);
}
就我所知,这没有任何作用,无论ban状态是否为0,我都将登录用户,我应该在哪里执行此附加条件?长话短说,您在发布的代码中实际要做的是检查从$request传入的ban_状态值,或者换句话说,就是登录表单 我对你的问题的理解是,这并不是你真正想要的 相反,请尝试以下方法: 通过在LoginController中定义AuthenticateUsers的登录方法来覆盖该方法,并添加以下内容以检查您的ban_状态:
长话短说,您在发布的代码中实际要做的是检查从$request传入的ban_状态值,或者换句话说,是登录表单 我对你的问题的理解是,这并不是你真正想要的 相反,请尝试以下方法: 通过在LoginController中定义AuthenticateUsers的登录方法来覆盖该方法,并添加以下内容以检查您的ban_状态:
您还可以手动对用户进行身份验证:
public function authenticate(Request $request)
{
$password=$request->get('password');
$email=$request->get('email');
if (Auth::attempt(['email' => $email, 'password' => $password,'ban_status'=>0]) )
{
return redirect()->intended('/');
}
else
{
return redirect('/login');
}
}
您还可以手动对用户进行身份验证:
public function authenticate(Request $request)
{
$password=$request->get('password');
$email=$request->get('email');
if (Auth::attempt(['email' => $email, 'password' => $password,'ban_status'=>0]) )
{
return redirect()->intended('/');
}
else
{
return redirect('/login');
}
}
在tam的回答的基础上,我添加了一个基于失败禁止状态的重定向,因为否则我仍然会登录,即使条件为false。下面是对LoginController.php中的login函数的覆盖:
public function login(Request $request)
{
$this->validateLogin($request);
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
$credentials = $this->credentials($request);
if ($this->guard()->attempt($credentials, $request->has('remember')))
{
if ($this->guard()->user()->ban_status === 0) { // ADDED THIS CHECK
return $this->sendLoginResponse($request);
} else { // logout and redirect if failed
$this->guard()->logout();
return redirect()->back()
->withInput($request->only($this->username(), 'remember'))
->withErrors([
$this->username() => 'You have been banned',
]);
}
}
// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
$this->incrementLoginAttempts($request);
return $this->sendFailedLoginResponse($request);
}
在tam的回答的基础上,我添加了一个基于失败禁止状态的重定向,因为否则我仍然会登录,即使条件为false。下面是对LoginController.php中的login函数的覆盖:
public function login(Request $request)
{
$this->validateLogin($request);
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
$credentials = $this->credentials($request);
if ($this->guard()->attempt($credentials, $request->has('remember')))
{
if ($this->guard()->user()->ban_status === 0) { // ADDED THIS CHECK
return $this->sendLoginResponse($request);
} else { // logout and redirect if failed
$this->guard()->logout();
return redirect()->back()
->withInput($request->only($this->username(), 'remember'))
->withErrors([
$this->username() => 'You have been banned',
]);
}
}
// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
$this->incrementLoginAttempts($request);
return $this->sendFailedLoginResponse($request);
}
与其像在接受的答案中那样重写登录函数,不如重写凭证函数。此函数将返回一个要在数据库中检查的值数组
当与原始问题中的固定值进行比较时,只需制作一个数组并将其合并到:
protected function credentials(Request $request)
{
return array_merge(
$request->only($this->username(), "password"),
["ban_status" => 0]
);
}
或者,要与动态值进行比较,例如登录表单中有一个值,则只需将其添加到返回的请求字段列表中即可
protected function credentials(Request $request)
{
return $request->only($this->username(), "password", "your_field");
}
为什么这样更好?用户在系统中从未经过身份验证–除非所有条件都匹配,否则数据库查询不会返回结果。在接受的答案中,用户最初通过了登录尝试。对于用户3703567,这可能会导致问题。与其像接受的答案那样重写登录函数,不如重写凭据函数。此函数将返回一个要在数据库中检查的值数组
当与原始问题中的固定值进行比较时,只需制作一个数组并将其合并到:
protected function credentials(Request $request)
{
return array_merge(
$request->only($this->username(), "password"),
["ban_status" => 0]
);
}
或者,要与动态值进行比较,例如登录表单中有一个值,则只需将其添加到返回的请求字段列表中即可
protected function credentials(Request $request)
{
return $request->only($this->username(), "password", "your_field");
}
为什么这样更好?用户在系统中从未经过身份验证–除非所有条件都匹配,否则数据库查询不会返回结果。在接受的答案中,用户最初通过了登录尝试。作为用户3703567,这可能会导致问题。如果这样做,您将失去节流功能如果这样做,您将失去节流功能如果这样做,我将如何覆盖它?我是否会为post请求/登录设置一条路径,以转到LoginController@login?实际上,您需要做的只是将新代码复制并粘贴到您的LoginController。其他一切都可以保持不变。我们正在做的是覆盖AuthenticatesUsers特性的登录方法。如果您不熟悉这个概念,请检查。啊,是的,它起了作用,我的问题是我使用了错误的请求,如果登录失败,我将在哪里定义用户去哪里?i、 e是否存在禁止状态?是否存在任何故障?或者特别是如果用户被禁止?我已经这么做了,你觉得怎么样,安全吗?我如何覆盖这个?我是否会为post请求/登录设置一条路径,以转到LoginController@login?实际上,您需要做的只是将新代码复制并粘贴到您的LoginController。其他一切都可以保持不变。我们正在做的是覆盖AuthenticatesUsers特性的登录方法。如果您不熟悉这个概念,请检查。啊,是的,它起了作用,我的问题是我使用了错误的请求,如果登录失败,我将在哪里定义用户去哪里?i、 e是否存在禁止状态?是否存在任何故障?或者特别是如果用户被禁止?好吧,我已经这样做了,你觉得怎么样,安全吗?