Skip to content

Commit 0216a38

Browse files
GhassenKefibackportbot[bot]
authored andcommitted
Fix: password notification to share owner behavior
Signed-off-by: Ghassen kefi <ghassen.kefi.dev@gmail.com>
1 parent a79af7c commit 0216a38

2 files changed

Lines changed: 40 additions & 28 deletions

File tree

apps/sharebymail/lib/ShareByMailProvider.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -269,16 +269,19 @@ public function sendMailNotification(IShare $share): bool {
269269

270270
// If we have a password set, we send it to the recipient
271271
if ($share->getPassword() !== null) {
272-
// If share-by-talk password is enabled, we do not send the notification
273-
// to the recipient. They will have to request it to the owner after opening the link.
274-
// Secondly, if the password expiration is disabled, we send the notification to the recipient
275-
// Lastly, if the mail to recipient failed, we send the password to the owner as a fallback.
276-
// If a password expires, the recipient will still be able to request a new one via talk.
277-
$passwordExpire = $this->config->getSystemValue('sharing.enable_mail_link_password_expiration', false);
278-
if ($passwordExpire === false || $share->getSendPasswordByTalk()) {
272+
// If sending the password by mail is disabled, we send the password to the owner.
273+
// If share-by-talk password is enabled, we do not send the password to the recipient.
274+
// They can request it from the owner after opening the link.
275+
// Otherwise, we send the password to the recipient.
276+
// If sending the password to the recipient fails, we send the password to the owner as a fallback.
277+
// Password expiration does not affect this flow: the password is either sent to the recipient
278+
// or, if sending fails, sent to the owner as a fallback.
279+
if ($this->settingsManager->sendPasswordByMail() === false || $share->getSendPasswordByTalk()) {
280+
$this->trySendPasswordToOwner($share);
281+
} else {
279282
$send = $this->sendPassword($share, $share->getPassword(), $validEmails);
280283
if ($send === false) {
281-
$this->sendPasswordToOwner($share, $share->getPassword());
284+
$this->trySendPasswordToOwner($share);
282285
}
283286
}
284287
}
@@ -307,6 +310,21 @@ public function sendMailNotification(IShare $share): bool {
307310
return false;
308311
}
309312

313+
/**
314+
* Notifying the owner of the password must not abort an otherwise
315+
* successful share creation, e.g. when the owner has no email address set.
316+
*/
317+
private function trySendPasswordToOwner(IShare $share): void {
318+
try {
319+
$this->sendPasswordToOwner($share, $share->getPassword());
320+
} catch (\Exception $e) {
321+
$this->logger->error('Failed to send password to the owner of the share.', [
322+
'app' => 'sharebymail',
323+
'exception' => $e,
324+
]);
325+
}
326+
}
327+
310328
/**
311329
* @param IShare $share The share to send the email for
312330
* @param array $emails The email addresses to send the email to

apps/sharebymail/tests/ShareByMailProviderTest.php

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,12 @@ public function testCreateSendPasswordByMailWithPasswordAndWithoutEnforcedPasswo
258258
// The given password (but not the autogenerated password) should not be
259259
// mailed to the receiver of the share because permanent passwords are not enforced.
260260
$this->shareManager->expects($this->any())->method('shareApiLinkEnforcePassword')->willReturn(false);
261-
$this->config->expects($this->once())->method('getSystemValue')->with('sharing.enable_mail_link_password_expiration')->willReturn(false);
261+
$this->settingsManager->expects($this->once())->method('sendPasswordByMail')->willReturn(true);
262262
$instance->expects($this->never())->method('autoGeneratePassword');
263263

264264
// A password is set but no password sent via talk has been requested
265265
$instance->expects($this->once())->method('sendEmail')->with($share, ['receiver@example.com']);
266-
$instance->expects($this->once())->method('sendPassword')->with($share, 'password');
266+
$instance->expects($this->once())->method('sendPassword')->with($share, 'password')->willReturn(true);
267267
$instance->expects($this->never())->method('sendPasswordToOwner');
268268

269269
$this->assertSame($expectedShare, $instance->create($share));
@@ -306,66 +306,60 @@ public function testCreateSendPasswordByMailWithPasswordAndWithoutEnforcedPasswo
306306
// aside from the main email notification.
307307
$this->shareManager->expects($this->any())->method('shareApiLinkEnforcePassword')->willReturn(false);
308308
$instance->expects($this->never())->method('autoGeneratePassword');
309-
$this->config->expects($this->once())->method('getSystemValue')
310-
->with('sharing.enable_mail_link_password_expiration')
311-
->willReturn(true);
312309

313310
$this->settingsManager->expects($this->once())->method('sendPasswordByMail')->willReturn(true);
314311

315312
// No password has been set and no password sent via talk has been requested,
316313
// but password has been enforced for the whole instance and will be generated.
317314
$instance->expects($this->once())->method('sendEmail')->with($share, ['receiver@example.com']);
318-
$instance->expects($this->never())->method('sendPassword');
315+
$instance->expects($this->once())->method('sendPassword')->with($share, 'password')->willReturn(true);
319316
$instance->expects($this->never())->method('sendPasswordToOwner');
320317

321318
$this->assertSame($expectedShare, $instance->create($share));
322319
$instance->sendMailNotification($share);
323320
}
324321

325-
326322
public function testCreateSendPasswordToOwnerWhenSendPasswordByMailIsDisabled(): void {
327323
$expectedShare = $this->createMock(IShare::class);
328324
$node = $this->getMockBuilder(File::class)->getMock();
329325
$node->method('getName')->willReturn('filename');
330-
326+
331327
$share = $this->getMockBuilder(IShare::class)->getMock();
332328
$share->method('getSharedWith')->willReturn('receiver@example.com');
333329
$share->method('getSendPasswordByTalk')->willReturn(false);
334330
$share->method('getSharedBy')->willReturn('owner');
335331
$share->method('getNode')->willReturn($node);
336-
$share->method('getId')->willReturn(42);
332+
$share->method('getId')->willReturn('42');
337333
$share->method('getNote')->willReturn('');
338334
$share->method('getToken')->willReturn('token');
339335
$share->method('getPassword')->willReturn('password');
340-
336+
341337
$this->mailer->method('validateMailAddress')->willReturn(true);
342338
$this->hasher->expects($this->once())->method('hash')->with('password')->willReturn('passwordHashed');
343339
$share->expects($this->once())->method('setPassword')->with('passwordHashed');
344-
340+
345341
$instance = $this->getInstance([
346342
'getSharedWith', 'createMailShare', 'getRawShare', 'createShareObject',
347343
'createShareActivity', 'autoGeneratePassword', 'createPasswordSendActivity',
348344
'sendEmail', 'sendPassword', 'sendPasswordToOwner',
349345
]);
350-
346+
351347
$instance->expects($this->once())->method('getSharedWith')->willReturn([]);
352-
$instance->expects($this->once())->method('createMailShare')->with($share)->willReturn(42);
348+
$instance->expects($this->once())->method('createMailShare')->with($share)->willReturn('42');
353349
$instance->expects($this->once())->method('createShareActivity')->with($share);
354350
$instance->expects($this->once())->method('getRawShare')->with(42)->willReturn(['rawShare', 'password' => 'password']);
355351
$instance->expects($this->once())->method('createShareObject')->with(['rawShare', 'password' => 'password'])->willReturn($expectedShare);
356-
352+
357353
$this->shareManager->method('shareApiLinkEnforcePassword')->willReturn(false);
358-
$this->config->expects($this->once())->method('getSystemValue')->with('sharing.enable_mail_link_password_expiration')->willReturn(true);
359354
$this->settingsManager->expects($this->once())->method('sendPasswordByMail')->willReturn(false);
360-
361-
$instance->expects($this->once())->method('sendPasswordToOwner')->with($share);
355+
356+
$instance->expects($this->once())->method('sendPasswordToOwner')->with($share, 'password');
362357
$instance->expects($this->once())->method('sendEmail')->with($share, ['receiver@example.com']);
363358
$instance->expects($this->never())->method('sendPassword');
364-
359+
365360
$this->assertSame($expectedShare, $instance->create($share));
366361
$instance->sendMailNotification($share);
367362
}
368-
369363

370364
public function testCreateSendPasswordByMailWithEnforcedPasswordProtectionWithPermanentPassword(): void {
371365
$expectedShare = $this->createMock(IShare::class);
@@ -570,7 +564,7 @@ public function testCreateSendPasswordByTalkWithEnforcedPasswordProtectionWithPe
570564
$instance->expects($this->once())->method('getRawShare')->with(42)->willReturn(['rawShare', 'password' => 'autogeneratedPassword']);
571565
$instance->expects($this->once())->method('createShareObject')->with(['rawShare', 'password' => 'autogeneratedPassword'])->willReturn($expectedShare);
572566

573-
$share->expects($this->exactly(4))->method('getPassword')->willReturnOnConsecutiveCalls(null, 'autogeneratedPassword', 'autogeneratedPassword', 'autogeneratedPassword');
567+
$share->expects($this->exactly(3))->method('getPassword')->willReturnOnConsecutiveCalls(null, 'autogeneratedPassword', 'autogeneratedPassword');
574568
$this->hasher->expects($this->once())->method('hash')->with('autogeneratedPassword')->willReturn('autogeneratedPasswordHashed');
575569
$share->expects($this->once())->method('setPassword')->with('autogeneratedPasswordHashed');
576570

0 commit comments

Comments
 (0)