Passed
Push — master ( 72576e...ceb7cb )
by Paul
04:15
created

UserPolicy::update()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 3
rs 10
1
<?php
2
3
namespace  Devpri\Tinre\Policies;
4
5
use Devpri\Tinre\Models\User;
6
use Illuminate\Auth\Access\HandlesAuthorization;
7
use Illuminate\Support\Facades\Route;
8
9
class UserPolicy
10
{
11
    use HandlesAuthorization;
12
13 16
    public function viewAny(User $user): bool
14
    {
15 16
        if ($user->hasPermissionTo('user:view:any')) {
16 7
            return true;
17
        }
18
19 9
        return false;
20
    }
21
22 15
    public function view(User $user, User $userModel): bool
23
    {
24 15
        if ($user->hasPermissionTo('user:view:any')) {
25 9
            return true;
26
        }
27
28 6
        if (! $user->hasPermissionTo('user:view')) {
29 1
            return false;
30
        }
31
        
32 5
        if ($user->id === $userModel->id) {
33 5
            return true;
34
        }
35
36
        return false;
37
    }
38
39 12
    public function create(User $user): bool
40
    {
41 12
        if ($user->hasPermissionTo('user:create')) {
42 6
            return true;
43
        }
44
45 6
        return false;
46
    }
47
48 14
    public function update(User $user, User $userModel): bool
49
    {
50 14
        if ($user->id === $userModel->id) {
51 8
            return false;
52
        }
53
54 8
        if ($user->hasPermissionTo('user:update:any')) {
55 5
            return true;
56
        }
57
58 3
        return false;
59
    }
60
61 12
    public function updateOwn(User $user, User $userModel): bool
62
    {
63 12
        if (! $user->hasPermissionTo('user:update')) {
64 1
            return false;
65
        }
66
67 11
        if ($user->id === $userModel->id) {
68 8
            return true;
69
        }
70
71 5
        return false;
72
    }
73
74 11
    public function changeEmail(User $user): bool
75
    {
76 11
        if (! $user->hasPermissionTo('user:change_email')) {
77 1
            return false;
78
        }
79
80 10
        if (Route::has('email.change')) {
81 10
            return true;
82
        }
83
84
        return false;
85
    }
86
87 14
    public function delete(User $user, User $userModel): bool
88
    {
89 14
        if ($user->id === $userModel->id) {
90 7
            return false;
91
        }
92
93 9
        if ($user->hasPermissionTo('user:delete:any')) {
94 6
            return true;
95
        }
96
97 3
        return false;
98
    }
99
}
100