|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Circles - Bring cloud-users closer together. |
|
8
|
|
|
* |
|
9
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
10
|
|
|
* later. See the COPYING file. |
|
11
|
|
|
* |
|
12
|
|
|
* @author Maxence Lange <[email protected]> |
|
13
|
|
|
* @copyright 2021 |
|
14
|
|
|
* @license GNU AGPL version 3 or any later version |
|
15
|
|
|
* |
|
16
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
17
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
18
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
19
|
|
|
* License, or (at your option) any later version. |
|
20
|
|
|
* |
|
21
|
|
|
* This program is distributed in the hope that it will be useful, |
|
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
24
|
|
|
* GNU Affero General Public License for more details. |
|
25
|
|
|
* |
|
26
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
27
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
28
|
|
|
* |
|
29
|
|
|
*/ |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
namespace OCA\Circles\Service; |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
use daita\MySmallPhpTools\Model\SimpleDataStore; |
|
36
|
|
|
use daita\MySmallPhpTools\Traits\Nextcloud\nc22\TNC22Logger; |
|
37
|
|
|
use daita\MySmallPhpTools\Traits\TArrayTools; |
|
38
|
|
|
use daita\MySmallPhpTools\Traits\TStringTools; |
|
39
|
|
|
use OCA\Circles\Db\CircleRequest; |
|
40
|
|
|
use OCA\Circles\Db\MemberRequest; |
|
41
|
|
|
use OCA\Circles\Exceptions\CircleNotFoundException; |
|
42
|
|
|
use OCA\Circles\Exceptions\FederatedEventException; |
|
43
|
|
|
use OCA\Circles\Exceptions\FederatedItemException; |
|
44
|
|
|
use OCA\Circles\Exceptions\InitiatorNotConfirmedException; |
|
45
|
|
|
use OCA\Circles\Exceptions\InitiatorNotFoundException; |
|
46
|
|
|
use OCA\Circles\Exceptions\MemberNotFoundException; |
|
47
|
|
|
use OCA\Circles\Exceptions\OwnerNotFoundException; |
|
48
|
|
|
use OCA\Circles\Exceptions\RemoteInstanceException; |
|
49
|
|
|
use OCA\Circles\Exceptions\RemoteNotFoundException; |
|
50
|
|
|
use OCA\Circles\Exceptions\RemoteResourceNotFoundException; |
|
51
|
|
|
use OCA\Circles\Exceptions\RequestBuilderException; |
|
52
|
|
|
use OCA\Circles\Exceptions\UnknownRemoteException; |
|
53
|
|
|
use OCA\Circles\FederatedItems\MassiveMemberAdd; |
|
54
|
|
|
use OCA\Circles\FederatedItems\MemberLevel; |
|
55
|
|
|
use OCA\Circles\FederatedItems\MemberRemove; |
|
56
|
|
|
use OCA\Circles\FederatedItems\SingleMemberAdd; |
|
57
|
|
|
use OCA\Circles\IFederatedUser; |
|
58
|
|
|
use OCA\Circles\Model\Federated\FederatedEvent; |
|
59
|
|
|
use OCA\Circles\Model\FederatedUser; |
|
60
|
|
|
use OCA\Circles\Model\Member; |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Class MemberService |
|
65
|
|
|
* |
|
66
|
|
|
* @package OCA\Circles\Service |
|
67
|
|
|
*/ |
|
68
|
|
|
class MemberService { |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
use TArrayTools; |
|
72
|
|
|
use TStringTools; |
|
73
|
|
|
use TNC22Logger; |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
/** @var CircleRequest */ |
|
77
|
|
|
private $circleRequest; |
|
78
|
|
|
|
|
79
|
|
|
/** @var MemberRequest */ |
|
80
|
|
|
private $memberRequest; |
|
81
|
|
|
|
|
82
|
|
|
/** @var FederatedUserService */ |
|
83
|
|
|
private $federatedUserService; |
|
84
|
|
|
|
|
85
|
|
|
/** @var FederatedEventService */ |
|
86
|
|
|
private $federatedEventService; |
|
87
|
|
|
|
|
88
|
|
|
/** @var RemoteStreamService */ |
|
89
|
|
|
private $remoteStreamService; |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* MemberService constructor. |
|
94
|
|
|
* |
|
95
|
|
|
* @param CircleRequest $circleRequest |
|
96
|
|
|
* @param MemberRequest $memberRequest |
|
97
|
|
|
* @param FederatedUserService $federatedUserService |
|
98
|
|
|
* @param FederatedEventService $federatedEventService |
|
99
|
|
|
* @param RemoteStreamService $remoteStreamService |
|
100
|
|
|
*/ |
|
101
|
|
|
public function __construct( |
|
102
|
|
|
CircleRequest $circleRequest, MemberRequest $memberRequest, |
|
103
|
|
|
FederatedUserService $federatedUserService, FederatedEventService $federatedEventService, |
|
104
|
|
|
RemoteStreamService $remoteStreamService |
|
105
|
|
|
) { |
|
106
|
|
|
$this->circleRequest = $circleRequest; |
|
107
|
|
|
$this->memberRequest = $memberRequest; |
|
108
|
|
|
$this->federatedUserService = $federatedUserService; |
|
109
|
|
|
$this->federatedEventService = $federatedEventService; |
|
110
|
|
|
$this->remoteStreamService = $remoteStreamService; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
// |
|
114
|
|
|
// /** |
|
115
|
|
|
// * @param Member $member |
|
116
|
|
|
// * |
|
117
|
|
|
// * @throws MemberAlreadyExistsException |
|
118
|
|
|
// */ |
|
119
|
|
|
// public function saveMember(Member $member) { |
|
120
|
|
|
// $member->setId($this->token(Member::ID_LENGTH)); |
|
121
|
|
|
// $this->memberRequest->save($member); |
|
122
|
|
|
// } |
|
123
|
|
|
// |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param string $memberId |
|
128
|
|
|
* @param string $circleId |
|
129
|
|
|
* |
|
130
|
|
|
* @return Member |
|
131
|
|
|
* @throws InitiatorNotFoundException |
|
132
|
|
|
* @throws MemberNotFoundException |
|
133
|
|
|
* @throws RequestBuilderException |
|
134
|
|
|
*/ |
|
135
|
|
|
public function getMemberById(string $memberId, string $circleId = ''): Member { |
|
136
|
|
|
$this->federatedUserService->mustHaveCurrentUser(); |
|
137
|
|
|
|
|
138
|
|
|
$member = |
|
139
|
|
|
$this->memberRequest->getMemberById($memberId, $this->federatedUserService->getCurrentUser()); |
|
140
|
|
|
if ($circleId !== '' && $member->getCircle()->getSingleId() !== $circleId) { |
|
141
|
|
|
throw new MemberNotFoundException(); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
// TODO: useless ? |
|
145
|
|
|
// $circle = $this->circleRequest->getCircle( |
|
146
|
|
|
// $member->getCircleId(), $this->federatedUserService->getCurrentUser() |
|
147
|
|
|
// ); |
|
148
|
|
|
|
|
149
|
|
|
// if (!$circle->getInitiator()->isMember()) { |
|
150
|
|
|
// throw new MemberLevelException(); |
|
151
|
|
|
// } |
|
152
|
|
|
|
|
153
|
|
|
return $member; |
|
154
|
|
|
// } catch (Exception $e) { |
|
155
|
|
|
// $this->e($e, ['id' => $memberId, 'initiator' => $this->federatedUserService->getCurrentUser()]); |
|
156
|
|
|
// throw new MemberLevelException('insufficient rights'); |
|
157
|
|
|
// } |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @param string $circleId |
|
163
|
|
|
* |
|
164
|
|
|
* @return Member[] |
|
165
|
|
|
* @throws InitiatorNotFoundException |
|
166
|
|
|
* @throws RequestBuilderException |
|
167
|
|
|
*/ |
|
168
|
|
|
public function getMembers(string $circleId): array { |
|
169
|
|
|
$this->federatedUserService->mustHaveCurrentUser(); |
|
170
|
|
|
|
|
171
|
|
|
return $this->memberRequest->getMembers( |
|
172
|
|
|
$circleId, |
|
173
|
|
|
$this->federatedUserService->getCurrentUser(), |
|
174
|
|
|
$this->federatedUserService->getRemoteInstance() |
|
175
|
|
|
); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param string $circleId |
|
181
|
|
|
* @param IFederatedUser $federatedUser |
|
182
|
|
|
* |
|
183
|
|
|
* @return array |
|
184
|
|
|
* @throws CircleNotFoundException |
|
185
|
|
|
* @throws FederatedEventException |
|
186
|
|
|
* @throws FederatedItemException |
|
187
|
|
|
* @throws InitiatorNotConfirmedException |
|
188
|
|
|
* @throws InitiatorNotFoundException |
|
189
|
|
|
* @throws OwnerNotFoundException |
|
190
|
|
|
* @throws RemoteNotFoundException |
|
191
|
|
|
* @throws RemoteResourceNotFoundException |
|
192
|
|
|
* @throws UnknownRemoteException |
|
193
|
|
|
* @throws RemoteInstanceException |
|
194
|
|
|
* @throws RequestBuilderException |
|
195
|
|
|
*/ |
|
196
|
|
|
public function addMember(string $circleId, FederatedUser $federatedUser): array { |
|
197
|
|
|
$this->federatedUserService->mustHaveCurrentUser(); |
|
198
|
|
|
$circle = $this->circleRequest->getCircle($circleId, $this->federatedUserService->getCurrentUser()); |
|
199
|
|
|
|
|
200
|
|
|
$member = new Member(); |
|
201
|
|
|
$member->importFromIFederatedUser($federatedUser); |
|
202
|
|
|
|
|
203
|
|
|
$event = new FederatedEvent(SingleMemberAdd::class); |
|
204
|
|
|
$event->setSeverity(FederatedEvent::SEVERITY_HIGH); |
|
205
|
|
|
$event->setCircle($circle); |
|
206
|
|
|
$event->setMember($member); |
|
207
|
|
|
|
|
208
|
|
|
$this->federatedEventService->newEvent($event); |
|
209
|
|
|
|
|
210
|
|
|
return $event->getOutcome(); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* @param string $circleId |
|
216
|
|
|
* @param IFederatedUser[] $members |
|
|
|
|
|
|
217
|
|
|
* |
|
218
|
|
|
* @return FederatedUser[] |
|
219
|
|
|
* @throws CircleNotFoundException |
|
220
|
|
|
* @throws FederatedEventException |
|
221
|
|
|
* @throws FederatedItemException |
|
222
|
|
|
* @throws InitiatorNotConfirmedException |
|
223
|
|
|
* @throws InitiatorNotFoundException |
|
224
|
|
|
* @throws OwnerNotFoundException |
|
225
|
|
|
* @throws RemoteNotFoundException |
|
226
|
|
|
* @throws RemoteResourceNotFoundException |
|
227
|
|
|
* @throws UnknownRemoteException |
|
228
|
|
|
* @throws RemoteInstanceException |
|
229
|
|
|
* @throws RequestBuilderException |
|
230
|
|
|
*/ |
|
231
|
|
|
public function addMembers(string $circleId, array $federatedUsers): array { |
|
232
|
|
|
$this->federatedUserService->mustHaveCurrentUser(); |
|
233
|
|
|
$circle = $this->circleRequest->getCircle($circleId, $this->federatedUserService->getCurrentUser()); |
|
234
|
|
|
|
|
235
|
|
|
$members = array_map( |
|
236
|
|
|
function(FederatedUser $federatedUser) { |
|
237
|
|
|
$member = new Member(); |
|
238
|
|
|
$member->importFromIFederatedUser($federatedUser); |
|
239
|
|
|
|
|
240
|
|
|
return $member; |
|
241
|
|
|
}, $federatedUsers |
|
242
|
|
|
); |
|
243
|
|
|
|
|
244
|
|
|
$event = new FederatedEvent(MassiveMemberAdd::class); |
|
245
|
|
|
$event->setSeverity(FederatedEvent::SEVERITY_HIGH); |
|
246
|
|
|
$event->setCircle($circle); |
|
247
|
|
|
$event->setMembers($members); |
|
248
|
|
|
$event->setData(new SimpleDataStore(['federatedUsers' => $members])); |
|
249
|
|
|
|
|
250
|
|
|
$this->federatedEventService->newEvent($event); |
|
251
|
|
|
|
|
252
|
|
|
return $event->getOutcome(); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* @param string $memberId |
|
258
|
|
|
* |
|
259
|
|
|
* @return array |
|
260
|
|
|
* @throws FederatedEventException |
|
261
|
|
|
* @throws FederatedItemException |
|
262
|
|
|
* @throws InitiatorNotConfirmedException |
|
263
|
|
|
* @throws InitiatorNotFoundException |
|
264
|
|
|
* @throws MemberNotFoundException |
|
265
|
|
|
* @throws OwnerNotFoundException |
|
266
|
|
|
* @throws RemoteNotFoundException |
|
267
|
|
|
* @throws RemoteResourceNotFoundException |
|
268
|
|
|
* @throws UnknownRemoteException |
|
269
|
|
|
*/ |
|
270
|
|
|
public function removeMember(string $memberId): array { |
|
271
|
|
|
$this->federatedUserService->mustHaveCurrentUser(); |
|
272
|
|
|
$member = |
|
273
|
|
|
$this->memberRequest->getMemberById($memberId, $this->federatedUserService->getCurrentUser()); |
|
274
|
|
|
|
|
275
|
|
|
$event = new FederatedEvent(MemberRemove::class); |
|
276
|
|
|
$event->setCircle($member->getCircle()); |
|
277
|
|
|
$event->setMember($member); |
|
278
|
|
|
|
|
279
|
|
|
$this->federatedEventService->newEvent($event); |
|
280
|
|
|
|
|
281
|
|
|
return $event->getOutcome(); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* @param string $memberId |
|
286
|
|
|
* @param int $level |
|
287
|
|
|
* |
|
288
|
|
|
* @return array |
|
289
|
|
|
* @throws FederatedEventException |
|
290
|
|
|
* @throws InitiatorNotConfirmedException |
|
291
|
|
|
* @throws InitiatorNotFoundException |
|
292
|
|
|
* @throws MemberNotFoundException |
|
293
|
|
|
* @throws OwnerNotFoundException |
|
294
|
|
|
* @throws RemoteNotFoundException |
|
295
|
|
|
* @throws RemoteResourceNotFoundException |
|
296
|
|
|
* @throws UnknownRemoteException |
|
297
|
|
|
* @throws FederatedItemException |
|
298
|
|
|
*/ |
|
299
|
|
|
public function memberLevel(string $memberId, int $level): array { |
|
300
|
|
|
$this->federatedUserService->mustHaveCurrentUser(); |
|
301
|
|
|
$member = |
|
302
|
|
|
$this->memberRequest->getMemberById($memberId, $this->federatedUserService->getCurrentUser()); |
|
303
|
|
|
|
|
304
|
|
|
$event = new FederatedEvent(MemberLevel::class); |
|
305
|
|
|
$event->setCircle($member->getCircle()); |
|
306
|
|
|
$event->setMember($member); |
|
307
|
|
|
$event->setData(new SimpleDataStore(['level' => $level])); |
|
308
|
|
|
|
|
309
|
|
|
$this->federatedEventService->newEvent($event); |
|
310
|
|
|
|
|
311
|
|
|
return $event->getOutcome(); |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.