45
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Admin\Concerns\ScopedToTenant;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\AuditLog;
|
||||
use App\Models\Tenant;
|
||||
use App\Services\Graph\DirectorySync;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Throwable;
|
||||
|
||||
/** Synchro annuaire lancée à la main depuis le tableau de bord (en plus de la tâche planifiée). */
|
||||
class SyncController extends Controller
|
||||
{
|
||||
use ScopedToTenant;
|
||||
|
||||
public function __invoke(Request $request, DirectorySync $sync): RedirectResponse
|
||||
{
|
||||
$tenant = Tenant::findOrFail($this->tenantId());
|
||||
$full = $request->boolean('full');
|
||||
|
||||
// Même verrou que la commande planifiée : jamais deux synchros en parallèle.
|
||||
$lock = Cache::lock("signature-sync:{$tenant->id}", 600);
|
||||
if (! $lock->get()) {
|
||||
return back()->withErrors(['sync' => 'Une synchronisation est déjà en cours. Réessayez dans une minute.']);
|
||||
}
|
||||
|
||||
@set_time_limit(300);
|
||||
try {
|
||||
$s = $sync->run($tenant, $full);
|
||||
} catch (Throwable $e) {
|
||||
return back()->withErrors(['sync' => 'Synchronisation en échec : '.mb_substr($e->getMessage(), 0, 300)]);
|
||||
} finally {
|
||||
$lock->release();
|
||||
}
|
||||
|
||||
AuditLog::record('admin', $this->actor(), $full ? 'sync.full' : 'sync.delta', $tenant, $s, $tenant->id);
|
||||
|
||||
return back()->with('status', sprintf(
|
||||
'Synchronisation %s terminée : %d agents mis à jour, %d retirés, %d groupes, %d appartenances.',
|
||||
$full ? 'complète' : 'incrémentale', $s['users'], $s['removed'], $s['groups'], $s['memberships'],
|
||||
));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user