1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bakery\Eloquent\Concerns; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Illuminate\Support\Facades\Gate; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Illuminate\Auth\Access\AuthorizationException; |
9
|
|
|
|
10
|
|
|
trait Authorizable |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Determine if the current user can create a new model or throw an exception. |
14
|
|
|
* |
15
|
|
|
* @return void |
16
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
17
|
|
|
*/ |
18
|
|
|
public function authorizeToCreate(): void |
19
|
|
|
{ |
20
|
|
|
if (! static::authorizedToCreate()) { |
|
|
|
|
21
|
|
|
throw new AuthorizationException("Not allowed to perform create on {$this->getModelClass()}"); |
|
|
|
|
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Determine if the current user can create a new model. |
27
|
|
|
* |
28
|
|
|
* @return bool |
29
|
|
|
*/ |
30
|
|
|
public function authorizedToCreate(): bool |
31
|
|
|
{ |
32
|
|
|
return $this->authorized('create'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Determine if the current user can update the model or throw an exception. |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
40
|
|
|
*/ |
41
|
|
|
public function authorizeToUpdate(): void |
42
|
|
|
{ |
43
|
|
|
$this->authorize('update'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Determine if the current user can update the model. |
48
|
|
|
* |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
|
|
public function authorizedToUpdate(): bool |
52
|
|
|
{ |
53
|
|
|
return $this->authorized('update'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Determine if the current user can delete the model or throw an exception. |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
61
|
|
|
*/ |
62
|
|
|
public function authorizeToDelete(): void |
63
|
|
|
{ |
64
|
|
|
$this->authorize('delete'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Determine if the current user can delete the model. |
69
|
|
|
* |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
|
|
public function authorizedToDelete(): bool |
73
|
|
|
{ |
74
|
|
|
return $this->authorized('delete'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Determine if the current user can add the given model to the model or throw an exception. |
79
|
|
|
* |
80
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
81
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
82
|
|
|
*/ |
83
|
|
|
public function authorizeToAdd(Model $model): void |
84
|
|
|
{ |
85
|
|
|
$method = 'add'.Str::singular(class_basename($model)); |
86
|
|
|
|
87
|
|
|
$this->authorize($method, [$model]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Determine if the current user can add the given model to the model. |
92
|
|
|
* |
93
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
|
public function authorizedToAdd(Model $model): bool |
97
|
|
|
{ |
98
|
|
|
$method = 'add'.Str::singular(class_basename($model)); |
99
|
|
|
|
100
|
|
|
return $this->authorized($method, [$model]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Determine if the current user can attach the given model to the model. |
105
|
|
|
* |
106
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
107
|
|
|
* @param array|null $pivot |
108
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
109
|
|
|
*/ |
110
|
|
|
public function authorizeToAttach(Model $model, array $pivot = null): void |
111
|
|
|
{ |
112
|
|
|
$method = 'attach'.Str::singular(class_basename($model)); |
113
|
|
|
|
114
|
|
|
$this->authorize($method, [$model, $pivot]); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Determine if the current user can attach the given model to the model. |
119
|
|
|
* |
120
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
121
|
|
|
* @param array|null $pivot |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
public function authorizedToAttach(Model $model, array $pivot = null): bool |
125
|
|
|
{ |
126
|
|
|
$method = 'attach'.Str::singular(class_basename($model)); |
127
|
|
|
|
128
|
|
|
return $this->authorized($method, [$model, $pivot]); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Determine if the current user can detach the given model from the model. |
133
|
|
|
* |
134
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
135
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
136
|
|
|
*/ |
137
|
|
|
public function authorizeToDetach(Model $model): void |
138
|
|
|
{ |
139
|
|
|
$method = 'detach'.Str::singular(class_basename($model)); |
140
|
|
|
|
141
|
|
|
$this->authorize($method, [$model]); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Determine if the current user can detach the given model from the model. |
146
|
|
|
* |
147
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
public function authorizedToDetach(Model $model): bool |
151
|
|
|
{ |
152
|
|
|
$method = 'detach'.Str::singular(class_basename($model)); |
153
|
|
|
|
154
|
|
|
return $this->authorized($method, [$model]); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Determine if the current user has a given ability or throw an exception. |
159
|
|
|
* |
160
|
|
|
* @param string $ability |
161
|
|
|
* @param array|null $arguments |
162
|
|
|
* @return void |
163
|
|
|
* |
164
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
165
|
|
|
*/ |
166
|
|
|
public function authorize(string $ability, $arguments = []): void |
167
|
|
|
{ |
168
|
|
|
if (! $this->authorized($ability, $arguments)) { |
|
|
|
|
169
|
|
|
throw new AuthorizationException("Not allowed to perform {$ability} on {$this->getModelClass()}"); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Determine if the current user has a given ability. |
175
|
|
|
* |
176
|
|
|
* @param string $ability |
177
|
|
|
* @param array $arguments |
178
|
|
|
* @return bool |
179
|
|
|
*/ |
180
|
|
|
public function authorized(string $ability, array $arguments = []): bool |
181
|
|
|
{ |
182
|
|
|
$arguments = array_merge([$this->instance ?? $this->getModelClass()], $arguments); |
183
|
|
|
|
184
|
|
|
return Gate::check($ability, $arguments); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|