1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: arthur |
5
|
|
|
* Date: 11.10.18 |
6
|
|
|
* Time: 15:35. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Foundation\Policies; |
10
|
|
|
|
11
|
|
|
use Foundation\Abstracts\Policies\Policy; |
12
|
|
|
use Foundation\Contracts\ModelPolicyContract; |
13
|
|
|
use Foundation\Contracts\Ownable; |
14
|
|
|
use Foundation\Exceptions\Exception; |
15
|
|
|
use Illuminate\Auth\Access\HandlesAuthorization; |
16
|
|
|
use Illuminate\Contracts\Auth\Authenticatable; |
17
|
|
|
use Modules\User\Entities\User; |
18
|
|
|
|
19
|
|
|
class OwnershipPolicy extends Policy implements ModelPolicyContract |
20
|
|
|
{ |
21
|
|
|
use HandlesAuthorization; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Determine if the given user can access the model. |
25
|
|
|
* |
26
|
|
|
* @param User $user |
27
|
|
|
* |
28
|
|
|
* @throws Exception |
29
|
|
|
* |
30
|
|
|
* @return bool |
31
|
|
|
*/ |
32
|
|
|
public function access($user, $model): bool |
33
|
|
|
{ |
34
|
|
|
return $this->userIsModelOwner($user, $model); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param User $user |
39
|
|
|
* @param Ownable $model |
40
|
|
|
* |
41
|
|
|
* @throws Exception |
42
|
|
|
* |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
|
|
private function userIsModelOwner(User $user, Ownable $model): bool |
46
|
|
|
{ |
47
|
|
|
if (class_implements_interface($model->ownedBy(), Authenticatable::class)) { |
48
|
|
|
return $user->id === $model->ownerId(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$ownerModel = $model->ownedBy(); |
52
|
|
|
$owner = $ownerModel::find($model->ownerId()); |
53
|
|
|
|
54
|
|
|
if (class_implements_interface($owner, Ownable::class)) { |
55
|
|
|
return $this->userIsModelOwner($user, $owner); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
throw new Exception("recursive ownershippolicy lookup failed. Not all models implemented ownable so couldn't identify if user owned model"); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Determine if the given user can access the model. |
63
|
|
|
* |
64
|
|
|
* @param User $user |
65
|
|
|
* |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
|
|
public function create(User $user): bool |
69
|
|
|
{ |
70
|
|
|
return true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Determine if the given user can update the model. |
75
|
|
|
* |
76
|
|
|
* @param User $user |
77
|
|
|
* |
78
|
|
|
* @throws Exception |
79
|
|
|
* |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
public function update(User $user, $model): bool |
83
|
|
|
{ |
84
|
|
|
return $this->userIsModelOwner($user, $model); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param User $user |
89
|
|
|
* @param $model |
90
|
|
|
* |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
public function delete(User $user, $model): bool |
94
|
|
|
{ |
95
|
|
|
return true; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/* |
99
|
|
|
* @param $user |
100
|
|
|
* @param $ability |
101
|
|
|
* @return null |
102
|
|
|
*/ |
103
|
|
|
public function before($user, $ability) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
if ($user->isAdmin()) |
106
|
|
|
return true; |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.