modifica - da terminare gestioen doc remoti - resto ok
This commit is contained in:
@@ -3,14 +3,15 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\AppSetting;
|
||||
use App\Models\Documento;
|
||||
use App\Models\EmailSetting;
|
||||
use App\Models\Ruolo;
|
||||
use App\Models\SenderAccount;
|
||||
use App\Models\StorageRepository;
|
||||
use App\Models\TipologiaDocumento;
|
||||
use App\Models\TipologiaEvento;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Crypt;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
@@ -27,8 +28,9 @@ class ImpostazioniController extends Controller
|
||||
$emailSettings = EmailSetting::first() ?? new EmailSetting();
|
||||
$senderAccounts = SenderAccount::orderBy('email_address')->get();
|
||||
$repositories = StorageRepository::orderBy('ordine')->get();
|
||||
$googleDriveNewToken = session('google_drive_new_token');
|
||||
|
||||
return view('impostazioni.index', compact('tipologie', 'tipologieEventi', 'ruoli', 'appSettings', 'emailSettings', 'senderAccounts', 'repositories'));
|
||||
return view('impostazioni.index', compact('tipologie', 'tipologieEventi', 'ruoli', 'appSettings', 'emailSettings', 'senderAccounts', 'repositories', 'googleDriveNewToken'));
|
||||
}
|
||||
|
||||
public function saveAppSettings(Request $request)
|
||||
|
||||
@@ -9,6 +9,7 @@ use App\Services\StorageRepositoryService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\View\View;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
|
||||
class StorageRepositoryController extends Controller
|
||||
@@ -64,7 +65,11 @@ class StorageRepositoryController extends Controller
|
||||
'ordine' => 'nullable|integer|min:0',
|
||||
]);
|
||||
|
||||
$validated['config'] = $this->repoService->encryptSensitiveConfig($validated['config'], $validated['tipo']);
|
||||
$validated['config'] = $this->repoService->encryptSensitiveConfig(
|
||||
$validated['config'],
|
||||
$validated['tipo'],
|
||||
$storageRepository->config
|
||||
);
|
||||
|
||||
$storageRepository->update($validated);
|
||||
|
||||
@@ -194,4 +199,110 @@ class StorageRepositoryController extends Controller
|
||||
'Content-Disposition' => 'inline; filename="' . $basename . '"',
|
||||
]);
|
||||
}
|
||||
|
||||
public function oauthRedirect(Request $request): RedirectResponse
|
||||
{
|
||||
$this->authorizeWrite('settings');
|
||||
|
||||
$clientId = $request->get('client_id');
|
||||
$clientSecret = $request->get('client_secret');
|
||||
$repoId = (int) $request->get('repo_id', 0);
|
||||
|
||||
if (empty($clientId) || empty($clientSecret)) {
|
||||
return redirect('/impostazioni#repository')->with('error', 'Inserisci Client ID e Client Secret prima di autorizzare.');
|
||||
}
|
||||
|
||||
session(['google_drive_oauth' => [
|
||||
'client_id' => $clientId,
|
||||
'client_secret' => $clientSecret,
|
||||
'repo_id' => $repoId,
|
||||
'nome' => $request->get('nome', ''),
|
||||
'is_active' => $request->get('is_active', '1'),
|
||||
'root_folder_id' => $request->get('root_folder_id', 'root'),
|
||||
]]);
|
||||
|
||||
$client = new \Google_Client();
|
||||
$client->setClientId($clientId);
|
||||
$client->setClientSecret($clientSecret);
|
||||
$client->setRedirectUri(url('/auth/google-drive/callback'));
|
||||
$client->addScope(\Google_Service_Drive::DRIVE);
|
||||
$client->setAccessType('offline');
|
||||
$client->setPrompt('consent');
|
||||
|
||||
$authUrl = $client->createAuthUrl();
|
||||
|
||||
return redirect()->away($authUrl);
|
||||
}
|
||||
|
||||
public function oauthCallback(Request $request): RedirectResponse
|
||||
{
|
||||
$this->authorizeWrite('settings');
|
||||
|
||||
if ($request->get('error')) {
|
||||
return redirect('/impostazioni#repository')->with('error', 'Autorizzazione Google Drive annullata o fallita.');
|
||||
}
|
||||
|
||||
$code = $request->get('code');
|
||||
if (!$code) {
|
||||
return redirect('/impostazioni#repository')->with('error', 'Codice di autorizzazione mancante.');
|
||||
}
|
||||
|
||||
$oauthData = session('google_drive_oauth');
|
||||
if (!$oauthData) {
|
||||
return redirect('/impostazioni#repository')->with('error', 'Sessione scaduta. Riprova l\'autorizzazione.');
|
||||
}
|
||||
|
||||
try {
|
||||
$client = new \Google_Client();
|
||||
$client->setClientId($oauthData['client_id']);
|
||||
$client->setClientSecret($oauthData['client_secret']);
|
||||
$client->setRedirectUri(url('/auth/google-drive/callback'));
|
||||
$client->addScope(\Google_Service_Drive::DRIVE);
|
||||
|
||||
$token = $client->fetchAccessTokenWithAuthCode($code);
|
||||
|
||||
if (isset($token['error'])) {
|
||||
throw new \Exception($token['error_description'] ?? $token['error']);
|
||||
}
|
||||
|
||||
$refreshToken = $client->getRefreshToken();
|
||||
if (!$refreshToken) {
|
||||
throw new \Exception('Refresh token non ricevuto. Assicurati che l\'app OAuth sia configurata con access_type=offline e prompt=consent nella Google Cloud Console.');
|
||||
}
|
||||
|
||||
$repoId = $oauthData['repo_id'];
|
||||
session()->forget('google_drive_oauth');
|
||||
|
||||
if ($repoId > 0) {
|
||||
$repo = StorageRepository::find($repoId);
|
||||
if ($repo) {
|
||||
$config = $repo->config;
|
||||
$config['client_id'] = $oauthData['client_id'];
|
||||
$config['client_secret'] = $oauthData['client_secret'];
|
||||
$config['refresh_token'] = $refreshToken;
|
||||
|
||||
$config = $this->repoService->encryptSensitiveConfig($config, 'google_drive');
|
||||
|
||||
$repo->update(['config' => $config]);
|
||||
|
||||
return redirect('/impostazioni#repository')->with('success', 'Google Drive autorizzato con successo! Refresh token salvato.');
|
||||
}
|
||||
|
||||
return redirect('/impostazioni#repository')->with('error', 'Repository non trovato (ID: ' . $repoId . ').');
|
||||
}
|
||||
|
||||
session()->flash('google_drive_new_token', [
|
||||
'refresh_token' => $refreshToken,
|
||||
'client_id' => $oauthData['client_id'],
|
||||
'client_secret' => $oauthData['client_secret'],
|
||||
'nome' => $oauthData['nome'] ?? '',
|
||||
'is_active' => $oauthData['is_active'] ?? '1',
|
||||
'root_folder_id' => $oauthData['root_folder_id'] ?? 'root',
|
||||
]);
|
||||
|
||||
return redirect('/impostazioni#repository')->with('success', 'Autorizzazione Google Drive completata! Ora completa la creazione del repository e salva.');
|
||||
} catch (\Exception $e) {
|
||||
return redirect('/impostazioni#repository')->with('error', 'Errore autorizzazione Google Drive: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,14 @@ class StorageRepositoryService
|
||||
public function testConnection(StorageRepository $repo): array
|
||||
{
|
||||
try {
|
||||
$config = $repo->getDecryptedConfig();
|
||||
|
||||
if ($repo->tipo === 'google_drive') {
|
||||
if (empty($config['client_id']) || empty($config['client_secret']) || empty($config['refresh_token'])) {
|
||||
return ['success' => false, 'message' => 'Configurazione incompleta: client_id, client_secret e refresh_token sono necessari per Google Drive.'];
|
||||
}
|
||||
}
|
||||
|
||||
$filesystem = $this->buildFilesystem($repo);
|
||||
if (!$filesystem) {
|
||||
return ['success' => false, 'message' => 'Impossibile costruire il filesystem per questo tipo di repository.'];
|
||||
@@ -49,12 +57,21 @@ class StorageRepositoryService
|
||||
|
||||
public function listContents(StorageRepository $repo, string $path = '/'): array
|
||||
{
|
||||
$filesystem = $this->buildFilesystem($repo);
|
||||
if (!$filesystem) {
|
||||
return [];
|
||||
}
|
||||
|
||||
try {
|
||||
$config = $repo->getDecryptedConfig();
|
||||
|
||||
if ($repo->tipo === 'google_drive') {
|
||||
if (empty($config['client_id']) || empty($config['client_secret']) || empty($config['refresh_token'])) {
|
||||
Log::warning("StorageRepository: listContents failed for repo #{$repo->id} ({$repo->nome}): credenziali Google Drive mancanti");
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
$filesystem = $this->buildFilesystem($repo);
|
||||
if (!$filesystem) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$normalizedPath = $this->normalizePath($path);
|
||||
$items = $filesystem->listContents($normalizedPath, false)->toArray();
|
||||
$result = [];
|
||||
@@ -76,7 +93,7 @@ class StorageRepositoryService
|
||||
}
|
||||
}
|
||||
|
||||
public function encryptSensitiveConfig(array $config, string $tipo): array
|
||||
public function encryptSensitiveConfig(array $config, string $tipo, array $existingConfig = []): array
|
||||
{
|
||||
$sensitiveFields = match ($tipo) {
|
||||
'webdav' => ['password'],
|
||||
@@ -87,6 +104,8 @@ class StorageRepositoryService
|
||||
foreach ($sensitiveFields as $field) {
|
||||
if (!empty($config[$field]) && !$this->isEncrypted($config[$field])) {
|
||||
$config[$field] = Crypt::encryptString($config[$field]);
|
||||
} elseif (empty($config[$field]) && !empty($existingConfig[$field])) {
|
||||
$config[$field] = $existingConfig[$field];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,7 +119,7 @@ class StorageRepositoryService
|
||||
return '';
|
||||
}
|
||||
if ($path === '/') {
|
||||
return '/';
|
||||
return '';
|
||||
}
|
||||
return ltrim($path, '/\\');
|
||||
}
|
||||
@@ -112,11 +131,18 @@ class StorageRepositoryService
|
||||
|
||||
private function buildWebDAV(array $config): ?Filesystem
|
||||
{
|
||||
$authType = match ($config['auth_type'] ?? 'basic') {
|
||||
'basic' => WebDAVClient::AUTH_BASIC,
|
||||
'digest' => WebDAVClient::AUTH_DIGEST,
|
||||
'ntlm' => WebDAVClient::AUTH_NTLM,
|
||||
default => WebDAVClient::AUTH_BASIC,
|
||||
};
|
||||
|
||||
$client = new WebDAVClient([
|
||||
'baseUri' => rtrim($config['base_uri'] ?? '', '/') . '/',
|
||||
'userName' => $config['username'] ?? '',
|
||||
'password' => $config['password'] ?? '',
|
||||
'authType' => $config['auth_type'] ?? 'basic',
|
||||
'authType' => $authType,
|
||||
]);
|
||||
|
||||
$root = ltrim($config['root'] ?? '/', '/');
|
||||
@@ -127,10 +153,14 @@ class StorageRepositoryService
|
||||
|
||||
private function buildGoogleDrive(array $config): ?Filesystem
|
||||
{
|
||||
if (empty($config['client_id']) || empty($config['client_secret']) || empty($config['refresh_token'])) {
|
||||
throw new \InvalidArgumentException('Google Drive richiede client_id, client_secret e refresh_token per l\'autenticazione.');
|
||||
}
|
||||
|
||||
$client = new \Google_Client();
|
||||
$client->setClientId($config['client_id'] ?? '');
|
||||
$client->setClientSecret($config['client_secret'] ?? '');
|
||||
$client->refreshToken($config['refresh_token'] ?? '');
|
||||
$client->setClientId($config['client_id']);
|
||||
$client->setClientSecret($config['client_secret']);
|
||||
$client->refreshToken($config['refresh_token']);
|
||||
$client->addScope(\Google_Service_Drive::DRIVE);
|
||||
|
||||
$service = new \Google_Service_Drive($client);
|
||||
|
||||
Reference in New Issue
Block a user