|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Circles - Bring cloud-users closer together. |
|
6
|
|
|
* |
|
7
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
8
|
|
|
* later. See the COPYING file. |
|
9
|
|
|
* |
|
10
|
|
|
* @author Maxence Lange <[email protected]> |
|
11
|
|
|
* @copyright 2017 |
|
12
|
|
|
* @license GNU AGPL version 3 or any later version |
|
13
|
|
|
* |
|
14
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
15
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
16
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
17
|
|
|
* License, or (at your option) any later version. |
|
18
|
|
|
* |
|
19
|
|
|
* This program is distributed in the hope that it will be useful, |
|
20
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
21
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
22
|
|
|
* GNU Affero General Public License for more details. |
|
23
|
|
|
* |
|
24
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
25
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
26
|
|
|
* |
|
27
|
|
|
*/ |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
namespace OCA\Circles\GlobalScale; |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
use OCA\Circles\Db\CirclesRequest; |
|
34
|
|
|
use OCA\Circles\Db\GSSharesRequest; |
|
35
|
|
|
use OCA\Circles\Db\MembersRequest; |
|
36
|
|
|
use OCA\Circles\Db\SharesRequest; |
|
37
|
|
|
use OCA\Circles\Db\TokensRequest; |
|
38
|
|
|
use OCA\Circles\Exceptions\CircleDoesNotExistException; |
|
39
|
|
|
use OCA\Circles\Exceptions\ConfigNoCircleAvailableException; |
|
40
|
|
|
use OCA\Circles\Exceptions\GlobalScaleDSyncException; |
|
41
|
|
|
use OCA\Circles\Exceptions\GlobalScaleEventException; |
|
42
|
|
|
use OCA\Circles\Model\Circle; |
|
43
|
|
|
use OCA\Circles\Model\GlobalScale\GSEvent; |
|
44
|
|
|
use OCA\Circles\Model\Member; |
|
45
|
|
|
use OCA\Circles\Service\CirclesService; |
|
46
|
|
|
use OCA\Circles\Service\ConfigService; |
|
47
|
|
|
use OCA\Circles\Service\EventsService; |
|
48
|
|
|
use OCA\Circles\Service\MembersService; |
|
49
|
|
|
use OCA\Circles\Service\MiscService; |
|
50
|
|
|
use OCP\Defaults; |
|
51
|
|
|
use OCP\Files\IRootFolder; |
|
52
|
|
|
use OCP\IURLGenerator; |
|
53
|
|
|
use OCP\IUserManager; |
|
54
|
|
|
use OCP\Mail\IMailer; |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Class AGlobalScaleEvent |
|
59
|
|
|
* |
|
60
|
|
|
* @package OCA\Circles\GlobalScale |
|
61
|
|
|
*/ |
|
62
|
|
|
abstract class AGlobalScaleEvent { |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
/** @var IRootFolder */ |
|
66
|
|
|
protected $rootFolder; |
|
67
|
|
|
|
|
68
|
|
|
/** @var IURLGenerator */ |
|
69
|
|
|
protected $urlGenerator; |
|
70
|
|
|
|
|
71
|
|
|
/** @var IMailer */ |
|
72
|
|
|
protected $mailer; |
|
73
|
|
|
|
|
74
|
|
|
/** @var Defaults */ |
|
75
|
|
|
protected $defaults; |
|
76
|
|
|
|
|
77
|
|
|
/** @var IUserManager */ |
|
78
|
|
|
protected $userManager; |
|
79
|
|
|
|
|
80
|
|
|
/** @var SharesRequest */ |
|
81
|
|
|
protected $sharesRequest; |
|
82
|
|
|
|
|
83
|
|
|
/** @var TokensRequest */ |
|
84
|
|
|
protected $tokensRequest; |
|
85
|
|
|
|
|
86
|
|
|
/** @var CirclesRequest */ |
|
87
|
|
|
protected $circlesRequest; |
|
88
|
|
|
|
|
89
|
|
|
/** @var MembersRequest */ |
|
90
|
|
|
protected $membersRequest; |
|
91
|
|
|
|
|
92
|
|
|
/** @var GSSharesRequest */ |
|
93
|
|
|
protected $gsSharesRequest; |
|
94
|
|
|
|
|
95
|
|
|
/** @var CirclesService */ |
|
96
|
|
|
protected $circlesService; |
|
97
|
|
|
|
|
98
|
|
|
/** @var MembersService */ |
|
99
|
|
|
protected $membersService; |
|
100
|
|
|
|
|
101
|
|
|
/** @var EventsService */ |
|
102
|
|
|
protected $eventsService; |
|
103
|
|
|
|
|
104
|
|
|
/** @var ConfigService */ |
|
105
|
|
|
protected $configService; |
|
106
|
|
|
|
|
107
|
|
|
/** @var MiscService */ |
|
108
|
|
|
protected $miscService; |
|
109
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* AGlobalScaleEvent constructor. |
|
113
|
|
|
* |
|
114
|
|
|
* @param IRootFolder $rootFolder |
|
115
|
|
|
* @param IURLGenerator $urlGenerator |
|
116
|
|
|
* @param IMailer $mailer |
|
117
|
|
|
* @param Defaults $defaults |
|
118
|
|
|
* @param IUserManager $userManager |
|
119
|
|
|
* @param SharesRequest $sharesRequest |
|
120
|
|
|
* @param TokensRequest $tokensRequest |
|
121
|
|
|
* @param CirclesRequest $circlesRequest |
|
122
|
|
|
* @param MembersRequest $membersRequest |
|
123
|
|
|
* @param GSSharesRequest $gsSharesRequest |
|
124
|
|
|
* @param CirclesService $circlesService |
|
125
|
|
|
* @param MembersService $membersService |
|
126
|
|
|
* @param EventsService $eventsService |
|
127
|
|
|
* @param ConfigService $configService |
|
128
|
|
|
* @param MiscService $miscService |
|
129
|
|
|
*/ |
|
130
|
|
|
public function __construct( |
|
131
|
|
|
IRootFolder $rootFolder, |
|
132
|
|
|
IURLGenerator $urlGenerator, |
|
133
|
|
|
IMailer $mailer, |
|
134
|
|
|
Defaults $defaults, |
|
135
|
|
|
IUserManager $userManager, |
|
136
|
|
|
SharesRequest $sharesRequest, |
|
137
|
|
|
TokensRequest $tokensRequest, |
|
138
|
|
|
CirclesRequest $circlesRequest, |
|
139
|
|
|
MembersRequest $membersRequest, |
|
140
|
|
|
GSSharesRequest $gsSharesRequest, |
|
141
|
|
|
CirclesService $circlesService, |
|
142
|
|
|
MembersService $membersService, |
|
143
|
|
|
EventsService $eventsService, |
|
144
|
|
|
ConfigService $configService, |
|
145
|
|
|
MiscService $miscService |
|
146
|
|
|
) { |
|
147
|
|
|
$this->rootFolder = $rootFolder; |
|
148
|
|
|
$this->urlGenerator = $urlGenerator; |
|
149
|
|
|
$this->mailer = $mailer; |
|
150
|
|
|
$this->defaults = $defaults; |
|
151
|
|
|
$this->userManager = $userManager; |
|
152
|
|
|
$this->sharesRequest = $sharesRequest; |
|
153
|
|
|
$this->tokensRequest = $tokensRequest; |
|
154
|
|
|
$this->circlesRequest = $circlesRequest; |
|
155
|
|
|
$this->membersRequest = $membersRequest; |
|
156
|
|
|
$this->gsSharesRequest = $gsSharesRequest; |
|
157
|
|
|
$this->circlesService = $circlesService; |
|
158
|
|
|
$this->membersService = $membersService; |
|
159
|
|
|
$this->eventsService = $eventsService; |
|
160
|
|
|
$this->configService = $configService; |
|
161
|
|
|
$this->miscService = $miscService; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @param GSEvent $event |
|
167
|
|
|
* @param bool $localCheck |
|
168
|
|
|
* |
|
169
|
|
|
* @param bool $mustBeCheck |
|
170
|
|
|
* |
|
171
|
|
|
* @throws CircleDoesNotExistException |
|
172
|
|
|
* @throws ConfigNoCircleAvailableException |
|
173
|
|
|
* @throws GlobalScaleDSyncException |
|
174
|
|
|
* @throws GlobalScaleEventException |
|
175
|
|
|
*/ |
|
176
|
|
|
public function verify(GSEvent $event, bool $localCheck = false, bool $mustBeCheck = false): void { |
|
177
|
|
|
if ($localCheck && !$event->isForced()) { |
|
178
|
|
|
$this->checkViewer($event, $mustBeCheck); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @param GSEvent $event |
|
185
|
|
|
*/ |
|
186
|
|
|
abstract public function manage(GSEvent $event): void; |
|
187
|
|
|
|
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @param GSEvent[] $events |
|
191
|
|
|
*/ |
|
192
|
|
|
abstract public function result(array $events): void; |
|
193
|
|
|
|
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @param GSEvent $event |
|
197
|
|
|
* @param bool $mustBeChecked |
|
198
|
|
|
* |
|
199
|
|
|
* @throws CircleDoesNotExistException |
|
200
|
|
|
* @throws ConfigNoCircleAvailableException |
|
201
|
|
|
* @throws GlobalScaleDSyncException |
|
202
|
|
|
* @throws GlobalScaleEventException |
|
203
|
|
|
*/ |
|
204
|
|
|
private function checkViewer(GSEvent $event, bool $mustBeChecked) { |
|
205
|
|
|
if (!$event->hasCircle() |
|
206
|
|
|
|| !$event->getCircle() |
|
207
|
|
|
->hasViewer()) { |
|
208
|
|
|
if ($mustBeChecked) { |
|
209
|
|
|
throw new GlobalScaleEventException('GSEvent cannot be checked'); |
|
210
|
|
|
} else { |
|
211
|
|
|
return; |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
$circle = $event->getCircle(); |
|
216
|
|
|
$viewer = $circle->getViewer(); |
|
217
|
|
|
$this->cleanMember($viewer); |
|
218
|
|
|
|
|
219
|
|
|
$localCircle = $this->circlesRequest->getCircle( |
|
220
|
|
|
$circle->getUniqueId(), $viewer->getUserId(), $viewer->getInstance() |
|
221
|
|
|
); |
|
222
|
|
|
|
|
223
|
|
|
if (!$this->compareMembers($viewer, $localCircle->getViewer())) { |
|
224
|
|
|
throw new GlobalScaleDSyncException('Viewer seems DSync'); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
$event->setCircle($localCircle); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @param Member $member1 |
|
233
|
|
|
* @param Member $member2 |
|
234
|
|
|
* |
|
235
|
|
|
* @return bool |
|
236
|
|
|
*/ |
|
237
|
|
|
protected function compareMembers(Member $member1, Member $member2) { |
|
238
|
|
|
if ($member1->getInstance() === '') { |
|
239
|
|
|
$member1->setInstance($this->configService->getLocalCloudId()); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
if ($member2->getInstance() === '') { |
|
243
|
|
|
$member2->setInstance($this->configService->getLocalCloudId()); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
|
|
247
|
|
|
if ($member1->getCircleId() !== $member2->getCircleId() |
|
|
|
|
|
|
248
|
|
|
|| $member1->getUserId() !== $member2->getUserId() |
|
249
|
|
|
|| $member1->getType() <> $member2->getType() |
|
250
|
|
|
|| $member1->getLevel() <> $member2->getLevel() |
|
251
|
|
|
|| $member1->getStatus() !== $member2->getStatus() |
|
252
|
|
|
|| $member1->getInstance() !== $member2->getInstance()) { |
|
253
|
|
|
return false; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
return true; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* @param Circle $circle1 |
|
262
|
|
|
* @param Circle $circle2 |
|
263
|
|
|
* |
|
264
|
|
|
* @return bool |
|
265
|
|
|
*/ |
|
266
|
|
|
protected function compareCircles(Circle $circle1, Circle $circle2): bool { |
|
267
|
|
|
if ($circle1->getName() !== $circle2->getName() |
|
|
|
|
|
|
268
|
|
|
|| $circle1->getDescription() !== $circle2->getDescription() |
|
269
|
|
|
|| $circle1->getSettings(true) !== $circle2->getSettings(true) |
|
270
|
|
|
|| $circle1->getType() !== $circle2->getType() |
|
271
|
|
|
|| $circle1->getUniqueId() !== $circle2->getUniqueId()) { |
|
272
|
|
|
return false; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
return true; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
|
|
279
|
|
|
protected function cleanMember(Member $member) { |
|
280
|
|
|
if ($member->getInstance() === $this->configService->getLocalCloudId()) { |
|
281
|
|
|
$member->setInstance(''); |
|
282
|
|
|
} |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
|