diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 0a72108..3293c39 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -4,7 +4,9 @@ namespace App\Controller; use App\Entity\User; use App\Entity\UsersOrganizations; +use App\Form\NewUserForm; use App\Service\UserOrganizationService; +use App\Service\UserService; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; @@ -14,11 +16,17 @@ use Symfony\Component\Routing\Attribute\Route; #[Route(path: '/user', name: 'user_')] class UserController extends AbstractController { - public function __construct(private readonly UserOrganizationService $userOrganizationService) + public function __construct( + private readonly UserOrganizationService $userOrganizationService, + private readonly EntityManagerInterface $entityManager, + private readonly UserService $userService) { } - #[Route('/dashboard', name: 'dashboard')] + /** + * GET /user - List all users (index/collection) + */ + #[Route('/', name: 'index', methods: ['GET'])] public function index(EntityManagerInterface $entityManager): Response { if ($this->isGranted('ROLE_SUDALYS_ADMIN')) { @@ -32,25 +40,66 @@ class UserController extends AbstractController ]); } - #[Route('/{id}', name: 'view', methods: ['GET'])] - public function userProfile(Request $request, EntityManagerInterface $entityManager): Response + /** + * GET /user/{id} - Show specific user (show/member) + */ + #[Route('/{id}', name: 'show', methods: ['GET'], requirements: ['id' => '\d+'])] + public function show(int $id, EntityManagerInterface $entityManager): Response { - if ($this->isGranted('ROLE_SUDALYS_ADMIN')) { - $userId = $request->attributes->get('id'); - $user = $entityManager->getRepository(User::class)->find($userId); - if(!$user) { - throw $this->createNotFoundException('User not found'); - } - $userOrganizations = $this->userOrganizationService->getUserOrganizations($user); - return $this->render('user/profile.html.twig', [ - 'user' => $user, - 'userOrganizations' => $userOrganizations, - ]); + if (!$this->isGranted('ROLE_SUDALYS_ADMIN')) { + throw $this->createAccessDeniedException('Access denied'); } - return new Response('Unauthorized', Response::HTTP_UNAUTHORIZED); + $user = $entityManager->getRepository(User::class)->find($id); + if (!$user) { + throw $this->createNotFoundException('User not found'); + } + + $userOrganizations = $this->userOrganizationService->getUserOrganizations($user); + + return $this->render('user/profile.html.twig', [ + 'user' => $user, + 'userOrganizations' => $userOrganizations, + ]); } + /** + * GET /user/new - Show form to create new user and handle submission + */ + #[Route('/new', name: 'new', methods: ['GET', 'POST'])] + public function new(Request $request): Response + { + $form = $this->createForm(NewUserForm::class); + + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $data = $form->getData(); + // Handle user creation logic here + $user = new User(); + $user->setEmail($data['email']); + $user->setName($data['name']); + $user->setSurname($data['surname']); + $user->setPhoneNumber($data['number']); + + //FOR DEV PURPOSES ONLY + $user->setPictureUrl(""); + $user->setPassword($this->userService->generateRandomPassword()); + //FOR DEV PURPOSES ONLY + + $this->entityManager->persist($user); + $this->entityManager->flush(); + + // Redirect to user index + return $this->redirectToRoute('user_index'); + } + + return $this->render('user/new.html.twig', [ + 'form' => $form->createView(), + ]); + } + + #[Route('/deactivate/{id}', name: 'deactivate', methods: ['GET'])] public function userDeactivate(Request $request, EntityManagerInterface $entityManager): Response { @@ -68,4 +117,6 @@ class UserController extends AbstractController return new Response('Unauthorized', Response::HTTP_UNAUTHORIZED); } + + } diff --git a/src/Entity/User.php b/src/Entity/User.php index 6215cab..6774661 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -62,6 +62,14 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface #[ORM\Column(length: 20, nullable: true)] private ?string $phoneNumber = null; + public function __construct() + { + $this->createdAt = new \DateTimeImmutable(); + $this->modifiedAt = new \DateTimeImmutable(); + $this->isActive = true; + $this->isDeleted = false; + } + public function getId(): ?int { diff --git a/src/Form/NewUserForm.php b/src/Form/NewUserForm.php new file mode 100644 index 0000000..56ecf36 --- /dev/null +++ b/src/Form/NewUserForm.php @@ -0,0 +1,20 @@ +add('email', EmailType::class, ['required' => true, 'label' => 'Email*']) + ->add('name', TextType::class, ['required' => true, 'label' => 'Prénom*']) + ->add('surname', TextType::class, ['required' => true, 'label' => 'Nom*']) + ->add('number', TextType::class, ['required' => false, 'label' => 'Numéro de téléphone']); + } +} diff --git a/src/Service/UserService.php b/src/Service/UserService.php new file mode 100644 index 0000000..9c9b1cd --- /dev/null +++ b/src/Service/UserService.php @@ -0,0 +1,29 @@ + - + {{ ux_icon('bi:menu-up', {height: '15px', width: '15px'}) }} diff --git a/templates/user/index.html.twig b/templates/user/index.html.twig index ea39181..5a20742 100644 --- a/templates/user/index.html.twig +++ b/templates/user/index.html.twig @@ -6,7 +6,7 @@