Completed
Pull Request — master (#551)
by Maxence
02:15
created

SingleMemberAdd::insertOrUpdate()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 2
nc 4
nop 1
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\Exceptions\InvalidItemException;
36
use daita\MySmallPhpTools\Exceptions\RequestNetworkException;
37
use daita\MySmallPhpTools\Exceptions\SignatoryException;
38
use daita\MySmallPhpTools\Traits\Nextcloud\nc22\TNC22Logger;
39
use daita\MySmallPhpTools\Traits\TStringTools;
40
use Exception;
41
use OC\User\NoUserException;
42
use OCA\Circles\Db\MemberRequest;
43
use OCA\Circles\Exceptions\CircleNotFoundException;
44
use OCA\Circles\Exceptions\FederatedItemBadRequestException;
45
use OCA\Circles\Exceptions\FederatedItemException;
46
use OCA\Circles\Exceptions\FederatedItemNotFoundException;
47
use OCA\Circles\Exceptions\FederatedItemRemoteException;
48
use OCA\Circles\Exceptions\FederatedItemServerException;
49
use OCA\Circles\Exceptions\FederatedUserException;
50
use OCA\Circles\Exceptions\FederatedUserNotFoundException;
51
use OCA\Circles\Exceptions\InvalidIdException;
52
use OCA\Circles\Exceptions\MemberNotFoundException;
53
use OCA\Circles\Exceptions\MembersLimitException;
54
use OCA\Circles\Exceptions\OwnerNotFoundException;
55
use OCA\Circles\Exceptions\RemoteInstanceException;
56
use OCA\Circles\Exceptions\RemoteNotFoundException;
57
use OCA\Circles\Exceptions\RemoteResourceNotFoundException;
58
use OCA\Circles\Exceptions\RequestBuilderException;
59
use OCA\Circles\Exceptions\SingleCircleNotFoundException;
60
use OCA\Circles\Exceptions\TokenDoesNotExistException;
61
use OCA\Circles\Exceptions\UnknownRemoteException;
62
use OCA\Circles\Exceptions\UserTypeNotFoundException;
63
use OCA\Circles\IFederatedItem;
64
use OCA\Circles\IFederatedItemAsyncProcess;
65
use OCA\Circles\IFederatedItemMemberCheckNotRequired;
66
use OCA\Circles\IFederatedItemMemberRequired;
67
use OCA\Circles\IFederatedUser;
68
use OCA\Circles\Model\Circle;
69
use OCA\Circles\Model\DeprecatedCircle;
70
use OCA\Circles\Model\DeprecatedMember;
71
use OCA\Circles\Model\Federated\FederatedEvent;
72
use OCA\Circles\Model\Federated\RemoteInstance;
73
use OCA\Circles\Model\FederatedUser;
74
use OCA\Circles\Model\Helpers\MemberHelper;
75
use OCA\Circles\Model\ManagedModel;
76
use OCA\Circles\Model\Member;
77
use OCA\Circles\Model\SharesToken;
78
use OCA\Circles\Service\CircleService;
79
use OCA\Circles\Service\ConfigService;
80
use OCA\Circles\Service\EventService;
81
use OCA\Circles\Service\FederatedUserService;
82
use OCA\Circles\Service\MembershipService;
83
use OCA\Circles\Service\RemoteStreamService;
84
use OCA\Circles\StatusCode;
85
use OCP\IUser;
86
use OCP\IUserManager;
87
use OCP\Mail\IEMailTemplate;
88
use OCP\Util;
89
90
91
/**
92
 * Class MemberAdd
93
 *
94
 * @package OCA\Circles\GlobalScale
95
 */
96
class SingleMemberAdd implements
97
	IFederatedItem,
98
	IFederatedItemAsyncProcess,
99
	IFederatedItemMemberRequired,
100
	IFederatedItemMemberCheckNotRequired {
101
102
103
	use TStringTools;
104
	use TNC22Logger;
105
106
107
	/** @var IUserManager */
108
	protected $userManager;
109
110
	/** @var MemberRequest */
111
	protected $memberRequest;
112
113
	/** @var FederatedUserService */
114
	protected $federatedUserService;
115
116
	/** @var RemoteStreamService */
117
	protected $remoteStreamService;
118
119
	/** @var CircleService */
120
	protected $circleService;
121
122
	/** @var MembershipService */
123
	protected $membershipService;
124
125
	/** @var EventService */
126
	protected $eventService;
127
128
	/** @var ConfigService */
129
	protected $configService;
130
131
132
	/**
133
	 * SingleMemberAdd constructor.
134
	 *
135
	 * @param IUserManager $userManager
136
	 * @param MemberRequest $memberRequest
137
	 * @param FederatedUserService $federatedUserService
138
	 * @param CircleService $circleService
139
	 * @param MembershipService $membershipService
140
	 * @param EventService $eventService
141
	 * @param ConfigService $configService
142
	 */
143 View Code Duplication
	public function __construct(
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...
144
		IUserManager $userManager, MemberRequest $memberRequest, FederatedUserService $federatedUserService,
145
		RemoteStreamService $remoteStreamService, CircleService $circleService,
146
		MembershipService $membershipService, EventService $eventService, ConfigService $configService
147
	) {
148
		$this->userManager = $userManager;
149
		$this->memberRequest = $memberRequest;
150
		$this->federatedUserService = $federatedUserService;
151
		$this->remoteStreamService = $remoteStreamService;
152
		$this->circleService = $circleService;
153
		$this->membershipService = $membershipService;
154
		$this->eventService = $eventService;
155
		$this->configService = $configService;
156
	}
157
158
159
	/**
160
	 * @param FederatedEvent $event
161
	 *
162
	 * @throws FederatedItemBadRequestException
163
	 * @throws FederatedItemNotFoundException
164
	 * @throws FederatedItemServerException
165
	 * @throws FederatedItemRemoteException
166
	 * @throws FederatedItemException
167
	 */
168 View Code Duplication
	public function verify(FederatedEvent $event): void {
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...
169
		$member = $event->getMember();
170
		$circle = $event->getCircle();
171
		$initiator = $circle->getInitiator();
172
173
		$initiatorHelper = new MemberHelper($initiator);
174
		$initiatorHelper->mustBeModerator();
175
176
		$member = $this->generateMember($circle, $member);
177
178
		$event->setMembers([$member]);
179
		$event->setOutcome($member->jsonSerialize());
180
181
		return;
182
183
184
//		$member = $this->membersRequest->getFreshNewMember(
185
//			$circle->getUniqueId(), $ident, $eventMember->getType(), $eventMember->getInstance()
186
//		);
187
//		$member->hasToBeInviteAble()
188
//
189
//		$this->membersService->addMemberBasedOnItsType($circle, $member);
190
//
191
//		$password = '';
192
//		$sendPasswordByMail = false;
193
//		if ($this->configService->enforcePasswordProtection($circle)) {
194
//			if ($circle->getSetting('password_single_enabled') === 'true') {
195
//				$password = $circle->getPasswordSingle();
196
//			} else {
197
//				$sendPasswordByMail = true;
198
//				$password = $this->miscService->token(15);
199
//			}
200
//		}
201
//
202
//		$event->setData(
203
//			new SimpleDataStore(
204
//				[
205
//					'password'       => $password,
206
//					'passwordByMail' => $sendPasswordByMail
207
//				]
208
//			)
209
//		);
210
	}
211
212
213
	/**
214
	 * @param FederatedEvent $event
215
	 *
216
	 * @throws InvalidIdException
217
	 */
218
	public function manage(FederatedEvent $event): void {
219
		$member = $event->getMember();
220
221
		if (!$this->insertOrUpdate($member)) {
222
			return;
223
		}
224
225
		$this->membershipService->onUpdate($member->getSingleId());
226
		$this->eventService->singleMemberAdding($event);
227
228
//
229
//		//
230
//		// TODO: verifiez comment se passe le cached name sur un member_add
231
//		//
232
//		$cachedName = $member->getCachedName();
233
//		$password = $event->getData()
234
//						  ->g('password');
235
//
236
//		$shares = $this->generateUnknownSharesLinks($circle, $member, $password);
237
//		$result = [
238
//			'unknownShares' => $shares,
239
//			'cachedName'    => $cachedName
240
//		];
241
//
242
//		if ($member->getType() === DeprecatedMember::TYPE_CONTACT
243
//			&& $this->configService->isLocalInstance($member->getInstance())) {
244
//			$result['contact'] = $this->miscService->getInfosFromContact($member);
245
//		}
246
//
247
//		$event->setResult(new SimpleDataStore($result));
248
//		$this->eventsService->onMemberNew($circle, $member);
249
	}
250
251
252
	/**
253
	 * @param FederatedEvent $event
254
	 * @param array $results
255
	 */
256
	public function result(FederatedEvent $event, array $results): void {
257
		$this->eventService->singleMemberAdded($event, $results);
258
259
//		$password = $cachedName = '';
260
//		$circle = $member = null;
261
//		$links = [];
262
//		$recipients = [];
263
//		foreach ($events as $event) {
264
//			$data = $event->getData();
265
//			if ($data->gBool('passwordByMail') !== false) {
266
//				$password = $data->g('password');
267
//			}
268
//			$circle = $event->getDeprecatedCircle();
269
//			$member = $event->getMember();
270
//			$result = $event->getResult();
271
//			if ($result->g('cachedName') !== '') {
272
//				$cachedName = $result->g('cachedName');
273
//			}
274
//
275
//			$links = array_merge($links, $result->gArray('unknownShares'));
276
//			$contact = $result->gArray('contact');
277
//			if (!empty($contact)) {
278
//				$recipients = $contact['emails'];
279
//			}
280
//		}
281
//
282
//		if (empty($links) || $circle === null || $member === null) {
283
//			return;
284
//		}
285
//
286
//		if ($cachedName !== '') {
287
//			$member->setCachedName($cachedName);
288
//			$this->membersService->updateMember($member);
289
//		}
290
//
291
//		if ($member->getType() === DeprecatedMember::TYPE_MAIL
292
//			|| $member->getType() === DeprecatedMember::TYPE_CONTACT) {
293
//			if ($member->getType() === DeprecatedMember::TYPE_MAIL) {
294
//				$recipients = [$member->getUserId()];
295
//			}
296
//
297
//			foreach ($recipients as $recipient) {
298
//				$this->memberIsMailbox($circle, $recipient, $links, $password);
299
//			}
300
//		}
301
	}
302
303
304
	/**
305
	 * @param Circle $circle
306
	 * @param Member $member
307
	 *
308
	 * @return Member
309
	 * @throws CircleNotFoundException
310
	 * @throws FederatedItemBadRequestException
311
	 * @throws FederatedItemException
312
	 * @throws FederatedUserException
313
	 * @throws FederatedUserNotFoundException
314
	 * @throws InvalidIdException
315
	 * @throws MembersLimitException
316
	 * @throws OwnerNotFoundException
317
	 * @throws RemoteInstanceException
318
	 * @throws RemoteNotFoundException
319
	 * @throws RemoteResourceNotFoundException
320
	 * @throws SingleCircleNotFoundException
321
	 * @throws UnknownRemoteException
322
	 * @throws UserTypeNotFoundException
323
	 * @throws RequestBuilderException
324
	 */
325
	protected function generateMember(Circle $circle, Member $member): Member {
326
		try {
327
			if ($member->getSingleId() !== '') {
328
				$userId = $member->getSingleId() . '@' . $member->getInstance();
329
				$federatedUser = $this->federatedUserService->getFederatedUser($userId, Member::TYPE_SINGLE);
330
			} else {
331
				$userId = $member->getUserId() . '@' . $member->getInstance();
332
				$federatedUser =
333
					$this->federatedUserService->getFederatedUser($userId, $member->getUserType());
334
			}
335
		} catch (MemberNotFoundException $e) {
336
			throw new FederatedItemBadRequestException(StatusCode::$MEMBER_ADD[120], 120);
0 ignored issues
show
Bug introduced by
The property MEMBER_ADD cannot be accessed from this context as it is declared private in class OCA\Circles\StatusCode.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
337
		}
338
339
		if ($federatedUser->getBasedOn()->isConfig(Circle::CFG_ROOT)) {
340
			throw new FederatedItemBadRequestException(StatusCode::$MEMBER_ADD[125], 125);
0 ignored issues
show
Bug introduced by
The property MEMBER_ADD cannot be accessed from this context as it is declared private in class OCA\Circles\StatusCode.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
341
		}
342
343
		if (!$this->configService->isLocalInstance($member->getInstance())) {
344
			if ($circle->isConfig(Circle::CFG_LOCAL)) {
345
				throw new FederatedItemBadRequestException(StatusCode::$MEMBER_ADD[126], 126);
0 ignored issues
show
Bug introduced by
The property MEMBER_ADD cannot be accessed from this context as it is declared private in class OCA\Circles\StatusCode.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
346
			}
347
348
			if (!$circle->isConfig(Circle::CFG_FEDERATED)) {
349
				$remoteInstance = $this->remoteStreamService->getCachedRemoteInstance($member->getInstance());
350
				if ($remoteInstance->getType() !== RemoteInstance::TYPE_GLOBAL_SCALE) {
351
					throw new FederatedItemBadRequestException(StatusCode::$MEMBER_ADD[127], 127);
0 ignored issues
show
Bug introduced by
The property MEMBER_ADD cannot be accessed from this context as it is declared private in class OCA\Circles\StatusCode.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
352
				}
353
			}
354
		}
355
356
		$member->importFromIFederatedUser($federatedUser);
357
		$member->setCircleId($circle->getSingleId());
358
		$member->setCircle($circle);
359
		$this->manageMemberStatus($circle, $member);
360
361
		$this->circleService->confirmCircleNotFull($circle);
362
363
		// The idea is that adding the member during the self::verify() will help during the broadcasting
364
		// of the event to Federated RemoteInstance for their first member.
365
		$this->insertOrUpdate($member);
366
367
		//	$member->setDisplayName($member->getBasedOn()->getDisplayName());
368
369
		// TODO: Managing cached name
370
		//		$member->setCachedName($eventMember->getCachedName());
371
		return $member;
372
	}
373
374
375
	/**
376
	 * @param Circle $circle
377
	 * @param Member $member
378
	 *
379
	 * @throws FederatedItemBadRequestException
380
	 */
381
	private function manageMemberStatus(Circle $circle, Member $member) {
382
		try {
383
			$knownMember = $this->memberRequest->searchMember($member);
384
			$member->setId($knownMember->getId());
385
386
			if ($knownMember->getLevel() === Member::LEVEL_NONE) {
387
				switch ($knownMember->getStatus()) {
388
					case Member::STATUS_BLOCKED:
389
						if ($circle->isConfig(Circle::CFG_INVITE)) {
390
							$member->setStatus(Member::STATUS_INVITED);
391
						}
392
393
						return;
394
395
					case Member::STATUS_REQUEST:
396
						$member->setLevel(Member::LEVEL_MEMBER);
397
						$member->setStatus(Member::STATUS_MEMBER);
398
399
						return;
400
401
					case Member::STATUS_INVITED:
402
						throw new FederatedItemBadRequestException(StatusCode::$MEMBER_ADD[123], 123);
0 ignored issues
show
Bug introduced by
The property MEMBER_ADD cannot be accessed from this context as it is declared private in class OCA\Circles\StatusCode.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
403
				}
404
			}
405
406
			throw new FederatedItemBadRequestException(StatusCode::$MEMBER_ADD[122], 122);
0 ignored issues
show
Bug introduced by
The property MEMBER_ADD cannot be accessed from this context as it is declared private in class OCA\Circles\StatusCode.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
407
		} catch (MemberNotFoundException $e) {
408
409
			$member->setId($this->uuid(ManagedModel::ID_LENGTH));
410
411 View Code Duplication
			if ($circle->isConfig(Circle::CFG_INVITE)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
412
				$member->setStatus(Member::STATUS_INVITED);
413
			} else {
414
				$member->setLevel(Member::LEVEL_MEMBER);
415
				$member->setStatus(Member::STATUS_MEMBER);
416
			}
417
		}
418
	}
419
420
421
	/**
422
	 * @param Member $member
423
	 *
424
	 * @return bool
425
	 * @throws InvalidIdException
426
	 */
427
	private function insertOrUpdate(Member $member): bool {
428
		try {
429
			$federatedUser = new FederatedUser();
430
			$federatedUser->importFromIFederatedUser($member);
431
			$this->federatedUserService->confirmLocalSingleId($federatedUser);
432
		} catch (FederatedUserException $e) {
433
			$this->e($e, ['member' => $member]);
434
435
			return false;
436
		}
437
438
		$this->memberRequest->insertOrUpdate($member);
439
440
		return true;
441
	}
442
443
444
	/**
445
	 * confirm the validity of a UserId, based on UserType.
446
	 *
447
	 * @param IFederatedUser $member
448
	 *
449
	 * @throws FederatedUserException
450
	 * @throws InvalidIdException
451
	 * @throws UserTypeNotFoundException
452
	 * @throws CircleNotFoundException
453
	 * @throws FederatedUserNotFoundException
454
	 * @throws OwnerNotFoundException
455
	 * @throws RemoteInstanceException
456
	 * @throws RemoteNotFoundException
457
	 * @throws RemoteResourceNotFoundException
458
	 * @throws UnknownRemoteException
459
	 * @throws InvalidItemException
460
	 * @throws RequestNetworkException
461
	 * @throws SignatoryException
462
	 */
463
	private function confirmMember(IFederatedUser $member): void {
464
465
		// TODO: confirm SingleId ???
466
//		switch ($member->getUserType()) {
467
//			case Member::TYPE_USER:
468
		$this->federatedUserService->getFederatedUser($member->getUserId(), $member->getUserType());
469
//				break;
470
//
471
//			// TODO: confirm other UserType
472
//			default:
473
//				break;
474
////				throw new UserTypeNotFoundException();
475
//		}
476
	}
477
478
479
	/**
480
	 * @param IFederatedUser $member
481
	 *
482
	 * @throws NoUserException
483
	 */
484
	private function confirmMemberTypeUser(IFederatedUser $member): void {
485
		if ($this->configService->isLocalInstance($member->getInstance())) {
486
			$user = $this->userManager->get($member->getUserId());
487
			if ($user === null) {
488
				throw new NoUserException('user not found');
489
			}
490
491
			$member->setUserId($user->getUID());
492
493
			return;
494
		}
495
496
		// TODO #M002: request the remote instance and check that user exists
497
	}
498
499
//	/**
500
//	 * Verify if a local account is valid.
501
//	 *
502
//	 * @param $ident
503
//	 * @param $type
504
//	 *
505
//	 * @param string $instance
506
//	 *
507
//	 * @throws NoUserException
508
//	 */
509
//	private function verifyIdentLocalMember(&$ident, $type, string $instance = '') {
510
//		if ($type !== DeprecatedMember::TYPE_USER) {
511
//			return;
512
//		}
513
//
514
//		if ($instance === '') {
515
//			try {
516
//				$ident = $this->miscService->getRealUserId($ident);
517
//			} catch (NoUserException $e) {
518
//				throw new NoUserException($this->l10n->t("This user does not exist"));
519
//			}
520
//		}
521
//	}
522
//
523
//
524
//	/**
525
//	 * Verify if a mail have a valid format.
526
//	 *
527
//	 * @param string $ident
528
//	 * @param int $type
529
//	 *
530
//	 * @throws EmailAccountInvalidFormatException
531
//	 */
532
//	private function verifyIdentEmailAddress(string $ident, int $type) {
533
//		if ($type !== DeprecatedMember::TYPE_MAIL) {
534
//			return;
535
//		}
536
//
537
//		if ($this->configService->isAccountOnly()) {
538
//			throw new EmailAccountInvalidFormatException(
539
//				$this->l10n->t('You cannot add a mail address as member of your Circle')
540
//			);
541
//		}
542
//
543
//		if (!filter_var($ident, FILTER_VALIDATE_EMAIL)) {
544
//			throw new EmailAccountInvalidFormatException(
545
//				$this->l10n->t('Email format is not valid')
546
//			);
547
//		}
548
//	}
549
//
550
//
551
//	/**
552
//	 * Verify if a contact exist in current user address books.
553
//	 *
554
//	 * @param $ident
555
//	 * @param $type
556
//	 *
557
//	 * @throws NoUserException
558
//	 * @throws EmailAccountInvalidFormatException
559
//	 */
560
//	private function verifyIdentContact(&$ident, $type) {
561
//		if ($type !== DeprecatedMember::TYPE_CONTACT) {
562
//			return;
563
//		}
564
//
565
//		if ($this->configService->isAccountOnly()) {
566
//			throw new EmailAccountInvalidFormatException(
567
//				$this->l10n->t('You cannot add a contact as member of your Circle')
568
//			);
569
//		}
570
//
571
//		$tmpContact = $this->userId . ':' . $ident;
572
//		$result = MiscService::getContactData($tmpContact);
573
//		if (empty($result)) {
574
//			throw new NoUserException($this->l10n->t("This contact is not available"));
575
//		}
576
//
577
//		$ident = $tmpContact;
578
//	}
579
580
581
	/**
582
	 * @param DeprecatedCircle $circle
583
	 * @param string $recipient
584
	 * @param array $links
585
	 * @param string $password
586
	 */
587 View Code Duplication
	private function memberIsMailbox(
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...
588
		DeprecatedCircle $circle, string $recipient, array $links, string $password
589
	) {
590
		if ($circle->getViewer() === null) {
591
			$author = $circle->getOwner()
592
							 ->getUserId();
593
		} else {
594
			$author = $circle->getViewer()
595
							 ->getUserId();
596
		}
597
598
		try {
599
			$template = $this->generateMailExitingShares($author, $circle->getName());
600
			$this->fillMailExistingShares($template, $links);
601
			$this->sendMailExistingShares($template, $author, $recipient);
602
			$this->sendPasswordExistingShares($author, $recipient, $password);
603
		} catch (Exception $e) {
604
			$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...
605
		}
606
	}
607
608
609
	/**
610
	 * @param DeprecatedCircle $circle
611
	 * @param DeprecatedMember $member
612
	 * @param string $password
613
	 *
614
	 * @return array
615
	 */
616 View Code Duplication
	private function generateUnknownSharesLinks(
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...
617
		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...
618
	): array {
619
		$unknownShares = $this->getUnknownShares($member);
620
621
		$data = [];
622
		foreach ($unknownShares as $share) {
623
			try {
624
				$data[] = $this->getMailLinkFromShare($share, $member, $password);
625
			} catch (TokenDoesNotExistException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
626
			}
627
		}
628
629
		return $data;
630
	}
631
632
633
	/**
634
	 * @param DeprecatedMember $member
635
	 *
636
	 * @return array
637
	 */
638 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...
639
		$allShares = $this->fileSharesRequest->getSharesForCircle($member->getCircleId());
0 ignored issues
show
Bug introduced by
The property fileSharesRequest 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...
640
		$knownShares = array_map(
641
			function(SharesToken $shareToken) {
642
				return $shareToken->getShareId();
643
			},
644
			$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...
645
		);
646
647
		$unknownShares = [];
648
		foreach ($allShares as $share) {
649
			if (!in_array($share['id'], $knownShares)) {
650
				$unknownShares[] = $share;
651
			}
652
		}
653
654
		return $unknownShares;
655
	}
656
657
658
	/**
659
	 * @param array $share
660
	 * @param DeprecatedMember $member
661
	 * @param string $password
662
	 *
663
	 * @return array
664
	 * @throws TokenDoesNotExistException
665
	 */
666 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...
667
		$sharesToken = $this->tokensRequest->generateTokenForMember($member, (int)$share['id'], $password);
668
		$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...
669
			'files_sharing.sharecontroller.showShare',
670
			['token' => $sharesToken->getToken()]
671
		);
672
		$author = $share['uid_initiator'];
673
		$filename = basename($share['file_target']);
674
675
		return [
676
			'author'   => $author,
677
			'link'     => $link,
678
			'filename' => $filename
679
		];
680
	}
681
682
683
	/**
684
	 * @param string $author
685
	 * @param string $circleName
686
	 *
687
	 * @return IEMailTemplate
688
	 */
689 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...
690
		$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...
691
		$emailTemplate->addHeader();
692
693
		$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...
694
		$emailTemplate->addBodyText(htmlspecialchars($text), $text);
695
696
		return $emailTemplate;
697
	}
698
699
	/**
700
	 * @param IEMailTemplate $emailTemplate
701
	 * @param array $links
702
	 */
703 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...
704
		foreach ($links as $item) {
705
			$emailTemplate->addBodyButton(
706
				$this->l10n->t('Open »%s«', [htmlspecialchars($item['filename'])]), $item['link']
707
			);
708
		}
709
	}
710
711
712
	/**
713
	 * @param IEMailTemplate $emailTemplate
714
	 * @param string $author
715
	 * @param string $recipient
716
	 *
717
	 * @throws Exception
718
	 */
719 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...
720
	) {
721
		$subject = $this->l10n->t('%s shared multiple files with you.', [$author]);
722
723
		$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...
724
		$senderName = $this->l10n->t('%s on %s', [$author, $instanceName]);
725
726
		$message = $this->mailer->createMessage();
727
728
		$message->setFrom([Util::getDefaultEmailAddress($instanceName) => $senderName]);
729
		$message->setSubject($subject);
730
		$message->setPlainBody($emailTemplate->renderText());
731
		$message->setHtmlBody($emailTemplate->renderHtml());
732
		$message->setTo([$recipient]);
733
734
		$this->mailer->send($message);
735
	}
736
737
738
	/**
739
	 * @param string $author
740
	 * @param string $email
741
	 * @param string $password
742
	 *
743
	 * @throws Exception
744
	 */
745
	protected function sendPasswordExistingShares(string $author, string $email, string $password) {
746
		if ($password === '') {
747
			return;
748
		}
749
750
		$message = $this->mailer->createMessage();
751
752
		$authorUser = $this->userManager->get($author);
753
		$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...
754
		$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...
755
756
		$this->miscService->log("Sending password mail about existing files to '" . $email . "'", 0);
757
758
		$plainBodyPart = $this->l10n->t(
759
			"%1\$s shared multiple files with you.\nYou should have already received a separate mail with a link to access them.\n",
760
			[$authorName]
761
		);
762
		$htmlBodyPart = $this->l10n->t(
763
			'%1$s shared multiple files with you. You should have already received a separate mail with a link to access them.',
764
			[$authorName]
765
		);
766
767
		$emailTemplate = $this->mailer->createEMailTemplate(
768
			'sharebymail.RecipientPasswordNotification', [
769
														   'password' => $password,
770
														   'author'   => $author
771
													   ]
772
		);
773
774
		$emailTemplate->setSubject(
775
			$this->l10n->t(
776
				'Password to access files shared to you by %1$s', [$authorName]
777
			)
778
		);
779
		$emailTemplate->addHeader();
780
		$emailTemplate->addHeading($this->l10n->t('Password to access files'), false);
781
		$emailTemplate->addBodyText(htmlspecialchars($htmlBodyPart), $plainBodyPart);
782
		$emailTemplate->addBodyText($this->l10n->t('It is protected with the following password:'));
783
		$emailTemplate->addBodyText($password);
784
785
		// The "From" contains the sharers name
786
		$instanceName = $this->defaults->getName();
787
		$senderName = $this->l10n->t(
788
			'%1$s via %2$s',
789
			[
790
				$authorName,
791
				$instanceName
792
			]
793
		);
794
795
		$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
796
		if ($authorEmail !== null) {
797
			$message->setReplyTo([$authorEmail => $authorName]);
798
			$emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
799
		} else {
800
			$emailTemplate->addFooter();
801
		}
802
803
		$message->setTo([$email]);
804
		$message->useTemplate($emailTemplate);
805
		$this->mailer->send($message);
806
	}
807
808
}
809
810