Skip to content

Commit 9464308

Browse files
authored
Merge pull request #24978 from dems54/issue24139
2 parents c222bac + 69bf0c8 commit 9464308

File tree

6 files changed

+246
-26
lines changed

6 files changed

+246
-26
lines changed

apps/sharebymail/js/settings-admin.js

+8
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,12 @@ $(function() {
3535
OCP.AppConfig.setValue('sharebymail', 'enforcePasswordProtection', status);
3636
});
3737

38+
$('#replyToInitiator').on('change', function() {
39+
var status = 'no';
40+
if ($(this).is(':checked')) {
41+
status = 'yes';
42+
}
43+
OCP.AppConfig.setValue('sharebymail', 'replyToInitiator', status);
44+
});
45+
3846
});

apps/sharebymail/lib/Settings/Admin.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public function __construct(SettingsManager $settingsManager) {
4141
public function getForm() {
4242
$parameters = [
4343
'sendPasswordMail' => $this->settingsManager->sendPasswordByMail(),
44-
'enforcePasswordProtection' => $this->settingsManager->enforcePasswordProtection()
44+
'enforcePasswordProtection' => $this->settingsManager->enforcePasswordProtection(),
45+
'replyToInitiator' => $this->settingsManager->replyToInitiator()
4546
];
4647

4748
return new TemplateResponse('sharebymail', 'settings-admin', $parameters, '');

apps/sharebymail/lib/Settings/SettingsManager.php

+12
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class SettingsManager {
3838

3939
private $enforcePasswordProtectionDefault = 'no';
4040

41+
private $replyToInitiatorDefault = 'yes';
42+
4143
public function __construct(IConfig $config) {
4244
$this->config = $config;
4345
}
@@ -61,4 +63,14 @@ public function enforcePasswordProtection(): bool {
6163
$enforcePassword = $this->config->getAppValue('sharebymail', 'enforcePasswordProtection', $this->enforcePasswordProtectionDefault);
6264
return $enforcePassword === 'yes';
6365
}
66+
67+
/**
68+
* should add reply to with initiator mail
69+
*
70+
* @return bool
71+
*/
72+
public function replyToInitiator(): bool {
73+
$replyToInitiator = $this->config->getAppValue('sharebymail', 'replyToInitiator', $this->replyToInitiatorDefault);
74+
return $replyToInitiator === 'yes';
75+
}
6476
}

apps/sharebymail/lib/ShareByMailProvider.php

+33-24
Original file line numberDiff line numberDiff line change
@@ -399,19 +399,22 @@ protected function sendMailNotification($filename,
399399

400400
// The "From" contains the sharers name
401401
$instanceName = $this->defaults->getName();
402-
$senderName = $this->l->t(
403-
'%1$s via %2$s',
404-
[
405-
$initiatorDisplayName,
406-
$instanceName
407-
]
408-
);
402+
$senderName = $instanceName;
403+
if ($this->settingsManager->replyToInitiator()) {
404+
$senderName = $this->l->t(
405+
'%1$s via %2$s',
406+
[
407+
$initiatorDisplayName,
408+
$instanceName
409+
]
410+
);
411+
}
409412
$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
410413

411414
// The "Reply-To" is set to the sharer if an mail address is configured
412415
// also the default footer contains a "Do not reply" which needs to be adjusted.
413416
$initiatorEmail = $initiatorUser->getEMailAddress();
414-
if ($initiatorEmail !== null) {
417+
if ($this->settingsManager->replyToInitiator() && $initiatorEmail !== null) {
415418
$message->setReplyTo([$initiatorEmail => $initiatorDisplayName]);
416419
$emailTemplate->addFooter($instanceName . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : ''));
417420
} else {
@@ -464,15 +467,18 @@ protected function sendPassword(IShare $share, $password) {
464467

465468
// The "From" contains the sharers name
466469
$instanceName = $this->defaults->getName();
467-
$senderName = $this->l->t(
468-
'%1$s via %2$s',
469-
[
470-
$initiatorDisplayName,
471-
$instanceName
472-
]
473-
);
470+
$senderName = $instanceName;
471+
if ($this->settingsManager->replyToInitiator()) {
472+
$senderName = $this->l->t(
473+
'%1$s via %2$s',
474+
[
475+
$initiatorDisplayName,
476+
$instanceName
477+
]
478+
);
479+
}
474480
$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
475-
if ($initiatorEmailAddress !== null) {
481+
if ($this->settingsManager->replyToInitiator() && $initiatorEmailAddress !== null) {
476482
$message->setReplyTo([$initiatorEmailAddress => $initiatorDisplayName]);
477483
$emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
478484
} else {
@@ -521,15 +527,18 @@ protected function sendNote(IShare $share) {
521527

522528
// The "From" contains the sharers name
523529
$instanceName = $this->defaults->getName();
524-
$senderName = $this->l->t(
525-
'%1$s via %2$s',
526-
[
527-
$initiatorDisplayName,
528-
$instanceName
529-
]
530-
);
530+
$senderName = $instanceName;
531+
if ($this->settingsManager->replyToInitiator()) {
532+
$senderName = $this->l->t(
533+
'%1$s via %2$s',
534+
[
535+
$initiatorDisplayName,
536+
$instanceName
537+
]
538+
);
539+
}
531540
$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
532-
if ($initiatorEmailAddress !== null) {
541+
if ($this->settingsManager->replyToInitiator() && $initiatorEmailAddress !== null) {
533542
$message->setReplyTo([$initiatorEmailAddress => $initiatorDisplayName]);
534543
$emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
535544
} else {

apps/sharebymail/templates/settings-admin.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
<input id="enforcePasswordProtection" type="checkbox" class="checkbox" <?php if ($_['enforcePasswordProtection']) {
1818
p('checked');
1919
} ?> />
20-
<label for="enforcePasswordProtection"><?php p($l->t('Enforce password protection')); ?></label>
20+
<label for="enforcePasswordProtection"><?php p($l->t('Enforce password protection')); ?></label><br/>
21+
<input id="replyToInitiator" type="checkbox" class="checkbox" <?php if ($_['replyToInitiator']) {
22+
p('checked');
23+
} ?> />
24+
<label for="replyToInitiator"><?php p($l->t('Reply to initiator')); ?></label>
2125
</p>
2226

2327
</div>

apps/sharebymail/tests/ShareByMailProviderTest.php

+186
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,7 @@ public function testGetAccessList() {
10771077
public function testSendMailNotificationWithSameUserAndUserEmail() {
10781078
$provider = $this->getInstance();
10791079
$user = $this->createMock(IUser::class);
1080+
$this->settingsManager->expects($this->any())->method('replyToInitiator')->willReturn(true);
10801081
$this->userManager
10811082
->expects($this->once())
10821083
->method('get')
@@ -1175,6 +1176,7 @@ public function testSendMailNotificationWithSameUserAndUserEmail() {
11751176
public function testSendMailNotificationWithDifferentUserAndNoUserEmail() {
11761177
$provider = $this->getInstance();
11771178
$initiatorUser = $this->createMock(IUser::class);
1179+
$this->settingsManager->expects($this->any())->method('replyToInitiator')->willReturn(true);
11781180
$this->userManager
11791181
->expects($this->once())
11801182
->method('get')
@@ -1260,4 +1262,188 @@ public function testSendMailNotificationWithDifferentUserAndNoUserEmail() {
12601262
null,
12611263
]);
12621264
}
1265+
1266+
public function testSendMailNotificationWithSameUserAndUserEmailAndReplyToDesactivate() {
1267+
$provider = $this->getInstance();
1268+
$user = $this->createMock(IUser::class);
1269+
$this->settingsManager->expects($this->any())->method('replyToInitiator')->willReturn(false);
1270+
$this->userManager
1271+
->expects($this->once())
1272+
->method('get')
1273+
->with('OwnerUser')
1274+
->willReturn($user);
1275+
$user
1276+
->expects($this->once())
1277+
->method('getDisplayName')
1278+
->willReturn('Mrs. Owner User');
1279+
$message = $this->createMock(Message::class);
1280+
$this->mailer
1281+
->expects($this->once())
1282+
->method('createMessage')
1283+
->willReturn($message);
1284+
$template = $this->createMock(IEMailTemplate::class);
1285+
$this->mailer
1286+
->expects($this->once())
1287+
->method('createEMailTemplate')
1288+
->willReturn($template);
1289+
$template
1290+
->expects($this->once())
1291+
->method('addHeader');
1292+
$template
1293+
->expects($this->once())
1294+
->method('addHeading')
1295+
->with('Mrs. Owner User shared »file.txt« with you');
1296+
$template
1297+
->expects($this->once())
1298+
->method('addBodyText')
1299+
->with(
1300+
'Mrs. Owner User shared »file.txt« with you. Click the button below to open it.',
1301+
'Mrs. Owner User shared »file.txt« with you.'
1302+
);
1303+
$template
1304+
->expects($this->once())
1305+
->method('addBodyButton')
1306+
->with(
1307+
'Open »file.txt«',
1308+
'https://example.com/file.txt'
1309+
);
1310+
$message
1311+
->expects($this->once())
1312+
->method('setTo')
1313+
->with(['john@doe.com']);
1314+
$this->defaults
1315+
->expects($this->once())
1316+
->method('getName')
1317+
->willReturn('UnitTestCloud');
1318+
$message
1319+
->expects($this->once())
1320+
->method('setFrom')
1321+
->with([
1322+
\OCP\Util::getDefaultEmailAddress('UnitTestCloud') => 'UnitTestCloud'
1323+
]);
1324+
$user
1325+
->expects($this->once())
1326+
->method('getEMailAddress')
1327+
->willReturn('owner@example.com');
1328+
$message
1329+
->expects($this->never())
1330+
->method('setReplyTo');
1331+
$template
1332+
->expects($this->once())
1333+
->method('addFooter')
1334+
->with('');
1335+
$template
1336+
->expects($this->once())
1337+
->method('setSubject')
1338+
->with('Mrs. Owner User shared »file.txt« with you');
1339+
$message
1340+
->expects($this->once())
1341+
->method('useTemplate')
1342+
->with($template);
1343+
$this->mailer
1344+
->expects($this->once())
1345+
->method('send')
1346+
->with($message);
1347+
1348+
self::invokePrivate(
1349+
$provider,
1350+
'sendMailNotification',
1351+
[
1352+
'file.txt',
1353+
'https://example.com/file.txt',
1354+
'OwnerUser',
1355+
'john@doe.com',
1356+
null,
1357+
]);
1358+
}
1359+
1360+
public function testSendMailNotificationWithDifferentUserAndNoUserEmailAndReplyToDesactivate() {
1361+
$provider = $this->getInstance();
1362+
$initiatorUser = $this->createMock(IUser::class);
1363+
$this->settingsManager->expects($this->any())->method('replyToInitiator')->willReturn(false);
1364+
$this->userManager
1365+
->expects($this->once())
1366+
->method('get')
1367+
->with('InitiatorUser')
1368+
->willReturn($initiatorUser);
1369+
$initiatorUser
1370+
->expects($this->once())
1371+
->method('getDisplayName')
1372+
->willReturn('Mr. Initiator User');
1373+
$message = $this->createMock(Message::class);
1374+
$this->mailer
1375+
->expects($this->once())
1376+
->method('createMessage')
1377+
->willReturn($message);
1378+
$template = $this->createMock(IEMailTemplate::class);
1379+
$this->mailer
1380+
->expects($this->once())
1381+
->method('createEMailTemplate')
1382+
->willReturn($template);
1383+
$template
1384+
->expects($this->once())
1385+
->method('addHeader');
1386+
$template
1387+
->expects($this->once())
1388+
->method('addHeading')
1389+
->with('Mr. Initiator User shared »file.txt« with you');
1390+
$template
1391+
->expects($this->once())
1392+
->method('addBodyText')
1393+
->with(
1394+
'Mr. Initiator User shared »file.txt« with you. Click the button below to open it.',
1395+
'Mr. Initiator User shared »file.txt« with you.'
1396+
);
1397+
$template
1398+
->expects($this->once())
1399+
->method('addBodyButton')
1400+
->with(
1401+
'Open »file.txt«',
1402+
'https://example.com/file.txt'
1403+
);
1404+
$message
1405+
->expects($this->once())
1406+
->method('setTo')
1407+
->with(['john@doe.com']);
1408+
$this->defaults
1409+
->expects($this->once())
1410+
->method('getName')
1411+
->willReturn('UnitTestCloud');
1412+
$message
1413+
->expects($this->once())
1414+
->method('setFrom')
1415+
->with([
1416+
\OCP\Util::getDefaultEmailAddress('UnitTestCloud') => 'UnitTestCloud'
1417+
]);
1418+
$message
1419+
->expects($this->never())
1420+
->method('setReplyTo');
1421+
$template
1422+
->expects($this->once())
1423+
->method('addFooter')
1424+
->with('');
1425+
$template
1426+
->expects($this->once())
1427+
->method('setSubject')
1428+
->with('Mr. Initiator User shared »file.txt« with you');
1429+
$message
1430+
->expects($this->once())
1431+
->method('useTemplate')
1432+
->with($template);
1433+
$this->mailer
1434+
->expects($this->once())
1435+
->method('send')
1436+
->with($message);
1437+
1438+
self::invokePrivate(
1439+
$provider,
1440+
'sendMailNotification',
1441+
[
1442+
'file.txt',
1443+
'https://example.com/file.txt',
1444+
'InitiatorUser',
1445+
'john@doe.com',
1446+
null,
1447+
]);
1448+
}
12631449
}

0 commit comments

Comments
 (0)