|
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 daita\MySmallPhpTools\Model\SimpleDataStore; |
|
34
|
|
|
use Exception; |
|
35
|
|
|
use OC\User\NoUserException; |
|
36
|
|
|
use OCA\Circles\Exceptions\CircleDoesNotExistException; |
|
37
|
|
|
use OCA\Circles\Exceptions\CircleTypeNotValidException; |
|
38
|
|
|
use OCA\Circles\Exceptions\ConfigNoCircleAvailableException; |
|
39
|
|
|
use OCA\Circles\Exceptions\EmailAccountInvalidFormatException; |
|
40
|
|
|
use OCA\Circles\Exceptions\GlobalScaleDSyncException; |
|
41
|
|
|
use OCA\Circles\Exceptions\GlobalScaleEventException; |
|
42
|
|
|
use OCA\Circles\Exceptions\MemberAlreadyExistsException; |
|
43
|
|
|
use OCA\Circles\Exceptions\MemberCantJoinCircleException; |
|
44
|
|
|
use OCA\Circles\Exceptions\MemberIsNotModeratorException; |
|
45
|
|
|
use OCA\Circles\Exceptions\MembersLimitException; |
|
46
|
|
|
use OCA\Circles\Exceptions\TokenDoesNotExistException; |
|
47
|
|
|
use OCA\Circles\Model\Circle; |
|
48
|
|
|
use OCA\Circles\Model\GlobalScale\GSEvent; |
|
49
|
|
|
use OCA\Circles\Model\Member; |
|
50
|
|
|
use OCA\Circles\Model\SharesToken; |
|
51
|
|
|
use OCP\IUser; |
|
52
|
|
|
use OCP\Mail\IEMailTemplate; |
|
53
|
|
|
use OCP\Util; |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Class MemberAdd |
|
58
|
|
|
* |
|
59
|
|
|
* @package OCA\Circles\GlobalScale |
|
60
|
|
|
*/ |
|
61
|
|
|
class MemberAdd extends AGlobalScaleEvent { |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param GSEvent $event |
|
66
|
|
|
* @param bool $localCheck |
|
67
|
|
|
* @param bool $mustBeChecked |
|
68
|
|
|
* |
|
69
|
|
|
* @throws CircleDoesNotExistException |
|
70
|
|
|
* @throws ConfigNoCircleAvailableException |
|
71
|
|
|
* @throws EmailAccountInvalidFormatException |
|
72
|
|
|
* @throws GlobalScaleDSyncException |
|
73
|
|
|
* @throws GlobalScaleEventException |
|
74
|
|
|
* @throws MemberAlreadyExistsException |
|
75
|
|
|
* @throws MemberCantJoinCircleException |
|
76
|
|
|
* @throws MembersLimitException |
|
77
|
|
|
* @throws NoUserException |
|
78
|
|
|
* @throws CircleTypeNotValidException |
|
79
|
|
|
* @throws MemberIsNotModeratorException |
|
80
|
|
|
*/ |
|
81
|
|
|
public function verify(GSEvent $event, bool $localCheck = false, bool $mustBeChecked = false): void { |
|
82
|
|
|
parent::verify($event, $localCheck, true); |
|
83
|
|
|
|
|
84
|
|
|
$eventMember = $event->getMember(); |
|
85
|
|
|
$this->cleanMember($eventMember); |
|
86
|
|
|
|
|
87
|
|
|
if ($eventMember->getInstance() === '') { |
|
88
|
|
|
$eventMember->setInstance($event->getSource()); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$ident = $eventMember->getUserId(); |
|
92
|
|
|
$this->membersService->verifyIdentBasedOnItsType( |
|
93
|
|
|
$ident, $eventMember->getType(), $eventMember->getInstance() |
|
94
|
|
|
); |
|
95
|
|
|
|
|
96
|
|
|
$circle = $event->getCircle(); |
|
97
|
|
|
|
|
98
|
|
|
if (!$event->isForced()) { |
|
99
|
|
|
$circle->getHigherViewer() |
|
100
|
|
|
->hasToBeModerator(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$member = $this->membersRequest->getFreshNewMember( |
|
104
|
|
|
$circle->getUniqueId(), $ident, $eventMember->getType(), $eventMember->getInstance() |
|
105
|
|
|
); |
|
106
|
|
|
$member->hasToBeInviteAble(); |
|
107
|
|
|
|
|
108
|
|
|
$this->circlesService->checkThatCircleIsNotFull($circle); |
|
109
|
|
|
$this->membersService->addMemberBasedOnItsType($circle, $member); |
|
110
|
|
|
|
|
111
|
|
|
$password = ''; |
|
112
|
|
|
if ($this->configService->enforcePasswordProtection($circle)) { |
|
113
|
|
|
$password = $this->miscService->token(15); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$event->setData(new SimpleDataStore(['password' => $password])); |
|
117
|
|
|
$event->setMember($member); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param GSEvent $event |
|
123
|
|
|
* |
|
124
|
|
|
* @throws MemberAlreadyExistsException |
|
125
|
|
|
*/ |
|
126
|
|
|
public function manage(GSEvent $event): void { |
|
127
|
|
|
$circle = $event->getCircle(); |
|
128
|
|
|
$member = $event->getMember(); |
|
129
|
|
|
if ($member->getJoined() === '') { |
|
130
|
|
|
$this->membersRequest->createMember($member); |
|
131
|
|
|
} else { |
|
132
|
|
|
$this->membersRequest->updateMemberLevel($member); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
$this->miscService->updateCachedName($member); |
|
136
|
|
|
$cachedName = $member->getCachedName(); |
|
137
|
|
|
|
|
138
|
|
|
$password = $event->getData() |
|
139
|
|
|
->g('password'); |
|
140
|
|
|
$shares = $this->generateUnknownSharesLinks($circle, $member, $password); |
|
141
|
|
|
|
|
142
|
|
|
$event->setResult( |
|
143
|
|
|
new SimpleDataStore( |
|
144
|
|
|
[ |
|
145
|
|
|
'unknownShares' => $shares, |
|
146
|
|
|
'cachedName' => $cachedName |
|
147
|
|
|
] |
|
148
|
|
|
) |
|
149
|
|
|
); |
|
150
|
|
|
$this->eventsService->onMemberNew($circle, $member); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @param GSEvent[] $events |
|
156
|
|
|
* |
|
157
|
|
|
* @throws Exception |
|
158
|
|
|
*/ |
|
159
|
|
|
public function result(array $events): void { |
|
160
|
|
|
$password = $cachedName = ''; |
|
161
|
|
|
$circle = $member = null; |
|
162
|
|
|
$links = []; |
|
163
|
|
|
foreach ($events as $event) { |
|
164
|
|
|
$password = $event->getData() |
|
165
|
|
|
->g('password'); |
|
166
|
|
|
|
|
167
|
|
|
$circle = $event->getCircle(); |
|
168
|
|
|
$member = $event->getMember(); |
|
169
|
|
|
$result = $event->getResult(); |
|
170
|
|
|
if ($result->g('cachedName') !== '') { |
|
171
|
|
|
$cachedName = $result->g('cachedName'); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
$links = array_merge($links, $result->gArray('unknownShares')); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
if ($circle === null || $member === null) { |
|
178
|
|
|
return; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
if ($cachedName !== '') { |
|
182
|
|
|
$member->setCachedName($cachedName); |
|
183
|
|
|
$this->membersService->updateMember($member); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
if ($member->getType() === Member::TYPE_MAIL |
|
187
|
|
|
|| $member->getType() === Member::TYPE_CONTACT) { |
|
188
|
|
|
$this->memberIsMailbox($circle, $member, $links, $password); |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @param Circle|null $circle |
|
|
|
|
|
|
195
|
|
|
* @param Member|null $member |
|
|
|
|
|
|
196
|
|
|
* @param array $links |
|
197
|
|
|
* @param string $password |
|
198
|
|
|
*/ |
|
199
|
|
|
private function memberIsMailbox(Circle $circle, Member $member, array $links, string $password) { |
|
200
|
|
View Code Duplication |
if ($circle->getViewer() === null) { |
|
|
|
|
|
|
201
|
|
|
$author = $circle->getOwner() |
|
202
|
|
|
->getUserId(); |
|
203
|
|
|
} else { |
|
204
|
|
|
$author = $circle->getViewer() |
|
205
|
|
|
->getUserId(); |
|
206
|
|
|
} |
|
207
|
|
|
$recipient = $member->getUserId(); |
|
208
|
|
|
|
|
209
|
|
|
try { |
|
210
|
|
|
$template = $this->generateMailExitingShares($author, $circle->getName()); |
|
211
|
|
|
$this->fillMailExistingShares($template, $links); |
|
212
|
|
|
$this->sendMailExistingShares($template, $author, $recipient); |
|
213
|
|
|
$this->sendPasswordExistingShares($author, $recipient, $password); |
|
214
|
|
|
} catch (Exception $e) { |
|
215
|
|
|
$this->miscService->log('Failed to send mail about existing share ' . $e->getMessage()); |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* @param Circle $circle |
|
222
|
|
|
* @param Member $member |
|
223
|
|
|
* @param string $password |
|
224
|
|
|
* |
|
225
|
|
|
* @return array |
|
226
|
|
|
*/ |
|
227
|
|
|
private function generateUnknownSharesLinks(Circle $circle, Member $member, string $password): array { |
|
|
|
|
|
|
228
|
|
|
$unknownShares = $this->getUnknownShares($member); |
|
229
|
|
|
|
|
230
|
|
|
$data = []; |
|
231
|
|
|
foreach ($unknownShares as $share) { |
|
232
|
|
|
try { |
|
233
|
|
|
$data[] = $this->getMailLinkFromShare($share, $member, $password); |
|
234
|
|
|
} catch (TokenDoesNotExistException $e) { |
|
|
|
|
|
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
return $data; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* @param Member $member |
|
244
|
|
|
* |
|
245
|
|
|
* @return array |
|
246
|
|
|
*/ |
|
247
|
|
|
private function getUnknownShares(Member $member): array { |
|
248
|
|
|
$allShares = $this->sharesRequest->getSharesForCircle($member->getCircleId()); |
|
249
|
|
|
$knownShares = array_map( |
|
250
|
|
|
function(SharesToken $shareToken) { |
|
251
|
|
|
return $shareToken->getShareId(); |
|
252
|
|
|
}, |
|
253
|
|
|
$this->tokensRequest->getTokensFromMember($member) |
|
254
|
|
|
); |
|
255
|
|
|
|
|
256
|
|
|
$unknownShares = []; |
|
257
|
|
|
foreach ($allShares as $share) { |
|
258
|
|
|
if (!in_array($share['id'], $knownShares)) { |
|
259
|
|
|
$unknownShares[] = $share; |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
return $unknownShares; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* @param array $share |
|
269
|
|
|
* @param Member $member |
|
270
|
|
|
* @param string $password |
|
271
|
|
|
* |
|
272
|
|
|
* @return array |
|
273
|
|
|
* @throws TokenDoesNotExistException |
|
274
|
|
|
*/ |
|
275
|
|
View Code Duplication |
private function getMailLinkFromShare(array $share, Member $member, string $password = '') { |
|
|
|
|
|
|
276
|
|
|
$sharesToken = $this->tokensRequest->generateTokenForMember($member, (int)$share['id'], $password); |
|
277
|
|
|
$link = $this->urlGenerator->linkToRouteAbsolute( |
|
278
|
|
|
'files_sharing.sharecontroller.showShare', |
|
279
|
|
|
['token' => $sharesToken->getToken()] |
|
280
|
|
|
); |
|
281
|
|
|
$author = $share['uid_initiator']; |
|
282
|
|
|
$filename = basename($share['file_target']); |
|
283
|
|
|
|
|
284
|
|
|
return [ |
|
285
|
|
|
'author' => $author, |
|
286
|
|
|
'link' => $link, |
|
287
|
|
|
'filename' => $filename |
|
288
|
|
|
]; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* @param string $author |
|
294
|
|
|
* @param string $circleName |
|
295
|
|
|
* |
|
296
|
|
|
* @return IEMailTemplate |
|
297
|
|
|
*/ |
|
298
|
|
|
private function generateMailExitingShares(string $author, string $circleName): IEMailTemplate { |
|
299
|
|
|
$emailTemplate = $this->mailer->createEMailTemplate('circles.ExistingShareNotification', []); |
|
300
|
|
|
$emailTemplate->addHeader(); |
|
301
|
|
|
|
|
302
|
|
|
$text = $this->l10n->t('%s shared multiple files with \'%s\'.', [$author, $circleName]); |
|
303
|
|
|
$emailTemplate->addBodyText(htmlspecialchars($text), $text); |
|
304
|
|
|
|
|
305
|
|
|
return $emailTemplate; |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* @param IEMailTemplate $emailTemplate |
|
310
|
|
|
* @param array $links |
|
311
|
|
|
*/ |
|
312
|
|
View Code Duplication |
private function fillMailExistingShares(IEMailTemplate $emailTemplate, array $links) { |
|
|
|
|
|
|
313
|
|
|
foreach ($links as $item) { |
|
314
|
|
|
$emailTemplate->addBodyButton( |
|
315
|
|
|
$this->l10n->t('Open »%s«', [htmlspecialchars($item['filename'])]), $item['link'] |
|
316
|
|
|
); |
|
317
|
|
|
} |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* @param IEMailTemplate $emailTemplate |
|
323
|
|
|
* @param string $author |
|
324
|
|
|
* @param string $recipient |
|
325
|
|
|
* |
|
326
|
|
|
* @throws Exception |
|
327
|
|
|
*/ |
|
328
|
|
View Code Duplication |
private function sendMailExistingShares(IEMailTemplate $emailTemplate, string $author, string $recipient |
|
|
|
|
|
|
329
|
|
|
) { |
|
330
|
|
|
$subject = $this->l10n->t('%s shared multiple files with you.', [$author]); |
|
331
|
|
|
|
|
332
|
|
|
$instanceName = $this->defaults->getName(); |
|
333
|
|
|
$senderName = $this->l10n->t('%s on %s', [$author, $instanceName]); |
|
334
|
|
|
|
|
335
|
|
|
$message = $this->mailer->createMessage(); |
|
336
|
|
|
|
|
337
|
|
|
$message->setFrom([Util::getDefaultEmailAddress($instanceName) => $senderName]); |
|
338
|
|
|
$message->setSubject($subject); |
|
339
|
|
|
$message->setPlainBody($emailTemplate->renderText()); |
|
340
|
|
|
$message->setHtmlBody($emailTemplate->renderHtml()); |
|
341
|
|
|
$message->setTo([$recipient]); |
|
342
|
|
|
|
|
343
|
|
|
$this->mailer->send($message); |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
|
|
347
|
|
|
/** |
|
348
|
|
|
* @param string $author |
|
349
|
|
|
* @param string $email |
|
350
|
|
|
* @param string $password |
|
351
|
|
|
* |
|
352
|
|
|
* @throws Exception |
|
353
|
|
|
*/ |
|
354
|
|
View Code Duplication |
protected function sendPasswordExistingShares(string $author, string $email, string $password) { |
|
|
|
|
|
|
355
|
|
|
if ($password === '') { |
|
356
|
|
|
return; |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
$message = $this->mailer->createMessage(); |
|
360
|
|
|
|
|
361
|
|
|
$authorUser = $this->userManager->get($author); |
|
362
|
|
|
$authorName = ($authorUser instanceof IUser) ? $authorUser->getDisplayName() : $author; |
|
|
|
|
|
|
363
|
|
|
$authorEmail = ($authorUser instanceof IUser) ? $authorUser->getEMailAddress() : null; |
|
|
|
|
|
|
364
|
|
|
|
|
365
|
|
|
$this->miscService->log("Sending password mail about existing files to '" . $email . "'", 0); |
|
366
|
|
|
|
|
367
|
|
|
$plainBodyPart = $this->l10n->t( |
|
368
|
|
|
"%1\$s shared multiple files with you.\nYou should have already received a separate mail with a link to access them.\n", |
|
369
|
|
|
[$authorName] |
|
370
|
|
|
); |
|
371
|
|
|
$htmlBodyPart = $this->l10n->t( |
|
372
|
|
|
'%1$s shared multiple files with you. You should have already received a separate mail with a link to access them.', |
|
373
|
|
|
[$authorName] |
|
374
|
|
|
); |
|
375
|
|
|
|
|
376
|
|
|
$emailTemplate = $this->mailer->createEMailTemplate( |
|
377
|
|
|
'sharebymail.RecipientPasswordNotification', [ |
|
378
|
|
|
'password' => $password, |
|
379
|
|
|
'author' => $author |
|
380
|
|
|
] |
|
381
|
|
|
); |
|
382
|
|
|
|
|
383
|
|
|
$emailTemplate->setSubject( |
|
384
|
|
|
$this->l10n->t( |
|
385
|
|
|
'Password to access files shared to you by %1$s', [$authorName] |
|
386
|
|
|
) |
|
387
|
|
|
); |
|
388
|
|
|
$emailTemplate->addHeader(); |
|
389
|
|
|
$emailTemplate->addHeading($this->l10n->t('Password to access files'), false); |
|
390
|
|
|
$emailTemplate->addBodyText(htmlspecialchars($htmlBodyPart), $plainBodyPart); |
|
391
|
|
|
$emailTemplate->addBodyText($this->l10n->t('It is protected with the following password:')); |
|
392
|
|
|
$emailTemplate->addBodyText($password); |
|
393
|
|
|
|
|
394
|
|
|
// The "From" contains the sharers name |
|
395
|
|
|
$instanceName = $this->defaults->getName(); |
|
396
|
|
|
$senderName = $this->l10n->t( |
|
397
|
|
|
'%1$s via %2$s', |
|
398
|
|
|
[ |
|
399
|
|
|
$authorName, |
|
400
|
|
|
$instanceName |
|
401
|
|
|
] |
|
402
|
|
|
); |
|
403
|
|
|
|
|
404
|
|
|
$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]); |
|
405
|
|
|
if ($authorEmail !== null) { |
|
406
|
|
|
$message->setReplyTo([$authorEmail => $authorName]); |
|
407
|
|
|
$emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan()); |
|
408
|
|
|
} else { |
|
409
|
|
|
$emailTemplate->addFooter(); |
|
410
|
|
|
} |
|
411
|
|
|
|
|
412
|
|
|
$message->setTo([$email]); |
|
413
|
|
|
$message->useTemplate($emailTemplate); |
|
414
|
|
|
$this->mailer->send($message); |
|
415
|
|
|
} |
|
416
|
|
|
|
|
417
|
|
|
} |
|
418
|
|
|
|