Add default scopes on the creation of accompanying periods

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
julielenaertsandClaude Opus 5.5 committed 2026-11-23 16:38:57 +01:00
1 parent c299458724
commit cd03c9c45f
1 file changed
+66
+66
View File
@@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Pre-select the scopes of the user when creating an accompanying period.
*
* For each permissions group, the default scopes for the event
* `chill_person.accompanying_period.create` are the active scopes on which this
* group may create an accompanying period: the role `CHILL_PERSON_ACCOMPANYING_PERIOD_CREATE`,
* or `CHILL_PERSON_ACCOMPANYING_PERIOD_FULL`, which includes it in the role hierarchy.
*
* Groups which already have default scopes for this event are left untouched.
*
* @see vendor/chill-project/chill-bundles/docs/source/development/default-scopes-for-an-entity.md
*/
final class Version20261123150000 extends AbstractMigration
{
private const ON_EVENT = 'chill_person.accompanying_period.create';
private const ROLE_CREATE = 'CHILL_PERSON_ACCOMPANYING_PERIOD_CREATE';
private const ROLE_FULL = 'CHILL_PERSON_ACCOMPANYING_PERIOD_FULL';
public function getDescription(): string
{
return 'Add default scopes on the creation of accompanying periods, from the scopes on which each permissions group may create them';
}
public function up(Schema $schema): void
{
$this->addSql(
<<<'SQL'
WITH group_scopes AS (
SELECT DISTINCT prs.permissionsgroup_id AS group_id, rs.scope_id
FROM permissionsgroup_rolescope prs
JOIN role_scopes rs ON rs.id = prs.rolescope_id
JOIN scopes s ON s.id = rs.scope_id
WHERE rs.role IN (:role_create, :role_full) AND s.active
),
inserted AS (
INSERT INTO chill_main_permission_group_default_scopes (id, group_id, on_event)
SELECT nextval('chill_main_permission_group_default_scopes_id_seq'), g.group_id, :on_event
FROM (SELECT DISTINCT group_id FROM group_scopes) g
ON CONFLICT (group_id, on_event) DO NOTHING
RETURNING id, group_id
)
INSERT INTO chill_main_permission_group_default_scopes_scope (defaultscopes_id, scope_id)
SELECT inserted.id, group_scopes.scope_id
FROM inserted
JOIN group_scopes ON group_scopes.group_id = inserted.group_id
SQL,
['role_create' => self::ROLE_CREATE, 'role_full' => self::ROLE_FULL, 'on_event' => self::ON_EVENT],
);
}
public function down(Schema $schema): void
{
$this->throwIrreversibleMigrationException('The default scopes created by this migration cannot be distinguished from those added by hand.');
}
}