feat: inversion relation User→Projects, les projets pointent vers les utilisateurs
All checks were successful
Deploy Preprod / Build and Deploy to Preprod (push) Successful in 31s
All checks were successful
Deploy Preprod / Build and Deploy to Preprod (push) Successful in 31s
Le champ `users` est désormais sur le blueprint projet. Les blueprints pochet/client perdent leur champ `projects`. La logique PHP (user-projects, managers, controller, template, mark-all-read) lit project.users au lieu de user.projects. Script de migration inclus. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
075e511a6a
commit
ea90f512cf
10 changed files with 101 additions and 25 deletions
|
|
@ -44,6 +44,7 @@ return [
|
|||
require(__DIR__ . '/routes/request-project-creation.php'),
|
||||
require(__DIR__ . '/routes/request-optimization-appointment.php'),
|
||||
require(__DIR__ . '/routes/migrate-notifications.php'),
|
||||
require(__DIR__ . '/routes/migrate-user-projects.php'),
|
||||
],
|
||||
'hooks' => [
|
||||
'page.create:after' => require_once(__DIR__ . '/hooks/create-steps.php'),
|
||||
|
|
|
|||
75
public/site/config/routes/migrate-user-projects.php
Normal file
75
public/site/config/routes/migrate-user-projects.php
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Script de migration : inverse la relation User→Projects en Project→Users.
|
||||
*
|
||||
* Pour chaque user (non-admin) ayant un champ projects non vide,
|
||||
* ajoute ce user dans le champ `users` du projet correspondant.
|
||||
*
|
||||
* Idempotent : pas de doublons si exécuté plusieurs fois.
|
||||
* À supprimer après migration.
|
||||
*
|
||||
* Usage: POST /migrate-user-projects.json
|
||||
*/
|
||||
|
||||
return [
|
||||
'pattern' => '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
|
||||
];
|
||||
}
|
||||
];
|
||||
Loading…
Add table
Add a link
Reference in a new issue