Passed
Push — dev5 ( 1c1697...6a3ae7 )
by Ron
08:21
created

GatePolicy::hasAccess()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.009

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
c 1
b 0
f 0
nc 9
nop 2
dl 0
loc 24
ccs 13
cts 14
cp 0.9286
crap 5.009
rs 9.5222
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 406
    public function __construct()
20
    {
21
        //
22 406
    }
23
24
    //  Determine if the user is a Installer/Super Admin
25 406
    public function isInstaller(User $user)
26
    {
27 406
        $role = User::find($user->user_id);
28
29 406
        if($role->role_id == 1)
30
        {
31 166
            return true;
32
        }
33
34 240
        return false;
35
    }
36
37
    //  Determine if a user can see the Administration Nav Link
38 110
    public function seeAdminLink(User $user)
39
    {
40 110
        if ($this->isInstaller($user))
41
        {
42 42
            return true;
43
        }
44
45 68
        $data = UserRolePermissions::with('UserRolePermissionTypes')
46
            ->whereHas('UserRolePermissionTypes', function ($query) {
47 68
                $query->where('description', 'Manage Users')
48 68
                    ->orWhere('description', 'Manage User Roles')
49 68
                    ->orWhere('description', 'Manage Customers')
50 68
                    ->orWhere('description', 'Manage Equipment');
51 68
            })
52 68
            ->where('role_id', $user->role_id)
53 68
            ->where('allow', 1)
54 68
            ->get();
55
56 68
        $allow = $data->isEmpty() ? 'Denied' : 'Allowed';
57 68
        Log::debug('User ' . $user->full_name . ' is trying to see admin link.  Result - ' . $allow);
58
59 68
        return  $data->isEmpty() ? false : true;
60
    }
61
62
    //  Determine if a user has permissions for a task
63 380
    public function hasAccess(User $user, $task)
64
    {
65
        //  Check if user is super user first
66 380
        if($this->isInstaller($user))
67
        {
68 162
            return true;
69
        }
70
71 218
        $data = UserRolePermissions::with('UserRolePermissionTypes')
72
            ->whereHas('UserRolePermissionTypes', function ($query) use ($task) {
73 218
                $query->where('description', $task);
74 218
            })
75 218
            ->where('role_id', $user->role_id)
76 218
            ->where('allow', 1)
77 218
            ->get();
78
79 218
        $allow = $data->isEmpty() ? 'false' : 'true';
80 218
        Log::debug('User '.$user->full_name.' is trying to access '.$task.'.  Result - ' . $allow);
81 218
        if ($allow === 'Denied')
82
        {
83
            Log::alert('User ' . $user->full_name . ' was denied from accessing '.$task.' link.');
84
        }
85
86 218
        return  $data->isEmpty() ? false : true;
87
    }
88
}
89