From 13e12baaf02380b9ef574e7d630b9fa61fd385d2 Mon Sep 17 00:00:00 2001 From: nuxsmin Date: Mon, 19 Nov 2018 18:44:18 +0100 Subject: [PATCH] * [FIX] Fixed wrong behavior when setting mail parameters * [MOD] Improved email message templates Signed-off-by: nuxsmin --- .../web/Controllers/AccountController.php | 7 ++++-- .../Controllers/ConfigAccountController.php | 13 +++++++--- .../ConfigEncryptionController.php | 18 +++++++++++--- .../Controllers/ConfigGeneralController.php | 13 +++++++--- .../Controllers/ConfigImportController.php | 12 ++++++++-- .../web/Controllers/ConfigMailController.php | 24 +++++++++++++------ .../material-blue/views/config/mail.inc | 4 ++-- lib/SP/Core/Events/EventMessage.php | 2 +- lib/SP/Providers/Mail/MailHandler.php | 13 ++++++---- .../Notification/NotificationHandler.php | 2 +- lib/SP/Services/Install/Installer.php | 4 ++-- lib/SP/Services/Mail/MailService.php | 19 +++++++++------ tests/res/config/config.xml | 8 +++---- 13 files changed, 98 insertions(+), 41 deletions(-) diff --git a/app/modules/web/Controllers/AccountController.php b/app/modules/web/Controllers/AccountController.php index f254ed5e..e7378eba 100644 --- a/app/modules/web/Controllers/AccountController.php +++ b/app/modules/web/Controllers/AccountController.php @@ -1054,6 +1054,9 @@ final class AccountController extends ControllerBase implements CrudControllerIn $accountDetails = $this->accountService->getById($id)->getAccountVData(); + $deepLink = new Uri(Bootstrap::$WEBURI . Bootstrap::$SUBURI); + $deepLink->addParam('r', Acl::getActionRoute(ActionsInterface::ACCOUNT_VIEW) . '/' . $id); + $this->eventDispatcher->notifyEvent('request.account', new Event($this, EventMessage::factory() ->addDescription(__u('Request')) @@ -1061,10 +1064,10 @@ final class AccountController extends ControllerBase implements CrudControllerIn ->addDetail(__u('Account'), $accountDetails->getName()) ->addDetail(__u('Client'), $accountDetails->getClientName()) ->addDetail(__u('Description'), $description) + ->addDetail(__u('Link'), $deepLink->getUriSigned($this->configData->getPasswordSalt())) ->addExtra('accountId', $id) ->addExtra('whoId', $this->userData->getId()) - ->addExtra('userId', $accountDetails->userId) - ->addExtra('userId', $accountDetails->userEditId)) + ->setExtra('userId', [$accountDetails->userId, $accountDetails->userEditId])) ); return $this->returnJsonResponseData( diff --git a/app/modules/web/Controllers/ConfigAccountController.php b/app/modules/web/Controllers/ConfigAccountController.php index 471469b5..1ab100f2 100644 --- a/app/modules/web/Controllers/ConfigAccountController.php +++ b/app/modules/web/Controllers/ConfigAccountController.php @@ -105,9 +105,16 @@ final class ConfigAccountController extends SimpleControllerBase } - return $this->saveConfig($configData, $this->config, function () use ($eventMessage) { - $this->eventDispatcher->notifyEvent('save.config.account', new Event($this, $eventMessage)); - }); + return $this->saveConfig( + $configData, + $this->config, + function () use ($eventMessage) { + $this->eventDispatcher->notifyEvent( + 'save.config.account', + new Event($this, $eventMessage) + ); + } + ); } /** diff --git a/app/modules/web/Controllers/ConfigEncryptionController.php b/app/modules/web/Controllers/ConfigEncryptionController.php index 547d70d5..b71008e9 100644 --- a/app/modules/web/Controllers/ConfigEncryptionController.php +++ b/app/modules/web/Controllers/ConfigEncryptionController.php @@ -144,7 +144,11 @@ final class ConfigEncryptionController extends SimpleControllerBase } } - return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS_STICKY, __u('Master password updated'), [__u('Please, restart the session for update it')]); + return $this->returnJsonResponse( + JsonResponse::JSON_SUCCESS_STICKY, + __u('Master password updated'), + [__u('Please, restart the session for update it')] + ); } /** @@ -196,13 +200,21 @@ final class ConfigEncryptionController extends SimpleControllerBase try { $temporaryMasterPassService->sendByEmailForGroup($groupId, $key); - return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Temporary password generated'), [__u('Email sent')]); + return $this->returnJsonResponse( + JsonResponse::JSON_SUCCESS, + __u('Temporary password generated'), + [__u('Email sent')] + ); } catch (\Exception $e) { processException($e); $this->eventDispatcher->notifyEvent('exception', new Event($e)); - return $this->returnJsonResponse(JsonResponse::JSON_WARNING, __u('Temporary password generated'), [__u('Error while sending the email')]); + return $this->returnJsonResponse( + JsonResponse::JSON_WARNING, + __u('Temporary password generated'), + [__u('Error while sending the email')] + ); } } diff --git a/app/modules/web/Controllers/ConfigGeneralController.php b/app/modules/web/Controllers/ConfigGeneralController.php index 20f4b8a0..541f5bab 100644 --- a/app/modules/web/Controllers/ConfigGeneralController.php +++ b/app/modules/web/Controllers/ConfigGeneralController.php @@ -166,9 +166,16 @@ final class ConfigGeneralController extends SimpleControllerBase $eventMessage->addDescription(__u('Auth Basic disabled')); } - return $this->saveConfig($configData, $this->config, function () use ($eventMessage) { - $this->eventDispatcher->notifyEvent('save.config.general', new Event($this, $eventMessage)); - }); + return $this->saveConfig( + $configData, + $this->config, + function () use ($eventMessage) { + $this->eventDispatcher->notifyEvent( + 'save.config.general', + new Event($this, $eventMessage) + ); + } + ); } /** diff --git a/app/modules/web/Controllers/ConfigImportController.php b/app/modules/web/Controllers/ConfigImportController.php index 274ff2e7..20dbcc10 100644 --- a/app/modules/web/Controllers/ConfigImportController.php +++ b/app/modules/web/Controllers/ConfigImportController.php @@ -79,10 +79,18 @@ final class ConfigImportController extends SimpleControllerBase ); if ($counter > 0) { - return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Import finished'), [__u('Please check out the event log for more details')]); + return $this->returnJsonResponse( + JsonResponse::JSON_SUCCESS, + __u('Import finished'), + [__u('Please check out the event log for more details')] + ); } - return $this->returnJsonResponse(JsonResponse::JSON_WARNING, __u('No accounts were imported'), [__u('Please check out the event log for more details')]); + return $this->returnJsonResponse( + JsonResponse::JSON_WARNING, + __u('No accounts were imported'), + [__u('Please check out the event log for more details')] + ); } catch (\Exception $e) { processException($e); diff --git a/app/modules/web/Controllers/ConfigMailController.php b/app/modules/web/Controllers/ConfigMailController.php index 62ece0ba..304a484a 100644 --- a/app/modules/web/Controllers/ConfigMailController.php +++ b/app/modules/web/Controllers/ConfigMailController.php @@ -68,7 +68,9 @@ final class ConfigMailController extends SimpleControllerBase $mailRecipients = ConfigUtil::mailAddressesAdapter($this->request->analyzeString('mail_recipients')); // Valores para la configuración del Correo - if ($mailEnabled && (!$mailServer || !$mailFrom || count($mailRecipients) === 0)) { + if ($mailEnabled + && (empty($mailServer) || empty($mailFrom) || empty($mailRecipients)) + ) { return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('Missing Mail parameters')); } @@ -106,9 +108,16 @@ final class ConfigMailController extends SimpleControllerBase return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('No changes')); } - return $this->saveConfig($configData, $this->config, function () use ($eventMessage) { - $this->eventDispatcher->notifyEvent('save.config.mail', new Event($this, $eventMessage)); - }); + return $this->saveConfig( + $configData, + $this->config, + function () use ($eventMessage) { + $this->eventDispatcher->notifyEvent( + 'save.config.mail', + new Event($this, $eventMessage) + ); + } + ); } /** @@ -125,11 +134,11 @@ final class ConfigMailController extends SimpleControllerBase $mailParams->port = $this->request->analyzeInt('mail_port', 25); $mailParams->security = $this->request->analyzeString('mail_security'); $mailParams->from = $this->request->analyzeEmail('mail_from'); - $mailParams->mailAuthenabled = $this->request->analyzeBool('mail_authenabled', false); + $mailParams->mailAuthenabled = $this->request->analyzeBool('mail_auth_enabled', false); $mailRecipients = ConfigUtil::mailAddressesAdapter($this->request->analyzeString('mail_recipients')); // Valores para la configuración del Correo - if (!$mailParams->server || empty($mailParams->from) || empty($mailRecipients)) { + if (empty($mailParams->server) || empty($mailParams->from) || empty($mailRecipients)) { return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('Missing Mail parameters')); } @@ -164,7 +173,8 @@ final class ConfigMailController extends SimpleControllerBase /** * @return bool */ - protected function initialize() + protected + function initialize() { try { $this->checks(); diff --git a/app/modules/web/themes/material-blue/views/config/mail.inc b/app/modules/web/themes/material-blue/views/config/mail.inc index 401ce0a1..6f48d4c6 100644 --- a/app/modules/web/themes/material-blue/views/config/mail.inc +++ b/app/modules/web/themes/material-blue/views/config/mail.inc @@ -145,7 +145,7 @@ - +
@@ -153,7 +153,7 @@ class="mdl-textfield__input mdl-color-text--indigo-400" maxlength="128" value="getMailFrom(); ?>"/> + for="mail_from">
diff --git a/lib/SP/Core/Events/EventMessage.php b/lib/SP/Core/Events/EventMessage.php index c69b28ac..7ec0398b 100644 --- a/lib/SP/Core/Events/EventMessage.php +++ b/lib/SP/Core/Events/EventMessage.php @@ -141,7 +141,7 @@ final class EventMessage implements MessageInterface */ public function composeText($delimiter = PHP_EOL) { - $formatter = new TextFormatter(); + $formatter = new TextFormatter($delimiter); return implode($delimiter, [ $this->getDescription($formatter, true), diff --git a/lib/SP/Providers/Mail/MailHandler.php b/lib/SP/Providers/Mail/MailHandler.php index 946f37d6..fa8f7423 100644 --- a/lib/SP/Providers/Mail/MailHandler.php +++ b/lib/SP/Providers/Mail/MailHandler.php @@ -54,7 +54,8 @@ final class MailHandler extends Provider implements EventReceiver 'update.masterPassword.end', 'import.ldap.end', 'run.backup.end', - 'run.import.end' + 'run.import.end', + 'request.account' ]; /** @@ -121,13 +122,17 @@ final class MailHandler extends Provider implements EventReceiver $userData = $this->context->getUserData(); $mailMessage = new MailMessage(); - $mailMessage->addDescription($eventMessage->composeText()); + $mailMessage->addDescription($eventMessage->composeText('
')); + $mailMessage->addDescriptionLine(); $mailMessage->addDescription(sprintf(__('Performed by: %s (%s)'), $userData->getName(), $userData->getLogin())); $mailMessage->addDescription(sprintf(__('IP Address: %s'), $this->request->getClientAddress(true))); + $subject = $eventMessage->getDescription(new TextFormatter(), true) ?: $eventType; + $this->mailService->send( - $eventMessage->getDescription(new TextFormatter(), true), - $configData->getMailFrom(), $mailMessage + $subject, + $configData->getMailRecipients(), + $mailMessage ); } catch (\Exception $e) { processException($e); diff --git a/lib/SP/Providers/Notification/NotificationHandler.php b/lib/SP/Providers/Notification/NotificationHandler.php index dbb3574c..0e9b41e2 100644 --- a/lib/SP/Providers/Notification/NotificationHandler.php +++ b/lib/SP/Providers/Notification/NotificationHandler.php @@ -124,7 +124,7 @@ final class NotificationHandler extends Provider implements EventReceiver $notificationData->setType(__('Request')); $notificationData->setComponent(__('Accounts')); $notificationData->setUserId($userId); - $notificationData->setDescription($eventMessage); + $notificationData->setDescription($eventMessage, true); $this->notify($notificationData); } diff --git a/lib/SP/Services/Install/Installer.php b/lib/SP/Services/Install/Installer.php index e769ebc9..caa70a21 100644 --- a/lib/SP/Services/Install/Installer.php +++ b/lib/SP/Services/Install/Installer.php @@ -56,8 +56,8 @@ final class Installer extends Service * sysPass' version and build number */ const VERSION = [3, 0, 0]; - const VERSION_TEXT = '3.0-rc3'; - const BUILD = 18111901; + const VERSION_TEXT = '3.0-rc4'; + const BUILD = 18111902; /** * @var DatabaseSetupInterface diff --git a/lib/SP/Services/Mail/MailService.php b/lib/SP/Services/Mail/MailService.php index 83e5a7ef..0de0b1c5 100644 --- a/lib/SP/Services/Mail/MailService.php +++ b/lib/SP/Services/Mail/MailService.php @@ -112,16 +112,24 @@ final class MailService extends Service } /** - * @param string $subject - * @param string $to - * @param MailMessage $mailMessage + * @param string $subject + * @param array|string $to + * @param MailMessage $mailMessage * * @throws ServiceException */ public function send($subject, $to, MailMessage $mailMessage) { $this->mailer->isHTML(); - $this->mailer->addAddress($to); + + if (is_array($to)) { + foreach ($to as $addr) { + $this->mailer->addAddress($addr); + } + } else { + $this->mailer->addAddress($to); + } + $this->mailer->Subject = $this->getSubjectForAction($subject); $this->mailer->Body = $mailMessage->setFooter($this->getEmailFooter())->composeHtml(); @@ -175,15 +183,12 @@ final class MailService extends Service /** * @throws MailProviderException - * @throws ServiceException */ protected function initialize() { if ($this->config->getConfigData()->isMailEnabled()) { $this->mailer = $this->dic->get(MailProvider::class) ->getMailer($this->getParamsFromConfig()); - } else { - throw new ServiceException(__u('Mail service unavailable')); } } diff --git a/tests/res/config/config.xml b/tests/res/config/config.xml index 53244c27..7e1237c3 100644 --- a/tests/res/config/config.xml +++ b/tests/res/config/config.xml @@ -9,11 +9,11 @@ 1 1 - 9f46d4211297e5181cd3429f664a764642eb6e89 + 5f5eef95895649260f8bfb4f114b39251cf521f4 0 0 - 1542566884 - 4c1542f238fd13ce49f76a40492e0aeb2f366a25 + 1542583425 + 5f791f08c264f6053eb9de6f84825ce6baa6c0df @@ -32,7 +32,7 @@ 0 - f87c94a0f34f0f305864a68556118f058d3ea842 + 1c3433465d2b0a65bfbae4fe1dae9c2f308d2f4d PDF JPG