diff --git a/src/CoreBundle/Controller/PermissionController.php b/src/CoreBundle/Controller/PermissionController.php deleted file mode 100644 index 67d11e1338..0000000000 --- a/src/CoreBundle/Controller/PermissionController.php +++ /dev/null @@ -1,84 +0,0 @@ -findAll(); - $roles = $this->permissionServiceHelper->getUserRoles(); - - if ($request->isMethod('POST')) { - $data = $request->request->all('permissions'); - foreach ($permissions as $permission) { - foreach ($roles as $role) { - $checkboxValue = isset($data[$permission->getSlug()][$role]); - $permRelRole = $permissionRelRoleRepo->findOneBy(['permission' => $permission, 'roleCode' => $role]); - - if ($checkboxValue) { - if (!$permRelRole) { - $permRelRole = new PermissionRelRole(); - $permRelRole->setPermission($permission); - $permRelRole->setRoleCode($role); - } - $permRelRole->setChangeable(true); - $permRelRole->setUpdatedAt(new \DateTime()); - $em->persist($permRelRole); - } else { - if ($permRelRole) { - $em->remove($permRelRole); - } - } - } - } - $em->flush(); - - return $this->redirectToRoute('permissions'); - } - - $forms = []; - foreach ($permissions as $permission) { - $defaultData = []; - foreach ($roles as $role) { - $permRelRole = $permissionRelRoleRepo->findOneBy(['permission' => $permission, 'roleCode' => $role]); - $defaultData[$role] = $permRelRole ? $permRelRole->isChangeable() : false; - } - - $form = $this->createForm(PermissionType::class, $defaultData, ['roles' => $roles]); - $forms[$permission->getSlug()] = $form->createView(); - } - - return $this->render('@ChamiloCore/Permission/index.html.twig', [ - 'permissions' => $permissions, - 'forms' => $forms, - 'roles' => $roles - ]); - } -} diff --git a/src/CoreBundle/Controller/PermissionsController.php b/src/CoreBundle/Controller/PermissionsController.php index 961a0f9e57..d384020330 100644 --- a/src/CoreBundle/Controller/PermissionsController.php +++ b/src/CoreBundle/Controller/PermissionsController.php @@ -6,15 +6,84 @@ declare(strict_types=1); namespace Chamilo\CoreBundle\Controller; +use Chamilo\CoreBundle\Entity\PermissionRelRole; +use Chamilo\CoreBundle\Form\PermissionType; +use Chamilo\CoreBundle\Repository\PermissionRelRoleRepository; +use Chamilo\CoreBundle\Repository\PermissionRepository; +use Chamilo\CoreBundle\ServiceHelper\PermissionServiceHelper; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\Routing\Attribute\Route; +use Symfony\Component\Routing\Annotation\Route; +use Symfony\Component\Security\Http\Attribute\IsGranted; #[Route('/permissions')] class PermissionsController extends AbstractController { - #[Route('/is_allowed_to_edit')] + public function __construct( + private PermissionServiceHelper $permissionServiceHelper + ) {} + + #[IsGranted('ROLE_ADMIN')] + #[Route('', name: 'permissions')] + public function index( + PermissionRepository $permissionRepo, + PermissionRelRoleRepository $permissionRelRoleRepo, + Request $request, + EntityManagerInterface $em + ): Response { + $permissions = $permissionRepo->findAll(); + $roles = $this->permissionServiceHelper->getUserRoles(); + + if ($request->isMethod('POST')) { + $data = $request->request->all('permissions'); + foreach ($permissions as $permission) { + foreach ($roles as $role) { + $checkboxValue = isset($data[$permission->getSlug()][$role]); + $permRelRole = $permissionRelRoleRepo->findOneBy(['permission' => $permission, 'roleCode' => $role]); + + if ($checkboxValue) { + if (!$permRelRole) { + $permRelRole = new PermissionRelRole(); + $permRelRole->setPermission($permission); + $permRelRole->setRoleCode($role); + } + $permRelRole->setChangeable(true); + $permRelRole->setUpdatedAt(new \DateTime()); + $em->persist($permRelRole); + } else { + if ($permRelRole) { + $em->remove($permRelRole); + } + } + } + } + $em->flush(); + + return $this->redirectToRoute('permissions'); + } + + $forms = []; + foreach ($permissions as $permission) { + $defaultData = []; + foreach ($roles as $role) { + $permRelRole = $permissionRelRoleRepo->findOneBy(['permission' => $permission, 'roleCode' => $role]); + $defaultData[$role] = $permRelRole ? $permRelRole->isChangeable() : false; + } + + $form = $this->createForm(PermissionType::class, $defaultData, ['roles' => $roles]); + $forms[$permission->getSlug()] = $form->createView(); + } + + return $this->render('@ChamiloCore/Permission/index.html.twig', [ + 'permissions' => $permissions, + 'forms' => $forms, + 'roles' => $roles + ]); + } + + #[Route('/is_allowed_to_edit', name: 'is_allowed_to_edit')] public function isAllowedToEdit(Request $request): Response { $tutor = $request->query->getBoolean('tutor');