Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 17 additions & 25 deletions apps/sharebymail/lib/ShareByMailProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Mail\EMailDetails;
use OCP\Mail\IEmailValidator;
use OCP\Mail\IMailer;
use OCP\Security\Events\GenerateSecurePasswordEvent;
Expand Down Expand Up @@ -351,6 +352,11 @@ protected function sendEmail(IShare $share, array $emails): void {

$initiatorUser = $this->userManager->get($initiator);
$initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
// The sharer's address is only exposed when replies go to them
$initiatorEmail = null;
if ($initiatorUser instanceof IUser && $this->settingsManager->replyToInitiator()) {
$initiatorEmail = $initiatorUser->getEMailAddress();
}
$message = $this->mailer->createMessage();

$emailTemplate = $this->mailer->createEMailTemplate('sharebymail.RecipientNotification', [
Expand All @@ -364,25 +370,22 @@ protected function sendEmail(IShare $share, array $emails): void {

$emailTemplate->setSubject($this->l->t('%1$s shared %2$s with you', [$initiatorDisplayName, $filename]));
$emailTemplate->addHeader();
$emailTemplate->addBodySender($initiatorDisplayName, $initiatorEmail ?? '');
$emailTemplate->addHeading($this->l->t('%1$s shared %2$s with you', [$initiatorDisplayName, $filename]), false);

if ($note !== '') {
$emailTemplate->addBodyListItem(
htmlspecialchars($note),
$this->l->t('Note:'),
$this->getAbsoluteImagePath('caldav/description.png'),
$note
);
$emailTemplate->addBodyNote($note, $this->l->t('Note from %s', [$initiatorDisplayName]));
}

$details = new EMailDetails($filename);
if ($expiration !== null) {
$dateString = (string)$this->l->l('date', $expiration, ['width' => 'medium']);
$emailTemplate->addBodyListItem(
$this->l->t('This share is valid until %s at midnight', [$dateString]),
$this->l->t('Expiration:'),
$this->getAbsoluteImagePath('caldav/time.png'),
);
$details->addRow($this->l->t('Valid until'))->text($dateString);
}
if ($share->getPassword() !== null) {
$details->addRow($this->l->t('Password'))->text($this->l->t('Required'));
}
$emailTemplate->addBodyDetails($details);

$emailTemplate->addBodyButton(
$this->l->t('Open shared item'),
Expand Down Expand Up @@ -413,14 +416,9 @@ protected function sendEmail(IShare $share, array $emails): void {

// The "Reply-To" is set to the sharer if an mail address is configured
// also the default footer contains a "Do not reply" which needs to be adjusted.
if ($initiatorUser && $this->settingsManager->replyToInitiator()) {
$initiatorEmail = $initiatorUser->getEMailAddress();
if ($initiatorEmail !== null) {
$message->setReplyTo([$initiatorEmail => $initiatorDisplayName]);
$emailTemplate->addFooter($instanceName . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : ''));
} else {
$emailTemplate->addFooter();
}
if ($initiatorEmail !== null) {
$message->setReplyTo([$initiatorEmail => $initiatorDisplayName]);
$emailTemplate->addFooter($instanceName . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : ''));
} else {
$emailTemplate->addFooter();
}
Expand Down Expand Up @@ -653,12 +651,6 @@ protected function sendPasswordToOwner(IShare $share, string $password): bool {
return true;
}

private function getAbsoluteImagePath(string $path):string {
return $this->urlGenerator->getAbsoluteURL(
$this->urlGenerator->imagePath('core', $path)
);
}

/**
* generate share token
*/
Expand Down
88 changes: 60 additions & 28 deletions apps/sharebymail/tests/ShareByMailProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Mail\EMailDetails;
use OCP\Mail\IEMailTemplate;
use OCP\Mail\IMailer;
use OCP\Mail\IMessage;
Expand Down Expand Up @@ -396,7 +397,7 @@ public function testCreateSendPasswordByMailWithEnforcedPasswordProtectionWithPe
$instance->expects($this->once())->method('createShareObject')->with(['rawShare', 'password' => 'autogeneratedPassword'])->willReturn($expectedShare);

// Initially not set, but will be set by the autoGeneratePassword method.
$share->expects($this->exactly(3))->method('getPassword')->willReturnOnConsecutiveCalls(null, 'autogeneratedPassword', 'autogeneratedPassword');
$share->expects($this->exactly(4))->method('getPassword')->willReturnOnConsecutiveCalls(null, 'autogeneratedPassword', 'autogeneratedPassword', 'autogeneratedPassword');
$share->expects($this->never())->method('setPassword');
$share->expects($this->once())->method('setPasswordHash')->with($this->callback(fn (string $hash): bool => Server::get(IHasher::class)->verify('autogeneratedPassword', $hash)));

Expand Down Expand Up @@ -475,7 +476,7 @@ public function testCreateSendPasswordByMailWithPasswordAndWithEnforcedPasswordP
$instance->expects($this->once())->method('getRawShare')->with('42')->willReturn(['rawShare', 'password' => 'password']);
$instance->expects($this->once())->method('createShareObject')->with(['rawShare', 'password' => 'password'])->willReturn($expectedShare);

$share->expects($this->exactly(3))->method('getPassword')->willReturn('password');
$share->expects($this->exactly(4))->method('getPassword')->willReturn('password');
$share->expects($this->never())->method('setPassword');
$share->expects($this->never())->method('setPasswordHash');

Expand Down Expand Up @@ -563,7 +564,7 @@ public function testCreateSendPasswordByTalkWithEnforcedPasswordProtectionWithPe
$instance->expects($this->once())->method('getRawShare')->with('42')->willReturn(['rawShare', 'password' => 'autogeneratedPassword']);
$instance->expects($this->once())->method('createShareObject')->with(['rawShare', 'password' => 'autogeneratedPassword'])->willReturn($expectedShare);

$share->expects($this->exactly(3))->method('getPassword')->willReturnOnConsecutiveCalls(null, 'autogeneratedPassword', 'autogeneratedPassword');
$share->expects($this->exactly(4))->method('getPassword')->willReturnOnConsecutiveCalls(null, 'autogeneratedPassword', 'autogeneratedPassword', 'autogeneratedPassword');
$share->expects($this->never())->method('setPassword');
$share->expects($this->once())->method('setPasswordHash')->with($this->callback(fn (string $hash): bool => Server::get(IHasher::class)->verify('autogeneratedPassword', $hash)));

Expand Down Expand Up @@ -1336,6 +1337,10 @@ public function testSendMailNotificationWithSameUserAndUserEmail(): void {
$template
->expects($this->once())
->method('addHeader');
$template
->expects($this->once())
->method('addBodySender')
->with('Mrs. Owner User', 'owner@example.com');
$template
->expects($this->once())
->method('addHeading')
Expand Down Expand Up @@ -1413,6 +1418,44 @@ public function testSendMailNotificationWithSameUserAndUserEmail(): void {
);
}

public function testSendEmailListsPasswordInDetails(): void {
$provider = $this->getInstance();
$this->settingsManager->method('replyToInitiator')->willReturn(false);
$this->userManager->method('get')->with('OwnerUser')->willReturn(null);
$this->mailer->method('createMessage')->willReturn($this->createMock(Message::class));
$this->mailer->method('send')->willReturn([]);
$this->defaults->method('getName')->willReturn('UnitTestCloud');
$this->urlGenerator->method('linkToRouteAbsolute')->willReturn('https://example.com/file.txt');

$template = $this->createMock(IEMailTemplate::class);
$this->mailer->method('createEMailTemplate')->willReturn($template);
$template->expects($this->once())
->method('addBodySender')
->with('OwnerUser', '');
$template->expects($this->never())
->method('addBodyNote');
$template->expects($this->once())
->method('addBodyDetails')
->with($this->callback(function (EMailDetails $details): bool {
$rows = $details->getRows();
return $details->getTitle() === 'file.txt'
&& count($rows) === 1
&& $rows[0]->getLabel() === 'Password'
&& $rows[0]->getParts()[0]['text'] === 'Required';
}));

$node = $this->createMock(File::class);
$node->method('getName')->willReturn('file.txt');
$share = $this->createMock(IShare::class);
$share->method('getSharedBy')->willReturn('OwnerUser');
$share->method('getNode')->willReturn($node);
$share->method('getNote')->willReturn('');
$share->method('getToken')->willReturn('token');
$share->method('getPassword')->willReturn('password');

self::invokePrivate($provider, 'sendEmail', [$share, ['john@doe.com']]);
}

public function testSendMailNotificationWithSameUserAndUserEmailAndNote(): void {
$provider = $this->getInstance();
$user = $this->createMock(IUser::class);
Expand Down Expand Up @@ -1444,21 +1487,10 @@ public function testSendMailNotificationWithSameUserAndUserEmailAndNote(): void
->method('addHeading')
->with('Mrs. Owner User shared file.txt with you');

$this->urlGenerator->expects($this->once())->method('imagePath')
->with('core', 'caldav/description.png')
->willReturn('core/img/caldav/description.png');
$this->urlGenerator->expects($this->once())->method('getAbsoluteURL')
->with('core/img/caldav/description.png')
->willReturn('https://example.com/core/img/caldav/description.png');
$template
->expects($this->once())
->method('addBodyListItem')
->with(
'This is a note to the recipient',
'Note:',
'https://example.com/core/img/caldav/description.png',
'This is a note to the recipient'
);
->method('addBodyNote')
->with('This is a note to the recipient', 'Note from Mrs. Owner User');
$template
->expects($this->once())
->method('addBodyButton')
Expand Down Expand Up @@ -1568,20 +1600,16 @@ public function testSendMailNotificationWithSameUserAndUserEmailAndExpiration():
->method('l')
->with('date', $expiration, ['width' => 'medium'])
->willReturn('2001-01-01');
$this->urlGenerator->expects($this->once())->method('imagePath')
->with('core', 'caldav/time.png')
->willReturn('core/img/caldav/time.png');
$this->urlGenerator->expects($this->once())->method('getAbsoluteURL')
->with('core/img/caldav/time.png')
->willReturn('https://example.com/core/img/caldav/time.png');
$template
->expects($this->once())
->method('addBodyListItem')
->with(
'This share is valid until 2001-01-01 at midnight',
'Expiration:',
'https://example.com/core/img/caldav/time.png',
);
->method('addBodyDetails')
->with($this->callback(function (EMailDetails $details): bool {
$rows = $details->getRows();
return $details->getTitle() === 'file.txt'
&& count($rows) === 1
&& $rows[0]->getLabel() === 'Valid until'
&& $rows[0]->getParts()[0]['text'] === '2001-01-01';
}));

$template
->expects($this->once())
Expand Down Expand Up @@ -1777,6 +1805,10 @@ public function testSendMailNotificationWithSameUserAndUserEmailAndReplyToDesact
$template
->expects($this->once())
->method('addHeader');
$template
->expects($this->once())
->method('addBodySender')
->with('Mrs. Owner User', '');
$template
->expects($this->once())
->method('addHeading')
Expand Down
Loading