feat(mail) : MailSettings ApiResource singleton (GET/PATCH /api/mail/configuration)
- ApiResource MailSettings expose les operations Get + Patch sur /api/mail/configuration - Provider + Processor relient le DTO a l'entite MailConfiguration (singleton) - password en write-only (jamais retourne) + hasPassword en lecture - chiffrement password via TokenEncryptor (sodium) - securite ROLE_ADMIN sur les deux operations Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
69
src/ApiResource/MailSettings.php
Normal file
69
src/ApiResource/MailSettings.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\ApiResource;
|
||||
|
||||
use ApiPlatform\Metadata\ApiResource;
|
||||
use ApiPlatform\Metadata\Get;
|
||||
use ApiPlatform\Metadata\Patch;
|
||||
use App\State\Mail\MailSettingsProcessor;
|
||||
use App\State\Mail\MailSettingsProvider;
|
||||
use Symfony\Component\Serializer\Attribute\Groups;
|
||||
|
||||
#[ApiResource(
|
||||
operations: [
|
||||
new Get(
|
||||
uriTemplate: '/mail/configuration',
|
||||
normalizationContext: ['groups' => ['mail_settings:read']],
|
||||
provider: MailSettingsProvider::class,
|
||||
security: "is_granted('ROLE_ADMIN')",
|
||||
),
|
||||
new Patch(
|
||||
uriTemplate: '/mail/configuration',
|
||||
denormalizationContext: ['groups' => ['mail_settings:write']],
|
||||
normalizationContext: ['groups' => ['mail_settings:read']],
|
||||
provider: MailSettingsProvider::class,
|
||||
processor: MailSettingsProcessor::class,
|
||||
security: "is_granted('ROLE_ADMIN')",
|
||||
),
|
||||
],
|
||||
)]
|
||||
final class MailSettings
|
||||
{
|
||||
#[Groups(['mail_settings:read', 'mail_settings:write'])]
|
||||
public ?string $protocol = null;
|
||||
|
||||
#[Groups(['mail_settings:read', 'mail_settings:write'])]
|
||||
public ?string $imapHost = null;
|
||||
|
||||
#[Groups(['mail_settings:read', 'mail_settings:write'])]
|
||||
public ?int $imapPort = null;
|
||||
|
||||
#[Groups(['mail_settings:read', 'mail_settings:write'])]
|
||||
public ?string $imapEncryption = null;
|
||||
|
||||
#[Groups(['mail_settings:read', 'mail_settings:write'])]
|
||||
public ?string $smtpHost = null;
|
||||
|
||||
#[Groups(['mail_settings:read', 'mail_settings:write'])]
|
||||
public ?int $smtpPort = null;
|
||||
|
||||
#[Groups(['mail_settings:read', 'mail_settings:write'])]
|
||||
public ?string $smtpEncryption = null;
|
||||
|
||||
#[Groups(['mail_settings:read', 'mail_settings:write'])]
|
||||
public ?string $username = null;
|
||||
|
||||
#[Groups(['mail_settings:write'])]
|
||||
public ?string $password = null;
|
||||
|
||||
#[Groups(['mail_settings:read', 'mail_settings:write'])]
|
||||
public ?string $sentFolderPath = null;
|
||||
|
||||
#[Groups(['mail_settings:read', 'mail_settings:write'])]
|
||||
public bool $enabled = false;
|
||||
|
||||
#[Groups(['mail_settings:read'])]
|
||||
public bool $hasPassword = false;
|
||||
}
|
||||
83
src/State/Mail/MailSettingsProcessor.php
Normal file
83
src/State/Mail/MailSettingsProcessor.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\State\Mail;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProcessorInterface;
|
||||
use App\ApiResource\MailSettings;
|
||||
use App\Entity\MailConfiguration;
|
||||
use App\Repository\MailConfigurationRepository;
|
||||
use App\Service\TokenEncryptor;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
final readonly class MailSettingsProcessor implements ProcessorInterface
|
||||
{
|
||||
public function __construct(
|
||||
private EntityManagerInterface $em,
|
||||
private MailConfigurationRepository $configRepository,
|
||||
private TokenEncryptor $tokenEncryptor,
|
||||
) {}
|
||||
|
||||
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): MailSettings
|
||||
{
|
||||
assert($data instanceof MailSettings);
|
||||
|
||||
$config = $this->configRepository->findSingleton();
|
||||
if (null === $config) {
|
||||
$config = new MailConfiguration();
|
||||
}
|
||||
|
||||
if (null !== $data->protocol) {
|
||||
$config->setProtocol($data->protocol);
|
||||
}
|
||||
if (null !== $data->imapHost) {
|
||||
$config->setImapHost($data->imapHost);
|
||||
}
|
||||
if (null !== $data->imapPort) {
|
||||
$config->setImapPort($data->imapPort);
|
||||
}
|
||||
if (null !== $data->imapEncryption) {
|
||||
$config->setImapEncryption($data->imapEncryption);
|
||||
}
|
||||
if (null !== $data->smtpHost) {
|
||||
$config->setSmtpHost($data->smtpHost);
|
||||
}
|
||||
if (null !== $data->smtpPort) {
|
||||
$config->setSmtpPort($data->smtpPort);
|
||||
}
|
||||
if (null !== $data->smtpEncryption) {
|
||||
$config->setSmtpEncryption($data->smtpEncryption);
|
||||
}
|
||||
if (null !== $data->username) {
|
||||
$config->setUsername($data->username);
|
||||
}
|
||||
if (null !== $data->sentFolderPath) {
|
||||
$config->setSentFolderPath($data->sentFolderPath);
|
||||
}
|
||||
$config->setEnabled($data->enabled);
|
||||
|
||||
if (null !== $data->password && '' !== $data->password) {
|
||||
$config->setEncryptedPassword($this->tokenEncryptor->encrypt($data->password));
|
||||
}
|
||||
|
||||
$this->em->persist($config);
|
||||
$this->em->flush();
|
||||
|
||||
$result = new MailSettings();
|
||||
$result->protocol = $config->getProtocol();
|
||||
$result->imapHost = $config->getImapHost();
|
||||
$result->imapPort = $config->getImapPort();
|
||||
$result->imapEncryption = $config->getImapEncryption();
|
||||
$result->smtpHost = $config->getSmtpHost();
|
||||
$result->smtpPort = $config->getSmtpPort();
|
||||
$result->smtpEncryption = $config->getSmtpEncryption();
|
||||
$result->username = $config->getUsername();
|
||||
$result->sentFolderPath = $config->getSentFolderPath();
|
||||
$result->enabled = $config->isEnabled();
|
||||
$result->hasPassword = $config->hasPassword();
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
39
src/State/Mail/MailSettingsProvider.php
Normal file
39
src/State/Mail/MailSettingsProvider.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\State\Mail;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\ApiResource\MailSettings;
|
||||
use App\Repository\MailConfigurationRepository;
|
||||
|
||||
final readonly class MailSettingsProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private MailConfigurationRepository $configRepository,
|
||||
) {}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): MailSettings
|
||||
{
|
||||
$config = $this->configRepository->findSingleton();
|
||||
$dto = new MailSettings();
|
||||
|
||||
if (null !== $config) {
|
||||
$dto->protocol = $config->getProtocol();
|
||||
$dto->imapHost = $config->getImapHost();
|
||||
$dto->imapPort = $config->getImapPort();
|
||||
$dto->imapEncryption = $config->getImapEncryption();
|
||||
$dto->smtpHost = $config->getSmtpHost();
|
||||
$dto->smtpPort = $config->getSmtpPort();
|
||||
$dto->smtpEncryption = $config->getSmtpEncryption();
|
||||
$dto->username = $config->getUsername();
|
||||
$dto->sentFolderPath = $config->getSentFolderPath();
|
||||
$dto->enabled = $config->isEnabled();
|
||||
$dto->hasPassword = $config->hasPassword();
|
||||
}
|
||||
|
||||
return $dto;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user