From 00838425098f72d1bdba73fc956e95a96caad392 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 25 Apr 2023 10:52:07 +0200 Subject: [PATCH] DX: modernize controller for permissions groups and order them alphabetically --- .../Controller/PermissionsGroupController.php | 167 +++++------------- .../Repository/PermissionsGroupRepository.php | 13 ++ 2 files changed, 61 insertions(+), 119 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Controller/PermissionsGroupController.php b/src/Bundle/ChillMainBundle/Controller/PermissionsGroupController.php index 5d32a8dbb..488092931 100644 --- a/src/Bundle/ChillMainBundle/Controller/PermissionsGroupController.php +++ b/src/Bundle/ChillMainBundle/Controller/PermissionsGroupController.php @@ -16,14 +16,18 @@ use Chill\MainBundle\Entity\RoleScope; use Chill\MainBundle\Entity\Scope; use Chill\MainBundle\Form\PermissionsGroupType; use Chill\MainBundle\Form\Type\ComposedRoleScopeType; +use Chill\MainBundle\Repository\PermissionsGroupRepository; +use Chill\MainBundle\Repository\RoleScopeRepository; use Chill\MainBundle\Security\RoleProvider; use Chill\MainBundle\Templating\TranslatableStringHelper; +use Doctrine\ORM\EntityManagerInterface; use RuntimeException; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Form\Extension\Core\Type\SubmitType; +use Symfony\Component\Form\FormInterface; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\Security\Core\Role\Role; -use Symfony\Component\Security\Core\Role\RoleHierarchy; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Security\Core\Role\RoleHierarchyInterface; use Symfony\Component\Validator\Validator\ValidatorInterface; use Symfony\Contracts\Translation\TranslatorInterface; @@ -32,62 +36,28 @@ use function array_key_exists; /** * Class PermissionsGroupController. */ -class PermissionsGroupController extends AbstractController +final class PermissionsGroupController extends AbstractController { - /** - * @var RoleHierarchy - */ - private $roleHierarchy; - - /** - * @var RoleProvider - */ - private $roleProvider; - - /** - * @var TranslatableStringHelper - */ - private $translatableStringHelper; - - /** - * @var TranslatorInterface - */ - private $translator; - - /** - * @var ValidatorInterface - */ - private $validator; - /** * PermissionsGroupController constructor. */ public function __construct( - TranslatableStringHelper $translatableStringHelper, - RoleProvider $roleProvider, - RoleHierarchy $roleHierarchy, - TranslatorInterface $translator, - ValidatorInterface $validator + private readonly TranslatableStringHelper $translatableStringHelper, + private readonly RoleProvider $roleProvider, + private readonly RoleHierarchyInterface $roleHierarchy, + private readonly TranslatorInterface $translator, + private readonly ValidatorInterface $validator, + private readonly EntityManagerInterface $em, + private readonly PermissionsGroupRepository $permissionsGroupRepository, + private readonly RoleScopeRepository $roleScopeRepository, ) { - $this->translatableStringHelper = $translatableStringHelper; - $this->roleProvider = $roleProvider; - $this->roleHierarchy = $roleHierarchy; - $this->translator = $translator; - $this->validator = $validator; } /** - * @param int $id - * - * @throws type - * - * @return Respon */ - public function addLinkRoleScopeAction(Request $request, $id) + public function addLinkRoleScopeAction(Request $request, int $id): Response { - $em = $this->getDoctrine()->getManager(); - - $permissionsGroup = $em->getRepository(\Chill\MainBundle\Entity\PermissionsGroup::class)->find($id); + $permissionsGroup = $this->permissionsGroupRepository->find($id); if (!$permissionsGroup) { throw $this->createNotFoundException('Unable to find PermissionsGroup entity.'); @@ -106,7 +76,7 @@ class PermissionsGroupController extends AbstractController $violations = $this->validator->validate($permissionsGroup); if ($violations->count() === 0) { - $em->flush(); + $this->em->flush(); $this->addFlash( 'notice', @@ -166,16 +136,15 @@ class PermissionsGroupController extends AbstractController /** * Creates a new PermissionsGroup entity. */ - public function createAction(Request $request) + public function createAction(Request $request): Response { $permissionsGroup = new PermissionsGroup(); $form = $this->createCreateForm($permissionsGroup); $form->handleRequest($request); if ($form->isValid()) { - $em = $this->getDoctrine()->getManager(); - $em->persist($permissionsGroup); - $em->flush(); + $this->em->persist($permissionsGroup); + $this->em->flush(); return $this->redirect($this->generateUrl( 'admin_permissionsgroup_edit', @@ -191,18 +160,12 @@ class PermissionsGroupController extends AbstractController /** * remove an association between permissionsGroup and roleScope. - * - * @param int $pgid permissionsGroup id - * @param int $rsid roleScope id - * - * @return redirection to edit form */ - public function deleteLinkRoleScopeAction($pgid, $rsid) + public function deleteLinkRoleScopeAction(int $pgid, int $rsid): Response { - $em = $this->getDoctrine()->getManager(); - $permissionsGroup = $em->getRepository(\Chill\MainBundle\Entity\PermissionsGroup::class)->find($pgid); - $roleScope = $em->getRepository(\Chill\MainBundle\Entity\RoleScope::class)->find($rsid); + $permissionsGroup = $this->permissionsGroupRepository->find($pgid); + $roleScope = $this->roleScopeRepository->find($rsid); if (!$permissionsGroup) { throw $this->createNotFoundException('Unable to find PermissionsGroup entity.'); @@ -214,7 +177,7 @@ class PermissionsGroupController extends AbstractController try { $permissionsGroup->removeRoleScope($roleScope); - } catch (RuntimeException $ex) { + } catch (RuntimeException) { $this->addFlash( 'notice', $this->translator->trans("The role '%role%' and circle " @@ -231,7 +194,7 @@ class PermissionsGroupController extends AbstractController )); } - $em->flush(); + $this->em->flush(); if ($roleScope->getScope() !== null) { $this->addFlash( @@ -260,14 +223,10 @@ class PermissionsGroupController extends AbstractController /** * Displays a form to edit an existing PermissionsGroup entity. - * - * @param mixed $id */ - public function editAction($id) + public function editAction(int $id): Response { - $em = $this->getDoctrine()->getManager(); - - $permissionsGroup = $em->getRepository(\Chill\MainBundle\Entity\PermissionsGroup::class)->find($id); + $permissionsGroup = $this->permissionsGroupRepository->find($id); if (!$permissionsGroup) { throw $this->createNotFoundException('Unable to find PermissionsGroup entity.'); @@ -311,11 +270,9 @@ class PermissionsGroupController extends AbstractController /** * Lists all PermissionsGroup entities. */ - public function indexAction() + public function indexAction(): Response { - $em = $this->getDoctrine()->getManager(); - - $entities = $em->getRepository(\Chill\MainBundle\Entity\PermissionsGroup::class)->findAll(); + $entities = $this->permissionsGroupRepository->findAllOrderedAlphabetically(); return $this->render('@ChillMain/PermissionsGroup/index.html.twig', [ 'entities' => $entities, @@ -325,7 +282,7 @@ class PermissionsGroupController extends AbstractController /** * Displays a form to create a new PermissionsGroup entity. */ - public function newAction() + public function newAction(): Response { $permissionsGroup = new PermissionsGroup(); $form = $this->createCreateForm($permissionsGroup); @@ -338,14 +295,10 @@ class PermissionsGroupController extends AbstractController /** * Finds and displays a PermissionsGroup entity. - * - * @param mixed $id */ - public function showAction($id) + public function showAction(int $id): Response { - $em = $this->getDoctrine()->getManager(); - - $permissionsGroup = $em->getRepository(\Chill\MainBundle\Entity\PermissionsGroup::class)->find($id); + $permissionsGroup = $this->permissionsGroupRepository->find($id); if (!$permissionsGroup) { throw $this->createNotFoundException('Unable to find PermissionsGroup entity.'); @@ -393,15 +346,10 @@ class PermissionsGroupController extends AbstractController /** * Edits an existing PermissionsGroup entity. - * - * @param mixed $id */ - public function updateAction(Request $request, $id) + public function updateAction(Request $request, int $id): Response { - $em = $this->getDoctrine()->getManager(); - - $permissionsGroup = $em - ->getRepository(\Chill\MainBundle\Entity\PermissionsGroup::class) + $permissionsGroup = $this->permissionsGroupRepository ->find($id); if (!$permissionsGroup) { @@ -413,7 +361,7 @@ class PermissionsGroupController extends AbstractController $editForm->handleRequest($request); if ($editForm->isValid()) { - $em->flush(); + $this->em->flush(); return $this->redirect($this->generateUrl('admin_permissionsgroup_edit', ['id' => $id])); } @@ -452,18 +400,11 @@ class PermissionsGroupController extends AbstractController /** * get a role scope by his parameters. The role scope is persisted if it - * doesn't exists in database. - * - * @param Scope $scope - * @param string $role - * - * @return RoleScope + * doesn't exist in database. */ - protected function getPersistentRoleScopeBy($role, ?Scope $scope = null) + protected function getPersistentRoleScopeBy(string $role, ?Scope $scope = null): RoleScope { - $em = $this->getDoctrine()->getManager(); - - $roleScope = $em->getRepository(\Chill\MainBundle\Entity\RoleScope::class) + $roleScope = $this->roleScopeRepository ->findOneBy(['role' => $role, 'scope' => $scope]); if (null === $roleScope) { @@ -471,7 +412,7 @@ class PermissionsGroupController extends AbstractController ->setRole($role) ->setScope($scope); - $em->persist($roleScope); + $this->em->persist($roleScope); } return $roleScope; @@ -479,10 +420,8 @@ class PermissionsGroupController extends AbstractController /** * creates a form to add a role scope to permissionsgroup. - * - * @return \Symfony\Component\Form\Form The form */ - private function createAddRoleScopeForm(PermissionsGroup $permissionsGroup) + private function createAddRoleScopeForm(PermissionsGroup $permissionsGroup): FormInterface { return $this->createFormBuilder() ->setAction($this->generateUrl( @@ -499,10 +438,8 @@ class PermissionsGroupController extends AbstractController * Creates a form to create a PermissionsGroup entity. * * @param PermissionsGroup $permissionsGroup The entity - * - * @return \Symfony\Component\Form\Form The form */ - private function createCreateForm(PermissionsGroup $permissionsGroup) + private function createCreateForm(PermissionsGroup $permissionsGroup): FormInterface { $form = $this->createForm(PermissionsGroupType::class, $permissionsGroup, [ 'action' => $this->generateUrl('admin_permissionsgroup_create'), @@ -518,13 +455,11 @@ class PermissionsGroupController extends AbstractController * Creates a form to delete a link to roleScope. * * @param mixed $permissionsGroup The entity id - * - * @return \Symfony\Component\Form\Form The form */ private function createDeleteRoleScopeForm( PermissionsGroup $permissionsGroup, RoleScope $roleScope - ) { + ): FormInterface { return $this->createFormBuilder() ->setAction($this->generateUrl( 'admin_permissionsgroup_delete_role_scope', @@ -537,12 +472,8 @@ class PermissionsGroupController extends AbstractController /** * Creates a form to edit a PermissionsGroup entity. - * - * @param PermissionsGroup $permissionsGroup The entity - * - * @return \Symfony\Component\Form\Form The form */ - private function createEditForm(PermissionsGroup $permissionsGroup) + private function createEditForm(PermissionsGroup $permissionsGroup): FormInterface { $form = $this->createForm(PermissionsGroupType::class, $permissionsGroup, [ 'action' => $this->generateUrl('admin_permissionsgroup_update', ['id' => $permissionsGroup->getId()]), @@ -556,10 +487,8 @@ class PermissionsGroupController extends AbstractController /** * expand roleScopes to be easily shown in template. - * - * @return array */ - private function getExpandedRoles(array $roleScopes) + private function getExpandedRoles(array $roleScopes): array { $expandedRoles = []; @@ -567,10 +496,10 @@ class PermissionsGroupController extends AbstractController if (!array_key_exists($roleScope->getRole(), $expandedRoles)) { $expandedRoles[$roleScope->getRole()] = array_map( - static fn (Role $role) => $role->getRole(), + static fn ($role) => $role, $this->roleHierarchy - ->getReachableRoles( - [new Role($roleScope->getRole())] + ->getReachableRoleNames( + [$roleScope->getRole()] ) ); } diff --git a/src/Bundle/ChillMainBundle/Repository/PermissionsGroupRepository.php b/src/Bundle/ChillMainBundle/Repository/PermissionsGroupRepository.php index 64863bf8c..0f57d0bac 100644 --- a/src/Bundle/ChillMainBundle/Repository/PermissionsGroupRepository.php +++ b/src/Bundle/ChillMainBundle/Repository/PermissionsGroupRepository.php @@ -38,6 +38,19 @@ final class PermissionsGroupRepository implements ObjectRepository return $this->repository->findAll(); } + /** + * @return list + */ + public function findAllOrderedAlphabetically(): array + { + $qb = $this->repository->createQueryBuilder('pg'); + + return $qb->select(['pg', 'pg.name AS HIDDEN sort_name']) + ->orderBy('sort_name') + ->getQuery() + ->getResult(); + } + /** * @param mixed|null $limit * @param mixed|null $offset