fix(mail) : le test de connexion fonctionne même si la config est désactivée + remonte l'erreur IMAP réelle

Le guard enabled dans getClient() bloquait le test de connexion alors que le
workflow naturel est configurer → tester → activer. getClient(requireEnabled)
permet au nouveau testConnection() de se connecter sans exiger enabled=true.
Le controller (ROLE_ADMIN) renvoie désormais le détail de l'erreur pour faciliter
le diagnostic.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-20 07:46:11 +02:00
parent 7a682b4662
commit f313e74c9e
3 changed files with 42 additions and 11 deletions

View File

@@ -23,21 +23,23 @@ class MailTestConnectionController extends AbstractController
public function __invoke(): JsonResponse public function __invoke(): JsonResponse
{ {
try { try {
$folders = $this->mailProvider->listFolders(); $foldersCount = $this->mailProvider->testConnection();
return $this->json([ return $this->json([
'ok' => true, 'ok' => true,
'foldersCount' => count($folders), 'foldersCount' => $foldersCount,
]); ]);
} catch (MailProviderException) { } catch (MailProviderException $e) {
return $this->json([ return $this->json([
'ok' => false, 'ok' => false,
'error' => 'Connexion IMAP impossible. Vérifiez la configuration.', 'error' => 'Connexion IMAP impossible. Vérifiez la configuration.',
'detail' => $e->getMessage(),
]); ]);
} catch (Throwable) { } catch (Throwable $e) {
return $this->json([ return $this->json([
'ok' => false, 'ok' => false,
'error' => 'Erreur inattendue lors du test de connexion.', 'error' => 'Erreur inattendue lors du test de connexion.',
'detail' => $e->getMessage(),
]); ]);
} }
} }

View File

@@ -26,6 +26,22 @@ final class ImapMailProvider implements MailProviderInterface
private readonly LoggerInterface $logger, private readonly LoggerInterface $logger,
) {} ) {}
public function testConnection(): int
{
$client = $this->getClient(requireEnabled: false);
try {
$folders = $client->getFolders(false);
$client->disconnect();
return count($folders);
} catch (Throwable $e) {
$this->logger->error('ImapMailProvider::testConnection failed: '.$e->getMessage());
throw MailProviderException::connectionFailed($e->getMessage());
}
}
public function listFolders(): array public function listFolders(): array
{ {
$client = $this->getClient(); $client = $this->getClient();
@@ -269,12 +285,16 @@ final class ImapMailProvider implements MailProviderInterface
} }
} }
private function getClient(): Client private function getClient(bool $requireEnabled = true): Client
{ {
$config = $this->configRepository->findSingleton(); $config = $this->configRepository->findSingleton();
if (null === $config || !$config->isEnabled()) { if (null === $config) {
throw MailProviderException::connectionFailed('Mail configuration is missing or disabled'); throw MailProviderException::connectionFailed('Mail configuration is missing');
}
if ($requireEnabled && !$config->isEnabled()) {
throw MailProviderException::connectionFailed('Mail configuration is disabled');
} }
if (null === $config->getEncryptedPassword()) { if (null === $config->getEncryptedPassword()) {

View File

@@ -11,6 +11,15 @@ use App\Mail\Exception\MailProviderException;
interface MailProviderInterface interface MailProviderInterface
{ {
/**
* Opens a connection using the stored configuration and returns the number
* of folders found. Used by the admin "test connection" endpoint, so it
* MUST work even when the configuration is not yet enabled.
*
* @throws MailProviderException
*/
public function testConnection(): int;
/** /**
* Returns the full folder tree of the configured mailbox. * Returns the full folder tree of the configured mailbox.
* *