Passed
Pull Request — master (#89)
by
unknown
40:26
created

GatePolicy   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 97.22%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 33
c 2
b 0
f 0
dl 0
loc 77
ccs 35
cts 36
cp 0.9722
rs 10
wmc 12

4 Methods

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