gpt4 book ai didi

php - 如果在 Laravel 中禁用用户时限制登录效果不佳

转载 作者:行者123 更新时间:2023-12-04 22:08:58 25 4
gpt4 key购买 nike

我正在尝试制作简单的管理功能来禁用/启用用户而不是删除它们。

到目前为止,我有管理功能,可以成功更新表用户并将状态更改为 0(启用)和 1(禁用)。

现在,当用户尝试登录并检查他的状态时,我遇到了问题。

这是我在 UserController.php 中的登录函数

public function loginSubmit() {     

$user = User::where('username', Input::get('username'))->first();
if (!$user) {
$validator->messages()->add('username', 'Invalid login or password.');
return Redirect::to('/users/login')->withErrors($validator->errors())->withInput(Input::except(['captcha']));
}

$user = User::where('is_disabled', 0)->first();
if ($user->is_disabled == 1) {
$validator->messages()->add('username', 'User not found.');
return Redirect::to('/users/login')->withErrors($validator->errors())->withInput(Input::except(['captcha']));
}

$user->last_login = \Carbon\Carbon::now();
$user->save();
Session::put('user', ['user_id' => $user->user_id]);

return Redirect::to('/');
}

问题是,当条件为真时 ($user->is_disabled == 1) 它会用表中的下一个用户登录,例如is_disabled = 0 的第一个用户。

我怎样才能以正确的方式做到这一点?

最佳答案

我认为当您使用用户名获取用户时出现问题,然后您获取另一个用户,请使用第一个获取的用户,一切都应该正常。

public function loginSubmit() {     

$user = User::where('username', Input::get('username'))->first();
if (!$user) {
$validator->messages()->add('username', 'Invalid login or password.');
return Redirect::to('/users/login')->withErrors($validator->errors())->withInput(Input::except(['captcha']));
}

// $user = User::where('is_disabled', 0)->first(); //why you get one more user here you should use $user above. , remove this line
if ($user->is_disabled == 1) {
$validator->messages()->add('username', 'User not found.');
return Redirect::to('/users/login')->withErrors($validator->errors())->withInput(Input::except(['captcha']));
}

$user->last_login = \Carbon\Carbon::now();
$user->save();
Session::put('user', ['user_id' => $user->user_id]);

return Redirect::to('/');
}

关于php - 如果在 Laravel 中禁用用户时限制登录效果不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44364653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com