I’m kind of new in Laravel, so I want to know if someone can tell me the best way to implement a temporary user. I tried to add a variable that bring me the value of active and then try to added on the condition for each role, but it didn’t work. Somebody can help me?
- I have this in my LoginController
public function login() { $credentials = $this->validate(request(), [ 'email' => 'email|required|string', 'password' => 'required|string', ]); if (Auth::attempt($credentials)) { //auth attemptdevuelve verdadero o falso en caso de que las credenciales correspondan o no //Inician cambios RDAN $user = Auth::user(); if ($user->hasRole('admin')) { return redirect('main'); } else if ($user->hasRole('externo')) { return redirect('es/user_form'); } else if ($user->hasRole('profesor')) { return redirect('main'); } else if ($user->hasRole('registrador')) { return redirect('select_lang'); } else { return back()->withErrors(['email' => 'Incorrect user permissions']) ->withInput(request(['email'])); } //Terminan cambios RDAN } else { return back()->withErrors(['email' => 'Incorrect user permissions']) ->withInput(request(['email'])); } }
In MySQL database I added an ‘active’ column of type ‘tinyint(1)’ in the table ‘users’. So I want to add a condition or something that validates if the user is active(1) or inactive(0). Also I would like to add it in the view where the user ‘admin’ can enable or disable a user with a checkbox.
- Index view:
<td class="small text-center"> <div class="row"> <div> <!--VIEW--> <button onclick="verUsuario({{$user->id}})" class="btn btn-sm btn-outline-success m-1"> <i class="bi bi-eye align-content-center"></i> </button> </div> <div> <!--EDIT--> <button onclick="editarUsuario({{$user->id}})" class="btn btn-sm btn-outline-warning m-1"> <i class="bi bi-pencil-fill align-content-center"></i> </button> </div> <!--Delete--> <form id="delete" action="{{url('Usuario/'.$user->id)}}" method="post"> {{csrf_field()}} {{method_field('DELETE')}} <button type="submit" onclick="return confirm('Va a borrar la usuario {{$user->name}} ¿está seguro?')" class="btn btn-sm btn-outline-danger m-1"> <i class="bi bi-trash-fill align-content-center"></i></button> </form> </div> </td>
Advertisement
Answer
You can add 1 more condition.
public function login() { $credentials = $this->validate(request(), [ 'email' => 'email|required|string', 'password' => 'required|string', ]); if (Auth::attempt($credentials)) { //auth attemptdevuelve verdadero o falso en caso de que las credenciales correspondan o no //Inician cambios RDAN $user = Auth::user(); if($user->active) // checking your user is active or not { if ($user->hasRole('admin')) { return redirect('main'); } else if ($user->hasRole('externo')) { return redirect('es/user_form'); } else if ($user->hasRole('profesor')) { return redirect('main'); } else if ($user->hasRole('registrador')) { return redirect('select_lang'); } else { return back()->withErrors(['email' => 'Incorrect user permissions']) ->withInput(request(['email'])); } } else { Auth::logout(); //to logout user return back()->withErrors(['email' => 'Account Deactivated please contact admin'])->withInput(request(['email'])); // error message if user deactivated. } //Terminan cambios RDAN } else { return back()->withErrors(['email' => 'Incorrect user permissions']) ->withInput(request(['email'])); } }
Coming to second part of your question you can simple call an ajax when checkbox checked or un-checked.