1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Application\Model; |
6
|
|
|
|
7
|
|
|
use Application\Acl\Acl; |
8
|
|
|
use Cake\Chronos\Chronos; |
9
|
|
|
use Doctrine\ORM\Mapping as ORM; |
10
|
|
|
use Ecodev\Felix\Api\Exception; |
11
|
|
|
use Ecodev\Felix\Model\HasOwner; |
12
|
|
|
use Ecodev\Felix\Model\Model; |
13
|
|
|
use GraphQL\Doctrine\Annotation as API; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Base class for all objects stored in database. |
17
|
|
|
* |
18
|
|
|
* It includes an automatic mechanism to timestamp objects with date and user. |
19
|
|
|
* |
20
|
|
|
* @ORM\MappedSuperclass |
21
|
|
|
* @ORM\HasLifecycleCallbacks |
22
|
|
|
* @API\Filters({ |
23
|
|
|
* @API\Filter(field="custom", operator="Application\Api\Input\Operator\SearchOperatorType", type="string"), |
24
|
|
|
* }) |
25
|
|
|
* @API\Sorting({ |
26
|
|
|
* "Application\Api\Input\Sorting\LatestModification", |
27
|
|
|
* "Application\Api\Input\Sorting\Owner" |
28
|
|
|
* }) |
29
|
|
|
*/ |
30
|
|
|
abstract class AbstractModel implements HasOwner, Model |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @ORM\Column(type="integer", nullable=false) |
34
|
|
|
* @ORM\Id |
35
|
|
|
* @ORM\GeneratedValue(strategy="IDENTITY") |
36
|
|
|
*/ |
37
|
|
|
private ?int $id = null; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @ORM\Column(type="datetime", nullable=true) |
41
|
|
|
*/ |
42
|
|
|
private ?\Cake\Chronos\Chronos $creationDate = null; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @ORM\Column(type="datetime", nullable=true) |
46
|
|
|
*/ |
47
|
|
|
private ?\Cake\Chronos\Chronos $updateDate = null; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @ORM\ManyToOne(targetEntity="User") |
51
|
|
|
* @ORM\JoinColumns({ |
52
|
|
|
* @ORM\JoinColumn(onDelete="SET NULL") |
53
|
|
|
* }) |
54
|
|
|
*/ |
55
|
|
|
private ?\Application\Model\User $creator = null; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @ORM\ManyToOne(targetEntity="User") |
59
|
|
|
* @ORM\JoinColumns({ |
60
|
|
|
* @ORM\JoinColumn(onDelete="SET NULL") |
61
|
|
|
* }) |
62
|
|
|
*/ |
63
|
|
|
private ?\Application\Model\User $owner = null; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @ORM\ManyToOne(targetEntity="User") |
67
|
|
|
* @ORM\JoinColumns({ |
68
|
|
|
* @ORM\JoinColumn(onDelete="SET NULL") |
69
|
|
|
* }) |
70
|
|
|
*/ |
71
|
|
|
private ?\Application\Model\User $updater = null; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get id. |
75
|
|
|
*/ |
76
|
40 |
|
public function getId(): ?int |
77
|
|
|
{ |
78
|
40 |
|
return $this->id; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Set creation date. |
83
|
|
|
*/ |
84
|
37 |
|
private function setCreationDate(Chronos $creationDate): void |
85
|
|
|
{ |
86
|
37 |
|
$this->creationDate = $creationDate; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get creation date. |
91
|
|
|
*/ |
92
|
|
|
public function getCreationDate(): ?Chronos |
93
|
|
|
{ |
94
|
|
|
return $this->creationDate; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Set update date. |
99
|
|
|
*/ |
100
|
12 |
|
private function setUpdateDate(Chronos $updateDate): void |
101
|
|
|
{ |
102
|
12 |
|
$this->updateDate = $updateDate; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get update date. |
107
|
|
|
*/ |
108
|
|
|
public function getUpdateDate(): ?Chronos |
109
|
|
|
{ |
110
|
|
|
return $this->updateDate; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set creator. |
115
|
|
|
*/ |
116
|
37 |
|
private function setCreator(?User $creator): void |
117
|
|
|
{ |
118
|
37 |
|
$this->creator = $creator; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get creator. |
123
|
|
|
*/ |
124
|
|
|
public function getCreator(): ?User |
125
|
|
|
{ |
126
|
|
|
return $this->creator; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Set owner. |
131
|
|
|
*/ |
132
|
44 |
|
public function setOwner(?User $owner): void |
133
|
|
|
{ |
134
|
44 |
|
if ($owner === $this->owner) { |
135
|
26 |
|
return; |
136
|
|
|
} |
137
|
|
|
|
138
|
26 |
|
$user = User::getCurrent(); |
139
|
26 |
|
$isAdmin = $user && $user->getRole() === User::ROLE_ADMINISTRATOR; |
140
|
26 |
|
$isOwner = $user === $this->owner; |
141
|
|
|
|
142
|
26 |
|
if ($this->owner && !$isAdmin && !$isOwner) { |
143
|
1 |
|
$currentLogin = $user ? $user->getName() : '[anonymous]'; |
144
|
1 |
|
$currentOwnerLogin = $this->owner->getName(); |
|
|
|
|
145
|
1 |
|
$futureOwnerLogin = $owner ? $owner->getName() : '[nobody]'; |
146
|
|
|
|
147
|
1 |
|
throw new Exception($currentLogin . ' is not allowed to change owner to ' . $futureOwnerLogin . ' because it belongs to ' . $currentOwnerLogin); |
148
|
|
|
} |
149
|
|
|
|
150
|
26 |
|
$this->owner = $owner; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get owner. |
155
|
|
|
*/ |
156
|
46 |
|
public function getOwner(): ?User |
157
|
|
|
{ |
158
|
46 |
|
return $this->owner; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Set updater. |
163
|
|
|
*/ |
164
|
12 |
|
private function setUpdater(?User $updater): void |
165
|
|
|
{ |
166
|
12 |
|
$this->updater = $updater; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get updater. |
171
|
|
|
*/ |
172
|
|
|
public function getUpdater(): ?User |
173
|
|
|
{ |
174
|
|
|
return $this->updater; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Get default owner for creation. |
179
|
|
|
*/ |
180
|
29 |
|
protected function getOwnerForCreation(): ?User |
181
|
|
|
{ |
182
|
29 |
|
return User::getCurrent(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Automatically called by Doctrine when the object is saved for the first time. |
187
|
|
|
* |
188
|
|
|
* @ORM\PrePersist |
189
|
|
|
*/ |
190
|
37 |
|
public function timestampCreation(): void |
191
|
|
|
{ |
192
|
37 |
|
$this->setCreationDate(new Chronos()); |
193
|
37 |
|
$this->setCreator(User::getCurrent()); |
194
|
|
|
|
195
|
37 |
|
if (!$this->getOwner()) { |
196
|
31 |
|
$this->setOwner($this->getOwnerForCreation()); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Automatically called by Doctrine when the object is updated. |
202
|
|
|
* |
203
|
|
|
* @ORM\PreUpdate |
204
|
|
|
*/ |
205
|
12 |
|
public function timestampUpdate(): void |
206
|
|
|
{ |
207
|
12 |
|
$this->setUpdateDate(new Chronos()); |
208
|
12 |
|
$this->setUpdater(User::getCurrent()); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Get permissions on this object for the current user. |
213
|
|
|
* |
214
|
|
|
* @API\Field(type="Permissions") |
215
|
|
|
*/ |
216
|
2 |
|
public function getPermissions(): array |
217
|
|
|
{ |
218
|
2 |
|
$acl = new Acl(); |
219
|
|
|
|
220
|
|
|
return [ |
221
|
2 |
|
'create' => $acl->isCurrentUserAllowed($this, 'create'), |
222
|
2 |
|
'read' => $acl->isCurrentUserAllowed($this, 'read'), |
223
|
2 |
|
'update' => $acl->isCurrentUserAllowed($this, 'update'), |
224
|
2 |
|
'delete' => $acl->isCurrentUserAllowed($this, 'delete'), |
225
|
|
|
]; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.