Don't allow login if user is pending or rejected; some refactoring of the registration code

This commit is contained in:
Nabeel Shahzad 2017-12-22 13:38:24 -06:00
parent 161623c85e
commit 1ebf2bd09c
8 changed files with 119 additions and 16 deletions

View File

@ -22,7 +22,7 @@ users:
flight_time: 43200
created_at: now
updated_at: now
active: 1
state: 1
- id: 2
name: Carla Walters
email: carla.walters68@example.com
@ -36,7 +36,7 @@ users:
flight_time: 43200
created_at: now
updated_at: now
active: 1
state: 0
- id: 3
name: Raymond Pearson
email: raymond.pearson56@example.com
@ -50,7 +50,7 @@ users:
flight_time: 43200
created_at: now
updated_at: now
active: 0
state: 1
role_user:
- user_id: 1

View File

@ -2,8 +2,13 @@
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\Controller;
use App\Models\Enums\PilotState;
class LoginController extends Controller
{
@ -20,4 +25,32 @@ class LoginController extends Controller
{
return $this->view('auth/login');
}
protected function sendLoginResponse(Request $request)
{
$request->session()->regenerate();
$this->clearLoginAttempts($request);
$user = Auth::user();
// TODO: How to handle ON_LEAVE?
if($user->state !== PilotState::ACTIVE) {
// Log them out
$this->guard()->logout();
$request->session()->invalidate();
// Redirect to one of the error pages
if($user->state === PilotState::PENDING) {
return $this->view('auth.pending');
}
elseif ($user->state === PilotState::REJECTED) {
return $this->view('auth.rejected');
}
}
return $this->authenticated($request, $this->guard()->user())
?: redirect()->intended($this->redirectPath());
}
}

View File

@ -2,6 +2,8 @@
namespace App\Http\Controllers\Auth;
use App\Models\Enums\PilotState;
use Log;
use App\Facades\Utils;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
@ -70,14 +72,6 @@ class RegisterController extends Controller
*/
protected function create(array $data)
{
$this->validate(request(), [
'name' => 'required',
'email' => 'required|email',
'airline' => 'required',
'home_airport' => 'required',
'password' => 'required|confirmed'
]);
$opts = [
'name' => $data['name'],
'email' => $data['email'],
@ -91,6 +85,32 @@ class RegisterController extends Controller
$user = User::create($opts);
$user = $this->userService->createPilot($user);
Log::info('User registered: ', $user->toArray());
return $user;
}
/**
* Handle a registration request for the application.
* @throws \RuntimeException
*/
public function register(Request $request)
{
$this->validate(request(), [
'name' => 'required',
'email' => 'required|unique:users|email',
'airline' => 'required',
'home_airport' => 'required',
'password' => 'required|confirmed'
]);
$user = $this->create($request->all());
if($user->state === PilotState::PENDING) {
return $this->view('auth.pending');
}
$this->guard()->login($user);
return redirect('/dashboard');
}
}

View File

@ -11,14 +11,16 @@ namespace App\Models\Enums;
class PilotState extends EnumBase
{
const PENDING = 1;
const ACTIVE = 2;
const PENDING = 0;
const ACTIVE = 1;
const REJECTED = 2;
const ON_LEAVE = 3;
const SUSPENDED = 4;
protected static $labels = [
PilotState::PENDING => 'Pending',
PilotState::ACTIVE => 'Active',
PilotState::REJECTED => 'Rejected',
PilotState::ON_LEAVE => 'On Leave',
PilotState::SUSPENDED => 'Suspended',
];

View File

@ -25,11 +25,11 @@ class UserService extends BaseService
public function createPilot(User $user)
{
# Determine if we want to auto accept
if(setting('pilot.auto_accept') === true) {
/*if(setting('pilot.auto_accept') === true) {
$user->state = PilotState::ACTIVE;
} else {
$user->state = PilotState::PENDING;
}
}*/
$user->save();

View File

@ -20,3 +20,21 @@
.border-blue-bottom {
border-bottom: 3px solid #067ec1;
}
.full-height {
height: 50vh;
}
.flex-center {
align-items: center;
display: flex;
justify-content: center;
}
.position-ref {
position: relative;
}
.m-b-md {
margin-bottom: 30px;
}

View File

@ -0,0 +1,14 @@
@extends('layouts.default.app')
@section('title', 'registration pending')
@section('content')
<div class="row">
<div class="col-md-12 " style="text-align: center;">
<div class="flex-center position-ref full-height">
<div class="title m-b-md">
<h2 class="description">your registration is pending approval. please check your email!</h2>
</div>
</div>
</div>
</div>
@endsection()

View File

@ -0,0 +1,16 @@
@extends('layouts.default.app')
@section('title', 'registration denied')
@section('content')
<div class="row">
<div class="col-md-12 " style="text-align: center;">
<div class="flex-center position-ref full-height">
<div class="title m-b-md">
<h2 class="description">
your registration was denied. please contact an administrator
</h2>
</div>
</div>
</div>
</div>
@endsection()