|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
namespace App\Http\Middleware; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use App\Models\UserRole; |
|
7
|
|
|
use App\Models\Applicant; |
|
8
|
|
|
use App\Models\Manager; |
|
9
|
|
|
use Illuminate\Support\Facades\Auth; |
|
10
|
|
|
use Illuminate\Support\Facades\App; |
|
11
|
|
|
use Illuminate\Support\Facades\Config; |
|
12
|
|
|
|
|
13
|
|
|
class InitializeUser |
|
|
|
|
|
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Ensure that a logged in user is initialized correctly according to its UserRole |
|
17
|
|
|
* (ie has an associated Applicant or Manager profile) |
|
18
|
|
|
* |
|
19
|
|
|
* @param \Illuminate\Http\Request $request |
|
|
|
|
|
|
20
|
|
|
* @param \Closure $next |
|
|
|
|
|
|
21
|
|
|
* @return mixed |
|
|
|
|
|
|
22
|
|
|
*/ |
|
23
|
8 |
|
public function handle($request, Closure $next) |
|
24
|
|
|
{ |
|
25
|
8 |
|
if (Auth::check()) { |
|
26
|
8 |
|
$user = Auth::user(); |
|
27
|
|
|
|
|
28
|
|
|
//If running in a local environment, and FORCE_ADMIN is true, |
|
29
|
|
|
//automatically set any logged in user to (temporarilly) be an admin |
|
30
|
8 |
|
if (App::environment() == 'local' && Config::get('app.force_admin')) { |
|
31
|
|
|
$adminRole = UserRole::where('name', 'admin')->firstOrFail(); |
|
32
|
|
|
$user->user_role_id = $adminRole->id; |
|
|
|
|
|
|
33
|
|
|
// $user->user_role = $adminRole; |
|
34
|
|
|
$user->save(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
//Ensure the user has a proper profile associated with it |
|
38
|
|
|
//If no profile exists yet create one. |
|
39
|
|
|
//Admins should be given an applicant and manager profile |
|
40
|
8 |
|
if ($user->hasRole('applicant') || |
|
41
|
8 |
|
$user->hasRole('admin') ) { |
|
|
|
|
|
|
42
|
|
|
$applicantProfile = $user->applicant; |
|
|
|
|
|
|
43
|
|
|
if ($applicantProfile === null) { |
|
44
|
|
|
$applicantProfile = new Applicant(); |
|
45
|
|
|
$applicantProfile->user_id = $user->id; |
|
|
|
|
|
|
46
|
|
|
$applicantProfile->save(); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
8 |
|
if ($user->hasRole('manager') || |
|
50
|
8 |
|
$user->hasRole('admin')) { |
|
|
|
|
|
|
51
|
8 |
|
$managerProfile = $user->manager; |
|
|
|
|
|
|
52
|
8 |
|
if ($managerProfile === null) { |
|
53
|
|
|
$managerProfile = new Manager(); |
|
54
|
|
|
$managerProfile->user_id = $user->id; |
|
55
|
|
|
$managerProfile->save(); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
8 |
|
return $next($request); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|