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