-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathHasOwner.php
executable file
·49 lines (42 loc) · 996 Bytes
/
HasOwner.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
namespace App\Policies;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
use Illuminate\Auth\Access\Response;
use Illuminate\Database\Eloquent\Model;
trait HasOwner
{
use HandlesAuthorization;
/**
* Determine whether the user can view the model.
*
* @param User $user
* @param Model $model
*
* @return mixed
*/
public function owner(User $user, Model $model)
{
if (! $model->exists) {
return Response::allow();
}
return $user->id === $model->user_id
? Response::allow()
: Response::denyAsNotFound();
}
/**
* Determine whether the user can view the model.
*
* @param User $user
* @param Model $model
*
* @return bool
*/
public function isOwner(User $user, Model $model): bool
{
if (! $model->exists) {
return true;
}
return $user->id === $model->user_id;
}
}