Merge branch 'permission-page/modernize' into 'master'

DX et UX: permissions page

See merge request Chill-Projet/chill-bundles!530
This commit is contained in:
Julien Fastré 2023-05-19 10:03:08 +00:00
commit dc6eeccaab
2 changed files with 61 additions and 119 deletions

View File

@ -16,14 +16,18 @@ use Chill\MainBundle\Entity\RoleScope;
use Chill\MainBundle\Entity\Scope; use Chill\MainBundle\Entity\Scope;
use Chill\MainBundle\Form\PermissionsGroupType; use Chill\MainBundle\Form\PermissionsGroupType;
use Chill\MainBundle\Form\Type\ComposedRoleScopeType; use Chill\MainBundle\Form\Type\ComposedRoleScopeType;
use Chill\MainBundle\Repository\PermissionsGroupRepository;
use Chill\MainBundle\Repository\RoleScopeRepository;
use Chill\MainBundle\Security\RoleProvider; use Chill\MainBundle\Security\RoleProvider;
use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\MainBundle\Templating\TranslatableStringHelper;
use Doctrine\ORM\EntityManagerInterface;
use RuntimeException; use RuntimeException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Role\Role; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Role\RoleHierarchy; use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface; use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface;
@ -32,62 +36,28 @@ use function array_key_exists;
/** /**
* Class PermissionsGroupController. * 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. * PermissionsGroupController constructor.
*/ */
public function __construct( public function __construct(
TranslatableStringHelper $translatableStringHelper, private readonly TranslatableStringHelper $translatableStringHelper,
RoleProvider $roleProvider, private readonly RoleProvider $roleProvider,
RoleHierarchy $roleHierarchy, private readonly RoleHierarchyInterface $roleHierarchy,
TranslatorInterface $translator, private readonly TranslatorInterface $translator,
ValidatorInterface $validator 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 = $this->permissionsGroupRepository->find($id);
$permissionsGroup = $em->getRepository(\Chill\MainBundle\Entity\PermissionsGroup::class)->find($id);
if (!$permissionsGroup) { if (!$permissionsGroup) {
throw $this->createNotFoundException('Unable to find PermissionsGroup entity.'); throw $this->createNotFoundException('Unable to find PermissionsGroup entity.');
@ -106,7 +76,7 @@ class PermissionsGroupController extends AbstractController
$violations = $this->validator->validate($permissionsGroup); $violations = $this->validator->validate($permissionsGroup);
if ($violations->count() === 0) { if ($violations->count() === 0) {
$em->flush(); $this->em->flush();
$this->addFlash( $this->addFlash(
'notice', 'notice',
@ -166,16 +136,15 @@ class PermissionsGroupController extends AbstractController
/** /**
* Creates a new PermissionsGroup entity. * Creates a new PermissionsGroup entity.
*/ */
public function createAction(Request $request) public function createAction(Request $request): Response
{ {
$permissionsGroup = new PermissionsGroup(); $permissionsGroup = new PermissionsGroup();
$form = $this->createCreateForm($permissionsGroup); $form = $this->createCreateForm($permissionsGroup);
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isValid()) { if ($form->isValid()) {
$em = $this->getDoctrine()->getManager(); $this->em->persist($permissionsGroup);
$em->persist($permissionsGroup); $this->em->flush();
$em->flush();
return $this->redirect($this->generateUrl( return $this->redirect($this->generateUrl(
'admin_permissionsgroup_edit', 'admin_permissionsgroup_edit',
@ -191,18 +160,12 @@ class PermissionsGroupController extends AbstractController
/** /**
* remove an association between permissionsGroup and roleScope. * 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); $permissionsGroup = $this->permissionsGroupRepository->find($pgid);
$roleScope = $em->getRepository(\Chill\MainBundle\Entity\RoleScope::class)->find($rsid); $roleScope = $this->roleScopeRepository->find($rsid);
if (!$permissionsGroup) { if (!$permissionsGroup) {
throw $this->createNotFoundException('Unable to find PermissionsGroup entity.'); throw $this->createNotFoundException('Unable to find PermissionsGroup entity.');
@ -214,7 +177,7 @@ class PermissionsGroupController extends AbstractController
try { try {
$permissionsGroup->removeRoleScope($roleScope); $permissionsGroup->removeRoleScope($roleScope);
} catch (RuntimeException $ex) { } catch (RuntimeException) {
$this->addFlash( $this->addFlash(
'notice', 'notice',
$this->translator->trans("The role '%role%' and circle " $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) { if ($roleScope->getScope() !== null) {
$this->addFlash( $this->addFlash(
@ -260,14 +223,10 @@ class PermissionsGroupController extends AbstractController
/** /**
* Displays a form to edit an existing PermissionsGroup entity. * 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 = $this->permissionsGroupRepository->find($id);
$permissionsGroup = $em->getRepository(\Chill\MainBundle\Entity\PermissionsGroup::class)->find($id);
if (!$permissionsGroup) { if (!$permissionsGroup) {
throw $this->createNotFoundException('Unable to find PermissionsGroup entity.'); throw $this->createNotFoundException('Unable to find PermissionsGroup entity.');
@ -311,11 +270,9 @@ class PermissionsGroupController extends AbstractController
/** /**
* Lists all PermissionsGroup entities. * Lists all PermissionsGroup entities.
*/ */
public function indexAction() public function indexAction(): Response
{ {
$em = $this->getDoctrine()->getManager(); $entities = $this->permissionsGroupRepository->findAllOrderedAlphabetically();
$entities = $em->getRepository(\Chill\MainBundle\Entity\PermissionsGroup::class)->findAll();
return $this->render('@ChillMain/PermissionsGroup/index.html.twig', [ return $this->render('@ChillMain/PermissionsGroup/index.html.twig', [
'entities' => $entities, 'entities' => $entities,
@ -325,7 +282,7 @@ class PermissionsGroupController extends AbstractController
/** /**
* Displays a form to create a new PermissionsGroup entity. * Displays a form to create a new PermissionsGroup entity.
*/ */
public function newAction() public function newAction(): Response
{ {
$permissionsGroup = new PermissionsGroup(); $permissionsGroup = new PermissionsGroup();
$form = $this->createCreateForm($permissionsGroup); $form = $this->createCreateForm($permissionsGroup);
@ -338,14 +295,10 @@ class PermissionsGroupController extends AbstractController
/** /**
* Finds and displays a PermissionsGroup entity. * Finds and displays a PermissionsGroup entity.
*
* @param mixed $id
*/ */
public function showAction($id) public function showAction(int $id): Response
{ {
$em = $this->getDoctrine()->getManager(); $permissionsGroup = $this->permissionsGroupRepository->find($id);
$permissionsGroup = $em->getRepository(\Chill\MainBundle\Entity\PermissionsGroup::class)->find($id);
if (!$permissionsGroup) { if (!$permissionsGroup) {
throw $this->createNotFoundException('Unable to find PermissionsGroup entity.'); throw $this->createNotFoundException('Unable to find PermissionsGroup entity.');
@ -393,15 +346,10 @@ class PermissionsGroupController extends AbstractController
/** /**
* Edits an existing PermissionsGroup entity. * 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 = $this->permissionsGroupRepository
$permissionsGroup = $em
->getRepository(\Chill\MainBundle\Entity\PermissionsGroup::class)
->find($id); ->find($id);
if (!$permissionsGroup) { if (!$permissionsGroup) {
@ -413,7 +361,7 @@ class PermissionsGroupController extends AbstractController
$editForm->handleRequest($request); $editForm->handleRequest($request);
if ($editForm->isValid()) { if ($editForm->isValid()) {
$em->flush(); $this->em->flush();
return $this->redirect($this->generateUrl('admin_permissionsgroup_edit', ['id' => $id])); 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 * get a role scope by his parameters. The role scope is persisted if it
* doesn't exists in database. * doesn't exist in database.
*
* @param Scope $scope
* @param string $role
*
* @return RoleScope
*/ */
protected function getPersistentRoleScopeBy($role, ?Scope $scope = null) protected function getPersistentRoleScopeBy(string $role, ?Scope $scope = null): RoleScope
{ {
$em = $this->getDoctrine()->getManager(); $roleScope = $this->roleScopeRepository
$roleScope = $em->getRepository(\Chill\MainBundle\Entity\RoleScope::class)
->findOneBy(['role' => $role, 'scope' => $scope]); ->findOneBy(['role' => $role, 'scope' => $scope]);
if (null === $roleScope) { if (null === $roleScope) {
@ -471,7 +412,7 @@ class PermissionsGroupController extends AbstractController
->setRole($role) ->setRole($role)
->setScope($scope); ->setScope($scope);
$em->persist($roleScope); $this->em->persist($roleScope);
} }
return $roleScope; return $roleScope;
@ -479,10 +420,8 @@ class PermissionsGroupController extends AbstractController
/** /**
* creates a form to add a role scope to permissionsgroup. * 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() return $this->createFormBuilder()
->setAction($this->generateUrl( ->setAction($this->generateUrl(
@ -499,10 +438,8 @@ class PermissionsGroupController extends AbstractController
* Creates a form to create a PermissionsGroup entity. * Creates a form to create a PermissionsGroup entity.
* *
* @param PermissionsGroup $permissionsGroup The 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, [ $form = $this->createForm(PermissionsGroupType::class, $permissionsGroup, [
'action' => $this->generateUrl('admin_permissionsgroup_create'), 'action' => $this->generateUrl('admin_permissionsgroup_create'),
@ -518,13 +455,11 @@ class PermissionsGroupController extends AbstractController
* Creates a form to delete a link to roleScope. * Creates a form to delete a link to roleScope.
* *
* @param mixed $permissionsGroup The entity id * @param mixed $permissionsGroup The entity id
*
* @return \Symfony\Component\Form\Form The form
*/ */
private function createDeleteRoleScopeForm( private function createDeleteRoleScopeForm(
PermissionsGroup $permissionsGroup, PermissionsGroup $permissionsGroup,
RoleScope $roleScope RoleScope $roleScope
) { ): FormInterface {
return $this->createFormBuilder() return $this->createFormBuilder()
->setAction($this->generateUrl( ->setAction($this->generateUrl(
'admin_permissionsgroup_delete_role_scope', 'admin_permissionsgroup_delete_role_scope',
@ -537,12 +472,8 @@ class PermissionsGroupController extends AbstractController
/** /**
* Creates a form to edit a PermissionsGroup entity. * 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, [ $form = $this->createForm(PermissionsGroupType::class, $permissionsGroup, [
'action' => $this->generateUrl('admin_permissionsgroup_update', ['id' => $permissionsGroup->getId()]), '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. * expand roleScopes to be easily shown in template.
*
* @return array
*/ */
private function getExpandedRoles(array $roleScopes) private function getExpandedRoles(array $roleScopes): array
{ {
$expandedRoles = []; $expandedRoles = [];
@ -567,10 +496,10 @@ class PermissionsGroupController extends AbstractController
if (!array_key_exists($roleScope->getRole(), $expandedRoles)) { if (!array_key_exists($roleScope->getRole(), $expandedRoles)) {
$expandedRoles[$roleScope->getRole()] = $expandedRoles[$roleScope->getRole()] =
array_map( array_map(
static fn (Role $role) => $role->getRole(), static fn ($role) => $role,
$this->roleHierarchy $this->roleHierarchy
->getReachableRoles( ->getReachableRoleNames(
[new Role($roleScope->getRole())] [$roleScope->getRole()]
) )
); );
} }

View File

@ -38,6 +38,19 @@ final class PermissionsGroupRepository implements ObjectRepository
return $this->repository->findAll(); return $this->repository->findAll();
} }
/**
* @return list<PermissionsGroup>
*/
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 $limit
* @param mixed|null $offset * @param mixed|null $offset