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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user