1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Policies; |
4
|
|
|
|
5
|
|
|
use Illuminate\Auth\Access\HandlesAuthorization; |
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Facades\Log; |
8
|
|
|
|
9
|
|
|
use App\User; |
10
|
|
|
use App\UserRolePermissions; |
11
|
|
|
|
12
|
|
|
class GatePolicy |
13
|
|
|
{ |
14
|
|
|
use HandlesAuthorization; |
15
|
|
|
|
16
|
|
|
// Determine if the user is a Installer/Super Admin |
17
|
12 |
|
public function isInstaller(User $user) |
18
|
|
|
{ |
19
|
12 |
|
$role = User::find($user->user_id); |
20
|
|
|
|
21
|
12 |
|
if($role->role_id == 1) |
22
|
|
|
{ |
23
|
4 |
|
return true; |
24
|
|
|
} |
25
|
|
|
|
26
|
8 |
|
return false; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
// Determine if a user can see the Administration Nav Link |
30
|
8 |
|
public function seeAdminLink(User $user) |
31
|
|
|
{ |
32
|
8 |
|
if($this->isInstaller($user)) |
33
|
|
|
{ |
34
|
2 |
|
return true; |
35
|
|
|
} |
36
|
|
|
|
37
|
6 |
|
$data = UserRolePermissions::with('UserRolePermissionTypes') |
38
|
|
|
->whereHas('UserRolePermissionTypes', function($query) { |
39
|
6 |
|
$query->where('description', 'Manage Users') |
40
|
6 |
|
->orWhere('description', 'Manage User Roles') |
41
|
6 |
|
->orWhere('description', 'Manage Customers') |
42
|
6 |
|
->orWhere('description', 'Manage Equipment'); |
43
|
6 |
|
}) |
44
|
6 |
|
->where('role_id', $user->role_id) |
45
|
6 |
|
->where('allow', 1) |
46
|
6 |
|
->get(); |
47
|
|
|
|
48
|
6 |
|
$allow = $data->isEmpty() ? 'Denied' : 'Allowed'; |
49
|
6 |
|
Log::debug('User '.$user->full_name.' is trying to see admin link. Result - '.$allow); |
50
|
|
|
|
51
|
6 |
|
return $data->isEmpty() ? false : true; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// Determine if a user has permissions for a task |
55
|
12 |
|
public function hasAccess(User $user, $task) |
56
|
|
|
{ |
57
|
|
|
// Check if user is super user first |
58
|
12 |
|
if($this->isInstaller($user)) |
59
|
|
|
{ |
60
|
4 |
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
8 |
|
$data = UserRolePermissions::with('UserRolePermissionTypes') |
64
|
|
|
->whereHas('UserRolePermissionTypes', function($query) use ($task) { |
65
|
8 |
|
$query->where('description', $task); |
66
|
8 |
|
}) |
67
|
8 |
|
->where('role_id', $user->role_id) |
68
|
8 |
|
->where('allow', 1) |
69
|
8 |
|
->get(); |
70
|
|
|
|
71
|
8 |
|
$allow = $data->isEmpty() ? 'Denied' : 'Allowed'; |
72
|
8 |
|
Log::debug('User '.$user->full_name.' is trying to access '.$task.'. Result - '.$allow); |
73
|
|
|
|
74
|
8 |
|
return $data->isEmpty() ? false : true; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|