feat : add Zimbra settings API (CRUD + test connection)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
51
src/ApiResource/ZimbraSettings.php
Normal file
51
src/ApiResource/ZimbraSettings.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\ApiResource;
|
||||
|
||||
use ApiPlatform\Metadata\ApiResource;
|
||||
use ApiPlatform\Metadata\Get;
|
||||
use ApiPlatform\Metadata\Put;
|
||||
use App\State\ZimbraSettingsProcessor;
|
||||
use App\State\ZimbraSettingsProvider;
|
||||
use Symfony\Component\Serializer\Attribute\Groups;
|
||||
|
||||
#[ApiResource(
|
||||
operations: [
|
||||
new Get(
|
||||
uriTemplate: '/settings/zimbra',
|
||||
normalizationContext: ['groups' => ['zimbra_settings:read']],
|
||||
provider: ZimbraSettingsProvider::class,
|
||||
security: "is_granted('ROLE_ADMIN')",
|
||||
),
|
||||
new Put(
|
||||
uriTemplate: '/settings/zimbra',
|
||||
denormalizationContext: ['groups' => ['zimbra_settings:write']],
|
||||
normalizationContext: ['groups' => ['zimbra_settings:read']],
|
||||
provider: ZimbraSettingsProvider::class,
|
||||
processor: ZimbraSettingsProcessor::class,
|
||||
security: "is_granted('ROLE_ADMIN')",
|
||||
),
|
||||
],
|
||||
)]
|
||||
final class ZimbraSettings
|
||||
{
|
||||
#[Groups(['zimbra_settings:read', 'zimbra_settings:write'])]
|
||||
public ?string $serverUrl = null;
|
||||
|
||||
#[Groups(['zimbra_settings:read', 'zimbra_settings:write'])]
|
||||
public ?string $username = null;
|
||||
|
||||
#[Groups(['zimbra_settings:read', 'zimbra_settings:write'])]
|
||||
public ?string $calendarPath = null;
|
||||
|
||||
#[Groups(['zimbra_settings:write'])]
|
||||
public ?string $password = null;
|
||||
|
||||
#[Groups(['zimbra_settings:read', 'zimbra_settings:write'])]
|
||||
public bool $enabled = false;
|
||||
|
||||
#[Groups(['zimbra_settings:read'])]
|
||||
public bool $hasPassword = false;
|
||||
}
|
||||
28
src/ApiResource/ZimbraTestConnection.php
Normal file
28
src/ApiResource/ZimbraTestConnection.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\ApiResource;
|
||||
|
||||
use ApiPlatform\Metadata\ApiResource;
|
||||
use ApiPlatform\Metadata\Post;
|
||||
use App\State\ZimbraTestConnectionProvider;
|
||||
use Symfony\Component\Serializer\Attribute\Groups;
|
||||
|
||||
#[ApiResource(
|
||||
operations: [
|
||||
new Post(
|
||||
uriTemplate: '/settings/zimbra/test',
|
||||
input: false,
|
||||
normalizationContext: ['groups' => ['zimbra_test:read']],
|
||||
provider: ZimbraTestConnectionProvider::class,
|
||||
processor: ZimbraTestConnectionProvider::class,
|
||||
security: "is_granted('ROLE_ADMIN')",
|
||||
),
|
||||
],
|
||||
)]
|
||||
final class ZimbraTestConnection
|
||||
{
|
||||
#[Groups(['zimbra_test:read'])]
|
||||
public bool $success = false;
|
||||
}
|
||||
53
src/State/ZimbraSettingsProcessor.php
Normal file
53
src/State/ZimbraSettingsProcessor.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProcessorInterface;
|
||||
use App\ApiResource\ZimbraSettings;
|
||||
use App\Entity\ZimbraConfiguration;
|
||||
use App\Repository\ZimbraConfigurationRepository;
|
||||
use App\Service\TokenEncryptor;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
final readonly class ZimbraSettingsProcessor implements ProcessorInterface
|
||||
{
|
||||
public function __construct(
|
||||
private EntityManagerInterface $em,
|
||||
private ZimbraConfigurationRepository $configRepository,
|
||||
private TokenEncryptor $tokenEncryptor,
|
||||
) {}
|
||||
|
||||
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): ZimbraSettings
|
||||
{
|
||||
assert($data instanceof ZimbraSettings);
|
||||
|
||||
$config = $this->configRepository->findSingleton();
|
||||
if (null === $config) {
|
||||
$config = new ZimbraConfiguration();
|
||||
}
|
||||
|
||||
$config->setServerUrl($data->serverUrl);
|
||||
$config->setUsername($data->username);
|
||||
$config->setCalendarPath($data->calendarPath);
|
||||
$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 ZimbraSettings();
|
||||
$result->serverUrl = $config->getServerUrl();
|
||||
$result->username = $config->getUsername();
|
||||
$result->calendarPath = $config->getCalendarPath();
|
||||
$result->enabled = $config->isEnabled();
|
||||
$result->hasPassword = $config->hasPassword();
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
33
src/State/ZimbraSettingsProvider.php
Normal file
33
src/State/ZimbraSettingsProvider.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\ApiResource\ZimbraSettings;
|
||||
use App\Repository\ZimbraConfigurationRepository;
|
||||
|
||||
final readonly class ZimbraSettingsProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private ZimbraConfigurationRepository $configRepository,
|
||||
) {}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): ZimbraSettings
|
||||
{
|
||||
$config = $this->configRepository->findSingleton();
|
||||
$dto = new ZimbraSettings();
|
||||
|
||||
if (null !== $config) {
|
||||
$dto->serverUrl = $config->getServerUrl();
|
||||
$dto->username = $config->getUsername();
|
||||
$dto->calendarPath = $config->getCalendarPath();
|
||||
$dto->enabled = $config->isEnabled();
|
||||
$dto->hasPassword = $config->hasPassword();
|
||||
}
|
||||
|
||||
return $dto;
|
||||
}
|
||||
}
|
||||
37
src/State/ZimbraTestConnectionProvider.php
Normal file
37
src/State/ZimbraTestConnectionProvider.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProcessorInterface;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\ApiResource\ZimbraTestConnection;
|
||||
use App\Service\CalDavService;
|
||||
use Throwable;
|
||||
|
||||
final readonly class ZimbraTestConnectionProvider implements ProviderInterface, ProcessorInterface
|
||||
{
|
||||
public function __construct(
|
||||
private CalDavService $calDavService,
|
||||
) {}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): ZimbraTestConnection
|
||||
{
|
||||
return new ZimbraTestConnection();
|
||||
}
|
||||
|
||||
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): ZimbraTestConnection
|
||||
{
|
||||
$result = new ZimbraTestConnection();
|
||||
|
||||
try {
|
||||
$result->success = $this->calDavService->testConnection();
|
||||
} catch (Throwable) {
|
||||
$result->success = false;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user