From 2fb1414228b28766742713247e97c8de34e6dbc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Melaine=20G=C3=A9rard?= Date: Sun, 29 Dec 2024 16:00:42 +0100 Subject: [PATCH] :sparkles: Ajout gestion des utilisateurs --- src/Command/CreateUserCommand.php | 1 + src/Controller/AdminController.php | 91 +++++++++++++++++++ ...oardController.php => FilesController.php} | 4 +- src/Entity/User.php | 9 +- src/Form/UserAdminType.php | 56 ++++++++++++ templates/admin/index.html.twig | 15 +++ templates/admin/user_edit.html.twig | 30 ++++++ templates/admin/user_index.html.twig | 63 +++++++++++++ templates/base-admin.html.twig | 18 ++++ .../{dashboard => files}/index.html.twig | 10 +- templates/partials/alerts.html.twig | 21 +++++ templates/partials/navbar-admin.html.twig | 28 ++++++ templates/partials/navbar.html.twig | 2 +- 13 files changed, 338 insertions(+), 10 deletions(-) create mode 100644 src/Controller/AdminController.php rename src/Controller/{DashboardController.php => FilesController.php} (96%) create mode 100644 src/Form/UserAdminType.php create mode 100644 templates/admin/index.html.twig create mode 100644 templates/admin/user_edit.html.twig create mode 100644 templates/admin/user_index.html.twig create mode 100644 templates/base-admin.html.twig rename templates/{dashboard => files}/index.html.twig (84%) create mode 100644 templates/partials/alerts.html.twig create mode 100644 templates/partials/navbar-admin.html.twig diff --git a/src/Command/CreateUserCommand.php b/src/Command/CreateUserCommand.php index 276a6e5..bd1ef0b 100644 --- a/src/Command/CreateUserCommand.php +++ b/src/Command/CreateUserCommand.php @@ -47,6 +47,7 @@ class CreateUserCommand extends Command $user->setEmail($email); $user->setPassword($this->passwordHasher->hashPassword($user, $password)); $user->setRoles($isAdmin ? ['ROLE_ADMIN'] : ['ROLE_USER']); + $user->initId(); $this->entityManager->persist($user); $this->entityManager->flush(); diff --git a/src/Controller/AdminController.php b/src/Controller/AdminController.php new file mode 100644 index 0000000..ccb28e9 --- /dev/null +++ b/src/Controller/AdminController.php @@ -0,0 +1,91 @@ +render('admin/index.html.twig'); + } + + #[Route('/users', name: 'user_index')] + public function indexUsers(): Response + { + $users = $this->userRepository->findAll(); + return $this->render('admin/user_index.html.twig', [ + 'users' => $users, + ]); + } + + + #[Route('/users/create', name: 'user_create')] + #[Route('/users/edit/{user}', name: 'user_edit')] + public function editUsers(#[MapEntity(id: 'user')] ?User $user, Request $request): Response + { + $isNew = false; + if (!$user) { + $user = new User(); + $isNew = true; + } + + $form = $this->createForm(UserAdminType::class, $user); + + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $role = $form->get('role')->getData(); + $user->setRoles([$role]); + $user->initId(); + + if ($form->has('plainPassword')) { + $plainPassword = $form->get('plainPassword')->getData(); + $user->setPassword($this->passwordEncoder->hashPassword($user, $plainPassword)); + } + + $this->entityManager->persist($user); + $this->entityManager->flush(); + + $this->addFlash('success', 'L\'utilisateur a bien été enregistré !'); + return $this->redirectToRoute('app_admin_user_index'); + } + + return $this->render('admin/user_edit.html.twig', [ + 'form' => $form->createView(), + 'user' => $user, + 'isNew' => $isNew, + ]); + } + + #[Route('/users/delete/{user}', name: 'user_delete')] + public function deleteUser(#[MapEntity(id: 'user')] User $user): Response + { + $this->entityManager->remove($user); + $this->entityManager->flush(); + + $this->addFlash('success', 'L\'utilisateur a bien été supprimé !'); + return $this->redirectToRoute('app_admin_user_index'); + } +} diff --git a/src/Controller/DashboardController.php b/src/Controller/FilesController.php similarity index 96% rename from src/Controller/DashboardController.php rename to src/Controller/FilesController.php index d18322f..02acf9d 100644 --- a/src/Controller/DashboardController.php +++ b/src/Controller/FilesController.php @@ -16,7 +16,7 @@ use Symfony\Component\Security\Http\Attribute\IsGranted; #[Route('/files', 'app_files_')] #[IsGranted('ROLE_USER')] -class DashboardController extends AbstractController +class FilesController extends AbstractController { /** * @throws FilesystemException @@ -62,7 +62,7 @@ class DashboardController extends AbstractController } }); - return $this->render('dashboard/index.html.twig', [ + return $this->render('files/index.html.twig', [ 'files' => $realFiles, 'path' => $path, ]); diff --git a/src/Entity/User.php b/src/Entity/User.php index 1824b8d..f561b73 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -17,7 +17,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface { #[ORM\Id] #[ORM\Column(type: 'uuid', length: 180)] - private ?Uuid $id; + private ?Uuid $id = null; #[ORM\Column(length: 255)] private ?string $email = null; @@ -34,8 +34,13 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface #[ORM\Column] private ?string $password = null; - public function __construct() + + public function initId(): void { + if ($this->id !== null) { + return; + } + $this->id = Uuid::v4(); } diff --git a/src/Form/UserAdminType.php b/src/Form/UserAdminType.php new file mode 100644 index 0000000..ef54f1a --- /dev/null +++ b/src/Form/UserAdminType.php @@ -0,0 +1,56 @@ +add('email', EmailType::class, [ + 'label' => 'Adresse email', + ]) + ->add('role', ChoiceType::class, [ + 'label' => 'Rôle', + 'choices' => [ + 'Utilisateur' => 'ROLE_USER', + 'Administrateur' => 'ROLE_ADMIN', + ], + 'mapped' => false, + ]) + ; + + // Si l'utilisateur est nouveau, on ajoute le champ de mot de passe + if (!$options['data']->getId()) { + $builder->add('plainPassword', PasswordType::class, [ + 'label' => 'Mot de passe', + 'required' => true, + 'mapped' => false, + ]); + } else { + // On set le rôle actuel de l'utilisateur + $builder->get('role')->setData($options['data']->getRoles()[0]); + } + + $builder->add('submit', SubmitType::class, [ + 'label' => 'Enregistrer', + ]); + + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults([ + 'data_class' => User::class, + ]); + } +} diff --git a/templates/admin/index.html.twig b/templates/admin/index.html.twig new file mode 100644 index 0000000..02887da --- /dev/null +++ b/templates/admin/index.html.twig @@ -0,0 +1,15 @@ +{% extends 'base-admin.html.twig' %} + +{% block title %}Le cloud de Camélia-Studio{% endblock %} + +{% block body %} +
+

Bienvenue sur l'administration

+

Gérez facilement les accès des membres de Camélia-Studio à l'espace de stockage partagé Kumora. Ajoutez, modifiez ou retirez les utilisateurs en quelques clics.

+ +
+{% endblock %} \ No newline at end of file diff --git a/templates/admin/user_edit.html.twig b/templates/admin/user_edit.html.twig new file mode 100644 index 0000000..4bd31c0 --- /dev/null +++ b/templates/admin/user_edit.html.twig @@ -0,0 +1,30 @@ +{% extends 'base-admin.html.twig' %} + +{% block body %} +
+
+

+ {% if not isNew %} + Edition de l'utilisateur {{ user.email }} + {% else %} + Création d'un nouvel utilisateur + {% endif %} +

+
+ {{ include('partials/alerts.html.twig') }} +
+ + {{ form(form) }} +
+
+{% endblock %} + + +{% block title %} + {% if not isNew %} + Edition de l'utilisateur {{ user.email }} + {% else %} + Création d'un nouvel utilisateur + {% endif %} +{% endblock %} + diff --git a/templates/admin/user_index.html.twig b/templates/admin/user_index.html.twig new file mode 100644 index 0000000..b5eed15 --- /dev/null +++ b/templates/admin/user_index.html.twig @@ -0,0 +1,63 @@ +{% extends 'base-admin.html.twig' %} + +{% block body %} +
+
+
Liste des utilisateurs
+ +
+ {{ include('partials/alerts.html.twig') }} +
+
+ + + + + + + + + + + {% for user in users %} + + + + + + + {% endfor %} + +
+ Id + + Email + + Rôle + + Action +
+ {{ user.id }} + + {{ user.email }} + + {{ user.roles[0] == 'ROLE_ADMIN' ? 'Administrateur' : 'Utilisateur' }} + + + +
+
+
+
+{% endblock %} + +{% block title %} + Liste des utilisateurs +{% endblock %} + diff --git a/templates/base-admin.html.twig b/templates/base-admin.html.twig new file mode 100644 index 0000000..0766c8f --- /dev/null +++ b/templates/base-admin.html.twig @@ -0,0 +1,18 @@ + + + + + Kumora - {% block title %}Accueil{% endblock %} + + {% block stylesheets %} + {% endblock %} + + {% block javascripts %} + {% block importmap %}{{ importmap('app') }}{% endblock %} + {% endblock %} + + + {% include "partials/navbar-admin.html.twig" %} + {% block body %}{% endblock %} + + diff --git a/templates/dashboard/index.html.twig b/templates/files/index.html.twig similarity index 84% rename from templates/dashboard/index.html.twig rename to templates/files/index.html.twig index 3b3cf97..19fa9c5 100644 --- a/templates/dashboard/index.html.twig +++ b/templates/files/index.html.twig @@ -5,11 +5,13 @@ {% block body %}

Liste des fichiers

+
+ {{ include('partials/alerts.html.twig') }} +
{% include 'partials/breadbrumb.html.twig' %}
-
@@ -49,13 +51,11 @@
{% if file.type == 'file' %} - - - + {% else %} - + {% endif %} diff --git a/templates/partials/alerts.html.twig b/templates/partials/alerts.html.twig new file mode 100644 index 0000000..df4ea03 --- /dev/null +++ b/templates/partials/alerts.html.twig @@ -0,0 +1,21 @@ +{% for label, messages in app.flashes %} + {% for message in messages %} + {% if label == 'success' %} + + {% elseif label == 'info' %} + + {% elseif label == 'error' %} + + {% elseif label == 'warning' %} + + {% endif %} + {% endfor %} +{% endfor %} \ No newline at end of file diff --git a/templates/partials/navbar-admin.html.twig b/templates/partials/navbar-admin.html.twig new file mode 100644 index 0000000..4e78c75 --- /dev/null +++ b/templates/partials/navbar-admin.html.twig @@ -0,0 +1,28 @@ + + diff --git a/templates/partials/navbar.html.twig b/templates/partials/navbar.html.twig index e12f0ef..2d5e5fc 100644 --- a/templates/partials/navbar.html.twig +++ b/templates/partials/navbar.html.twig @@ -25,7 +25,7 @@ {% else %} {% if is_granted('ROLE_ADMIN') %}
  • - Administration + Administration
  • {% endif %}