Completed
Pull Request — master (#551)
by Maxence
01:55
created

MemberAdd::getMailLinkFromShare()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 3
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\FederatedItems;
33
34
35
use daita\MySmallPhpTools\Traits\TStringTools;
36
use Exception;
37
use OC\User\NoUserException;
38
use OCA\Circles\Db\MemberRequest;
39
use OCA\Circles\Exceptions\MemberAlreadyExistsException;
40
use OCA\Circles\Exceptions\MemberLevelException;
41
use OCA\Circles\Exceptions\MemberNotFoundException;
42
use OCA\Circles\Exceptions\MembersLimitException;
43
use OCA\Circles\Exceptions\MemberTypeNotFoundException;
44
use OCA\Circles\Exceptions\TokenDoesNotExistException;
45
use OCA\Circles\Exceptions\UserTypeNotFoundException;
46
use OCA\Circles\IFederatedItem;
47
use OCA\Circles\IFederatedItemAsync;
48
use OCA\Circles\IFederatedItemMemberCheckNotRequired;
49
use OCA\Circles\IFederatedItemMemberRequired;
50
use OCA\Circles\IFederatedUser;
51
use OCA\Circles\Model\DeprecatedCircle;
52
use OCA\Circles\Model\DeprecatedMember;
53
use OCA\Circles\Model\Federated\FederatedEvent;
54
use OCA\Circles\Model\Helpers\MemberHelper;
55
use OCA\Circles\Model\ManagedModel;
56
use OCA\Circles\Model\Member;
57
use OCA\Circles\Model\SharesToken;
58
use OCA\Circles\Service\CircleService;
59
use OCA\Circles\Service\ConfigService;
60
use OCP\IUser;
61
use OCP\IUserManager;
62
use OCP\Mail\IEMailTemplate;
63
use OCP\Util;
64
65
66
/**
67
 * Class MemberAdd
68
 *
69
 * @package OCA\Circles\GlobalScale
70
 */
71
class MemberAdd implements
72
	IFederatedItem,
73
	IFederatedItemAsync,
74
	IFederatedItemMemberRequired,
75
	IFederatedItemMemberCheckNotRequired {
76
77
78
	use TStringTools;
79
80
81
	/** @var IUserManager */
82
	private $userManager;
83
84
	/** @var MemberRequest */
85
	private $memberRequest;
86
87
	/** @var CircleService */
88
	private $circleService;
89
90
	/** @var ConfigService */
91
	private $configService;
92
93
94
	/**
95
	 * MemberAdd constructor.
96
	 *
97
	 * @param IUserManager $userManager
98
	 * @param MemberRequest $memberRequest
99
	 * @param CircleService $circleService
100
	 * @param ConfigService $configService
101
	 */
102
	public function __construct(
103
		IUserManager $userManager, MemberRequest $memberRequest, CircleService $circleService,
104
		ConfigService $configService
105
	) {
106
		$this->userManager = $userManager;
107
		$this->memberRequest = $memberRequest;
108
		$this->circleService = $circleService;
109
		$this->configService = $configService;
110
	}
111
112
113
	/**
114
	 * @param FederatedEvent $event
115
	 *
116
	 * @throws MemberLevelException
117
	 * @throws NoUserException
118
	 * @throws UserTypeNotFoundException
119
	 * @throws MembersLimitException
120
	 * @throws MemberAlreadyExistsException
121
	 */
122
	public function verify(FederatedEvent $event): void {
123
		$member = $event->getMember();
124
		$circle = $event->getCircle();
125
		$initiator = $circle->getInitiator();
126
127
		$member->setCircleId($circle->getId());
128
129
		$initiatorHelper = new MemberHelper($initiator);
130
		$initiatorHelper->mustBeModerator();
131
132
		try {
133
			$this->memberRequest->searchMember($member);
134
			// TODO: maybe member is requesting access
135
			throw new MemberAlreadyExistsException('Member already exists');
136
		} catch (MemberNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
137
		}
138
139
		$this->confirmMemberFormat($member);
140
		$member->setId($this->uuid(ManagedModel::ID_LENGTH));
141
142
		// TODO: check Config on Circle to know if we set Level to 1 or just send an invitation
143
		$member->setLevel(Member::LEVEL_MEMBER);
144
		$member->setStatus(Member::STATUS_MEMBER);
145
		$event->setDataOutcome(['member' => $member]);
146
147
		// TODO: Managing cached name
148
		//		$member->setCachedName($eventMember->getCachedName());
149
		$this->circleService->confirmCircleNotFull($circle);
150
151
		$event->setReadingOutcome('Member %s have been added to Circle', ['userId' => $member->getUserId()]);
152
153
		return;
154
155
156
//		$member = $this->membersRequest->getFreshNewMember(
157
//			$circle->getUniqueId(), $ident, $eventMember->getType(), $eventMember->getInstance()
158
//		);
159
//		$member->hasToBeInviteAble();
160
//
161
//		$this->membersService->addMemberBasedOnItsType($circle, $member);
162
//
163
//		$password = '';
164
//		$sendPasswordByMail = false;
165
//		if ($this->configService->enforcePasswordProtection($circle)) {
166
//			if ($circle->getSetting('password_single_enabled') === 'true') {
167
//				$password = $circle->getPasswordSingle();
168
//			} else {
169
//				$sendPasswordByMail = true;
170
//				$password = $this->miscService->token(15);
171
//			}
172
//		}
173
//
174
//		$event->setData(
175
//			new SimpleDataStore(
176
//				[
177
//					'password'       => $password,
178
//					'passwordByMail' => $sendPasswordByMail
179
//				]
180
//			)
181
//		);
182
	}
183
184
185
	/**
186
	 * @param FederatedEvent $event
187
	 *
188
	 */
189
	public function manage(FederatedEvent $event): void {
190
		//$circle = $event->getCircle();
191
		$member = $event->getMember();
192
//		if ($member->getJoined() === '') {
193
//			$this->membersRequest->createMember($member);
194
//		} else {
195
//			$this->membersRequest->updateMemberLevel($member);
196
//		}
197
//
198
199
		// TODO: confirm MemberId is unique
200
		$this->memberRequest->save($member);
201
202
//
203
//		//
204
//		// TODO: verifiez comment se passe le cached name sur un member_add
205
//		//
206
//		$cachedName = $member->getCachedName();
207
//		$password = $event->getData()
208
//						  ->g('password');
209
//
210
//		$shares = $this->generateUnknownSharesLinks($circle, $member, $password);
211
//		$result = [
212
//			'unknownShares' => $shares,
213
//			'cachedName'    => $cachedName
214
//		];
215
//
216
//		if ($member->getType() === DeprecatedMember::TYPE_CONTACT
217
//			&& $this->configService->isLocalInstance($member->getInstance())) {
218
//			$result['contact'] = $this->miscService->getInfosFromContact($member);
219
//		}
220
//
221
//		$event->setResult(new SimpleDataStore($result));
222
//		$this->eventsService->onMemberNew($circle, $member);
223
	}
224
225
226
	/**
227
	 * @param FederatedEvent[] $events
228
	 *
229
	 * @throws Exception
230
	 */
231
	public function result(array $events): void {
232
//		$password = $cachedName = '';
233
//		$circle = $member = null;
234
//		$links = [];
235
//		$recipients = [];
236
//		foreach ($events as $event) {
237
//			$data = $event->getData();
238
//			if ($data->gBool('passwordByMail') !== false) {
239
//				$password = $data->g('password');
240
//			}
241
//			$circle = $event->getDeprecatedCircle();
242
//			$member = $event->getMember();
243
//			$result = $event->getResult();
244
//			if ($result->g('cachedName') !== '') {
245
//				$cachedName = $result->g('cachedName');
246
//			}
247
//
248
//			$links = array_merge($links, $result->gArray('unknownShares'));
249
//			$contact = $result->gArray('contact');
250
//			if (!empty($contact)) {
251
//				$recipients = $contact['emails'];
252
//			}
253
//		}
254
//
255
//		if (empty($links) || $circle === null || $member === null) {
256
//			return;
257
//		}
258
//
259
//		if ($cachedName !== '') {
260
//			$member->setCachedName($cachedName);
261
//			$this->membersService->updateMember($member);
262
//		}
263
//
264
//		if ($member->getType() === DeprecatedMember::TYPE_MAIL
265
//			|| $member->getType() === DeprecatedMember::TYPE_CONTACT) {
266
//			if ($member->getType() === DeprecatedMember::TYPE_MAIL) {
267
//				$recipients = [$member->getUserId()];
268
//			}
269
//
270
//			foreach ($recipients as $recipient) {
271
//				$this->memberIsMailbox($circle, $recipient, $links, $password);
272
//			}
273
//		}
274
	}
275
276
277
	/**
278
	 * confirm the format of UserId, based on UserType.
279
	 *
280
	 * @param IFederatedUser $member
281
	 *
282
	 * @throws UserTypeNotFoundException
283
	 * @throws NoUserException
284
	 */
285
	private function confirmMemberFormat(IFederatedUser $member): void {
286
		switch ($member->getUserType()) {
287
			case Member::TYPE_USER:
288
				$this->confirmMemberTypeUser($member);
289
				break;
290
291
			// TODO: confirm other UserType
292
			default:
293
				throw new UserTypeNotFoundException();
294
		}
295
	}
296
297
298
	/**
299
	 * @param IFederatedUser $member
300
	 *
301
	 * @throws NoUserException
302
	 */
303
	private function confirmMemberTypeUser(IFederatedUser $member): void {
304
		if ($this->configService->isLocalInstance($member->getInstance())) {
305
			$user = $this->userManager->get($member->getUserId());
306
			if ($user === null) {
307
				throw new NoUserException('user not found');
308
			}
309
310
			$member->setUserId($user->getUID());
311
312
			return;
313
		}
314
315
		// TODO #M002: request the remote instance and check that user exists
316
	}
317
318
//	/**
319
//	 * Verify if a local account is valid.
320
//	 *
321
//	 * @param $ident
322
//	 * @param $type
323
//	 *
324
//	 * @param string $instance
325
//	 *
326
//	 * @throws NoUserException
327
//	 */
328
//	private function verifyIdentLocalMember(&$ident, $type, string $instance = '') {
329
//		if ($type !== DeprecatedMember::TYPE_USER) {
330
//			return;
331
//		}
332
//
333
//		if ($instance === '') {
334
//			try {
335
//				$ident = $this->miscService->getRealUserId($ident);
336
//			} catch (NoUserException $e) {
337
//				throw new NoUserException($this->l10n->t("This user does not exist"));
338
//			}
339
//		}
340
//	}
341
//
342
//
343
//	/**
344
//	 * Verify if a mail have a valid format.
345
//	 *
346
//	 * @param string $ident
347
//	 * @param int $type
348
//	 *
349
//	 * @throws EmailAccountInvalidFormatException
350
//	 */
351
//	private function verifyIdentEmailAddress(string $ident, int $type) {
352
//		if ($type !== DeprecatedMember::TYPE_MAIL) {
353
//			return;
354
//		}
355
//
356
//		if ($this->configService->isAccountOnly()) {
357
//			throw new EmailAccountInvalidFormatException(
358
//				$this->l10n->t('You cannot add a mail address as member of your Circle')
359
//			);
360
//		}
361
//
362
//		if (!filter_var($ident, FILTER_VALIDATE_EMAIL)) {
363
//			throw new EmailAccountInvalidFormatException(
364
//				$this->l10n->t('Email format is not valid')
365
//			);
366
//		}
367
//	}
368
//
369
//
370
//	/**
371
//	 * Verify if a contact exist in current user address books.
372
//	 *
373
//	 * @param $ident
374
//	 * @param $type
375
//	 *
376
//	 * @throws NoUserException
377
//	 * @throws EmailAccountInvalidFormatException
378
//	 */
379
//	private function verifyIdentContact(&$ident, $type) {
380
//		if ($type !== DeprecatedMember::TYPE_CONTACT) {
381
//			return;
382
//		}
383
//
384
//		if ($this->configService->isAccountOnly()) {
385
//			throw new EmailAccountInvalidFormatException(
386
//				$this->l10n->t('You cannot add a contact as member of your Circle')
387
//			);
388
//		}
389
//
390
//		$tmpContact = $this->userId . ':' . $ident;
391
//		$result = MiscService::getContactData($tmpContact);
392
//		if (empty($result)) {
393
//			throw new NoUserException($this->l10n->t("This contact is not available"));
394
//		}
395
//
396
//		$ident = $tmpContact;
397
//	}
398
399
400
	/**
401
	 * @param DeprecatedCircle $circle
402
	 * @param string $recipient
403
	 * @param array $links
404
	 * @param string $password
405
	 */
406 View Code Duplication
	private function memberIsMailbox(
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
407
		DeprecatedCircle $circle, string $recipient, array $links, string $password
408
	) {
409
		if ($circle->getViewer() === null) {
410
			$author = $circle->getOwner()
411
							 ->getUserId();
412
		} else {
413
			$author = $circle->getViewer()
414
							 ->getUserId();
415
		}
416
417
		try {
418
			$template = $this->generateMailExitingShares($author, $circle->getName());
419
			$this->fillMailExistingShares($template, $links);
420
			$this->sendMailExistingShares($template, $author, $recipient);
421
			$this->sendPasswordExistingShares($author, $recipient, $password);
422
		} catch (Exception $e) {
423
			$this->miscService->log('Failed to send mail about existing share ' . $e->getMessage());
0 ignored issues
show
Bug introduced by
The property miscService does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
424
		}
425
	}
426
427
428
	/**
429
	 * @param DeprecatedCircle $circle
430
	 * @param DeprecatedMember $member
431
	 * @param string $password
432
	 *
433
	 * @return array
434
	 */
435 View Code Duplication
	private function generateUnknownSharesLinks(
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
436
		DeprecatedCircle $circle, DeprecatedMember $member, string $password
0 ignored issues
show
Unused Code introduced by
The parameter $circle is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
437
	): array {
438
		$unknownShares = $this->getUnknownShares($member);
439
440
		$data = [];
441
		foreach ($unknownShares as $share) {
442
			try {
443
				$data[] = $this->getMailLinkFromShare($share, $member, $password);
444
			} catch (TokenDoesNotExistException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
445
			}
446
		}
447
448
		return $data;
449
	}
450
451
452
	/**
453
	 * @param DeprecatedMember $member
454
	 *
455
	 * @return array
456
	 */
457 View Code Duplication
	private function getUnknownShares(DeprecatedMember $member): array {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
458
		$allShares = $this->sharesRequest->getSharesForCircle($member->getCircleId());
0 ignored issues
show
Bug introduced by
The property sharesRequest does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
459
		$knownShares = array_map(
460
			function(SharesToken $shareToken) {
461
				return $shareToken->getShareId();
462
			},
463
			$this->tokensRequest->getTokensFromMember($member)
0 ignored issues
show
Bug introduced by
The property tokensRequest does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
464
		);
465
466
		$unknownShares = [];
467
		foreach ($allShares as $share) {
468
			if (!in_array($share['id'], $knownShares)) {
469
				$unknownShares[] = $share;
470
			}
471
		}
472
473
		return $unknownShares;
474
	}
475
476
477
	/**
478
	 * @param array $share
479
	 * @param DeprecatedMember $member
480
	 * @param string $password
481
	 *
482
	 * @return array
483
	 * @throws TokenDoesNotExistException
484
	 */
485 View Code Duplication
	private function getMailLinkFromShare(array $share, DeprecatedMember $member, string $password = '') {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
486
		$sharesToken = $this->tokensRequest->generateTokenForMember($member, (int)$share['id'], $password);
487
		$link = $this->urlGenerator->linkToRouteAbsolute(
0 ignored issues
show
Bug introduced by
The property urlGenerator does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
488
			'files_sharing.sharecontroller.showShare',
489
			['token' => $sharesToken->getToken()]
490
		);
491
		$author = $share['uid_initiator'];
492
		$filename = basename($share['file_target']);
493
494
		return [
495
			'author'   => $author,
496
			'link'     => $link,
497
			'filename' => $filename
498
		];
499
	}
500
501
502
	/**
503
	 * @param string $author
504
	 * @param string $circleName
505
	 *
506
	 * @return IEMailTemplate
507
	 */
508 View Code Duplication
	private function generateMailExitingShares(string $author, string $circleName): IEMailTemplate {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
509
		$emailTemplate = $this->mailer->createEMailTemplate('circles.ExistingShareNotification', []);
0 ignored issues
show
Bug introduced by
The property mailer does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
510
		$emailTemplate->addHeader();
511
512
		$text = $this->l10n->t('%s shared multiple files with \'%s\'.', [$author, $circleName]);
0 ignored issues
show
Bug introduced by
The property l10n does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
513
		$emailTemplate->addBodyText(htmlspecialchars($text), $text);
514
515
		return $emailTemplate;
516
	}
517
518
	/**
519
	 * @param IEMailTemplate $emailTemplate
520
	 * @param array $links
521
	 */
522 View Code Duplication
	private function fillMailExistingShares(IEMailTemplate $emailTemplate, array $links) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
523
		foreach ($links as $item) {
524
			$emailTemplate->addBodyButton(
525
				$this->l10n->t('Open »%s«', [htmlspecialchars($item['filename'])]), $item['link']
526
			);
527
		}
528
	}
529
530
531
	/**
532
	 * @param IEMailTemplate $emailTemplate
533
	 * @param string $author
534
	 * @param string $recipient
535
	 *
536
	 * @throws Exception
537
	 */
538 View Code Duplication
	private function sendMailExistingShares(IEMailTemplate $emailTemplate, string $author, string $recipient
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
539
	) {
540
		$subject = $this->l10n->t('%s shared multiple files with you.', [$author]);
541
542
		$instanceName = $this->defaults->getName();
0 ignored issues
show
Bug introduced by
The property defaults does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
543
		$senderName = $this->l10n->t('%s on %s', [$author, $instanceName]);
544
545
		$message = $this->mailer->createMessage();
546
547
		$message->setFrom([Util::getDefaultEmailAddress($instanceName) => $senderName]);
548
		$message->setSubject($subject);
549
		$message->setPlainBody($emailTemplate->renderText());
550
		$message->setHtmlBody($emailTemplate->renderHtml());
551
		$message->setTo([$recipient]);
552
553
		$this->mailer->send($message);
554
	}
555
556
557
	/**
558
	 * @param string $author
559
	 * @param string $email
560
	 * @param string $password
561
	 *
562
	 * @throws Exception
563
	 */
564
	protected function sendPasswordExistingShares(string $author, string $email, string $password) {
565
		if ($password === '') {
566
			return;
567
		}
568
569
		$message = $this->mailer->createMessage();
570
571
		$authorUser = $this->userManager->get($author);
572
		$authorName = ($authorUser instanceof IUser) ? $authorUser->getDisplayName() : $author;
0 ignored issues
show
Bug introduced by
The class OCP\IUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
573
		$authorEmail = ($authorUser instanceof IUser) ? $authorUser->getEMailAddress() : null;
0 ignored issues
show
Bug introduced by
The class OCP\IUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
574
575
		$this->miscService->log("Sending password mail about existing files to '" . $email . "'", 0);
576
577
		$plainBodyPart = $this->l10n->t(
578
			"%1\$s shared multiple files with you.\nYou should have already received a separate mail with a link to access them.\n",
579
			[$authorName]
580
		);
581
		$htmlBodyPart = $this->l10n->t(
582
			'%1$s shared multiple files with you. You should have already received a separate mail with a link to access them.',
583
			[$authorName]
584
		);
585
586
		$emailTemplate = $this->mailer->createEMailTemplate(
587
			'sharebymail.RecipientPasswordNotification', [
588
														   'password' => $password,
589
														   'author'   => $author
590
													   ]
591
		);
592
593
		$emailTemplate->setSubject(
594
			$this->l10n->t(
595
				'Password to access files shared to you by %1$s', [$authorName]
596
			)
597
		);
598
		$emailTemplate->addHeader();
599
		$emailTemplate->addHeading($this->l10n->t('Password to access files'), false);
600
		$emailTemplate->addBodyText(htmlspecialchars($htmlBodyPart), $plainBodyPart);
601
		$emailTemplate->addBodyText($this->l10n->t('It is protected with the following password:'));
602
		$emailTemplate->addBodyText($password);
603
604
		// The "From" contains the sharers name
605
		$instanceName = $this->defaults->getName();
606
		$senderName = $this->l10n->t(
607
			'%1$s via %2$s',
608
			[
609
				$authorName,
610
				$instanceName
611
			]
612
		);
613
614
		$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
615
		if ($authorEmail !== null) {
616
			$message->setReplyTo([$authorEmail => $authorName]);
617
			$emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
618
		} else {
619
			$emailTemplate->addFooter();
620
		}
621
622
		$message->setTo([$email]);
623
		$message->useTemplate($emailTemplate);
624
		$this->mailer->send($message);
625
	}
626
627
}
628