Completed
Pull Request — master (#477)
by Maxence
02:01
created

MemberAdd::result()   B

Complexity

Conditions 6
Paths 22

Size

Total Lines 40

Duplication

Lines 7
Ratio 17.5 %

Importance

Changes 0
Metric Value
dl 7
loc 40
rs 8.6577
c 0
b 0
f 0
cc 6
nc 22
nop 1
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
//		$this->storeAuthorFromEvent($circle);
98
99
		if (!$event->isForced()) {
100
			$circle->getHigherViewer()
101
				   ->hasToBeModerator();
102
		}
103
104
		$member = $this->membersRequest->getFreshNewMember(
105
			$circle->getUniqueId(), $ident, $eventMember->getType(), $eventMember->getInstance()
106
		);
107
		$member->hasToBeInviteAble();
108
109
		$this->circlesService->checkThatCircleIsNotFull($circle);
110
		$this->membersService->addMemberBasedOnItsType($circle, $member);
111
112
		$password = '';
113
		if ($this->configService->enforcePasswordProtection($circle)) {
114
			$password = $this->miscService->token(15);
115
		}
116
117
		$this->miscService->updateCachedName($member);
118
119
		$event->setData(new SimpleDataStore(['password' => $password]));
120
		$event->setMember($member);
121
	}
122
123
124
	/**
125
	 * @param GSEvent $event
126
	 *
127
	 * @throws MemberAlreadyExistsException
128
	 */
129
	public function manage(GSEvent $event): void {
130
		$circle = $event->getCircle();
131
		$member = $event->getMember();
132
		if ($member->getJoined() === '') {
133
			$this->membersRequest->createMember($member);
134
		} else {
135
			$this->membersRequest->updateMember($member);
136
		}
137
138
		$password = $event->getData()
139
						  ->g('password');
140
		$shares = $this->generateUnknownSharesLinks($circle, $member, $password);
141
142
		$event->setResult(new SimpleDataStore(['unknownShares' => $shares]));
143
		$this->eventsService->onMemberNew($circle, $member);
144
	}
145
146
147
	/**
148
	 * @param GSEvent[] $events
149
	 */
150
	public function result(array $events): void {
151
		$password = '';
152
		$circle = $member = null;
153
		$links = [];
154
		foreach ($events as $event) {
155
			$password = $event->getData()
156
							  ->g('password');
157
158
			$circle = $event->getCircle();
159
			$member = $event->getMember();
160
161
			$links = array_merge(
162
				$links, $event->getResult()
163
							  ->gArray('unknownShares')
164
			);
165
		}
166
167
		if ($member->getType() !== Member::TYPE_MAIL
168
			&& $member->getType() !== Member::TYPE_CONTACT) {
169
			return;
170
		}
171
172 View Code Duplication
		if ($circle->getViewer() === null) {
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...
173
			$author = $circle->getOwner()
174
							 ->getUserId();
175
		} else {
176
			$author = $circle->getViewer()
177
							 ->getUserId();
178
		}
179
		$recipient = $member->getUserId();
180
181
		try {
182
			$template = $this->generateMailExitingShares($author, $circle->getName());
183
			$this->fillMailExistingShares($template, $links);
184
			$this->sendMailExistingShares($template, $author, $recipient);
185
			$this->sendPasswordExistingShares($author, $recipient, $password);
186
		} catch (Exception $e) {
187
			$this->miscService->log('Failed to send mail about existing share ' . $e->getMessage());
188
		}
189
	}
190
191
192
	/**
193
	 * @param Circle $circle
194
	 * @param Member $member
195
	 * @param string $password
196
	 *
197
	 * @return array
198
	 */
199
	private function generateUnknownSharesLinks(Circle $circle, Member $member, string $password): array {
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...
200
		$unknownShares = $this->getUnknownShares($member);
201
202
		$data = [];
203
		foreach ($unknownShares as $share) {
204
			try {
205
				$data[] = $this->getMailLinkFromShare($share, $member, $password);
206
			} catch (TokenDoesNotExistException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
207
			}
208
		}
209
210
		return $data;
211
	}
212
213
214
	/**
215
	 * @param Member $member
216
	 *
217
	 * @return array
218
	 */
219
	private function getUnknownShares(Member $member): array {
220
		$allShares = $this->sharesRequest->getSharesForCircle($member->getCircleId());
221
		$knownShares = array_map(
222
			function(SharesToken $shareToken) {
223
				return $shareToken->getShareId();
224
			},
225
			$this->tokensRequest->getTokensFromMember($member)
226
		);
227
228
		$unknownShares = [];
229
		foreach ($allShares as $share) {
230
			if (!in_array($share['id'], $knownShares)) {
231
				$unknownShares[] = $share;
232
			}
233
		}
234
235
		return $unknownShares;
236
	}
237
238
239
	/**
240
	 * @param array $share
241
	 * @param Member $member
242
	 * @param string $password
243
	 *
244
	 * @return array
245
	 * @throws TokenDoesNotExistException
246
	 */
247 View Code Duplication
	private function getMailLinkFromShare(array $share, Member $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...
248
		$sharesToken = $this->tokensRequest->generateTokenForMember($member, (int)$share['id'], $password);
249
		$link = $this->urlGenerator->linkToRouteAbsolute(
250
			'files_sharing.sharecontroller.showShare',
251
			['token' => $sharesToken->getToken()]
252
		);
253
		$author = $share['uid_initiator'];
254
		$filename = basename($share['file_target']);
255
256
		return [
257
			'author'   => $author,
258
			'link'     => $link,
259
			'filename' => $filename
260
		];
261
	}
262
263
264
	/**
265
	 * @param string $author
266
	 * @param string $circleName
267
	 *
268
	 * @return IEMailTemplate
269
	 */
270
	private function generateMailExitingShares(string $author, string $circleName): IEMailTemplate {
271
		$emailTemplate = $this->mailer->createEMailTemplate('circles.ExistingShareNotification', []);
272
		$emailTemplate->addHeader();
273
274
		$text = $this->l10n->t('%s shared multiple files with \'%s\'.', [$author, $circleName]);
275
		$emailTemplate->addBodyText(htmlspecialchars($text), $text);
276
277
		return $emailTemplate;
278
	}
279
280
	/**
281
	 * @param IEMailTemplate $emailTemplate
282
	 * @param array $links
283
	 */
284 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...
285
		foreach ($links as $item) {
286
			$emailTemplate->addBodyButton(
287
				$this->l10n->t('Open »%s«', [htmlspecialchars($item['filename'])]), $item['link']
288
			);
289
		}
290
	}
291
292
293
	/**
294
	 * @param IEMailTemplate $emailTemplate
295
	 * @param string $author
296
	 * @param string $recipient
297
	 *
298
	 * @throws Exception
299
	 */
300 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...
301
	) {
302
		$subject = $this->l10n->t('%s shared multiple files with you.', [$author]);
303
304
		$instanceName = $this->defaults->getName();
305
		$senderName = $this->l10n->t('%s on %s', [$author, $instanceName]);
306
307
		$message = $this->mailer->createMessage();
308
309
		$message->setFrom([Util::getDefaultEmailAddress($instanceName) => $senderName]);
310
		$message->setSubject($subject);
311
		$message->setPlainBody($emailTemplate->renderText());
312
		$message->setHtmlBody($emailTemplate->renderHtml());
313
		$message->setTo([$recipient]);
314
315
		$this->mailer->send($message);
316
	}
317
318
319
	/**
320
	 * @param string $author
321
	 * @param string $email
322
	 * @param string $password
323
	 *
324
	 * @throws Exception
325
	 */
326 View Code Duplication
	protected function sendPasswordExistingShares(string $author, string $email, 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...
327
		if ($password === '') {
328
			return;
329
		}
330
331
		$message = $this->mailer->createMessage();
332
333
		$authorUser = $this->userManager->get($author);
334
		$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...
335
		$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...
336
337
		$this->miscService->log("Sending password mail about existing files to '" . $email . "'", 0);
338
339
		$plainBodyPart = $this->l10n->t(
340
			"%1\$s shared multiple files with you.\nYou should have already received a separate mail with a link to access them.\n",
341
			[$authorName]
342
		);
343
		$htmlBodyPart = $this->l10n->t(
344
			'%1$s shared multiple files with you. You should have already received a separate mail with a link to access them.',
345
			[$authorName]
346
		);
347
348
		$emailTemplate = $this->mailer->createEMailTemplate(
349
			'sharebymail.RecipientPasswordNotification', [
350
														   'password' => $password,
351
														   'author'   => $author
352
													   ]
353
		);
354
355
		$emailTemplate->setSubject(
356
			$this->l10n->t(
357
				'Password to access files shared to you by %1$s', [$authorName]
358
			)
359
		);
360
		$emailTemplate->addHeader();
361
		$emailTemplate->addHeading($this->l10n->t('Password to access files'), false);
362
		$emailTemplate->addBodyText(htmlspecialchars($htmlBodyPart), $plainBodyPart);
363
		$emailTemplate->addBodyText($this->l10n->t('It is protected with the following password:'));
364
		$emailTemplate->addBodyText($password);
365
366
		// The "From" contains the sharers name
367
		$instanceName = $this->defaults->getName();
368
		$senderName = $this->l10n->t(
369
			'%1$s via %2$s',
370
			[
371
				$authorName,
372
				$instanceName
373
			]
374
		);
375
376
		$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
377
		if ($authorEmail !== null) {
378
			$message->setReplyTo([$authorEmail => $authorName]);
379
			$emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
380
		} else {
381
			$emailTemplate->addFooter();
382
		}
383
384
		$message->setTo([$email]);
385
		$message->useTemplate($emailTemplate);
386
		$this->mailer->send($message);
387
	}
388
389
390
}
391