'migrate-user-projects.json', 'method' => 'POST', 'action' => function () { $user = kirby()->user(); if (!$user || $user->role()->id() !== 'admin') { return [ 'status' => 'error', 'message' => 'Cette action nécessite les droits administrateur.' ]; } $migrated = []; $errors = []; $nonAdminUsers = kirby()->users()->filter(fn($u) => $u->role()->id() !== 'admin'); foreach ($nonAdminUsers as $u) { if (!$u->projects()->exists() || $u->projects()->isEmpty()) { continue; } $userProjects = $u->projects()->toPages(); foreach ($userProjects as $project) { try { $currentUsers = $project->users()->yaml(); $userUuid = $u->uuid()->toString(); if (in_array($userUuid, $currentUsers)) { continue; } $currentUsers[] = $userUuid; $project->update(['users' => $currentUsers]); $migrated[] = [ 'user' => $u->name()->value(), 'email' => $u->email(), 'project' => $project->title()->value(), ]; } catch (\Throwable $th) { $errors[] = [ 'user' => $u->email(), 'project' => $project->title()->value(), 'error' => $th->getMessage() ]; } } } return [ 'status' => 'success', 'message' => count($migrated) . ' assignations migrées.', 'migrated' => $migrated, 'errors' => $errors ]; } ];