2017-06-05 9 views
2

ユーザーを削除するのではなく、無効にする/有効にする簡単な管理機能を作成しようとしています。Laravelでユーザーが無効になっているとログインが制限される

これまでのところ、テーブルユーザーを正常に更新し、ステータスを0(有効)と1(無効)に変更する管理機能があります。

これで、ユーザーが自分のステータスをログに記録して確認しようとしたときに問題が発生しました。

この問題は条件が真のとき($user->is_disabled == 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('/'); 
} 

でログインのための私の関数であります最初のユーザーはis_disabled = 0です。

これをどのようにして適切に行うことができますか?あなたは物事が少し複雑になりました

+0

'ユーザー::( 'is_disabled'、0) - >最初の()は、' '文字通りのみis_disabled'が '0' ... –

+0

利用ミドルウェアの代わりに、この黒魔術である第一のユーザを選択します。 .. – Kyslik

+0

どうすればいいか分かります。私はそれが上級ではなく、基本から学ぶことを試みているだけです。 – Ivan

答えて

5

を助けることを願って、私はこの問題を考えて、このような何かを試してみてください、あなたはユーザーを2回チェックしている理由を知りませんその後、あなたは別のユーザーを取得し、使用してください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

ok、私は今それを編集する必要があります、ありがとう –

+0

まあ、ありがとう。すでに新しいユーザーを作成していない場合は、新しいユーザーを作成しないと意味があります。 – Ivan

+0

はい@イワン、あなたは歓迎です –

2

、私はあなたがusernaemでユーザーを取得するとき、それは

$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

答えをありがとう。今のところ私は2回のチェックに固執することを好み、他の答えとして2番目のユーザー作成を単に削除します。 – 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('/'); 
} 
関連する問題