118 lines
4.0 KiB
PHP
118 lines
4.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Enums\GoogleService;
|
|
use App\Models\GoogleOAuthConnection;
|
|
use App\Services\GoogleOAuthService;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\View\View;
|
|
|
|
class GoogleOAuthController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly GoogleOAuthService $googleOAuthService,
|
|
) {}
|
|
|
|
public function redirect(string $service): RedirectResponse
|
|
{
|
|
try {
|
|
$serviceEnum = GoogleService::from($service);
|
|
} catch (\ValueError $e) {
|
|
return redirect()->back()->withErrors(['error' => 'Servizio Google non valido: ' . $service]);
|
|
}
|
|
|
|
$authUrl = $this->googleOAuthService->getAuthUrl($service);
|
|
|
|
return redirect()->away($authUrl);
|
|
}
|
|
|
|
public function callback(Request $request): RedirectResponse
|
|
{
|
|
$error = $request->input('error');
|
|
if ($error) {
|
|
$message = match ($error) {
|
|
'access_denied' => 'Accesso negato. Autorizzazione non concessa.',
|
|
default => 'Errore Google: ' . $error,
|
|
};
|
|
return redirect()->route('impostazioni.index', ['tab' => 'google'])
|
|
->withErrors(['error' => $message]);
|
|
}
|
|
|
|
$code = $request->input('code');
|
|
if (!$code) {
|
|
return redirect()->route('impostazioni.index', ['tab' => 'google'])
|
|
->withErrors(['error' => 'Nessun codice di autorizzazione ricevuto']);
|
|
}
|
|
|
|
$state = $request->input('state', 'email');
|
|
try {
|
|
$service = GoogleService::from($state);
|
|
} catch (\ValueError $e) {
|
|
$service = GoogleService::Email;
|
|
}
|
|
|
|
try {
|
|
$connection = $this->googleOAuthService->handleCallback($code, $service);
|
|
|
|
$message = match ($service) {
|
|
GoogleService::Email => 'Account email Gmail connesso con successo!',
|
|
GoogleService::Drive => 'Account Google Drive connesso con successo!',
|
|
GoogleService::Calendar => 'Account Google Calendar connesso con successo!',
|
|
};
|
|
|
|
return redirect()->route('impostazioni.index', ['tab' => 'google'])
|
|
->with('success', $message);
|
|
} catch (\Exception $e) {
|
|
Log::error('Google OAuth callback error', [
|
|
'error' => $e->getMessage(),
|
|
'service' => $service->value,
|
|
]);
|
|
|
|
return redirect()->route('impostazioni.index', ['tab' => 'google'])
|
|
->withErrors(['error' => 'Errore durante la connessione: ' . $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
public function revoke(GoogleOAuthConnection $connection): RedirectResponse
|
|
{
|
|
if ($connection->user_id !== auth()->id()) {
|
|
abort(403);
|
|
}
|
|
|
|
try {
|
|
$service = GoogleService::from($connection->service);
|
|
|
|
$message = match ($service) {
|
|
GoogleService::Email => 'Account email Gmail disconnesso.',
|
|
GoogleService::Drive => 'Account Google Drive disconnesso.',
|
|
GoogleService::Calendar => 'Account Google Calendar disconnesso.',
|
|
};
|
|
|
|
$this->googleOAuthService->revoke($connection);
|
|
|
|
return redirect()->route('impostazioni.index', ['tab' => 'google'])
|
|
->with('success', $message);
|
|
} catch (\Exception $e) {
|
|
Log::error('Google OAuth revoke error', [
|
|
'error' => $e->getMessage(),
|
|
'connection_id' => $connection->id,
|
|
]);
|
|
|
|
return redirect()->route('impostazioni.index', ['tab' => 'google'])
|
|
->withErrors(['error' => 'Errore durante la disconnessione: ' . $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
public function status(): \Illuminate\Http\JsonResponse
|
|
{
|
|
$status = $this->googleOAuthService->getConnectionStatus();
|
|
|
|
return response()->json($status);
|
|
}
|
|
}
|