1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Middleware; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use App\Models\Applicant; |
7
|
|
|
use App\Models\HrAdvisor; |
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
|
58 |
|
public function handle($request, Closure $next) |
24
|
|
|
{ |
25
|
58 |
|
if (Auth::check()) { |
26
|
44 |
|
$user = $request->user(); |
27
|
|
|
|
28
|
|
|
// If running in a local environment, and FORCE_ADMIN is true, |
29
|
|
|
// automatically set any logged in user to (temporarily) be an admin |
30
|
44 |
|
if (App::environment() == 'local' && Config::get('app.force_admin')) { |
31
|
|
|
$user->setRole('admin'); |
32
|
|
|
$user->save(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
// Ensure the user has a proper profile associated with it |
36
|
|
|
// If no profile exists yet create one. |
37
|
|
|
// Admins should be given an applicant and manager profile |
38
|
|
|
if ($user->isApplicant() || |
39
|
|
|
$user->isAdmin()) { |
40
|
44 |
|
$applicantProfile = $user->applicant; |
41
|
44 |
|
if ($applicantProfile === null) { |
42
|
21 |
|
$applicantProfile = new Applicant(); |
43
|
21 |
|
$applicantProfile->user_id = $user->id; |
44
|
10 |
|
$applicantProfile->save(); |
45
|
10 |
|
$user->refresh(); |
46
|
10 |
|
} |
47
|
|
|
} |
48
|
|
|
if ($user->isManager() || |
49
|
44 |
|
$user->isAdmin()) { |
50
|
44 |
|
$managerProfile = $user->manager; |
51
|
32 |
|
if ($managerProfile === null) { |
52
|
32 |
|
$managerProfile = new Manager(); |
53
|
10 |
|
$managerProfile->user_id = $user->id; |
54
|
10 |
|
$managerProfile->save(); |
55
|
10 |
|
$user->refresh(); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
if ($user->isHrAdvisor() || |
59
|
|
|
$user->isAdmin()) { |
60
|
58 |
|
$hrAdvisorProfile = $user->hr_advisor; |
61
|
|
|
if ($hrAdvisorProfile === null) { |
62
|
|
|
$hrAdvisorProfile = new HrAdvisor(); |
63
|
|
|
$hrAdvisorProfile->user_id = $user->id; |
64
|
|
|
$hrAdvisorProfile->save(); |
65
|
|
|
$user->refresh(); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $next($request); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|