1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Policies; |
4
|
|
|
|
5
|
|
|
use App\Models\Item; |
6
|
|
|
use App\Models\Role; |
7
|
|
|
use App\Models\User; |
8
|
|
|
use Illuminate\Auth\Access\HandlesAuthorization; |
9
|
|
|
|
10
|
|
|
class ItemPolicy |
11
|
|
|
{ |
12
|
|
|
use HandlesAuthorization; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Perform pre-authorization checks. |
16
|
|
|
* |
17
|
|
|
* @param \App\Models\User|null $user |
18
|
|
|
* @param string $ability |
19
|
|
|
* @return void|bool |
20
|
|
|
*/ |
21
|
|
|
public function before(?User $user, $ability) |
22
|
|
|
{ |
23
|
|
|
if (in_array($ability, ['create', 'update', 'delete'])) { |
24
|
|
|
return; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
if ($user && $user->hasRole([Role::ROLE_ADMIN, ROLE::ROLE_STAFF])) { |
28
|
|
|
return true; |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Determine whether the user can view any models. |
34
|
|
|
* |
35
|
|
|
* @param \App\Models\User|null $user |
36
|
|
|
* @return \Illuminate\Auth\Access\Response|bool |
37
|
|
|
*/ |
38
|
|
|
public function viewAny(?User $user) |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
return false; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Determine whether the user can view the model. |
45
|
|
|
* |
46
|
|
|
* @param \App\Models\User|null $user |
47
|
|
|
* @param \App\Models\Item $item |
48
|
|
|
* @return \Illuminate\Auth\Access\Response|bool |
49
|
|
|
*/ |
50
|
|
|
public function view(?User $user, Item $item) |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
return true; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Determine whether the user can create models. |
57
|
|
|
* |
58
|
|
|
* @param \App\Models\User|null $user |
59
|
|
|
* @return \Illuminate\Auth\Access\Response|bool |
60
|
|
|
*/ |
61
|
|
|
public function create(User $user) |
62
|
|
|
{ |
63
|
|
|
$order = request()->route('order'); |
64
|
|
|
|
65
|
|
|
return |
66
|
|
|
$user && $user->hasRole(Role::ROLE_ADMIN) && |
67
|
|
|
!is_null($order) && !$order->latestStatus->hasBeenFinished() && !$order->latestStatus->hasBeenCanceled(); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Determine whether the user can update the model. |
72
|
|
|
* |
73
|
|
|
* @param \App\Models\User|null $user |
74
|
|
|
* @param \App\Models\Item $item |
75
|
|
|
* @return \Illuminate\Auth\Access\Response|bool |
76
|
|
|
*/ |
77
|
|
|
public function update(?User $user, Item $item) |
78
|
|
|
{ |
79
|
|
|
return |
80
|
|
|
$user && $user->hasRole(Role::ROLE_ADMIN) && |
81
|
|
|
!$item->order->latestStatus->hasBeenFinished() && !$item->order->latestStatus->hasBeenCanceled(); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Determine whether the user can delete the model. |
86
|
|
|
* |
87
|
|
|
* @param \App\Models\User|null $user |
88
|
|
|
* @param \App\Models\Item $item |
89
|
|
|
* @return \Illuminate\Auth\Access\Response|bool |
90
|
|
|
*/ |
91
|
|
|
public function delete(?User $user, Item $item) |
92
|
|
|
{ |
93
|
|
|
return |
94
|
|
|
$user && $user->hasRole(Role::ROLE_ADMIN) && |
95
|
|
|
!$item->order->latestStatus->hasBeenFinished() && !$item->order->latestStatus->hasBeenCanceled(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Determine whether the user can restore the model. |
100
|
|
|
* |
101
|
|
|
* @param \App\Models\User|null $user |
102
|
|
|
* @param \App\Models\Item $item |
103
|
|
|
* @return \Illuminate\Auth\Access\Response|bool |
104
|
|
|
*/ |
105
|
|
|
public function restore(?User $user, Item $item) |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
return $user && $user->hasRole(Role::ROLE_ADMIN); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Determine whether the user can permanently delete the model. |
112
|
|
|
* |
113
|
|
|
* @param \App\Models\User|null $user |
114
|
|
|
* @param \App\Models\Item $item |
115
|
|
|
* @return \Illuminate\Auth\Access\Response|bool |
116
|
|
|
*/ |
117
|
|
|
public function forceDelete(?User $user, Item $item) |
118
|
|
|
{ |
119
|
|
|
return |
120
|
|
|
$user && $user->hasRole(Role::ROLE_ADMIN) && |
121
|
|
|
!$item->order->latestStatus->hasBeenFinished() && !$item->order->latestStatus->hasBeenCanceled(); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.