*/ class RolesController extends AbstractAdminApiCrudController { protected string $entityClass = Entity\Role::class; protected string $resourceRouteName = 'api:admin:role'; public function __construct( protected Acl $acl, protected Entity\Repository\RolePermissionRepository $permissionRepo, ReloadableEntityManagerInterface $em, Serializer $serializer, ValidatorInterface $validator ) { parent::__construct($em, $serializer, $validator); } protected function deleteRecord(object $record): void { if (!($record instanceof Entity\Role)) { throw new InvalidArgumentException(sprintf('Record must be an instance of %s.', $this->entityClass)); } $superAdminRole = $this->permissionRepo->ensureSuperAdministratorRole(); if ($superAdminRole->getIdRequired() === $record->getIdRequired()) { throw new RuntimeException('Cannot remove the Super Administrator role.'); } parent::deleteRecord($record); } protected function fromArray(array $data, $record = null, array $context = []): object { return parent::fromArray( $data, $record, array_merge( $context, [ AbstractNormalizer::CALLBACKS => [ 'permissions' => function (array $value, Entity\Role $record) { $this->doUpdatePermissions($record, $value); return null; }, ], ] ) ); } protected function doUpdatePermissions(Entity\Role $role, array $newPermissions): void { $existingPerms = $role->getPermissions(); if ($existingPerms->count() > 0) { foreach ($existingPerms as $perm) { $this->em->remove($perm); } $this->em->flush(); $existingPerms->clear(); } if (isset($newPermissions['global'])) { foreach ($newPermissions['global'] as $perm_name) { if ($this->acl->isValidPermission($perm_name, true)) { $perm_record = new Entity\RolePermission($role, null, $perm_name); $this->em->persist($perm_record); } } } if (isset($newPermissions['station'])) { foreach ($newPermissions['station'] as $station_id => $station_perms) { $station = $this->em->find(Entity\Station::class, $station_id); if ($station instanceof Entity\Station) { foreach ($station_perms as $perm_name) { if ($this->acl->isValidPermission($perm_name, false)) { $perm_record = new Entity\RolePermission($role, $station, $perm_name); $this->em->persist($perm_record); } } } } } } }