Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move encrypt-all password email to EmailTemplate #36351

Merged
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
73 changes: 34 additions & 39 deletions apps/encryption/lib/Crypto/EncryptAll.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
use OCA\Encryption\Util;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IUser;
use OCP\IUserManager;
use OCP\L10N\IFactory;
use OCP\Mail\Headers\AutoSubmitted;
use OCP\Mail\IMailer;
use OCP\Security\ISecureRandom;
use Symfony\Component\Console\Helper\ProgressBar;
Expand Down Expand Up @@ -73,6 +76,9 @@ class EncryptAll {
/** @var IL10N */
protected $l;

/** @var IFactory */
protected $l10nFactory;

/** @var QuestionHelper */
protected $questionHelper;

Expand All @@ -85,18 +91,6 @@ class EncryptAll {
/** @var ISecureRandom */
protected $secureRandom;

/**
* @param Setup $userSetup
* @param IUserManager $userManager
* @param View $rootView
* @param KeyManager $keyManager
* @param Util $util
* @param IConfig $config
* @param IMailer $mailer
* @param IL10N $l
* @param QuestionHelper $questionHelper
* @param ISecureRandom $secureRandom
*/
public function __construct(
Setup $userSetup,
IUserManager $userManager,
Expand All @@ -106,6 +100,7 @@ public function __construct(
IConfig $config,
IMailer $mailer,
IL10N $l,
IFactory $l10nFactory,
QuestionHelper $questionHelper,
ISecureRandom $secureRandom
) {
Expand All @@ -117,6 +112,7 @@ public function __construct(
$this->config = $config;
$this->mailer = $mailer;
$this->l = $l;
$this->l10nFactory = $l10nFactory;
$this->questionHelper = $questionHelper;
$this->secureRandom = $secureRandom;
// store one time passwords for the users
Expand Down Expand Up @@ -413,6 +409,10 @@ protected function sendPasswordsByMail() {
$progress->advance();
if (!empty($password)) {
$recipient = $this->userManager->get($uid);
if (!$recipient instanceof IUser) {
continue;
}

$recipientDisplayName = $recipient->getDisplayName();
$to = $recipient->getEMailAddress();

Expand All @@ -421,20 +421,33 @@ protected function sendPasswordsByMail() {
continue;
}

$subject = $this->l->t('one-time password for server-side-encryption');
[$htmlBody, $textBody] = $this->createMailBody($password);
$l = $this->l10nFactory->get('encryption', $this->l10nFactory->getUserLanguage($recipient));

$template = $this->mailer->createEMailTemplate('encryption.encryptAllPassword', [
'user' => $recipient->getUID(),
'password' => $password,
]);

$template->setSubject($l->t('one-time password for server-side-encryption'));
// 'Hey there,<br><br>The administration enabled server-side-encryption. Your files were encrypted using the password <strong>%s</strong>.<br><br>
// 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>'
$template->addHeader();
$template->addHeading($l->t('Encryption password'));
$template->addBodyText(
$l->t('The administration enabled server-side-encryption. Your files were encrypted using the password <strong>%s</strong>.', [htmlspecialchars($password)]),
$l->t('The administration enabled server-side-encryption. Your files were encrypted using the password "%s".', $password)
);
$template->addBodyText(
$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.')
);
$template->addFooter();

// send it out now
try {
$message = $this->mailer->createMessage();
$message->setSubject($subject);
$message->setTo([$to => $recipientDisplayName]);
$message->setHtmlBody($htmlBody);
$message->setPlainBody($textBody);
$message->setFrom([
\OCP\Util::getDefaultEmailAddress('admin-noreply')
]);

$message->useTemplate($template);
$message->setAutoSubmitted(AutoSubmitted::VALUE_AUTO_GENERATED);
$this->mailer->send($message);
} catch (\Exception $e) {
$noMail[] = $uid;
Expand All @@ -458,22 +471,4 @@ protected function sendPasswordsByMail() {
$table->render();
}
}

/**
* create mail body for plain text and html mail
*
* @param string $password one-time encryption password
* @return array an array of the html mail body and the plain text mail body
*/
protected function createMailBody($password) {
$html = new \OC_Template("encryption", "mail", "");
$html->assign('password', $password);
$htmlMail = $html->fetchPage();

$plainText = new \OC_Template("encryption", "altmail", "");
$plainText->assign('password', $password);
$plainTextMail = $plainText->fetchPage();

return [$htmlMail, $plainTextMail];
}
}
16 changes: 0 additions & 16 deletions apps/encryption/templates/altmail.php

This file was deleted.

38 changes: 0 additions & 38 deletions apps/encryption/templates/mail.php

This file was deleted.

8 changes: 8 additions & 0 deletions apps/encryption/tests/Crypto/EncryptAllTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use OCP\IConfig;
use OCP\IL10N;
use OCP\IUserManager;
use OCP\L10N\IFactory;
use OCP\Mail\IMailer;
use OCP\Security\ISecureRandom;
use OCP\UserInterface;
Expand Down Expand Up @@ -106,6 +107,7 @@ protected function setUp(): void {
->disableOriginalConstructor()->getMock();
$this->mailer = $this->getMockBuilder(IMailer::class)
->disableOriginalConstructor()->getMock();
$this->l10nFactory = $this->createMock(IFactory::class);
$this->l = $this->getMockBuilder(IL10N::class)
->disableOriginalConstructor()->getMock();
$this->questionHelper = $this->getMockBuilder(QuestionHelper::class)
Expand Down Expand Up @@ -140,6 +142,7 @@ protected function setUp(): void {
$this->config,
$this->mailer,
$this->l,
$this->l10nFactory,
$this->questionHelper,
$this->secureRandom
);
Expand All @@ -158,6 +161,7 @@ public function testEncryptAll() {
$this->config,
$this->mailer,
$this->l,
$this->l10nFactory,
$this->questionHelper,
$this->secureRandom
]
Expand Down Expand Up @@ -186,6 +190,7 @@ public function testEncryptAllWithMasterKey() {
$this->config,
$this->mailer,
$this->l,
$this->l10nFactory,
$this->questionHelper,
$this->secureRandom
]
Expand Down Expand Up @@ -215,6 +220,7 @@ public function testCreateKeyPairs() {
$this->config,
$this->mailer,
$this->l,
$this->l10nFactory,
$this->questionHelper,
$this->secureRandom
]
Expand Down Expand Up @@ -264,6 +270,7 @@ public function testEncryptAllUsersFiles() {
$this->config,
$this->mailer,
$this->l,
$this->l10nFactory,
$this->questionHelper,
$this->secureRandom
]
Expand Down Expand Up @@ -299,6 +306,7 @@ public function testEncryptUsersFiles() {
$this->config,
$this->mailer,
$this->l,
$this->l10nFactory,
$this->questionHelper,
$this->secureRandom
]
Expand Down