Passed
Branch dev5a (c41270)
by Ron
12:51
created

GatePolicy::seeAdminLink()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 4
eloc 14
c 3
b 0
f 0
nc 5
nop 1
dl 0
loc 22
ccs 15
cts 15
cp 1
crap 4
rs 9.7998
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