|
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\Files\IRootFolder; |
|
51
|
|
|
use OCP\IUserManager; |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Class AGlobalScaleEvent |
|
56
|
|
|
* |
|
57
|
|
|
* @package OCA\Circles\GlobalScale |
|
58
|
|
|
*/ |
|
59
|
|
|
abstract class AGlobalScaleEvent { |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** @var IRootFolder */ |
|
63
|
|
|
protected $rootFolder; |
|
64
|
|
|
|
|
65
|
|
|
/** @var IUserManager */ |
|
66
|
|
|
protected $userManager; |
|
67
|
|
|
|
|
68
|
|
|
/** @var SharesRequest */ |
|
69
|
|
|
protected $sharesRequest; |
|
70
|
|
|
|
|
71
|
|
|
/** @var TokensRequest */ |
|
72
|
|
|
protected $tokensRequest; |
|
73
|
|
|
|
|
74
|
|
|
/** @var CirclesRequest */ |
|
75
|
|
|
protected $circlesRequest; |
|
76
|
|
|
|
|
77
|
|
|
/** @var MembersRequest */ |
|
78
|
|
|
protected $membersRequest; |
|
79
|
|
|
|
|
80
|
|
|
/** @var GSSharesRequest */ |
|
81
|
|
|
protected $gsSharesRequest; |
|
82
|
|
|
|
|
83
|
|
|
/** @var CirclesService */ |
|
84
|
|
|
protected $circlesService; |
|
85
|
|
|
|
|
86
|
|
|
/** @var MembersService */ |
|
87
|
|
|
protected $membersService; |
|
88
|
|
|
|
|
89
|
|
|
/** @var EventsService */ |
|
90
|
|
|
protected $eventsService; |
|
91
|
|
|
|
|
92
|
|
|
/** @var ConfigService */ |
|
93
|
|
|
protected $configService; |
|
94
|
|
|
|
|
95
|
|
|
/** @var MiscService */ |
|
96
|
|
|
protected $miscService; |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* AGlobalScaleEvent constructor. |
|
101
|
|
|
* |
|
102
|
|
|
* @param IRootFolder $rootFolder |
|
103
|
|
|
* @param IUserManager $userManager |
|
104
|
|
|
* @param SharesRequest $sharesRequest |
|
105
|
|
|
* @param TokensRequest $tokensRequest |
|
106
|
|
|
* @param CirclesRequest $circlesRequest |
|
107
|
|
|
* @param MembersRequest $membersRequest |
|
108
|
|
|
* @param GSSharesRequest $gsSharesRequest |
|
109
|
|
|
* @param CirclesService $circlesService |
|
110
|
|
|
* @param MembersService $membersService |
|
111
|
|
|
* @param EventsService $eventsService |
|
112
|
|
|
* @param ConfigService $configService |
|
113
|
|
|
* @param MiscService $miscService |
|
114
|
|
|
*/ |
|
115
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
|
|
116
|
|
|
IRootFolder $rootFolder, |
|
117
|
|
|
IUserManager $userManager, |
|
118
|
|
|
SharesRequest $sharesRequest, |
|
119
|
|
|
TokensRequest $tokensRequest, |
|
120
|
|
|
CirclesRequest $circlesRequest, |
|
121
|
|
|
MembersRequest $membersRequest, |
|
122
|
|
|
GSSharesRequest $gsSharesRequest, |
|
123
|
|
|
CirclesService $circlesService, |
|
124
|
|
|
MembersService $membersService, |
|
125
|
|
|
EventsService $eventsService, |
|
126
|
|
|
ConfigService $configService, |
|
127
|
|
|
MiscService $miscService |
|
128
|
|
|
) { |
|
129
|
|
|
$this->rootFolder = $rootFolder; |
|
130
|
|
|
$this->userManager = $userManager; |
|
131
|
|
|
$this->sharesRequest = $sharesRequest; |
|
132
|
|
|
$this->tokensRequest = $tokensRequest; |
|
133
|
|
|
$this->circlesRequest = $circlesRequest; |
|
134
|
|
|
$this->membersRequest = $membersRequest; |
|
135
|
|
|
$this->gsSharesRequest = $gsSharesRequest; |
|
136
|
|
|
$this->circlesService = $circlesService; |
|
137
|
|
|
$this->membersService = $membersService; |
|
138
|
|
|
$this->eventsService = $eventsService; |
|
139
|
|
|
$this->configService = $configService; |
|
140
|
|
|
$this->miscService = $miscService; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param GSEvent $event |
|
146
|
|
|
* @param bool $localCheck |
|
147
|
|
|
* |
|
148
|
|
|
* @param bool $mustBeCheck |
|
149
|
|
|
* |
|
150
|
|
|
* @throws CircleDoesNotExistException |
|
151
|
|
|
* @throws ConfigNoCircleAvailableException |
|
152
|
|
|
* @throws GlobalScaleDSyncException |
|
153
|
|
|
* @throws GlobalScaleEventException |
|
154
|
|
|
*/ |
|
155
|
|
|
public function verify(GSEvent $event, bool $localCheck = false, bool $mustBeCheck = false): void { |
|
156
|
|
|
if ($localCheck && !$event->isForced()) { |
|
157
|
|
|
$this->checkViewer($event, $mustBeCheck); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @param GSEvent $event |
|
164
|
|
|
*/ |
|
165
|
|
|
abstract public function manage(GSEvent $event): void; |
|
166
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param GSEvent[] $events |
|
170
|
|
|
*/ |
|
171
|
|
|
abstract public function result(array $events): void; |
|
172
|
|
|
|
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @param GSEvent $event |
|
176
|
|
|
* @param bool $mustBeChecked |
|
177
|
|
|
* |
|
178
|
|
|
* @throws CircleDoesNotExistException |
|
179
|
|
|
* @throws ConfigNoCircleAvailableException |
|
180
|
|
|
* @throws GlobalScaleDSyncException |
|
181
|
|
|
* @throws GlobalScaleEventException |
|
182
|
|
|
*/ |
|
183
|
|
|
private function checkViewer(GSEvent $event, bool $mustBeChecked) { |
|
184
|
|
|
if (!$event->hasCircle() |
|
185
|
|
|
|| !$event->getCircle() |
|
186
|
|
|
->hasViewer()) { |
|
187
|
|
|
if ($mustBeChecked) { |
|
188
|
|
|
throw new GlobalScaleEventException('GSEvent cannot be checked'); |
|
189
|
|
|
} else { |
|
190
|
|
|
return; |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
$circle = $event->getCircle(); |
|
195
|
|
|
$viewer = $circle->getViewer(); |
|
196
|
|
|
$this->cleanMember($viewer); |
|
197
|
|
|
|
|
198
|
|
|
$localCircle = $this->circlesRequest->getCircle( |
|
199
|
|
|
$circle->getUniqueId(), $viewer->getUserId(), $viewer->getInstance() |
|
200
|
|
|
); |
|
201
|
|
|
|
|
202
|
|
|
if (!$this->compareMembers($viewer, $localCircle->getViewer())) { |
|
203
|
|
|
throw new GlobalScaleDSyncException('Viewer seems DSync'); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
$event->setCircle($localCircle); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @param Member $member1 |
|
212
|
|
|
* @param Member $member2 |
|
213
|
|
|
* |
|
214
|
|
|
* @return bool |
|
215
|
|
|
*/ |
|
216
|
|
|
protected function compareMembers(Member $member1, Member $member2) { |
|
217
|
|
|
if ($member1->getInstance() === '') { |
|
218
|
|
|
$member1->setInstance($this->configService->getLocalCloudId()); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
if ($member2->getInstance() === '') { |
|
222
|
|
|
$member2->setInstance($this->configService->getLocalCloudId()); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
|
|
226
|
|
|
if ($member1->getCircleId() !== $member2->getCircleId() |
|
|
|
|
|
|
227
|
|
|
|| $member1->getUserId() !== $member2->getUserId() |
|
228
|
|
|
|| $member1->getType() <> $member2->getType() |
|
229
|
|
|
|| $member1->getLevel() <> $member2->getLevel() |
|
230
|
|
|
|| $member1->getStatus() !== $member2->getStatus() |
|
231
|
|
|
|| $member1->getInstance() !== $member2->getInstance()) { |
|
232
|
|
|
return false; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
return true; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @param Circle $circle1 |
|
241
|
|
|
* @param Circle $circle2 |
|
242
|
|
|
* |
|
243
|
|
|
* @return bool |
|
244
|
|
|
*/ |
|
245
|
|
|
protected function compareCircles(Circle $circle1, Circle $circle2): bool { |
|
246
|
|
|
if ($circle1->getName() !== $circle2->getName() |
|
|
|
|
|
|
247
|
|
|
|| $circle1->getDescription() !== $circle2->getDescription() |
|
248
|
|
|
|| $circle1->getSettings(true) !== $circle2->getSettings(true) |
|
249
|
|
|
|| $circle1->getType() !== $circle2->getType() |
|
250
|
|
|
|| $circle1->getUniqueId() !== $circle2->getUniqueId()) { |
|
251
|
|
|
return false; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
return true; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
|
|
258
|
|
|
protected function cleanMember(Member $member) { |
|
259
|
|
|
if ($member->getInstance() === $this->configService->getLocalCloudId()) { |
|
260
|
|
|
$member->setInstance(''); |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.