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
|
32 |
|
public function handle($request, Closure $next) |
24
|
|
|
{ |
25
|
32 |
|
if (Auth::check()) { |
26
|
18 |
|
$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
|
18 |
|
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
|
18 |
|
if ($user->hasRole('applicant') || |
41
|
18 |
|
$user->hasRole('admin') ) { |
|
|
|
|
42
|
7 |
|
$applicantProfile = $user->applicant; |
|
|
|
|
43
|
7 |
|
if ($applicantProfile === null) { |
44
|
1 |
|
$applicantProfile = new Applicant(); |
45
|
1 |
|
$applicantProfile->user_id = $user->id; |
|
|
|
|
46
|
1 |
|
$applicantProfile->save(); |
47
|
|
|
} |
48
|
|
|
} |
49
|
18 |
|
if ($user->hasRole('manager') || |
50
|
18 |
|
$user->hasRole('admin')) { |
|
|
|
|
51
|
11 |
|
$managerProfile = $user->manager; |
|
|
|
|
52
|
11 |
|
if ($managerProfile === null) { |
53
|
1 |
|
$managerProfile = new Manager(); |
54
|
1 |
|
$managerProfile->user_id = $user->id; |
55
|
1 |
|
$managerProfile->save(); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
32 |
|
return $next($request); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|