mirror of
https://github.com/nuxsmin/sysPass.git
synced 2026-03-03 07:04:07 +01:00
refactor: [WIP] Migrate config mail controller.
Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
150
app/modules/web/Controllers/ConfigMail/CheckController.php
Normal file
150
app/modules/web/Controllers/ConfigMail/CheckController.php
Normal file
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
/*
|
||||
* sysPass
|
||||
*
|
||||
* @author nuxsmin
|
||||
* @link https://syspass.org
|
||||
* @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org
|
||||
*
|
||||
* This file is part of sysPass.
|
||||
*
|
||||
* sysPass is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* sysPass is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace SP\Modules\Web\Controllers\ConfigMail;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Klein\Klein;
|
||||
use SP\Core\Acl\Acl;
|
||||
use SP\Core\Acl\ActionsInterface;
|
||||
use SP\Core\Acl\UnauthorizedPageException;
|
||||
use SP\Core\Application;
|
||||
use SP\Core\Events\Event;
|
||||
use SP\Core\Events\EventMessage;
|
||||
use SP\Core\Exceptions\SPException;
|
||||
use SP\Core\Exceptions\ValidationException;
|
||||
use SP\Core\PhpExtensionChecker;
|
||||
use SP\Core\UI\ThemeInterface;
|
||||
use SP\Domain\Config\Services\ConfigUtil;
|
||||
use SP\Domain\Notification\MailServiceInterface;
|
||||
use SP\Http\JsonResponse;
|
||||
use SP\Http\RequestInterface;
|
||||
use SP\Modules\Web\Controllers\SimpleControllerBase;
|
||||
use SP\Modules\Web\Controllers\Traits\ConfigTrait;
|
||||
use SP\Providers\Mail\MailParams;
|
||||
|
||||
/**
|
||||
* Class CheckController
|
||||
*/
|
||||
final class CheckController extends SimpleControllerBase
|
||||
{
|
||||
use ConfigTrait;
|
||||
|
||||
private MailServiceInterface $mailService;
|
||||
|
||||
public function __construct(
|
||||
Application $application,
|
||||
ThemeInterface $theme,
|
||||
Klein $router,
|
||||
Acl $acl,
|
||||
RequestInterface $request,
|
||||
PhpExtensionChecker $extensionChecker,
|
||||
MailServiceInterface $mailService
|
||||
) {
|
||||
parent::__construct($application, $theme, $router, $acl, $request, $extensionChecker);
|
||||
|
||||
$this->mailService = $mailService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function checkAction(): bool
|
||||
{
|
||||
try {
|
||||
$mailParams = $this->handleMailConfig();
|
||||
|
||||
$mailRecipients = ConfigUtil::mailAddressesAdapter($this->request->analyzeString('mail_recipients'));
|
||||
|
||||
// Valores para la configuración del Correo
|
||||
if (empty($mailParams->server) || empty($mailParams->from) || count($mailRecipients) === 0) {
|
||||
throw new ValidationException(SPException::ERROR, __u('Missing Mail parameters'));
|
||||
}
|
||||
|
||||
$this->mailService->check($mailParams, $mailRecipients[0]);
|
||||
|
||||
$this->eventDispatcher->notifyEvent(
|
||||
'send.mail.check',
|
||||
new Event(
|
||||
$this,
|
||||
EventMessage::factory()
|
||||
->addDescription(__u('Email sent'))
|
||||
->addDetail(__u('Recipient'), $mailRecipients[0])
|
||||
)
|
||||
);
|
||||
|
||||
return $this->returnJsonResponse(
|
||||
JsonResponse::JSON_SUCCESS,
|
||||
__u('Email sent'),
|
||||
[__u('Please, check your inbox')]
|
||||
);
|
||||
} catch (Exception $e) {
|
||||
processException($e);
|
||||
|
||||
$this->eventDispatcher->notifyEvent('exception', new Event($e));
|
||||
|
||||
return $this->returnJsonResponseException($e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \SP\Providers\Mail\MailParams
|
||||
*/
|
||||
private function handleMailConfig(): MailParams
|
||||
{
|
||||
$mailParams = new MailParams();
|
||||
$mailParams->server = $this->request->analyzeString('mail_server');
|
||||
$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_auth_enabled', false);
|
||||
|
||||
|
||||
if ($mailParams->mailAuthenabled) {
|
||||
$mailParams->user = $this->request->analyzeString('mail_user');
|
||||
$mailParams->pass = $this->request->analyzeEncrypted('mail_pass');
|
||||
}
|
||||
|
||||
return $mailParams;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws \JsonException
|
||||
* @throws \SP\Core\Exceptions\SessionTimeout
|
||||
*/
|
||||
protected function initialize(): void
|
||||
{
|
||||
try {
|
||||
$this->checks();
|
||||
$this->checkAccess(ActionsInterface::CONFIG_MAIL);
|
||||
} catch (UnauthorizedPageException $e) {
|
||||
$this->eventDispatcher->notifyEvent('exception', new Event($e));
|
||||
|
||||
$this->returnJsonResponseException($e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@
|
||||
*
|
||||
* @author nuxsmin
|
||||
* @link https://syspass.org
|
||||
* @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org
|
||||
* @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org
|
||||
*
|
||||
* This file is part of sysPass.
|
||||
*
|
||||
@@ -22,34 +22,28 @@
|
||||
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace SP\Modules\Web\Controllers;
|
||||
namespace SP\Modules\Web\Controllers\ConfigMail;
|
||||
|
||||
use DI\DependencyException;
|
||||
use DI\NotFoundException;
|
||||
use Exception;
|
||||
use SP\Core\Acl\ActionsInterface;
|
||||
use SP\Core\Acl\UnauthorizedPageException;
|
||||
use SP\Core\Events\Event;
|
||||
use SP\Core\Events\EventMessage;
|
||||
use SP\Domain\Config\Services\ConfigUtil;
|
||||
use SP\Domain\Notification\Services\MailService;
|
||||
use SP\Http\JsonResponse;
|
||||
use SP\Modules\Web\Controllers\SimpleControllerBase;
|
||||
use SP\Modules\Web\Controllers\Traits\ConfigTrait;
|
||||
use SP\Providers\Mail\MailParams;
|
||||
|
||||
/**
|
||||
* Class ConfigMailController
|
||||
* Class SaveController
|
||||
*
|
||||
* @package SP\Modules\Web\Controllers
|
||||
*/
|
||||
final class ConfigMailController extends SimpleControllerBase
|
||||
final class SaveController extends SimpleControllerBase
|
||||
{
|
||||
use ConfigTrait;
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @throws DependencyException
|
||||
* @throws NotFoundException
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function saveAction(): bool
|
||||
@@ -69,33 +63,24 @@ final class ConfigMailController extends SimpleControllerBase
|
||||
$mailAuth = $this->request->analyzeBool('mail_auth_enabled', false);
|
||||
$mailRecipients = ConfigUtil::mailAddressesAdapter($this->request->analyzeString('mail_recipients'));
|
||||
|
||||
// Valores para la configuración del Correo
|
||||
if ($mailEnabled
|
||||
&& (empty($mailServer) || empty($mailFrom))
|
||||
if ($mailEnabled && (empty($mailServer) || empty($mailFrom))
|
||||
) {
|
||||
return $this->returnJsonResponse(
|
||||
JsonResponse::JSON_ERROR,
|
||||
__u('Missing Mail parameters')
|
||||
);
|
||||
return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('Missing Mail parameters'));
|
||||
}
|
||||
|
||||
if ($mailEnabled) {
|
||||
$configData->setMailEnabled(true);
|
||||
$configData->setMailRequestsEnabled($mailRequests);
|
||||
$configData->setMailServer($mailServer);
|
||||
$configData->setMailPort($mailPort);
|
||||
$configData->setMailSecurity($mailSecurity);
|
||||
$configData->setMailFrom($mailFrom);
|
||||
$configData->setMailRecipients($mailRecipients);
|
||||
$configData->setMailAuthenabled($mailAuth);
|
||||
$configData->setMailEvents(
|
||||
$this->request->analyzeArray('mail_events',
|
||||
function ($items) {
|
||||
return ConfigUtil::eventsAdapter($items);
|
||||
}, [])
|
||||
$this->request->analyzeArray('mail_events', fn($items) => ConfigUtil::eventsAdapter($items), [])
|
||||
);
|
||||
|
||||
if ($mailAuth) {
|
||||
$configData->setMailAuthenabled($mailAuth);
|
||||
$configData->setMailUser($mailUser);
|
||||
|
||||
if ($mailPass !== '***') {
|
||||
@@ -104,6 +89,7 @@ final class ConfigMailController extends SimpleControllerBase
|
||||
}
|
||||
|
||||
if ($configData->isMailEnabled() === false) {
|
||||
$configData->setMailEnabled(true);
|
||||
$eventMessage->addDescription(__u('Mail enabled'));
|
||||
}
|
||||
} elseif ($configData->isMailEnabled()) {
|
||||
@@ -113,89 +99,20 @@ final class ConfigMailController extends SimpleControllerBase
|
||||
|
||||
$eventMessage->addDescription(__u('Mail disabled'));
|
||||
} else {
|
||||
return $this->returnJsonResponse(
|
||||
JsonResponse::JSON_SUCCESS,
|
||||
__u('No changes')
|
||||
);
|
||||
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)
|
||||
);
|
||||
$this->eventDispatcher->notifyEvent('save.config.mail', new Event($this, $eventMessage));
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @throws DependencyException
|
||||
* @throws NotFoundException
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function checkAction(): bool
|
||||
{
|
||||
$mailParams = new MailParams();
|
||||
$mailParams->server = $this->request->analyzeString('mail_server');
|
||||
$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_auth_enabled', false);
|
||||
$mailRecipients = ConfigUtil::mailAddressesAdapter($this->request->analyzeString('mail_recipients'));
|
||||
|
||||
// Valores para la configuración del Correo
|
||||
if (empty($mailParams->server)
|
||||
|| empty($mailParams->from)
|
||||
|| count($mailRecipients) === 0) {
|
||||
return $this->returnJsonResponse(
|
||||
JsonResponse::JSON_ERROR,
|
||||
__u('Missing Mail parameters')
|
||||
);
|
||||
}
|
||||
|
||||
if ($mailParams->mailAuthenabled) {
|
||||
$mailParams->user = $this->request->analyzeString('mail_user');
|
||||
$mailParams->pass = $this->request->analyzeEncrypted('mail_pass');
|
||||
}
|
||||
|
||||
try {
|
||||
$this->dic->get(MailService::class)->check($mailParams, $mailRecipients[0]);
|
||||
|
||||
$this->eventDispatcher->notifyEvent(
|
||||
'send.mail.check',
|
||||
new Event(
|
||||
$this,
|
||||
EventMessage::factory()
|
||||
->addDescription(__u('Email sent'))
|
||||
->addDetail(__u('Recipient'), $mailRecipients[0])
|
||||
)
|
||||
);
|
||||
|
||||
return $this->returnJsonResponse(
|
||||
JsonResponse::JSON_SUCCESS,
|
||||
__u('Email sent'),
|
||||
[__u('Please, check your inbox')]
|
||||
);
|
||||
} catch (Exception $e) {
|
||||
processException($e);
|
||||
|
||||
$this->eventDispatcher->notifyEvent(
|
||||
'exception',
|
||||
new Event($e)
|
||||
);
|
||||
|
||||
return $this->returnJsonResponseException($e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws \DI\DependencyException
|
||||
* @throws \DI\NotFoundException
|
||||
* @throws \JsonException
|
||||
* @throws \SP\Core\Exceptions\SessionTimeout
|
||||
*/
|
||||
@@ -205,10 +122,7 @@ final class ConfigMailController extends SimpleControllerBase
|
||||
$this->checks();
|
||||
$this->checkAccess(ActionsInterface::CONFIG_MAIL);
|
||||
} catch (UnauthorizedPageException $e) {
|
||||
$this->eventDispatcher->notifyEvent(
|
||||
'exception',
|
||||
new Event($e)
|
||||
);
|
||||
$this->eventDispatcher->notifyEvent('exception', new Event($e));
|
||||
|
||||
$this->returnJsonResponseException($e);
|
||||
}
|
||||
Reference in New Issue
Block a user