07ab2a7063
Paramètres du site : - Nouvelle section "Serveur SMTP" avec host, port, chiffrement, identifiant, mot de passe, adresse/nom d'expéditeur - Bouton "Envoyer un e-mail de test" (AJAX via Symfony EsmtpTransport) : tente la connexion + envoie un message réel à l'admin - Badge "Configuré — 2FA actif" quand SMTP est en place - Suppression de la configuration possible Authentification 2FA : - Si SMTP configuré : après validation identifiant/mot de passe, l'utilisateur est déconnecté, un PIN à 6 chiffres est généré, haché (bcrypt) et stocké en session, envoyé par e-mail (10 min) - Page /2fa : saisie du PIN, bouton "Renvoyer le code", retour login - Si l'envoi e-mail échoue : fallback sans 2FA (logue l'erreur) - Si SMTP non configuré : login standard inchangé Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
162 lines
6.4 KiB
PHP
162 lines
6.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Services\SiteSettingsService;
|
|
use App\Services\UpdateService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\View\View;
|
|
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
|
|
use Symfony\Component\Mime\Address;
|
|
use Symfony\Component\Mime\Email;
|
|
|
|
class SettingController extends Controller
|
|
{
|
|
public function index(UpdateService $updates): View
|
|
{
|
|
$installedVersion = $updates->getInstalledVersion();
|
|
$latestRelease = $updates->fetchLatestRelease();
|
|
$updateAvailable = $latestRelease
|
|
&& version_compare($latestRelease['version'], $installedVersion, '>');
|
|
|
|
return view('admin.parametres.index', [
|
|
'logoUrl' => SiteSettingsService::logoUrl(),
|
|
'registrationEnabled' => SiteSettingsService::registrationEnabled(),
|
|
'installedVersion' => $installedVersion,
|
|
'latestRelease' => $latestRelease,
|
|
'updateAvailable' => $updateAvailable,
|
|
]);
|
|
}
|
|
|
|
public function updateLogo(Request $request): RedirectResponse
|
|
{
|
|
$request->validate([
|
|
'logo' => ['required', 'file', 'max:2048', 'mimes:png,jpg,jpeg,gif,webp,svg'],
|
|
]);
|
|
|
|
// Supprimer l'ancien logo
|
|
$old = SiteSettingsService::get('logo_path');
|
|
if ($old && Storage::disk('public')->exists($old)) {
|
|
Storage::disk('public')->delete($old);
|
|
}
|
|
|
|
$file = $request->file('logo');
|
|
$ext = strtolower($file->getClientOriginalExtension());
|
|
$path = Storage::disk('public')->putFileAs('site', $file, "logo.{$ext}");
|
|
|
|
SiteSettingsService::set('logo_path', $path);
|
|
|
|
return back()->with('success', 'Logo mis à jour.');
|
|
}
|
|
|
|
public function deleteLogo(): RedirectResponse
|
|
{
|
|
$path = SiteSettingsService::get('logo_path');
|
|
if ($path && Storage::disk('public')->exists($path)) {
|
|
Storage::disk('public')->delete($path);
|
|
}
|
|
|
|
SiteSettingsService::set('logo_path', null);
|
|
|
|
return back()->with('success', 'Logo supprimé.');
|
|
}
|
|
|
|
// ── SMTP ──────────────────────────────────────────────────────────────────
|
|
|
|
public function updateSmtp(Request $request): RedirectResponse
|
|
{
|
|
$data = $request->validate([
|
|
'smtp_host' => ['required', 'string', 'max:255'],
|
|
'smtp_port' => ['required', 'integer', 'min:1', 'max:65535'],
|
|
'smtp_encryption' => ['nullable', 'in:tls,ssl'],
|
|
'smtp_username' => ['nullable', 'string', 'max:255'],
|
|
'smtp_password' => ['nullable', 'string', 'max:255'],
|
|
'smtp_from_address' => ['required', 'email', 'max:255'],
|
|
'smtp_from_name' => ['required', 'string', 'max:255'],
|
|
]);
|
|
|
|
SiteSettingsService::set('smtp', [
|
|
'host' => $data['smtp_host'],
|
|
'port' => (int) $data['smtp_port'],
|
|
'encryption' => $data['smtp_encryption'] ?? null,
|
|
'username' => $data['smtp_username'] ?? null,
|
|
'password' => $data['smtp_password'] ?? null,
|
|
'from_address' => $data['smtp_from_address'],
|
|
'from_name' => $data['smtp_from_name'],
|
|
]);
|
|
|
|
return back()->with('success', 'Configuration SMTP enregistrée. Le 2FA par e-mail est maintenant actif.');
|
|
}
|
|
|
|
public function deleteSmtp(): RedirectResponse
|
|
{
|
|
SiteSettingsService::set('smtp', []);
|
|
|
|
return back()->with('success', 'Configuration SMTP supprimée. Le 2FA est désactivé.');
|
|
}
|
|
|
|
public function testSmtp(Request $request): JsonResponse
|
|
{
|
|
$data = $request->validate([
|
|
'smtp_host' => ['required', 'string'],
|
|
'smtp_port' => ['required', 'integer'],
|
|
'smtp_encryption' => ['nullable', 'in:tls,ssl'],
|
|
'smtp_username' => ['nullable', 'string'],
|
|
'smtp_password' => ['nullable', 'string'],
|
|
'smtp_from_address' => ['required', 'email'],
|
|
'smtp_from_name' => ['required', 'string'],
|
|
]);
|
|
|
|
try {
|
|
$useSsl = ($data['smtp_encryption'] ?? '') === 'ssl';
|
|
$transport = new EsmtpTransport($data['smtp_host'], (int) $data['smtp_port'], $useSsl);
|
|
|
|
if (! empty($data['smtp_username'])) {
|
|
$transport->setUsername($data['smtp_username']);
|
|
$transport->setPassword($data['smtp_password'] ?? '');
|
|
}
|
|
|
|
$mailer = new \Symfony\Component\Mailer\Mailer($transport);
|
|
|
|
$email = (new Email())
|
|
->from(new Address($data['smtp_from_address'], $data['smtp_from_name']))
|
|
->to(auth()->user()->email)
|
|
->subject('Test SMTP — ' . config('app.name'))
|
|
->text(
|
|
"Ce message confirme que votre configuration SMTP fonctionne correctement.\n\n" .
|
|
"Serveur : {$data['smtp_host']}:{$data['smtp_port']}\n" .
|
|
"Chiffrement : " . ($data['smtp_encryption'] ?: 'aucun') . "\n\n" .
|
|
"— " . config('app.name')
|
|
);
|
|
|
|
$mailer->send($email);
|
|
|
|
return response()->json([
|
|
'ok' => true,
|
|
'message' => 'E-mail de test envoyé à ' . auth()->user()->email . '. Vérifiez votre boîte de réception.',
|
|
]);
|
|
} catch (\Throwable $e) {
|
|
return response()->json(['ok' => false, 'message' => $e->getMessage()], 422);
|
|
}
|
|
}
|
|
|
|
// ── Paramètres généraux ───────────────────────────────────────────────────
|
|
|
|
public function updateSettings(Request $request): RedirectResponse
|
|
{
|
|
$data = $request->validate([
|
|
'site_name' => ['nullable', 'string', 'max:100'],
|
|
]);
|
|
|
|
$siteName = trim($data['site_name'] ?? '');
|
|
SiteSettingsService::set('site_name', $siteName ?: null);
|
|
SiteSettingsService::set('registration_enabled', $request->boolean('registration_enabled'));
|
|
|
|
return back()->with('success', 'Paramètres enregistrés.');
|
|
}
|
|
}
|