我试图制作简单的管理功能来禁用/启用用户而不是删除它们。
到目前为止,我有一个管理功能,可以成功更新表用户并将状态更改为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.
我怎样才能以正确的方式做到这一点?
答案 0 :(得分:4)
我认为当你获得usernaem用户的问题,之后你得到另一个用户,请使用firt getted用户,一切都应该有用。
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('/');
}
答案 1 :(得分:1)
你让事情变得有点复杂,我不知道你为什么要检查用户2次,尝试这样的事情,希望它会有所帮助
$user = User::where('username', Input::get('username'))->first(['is_disabled']);
if (!$user || $user->is_disabled==1) {
$validator->messages()->add('username', 'Invalid login or password.');
return Redirect::to('/users/login')->withErrors($validator->errors())->withInput(Input::except(['captcha']));
}
else if($user && $user->is_disabled==0){
the code you want to process for logged in user
}
else{
$validator->messages()->add('username', 'Invalid login or password.');
return Redirect::to('/users/login')->withErrors($validator->errors())->withInput(Input::except(['captcha']));
}
答案 2 :(得分:1)
代码$user = User::where('is_disabled', 0)->first();
是不必要的,并且提取错误的用户。
public function redirectWithError($errors)
{
return Redirect::to('/users/login')
->withErrors($errors)
->withInput(Input::except(['captcha']));
}
public function loginSubmit()
{
$user = User::where('username', Input::get('username'))->first();
if (!$user) {
$validator->messages()->add('username', 'Invalid login or password.');
return $this->redirectWithError($validator->errors());
}
if ($user->is_disabled == 1) {
$validator->messages()->add('username', 'User not found.');
return $this->redirectWithError($validator->errors());
}
$user->last_login = \Carbon\Carbon::now();
$user->save();
Session::put('user', ['user_id' => $user->user_id]);
return Redirect::to('/');
}