Skip to content

Commit 0b88b51

Browse files
Merge pull request #36351 from nextcloud/bugfix/noid/move-encryption-password-email-to-template
Move encrypt-all password email to EmailTemplate
2 parents 8a79636 + f60e1f7 commit 0b88b51

File tree

4 files changed

+42
-93
lines changed

4 files changed

+42
-93
lines changed

apps/encryption/lib/Crypto/EncryptAll.php

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@
3434
use OCA\Encryption\Util;
3535
use OCP\IConfig;
3636
use OCP\IL10N;
37+
use OCP\IUser;
3738
use OCP\IUserManager;
39+
use OCP\L10N\IFactory;
40+
use OCP\Mail\Headers\AutoSubmitted;
3841
use OCP\Mail\IMailer;
3942
use OCP\Security\ISecureRandom;
4043
use Symfony\Component\Console\Helper\ProgressBar;
@@ -73,6 +76,9 @@ class EncryptAll {
7376
/** @var IL10N */
7477
protected $l;
7578

79+
/** @var IFactory */
80+
protected $l10nFactory;
81+
7682
/** @var QuestionHelper */
7783
protected $questionHelper;
7884

@@ -85,18 +91,6 @@ class EncryptAll {
8591
/** @var ISecureRandom */
8692
protected $secureRandom;
8793

88-
/**
89-
* @param Setup $userSetup
90-
* @param IUserManager $userManager
91-
* @param View $rootView
92-
* @param KeyManager $keyManager
93-
* @param Util $util
94-
* @param IConfig $config
95-
* @param IMailer $mailer
96-
* @param IL10N $l
97-
* @param QuestionHelper $questionHelper
98-
* @param ISecureRandom $secureRandom
99-
*/
10094
public function __construct(
10195
Setup $userSetup,
10296
IUserManager $userManager,
@@ -106,6 +100,7 @@ public function __construct(
106100
IConfig $config,
107101
IMailer $mailer,
108102
IL10N $l,
103+
IFactory $l10nFactory,
109104
QuestionHelper $questionHelper,
110105
ISecureRandom $secureRandom
111106
) {
@@ -117,6 +112,7 @@ public function __construct(
117112
$this->config = $config;
118113
$this->mailer = $mailer;
119114
$this->l = $l;
115+
$this->l10nFactory = $l10nFactory;
120116
$this->questionHelper = $questionHelper;
121117
$this->secureRandom = $secureRandom;
122118
// store one time passwords for the users
@@ -413,6 +409,10 @@ protected function sendPasswordsByMail() {
413409
$progress->advance();
414410
if (!empty($password)) {
415411
$recipient = $this->userManager->get($uid);
412+
if (!$recipient instanceof IUser) {
413+
continue;
414+
}
415+
416416
$recipientDisplayName = $recipient->getDisplayName();
417417
$to = $recipient->getEMailAddress();
418418

@@ -421,20 +421,33 @@ protected function sendPasswordsByMail() {
421421
continue;
422422
}
423423

424-
$subject = $this->l->t('one-time password for server-side-encryption');
425-
[$htmlBody, $textBody] = $this->createMailBody($password);
424+
$l = $this->l10nFactory->get('encryption', $this->l10nFactory->getUserLanguage($recipient));
425+
426+
$template = $this->mailer->createEMailTemplate('encryption.encryptAllPassword', [
427+
'user' => $recipient->getUID(),
428+
'password' => $password,
429+
]);
430+
431+
$template->setSubject($l->t('one-time password for server-side-encryption'));
432+
// 'Hey there,<br><br>The administration enabled server-side-encryption. Your files were encrypted using the password <strong>%s</strong>.<br><br>
433+
// Please login to the web interface, go to the section "Basic encryption module" of your personal settings and update your encryption password by entering this password into the "Old log-in password" field and your current login-password.<br><br>'
434+
$template->addHeader();
435+
$template->addHeading($l->t('Encryption password'));
436+
$template->addBodyText(
437+
$l->t('The administration enabled server-side-encryption. Your files were encrypted using the password <strong>%s</strong>.', [htmlspecialchars($password)]),
438+
$l->t('The administration enabled server-side-encryption. Your files were encrypted using the password "%s".', $password)
439+
);
440+
$template->addBodyText(
441+
$l->t('Please login to the web interface, go to the "Security" section of your personal settings and update your encryption password by entering this password into the "Old log-in password" field and your current login-password.')
442+
);
443+
$template->addFooter();
426444

427445
// send it out now
428446
try {
429447
$message = $this->mailer->createMessage();
430-
$message->setSubject($subject);
431448
$message->setTo([$to => $recipientDisplayName]);
432-
$message->setHtmlBody($htmlBody);
433-
$message->setPlainBody($textBody);
434-
$message->setFrom([
435-
\OCP\Util::getDefaultEmailAddress('admin-noreply')
436-
]);
437-
449+
$message->useTemplate($template);
450+
$message->setAutoSubmitted(AutoSubmitted::VALUE_AUTO_GENERATED);
438451
$this->mailer->send($message);
439452
} catch (\Exception $e) {
440453
$noMail[] = $uid;
@@ -458,22 +471,4 @@ protected function sendPasswordsByMail() {
458471
$table->render();
459472
}
460473
}
461-
462-
/**
463-
* create mail body for plain text and html mail
464-
*
465-
* @param string $password one-time encryption password
466-
* @return array an array of the html mail body and the plain text mail body
467-
*/
468-
protected function createMailBody($password) {
469-
$html = new \OC_Template("encryption", "mail", "");
470-
$html->assign('password', $password);
471-
$htmlMail = $html->fetchPage();
472-
473-
$plainText = new \OC_Template("encryption", "altmail", "");
474-
$plainText->assign('password', $password);
475-
$plainTextMail = $plainText->fetchPage();
476-
477-
return [$htmlMail, $plainTextMail];
478-
}
479474
}

apps/encryption/templates/altmail.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

apps/encryption/templates/mail.php

Lines changed: 0 additions & 38 deletions
This file was deleted.

apps/encryption/tests/Crypto/EncryptAllTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use OCP\IConfig;
3737
use OCP\IL10N;
3838
use OCP\IUserManager;
39+
use OCP\L10N\IFactory;
3940
use OCP\Mail\IMailer;
4041
use OCP\Security\ISecureRandom;
4142
use OCP\UserInterface;
@@ -106,6 +107,7 @@ protected function setUp(): void {
106107
->disableOriginalConstructor()->getMock();
107108
$this->mailer = $this->getMockBuilder(IMailer::class)
108109
->disableOriginalConstructor()->getMock();
110+
$this->l10nFactory = $this->createMock(IFactory::class);
109111
$this->l = $this->getMockBuilder(IL10N::class)
110112
->disableOriginalConstructor()->getMock();
111113
$this->questionHelper = $this->getMockBuilder(QuestionHelper::class)
@@ -140,6 +142,7 @@ protected function setUp(): void {
140142
$this->config,
141143
$this->mailer,
142144
$this->l,
145+
$this->l10nFactory,
143146
$this->questionHelper,
144147
$this->secureRandom
145148
);
@@ -158,6 +161,7 @@ public function testEncryptAll() {
158161
$this->config,
159162
$this->mailer,
160163
$this->l,
164+
$this->l10nFactory,
161165
$this->questionHelper,
162166
$this->secureRandom
163167
]
@@ -186,6 +190,7 @@ public function testEncryptAllWithMasterKey() {
186190
$this->config,
187191
$this->mailer,
188192
$this->l,
193+
$this->l10nFactory,
189194
$this->questionHelper,
190195
$this->secureRandom
191196
]
@@ -215,6 +220,7 @@ public function testCreateKeyPairs() {
215220
$this->config,
216221
$this->mailer,
217222
$this->l,
223+
$this->l10nFactory,
218224
$this->questionHelper,
219225
$this->secureRandom
220226
]
@@ -264,6 +270,7 @@ public function testEncryptAllUsersFiles() {
264270
$this->config,
265271
$this->mailer,
266272
$this->l,
273+
$this->l10nFactory,
267274
$this->questionHelper,
268275
$this->secureRandom
269276
]
@@ -299,6 +306,7 @@ public function testEncryptUsersFiles() {
299306
$this->config,
300307
$this->mailer,
301308
$this->l,
309+
$this->l10nFactory,
302310
$this->questionHelper,
303311
$this->secureRandom
304312
]

0 commit comments

Comments
 (0)