2017-06-05 98 views
2

我正在尝试使用简单的管理功能来禁用/启用用户而不是删除它们。如果Laravel中的用户被禁用,限制登录

到目前为止,我已经成功更新了表用户并将状态更改为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

'用户::其中( 'is_disabled',0) - >第一()'字面上仅选择其中'is_disabled'是'0' ... –

+0

使用中间件,而不是这个魔法第一用户。 .. – Kyslik

+0

我会如果我知道如何。我不是那么先进,只是试图从基础知识中学习它。 – Ivan

回答

5

我认为这个问题时,你得到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('/'); 
} 
+0

好的,我现在应该编辑它,谢谢 –

+0

恩,谢谢。当我已经有一个用户时,不要创建新用户是有道理的。 – Ivan

+0

是@Ivan,不客气 –

2

你做的事情有点复杂,我不知道为什么要检查用户的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'])); 
} 
+0

感谢您的回答。现在我更愿意坚持两次检查,并简单地删除第二个用户创建作为其他答案。 – Ivan

2

代码$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('/'); 
}