2017-06-05 36 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('/'); 
}