Create user
This commit is contained in:
parent
c99b575814
commit
d43b516826
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
{
|
||||
|
|
|
|||
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
namespace App\Form;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\EmailType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
|
||||
class NewUserForm extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
$builder
|
||||
->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']);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
|
||||
class UserService
|
||||
{
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
// Constructor logic if needed
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a random password for a new user until they set their own.
|
||||
*/
|
||||
public function generateRandomPassword(): string{
|
||||
$length = 50; // Length of the password
|
||||
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+';
|
||||
$charactersLength = strlen($characters);
|
||||
$randomPassword = '';
|
||||
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
$randomPassword .= $characters[rand(0, $charactersLength - 1)];
|
||||
}
|
||||
|
||||
return $randomPassword;
|
||||
}
|
||||
}
|
||||
|
|
@ -25,7 +25,7 @@
|
|||
{# if user is Super Admin#}
|
||||
{% if is_granted('ROLE_SUDALYS_ADMIN') %}
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="{{ path('user_dashboard') }}">
|
||||
<a class="nav-link" href="{{ path('user_index') }}">
|
||||
<i class="icon-grid menu-icon">{{ ux_icon('bi:menu-up', {height: '15px', width: '15px'}) }}</i>
|
||||
<span class="menu-title">Users</span>
|
||||
</a>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<div class="w-100 h-100 p-5 m-auto ">
|
||||
<div class="d-flex justify-content-between align-items-center mb-4">
|
||||
<h1>Gestion Utilisateurs</h1>
|
||||
<a href="#" class="btn btn-primary">Ajouter un utilisateur</a>
|
||||
<a href="{{ path('user_new') }}" class="btn btn-primary">Ajouter un utilisateur</a>
|
||||
</div>
|
||||
|
||||
|
||||
|
|
@ -28,7 +28,7 @@
|
|||
<td>{{ user.name }}</td>
|
||||
<td>{{ user.email }}</td>
|
||||
<td>
|
||||
<a href="{{ path('user_view', {'id': user.id}) }}" class="p-3 align-middle">
|
||||
<a href="{{ path('user_show', {'id': user.id}) }}" class="p-3 align-middle">
|
||||
<i class="icon-grid menu-icon color-primary">
|
||||
{{ ux_icon('fa6-regular:eye', {height: '30px', width: '30px'}) }}
|
||||
</a>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,16 @@
|
|||
{% extends 'base.html.twig' %}
|
||||
|
||||
{% block title %}Ajouter un utilisateur{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
<div class="w-100 h-100 p-5 m-auto">
|
||||
<h1>Ajouter un utilisateur</h1>
|
||||
<form method="post" action="{{ path('user_new') }}" enctype="multipart/form-data">
|
||||
{{ form_start(form) }}
|
||||
{{ form_widget(form) }}
|
||||
<button type="submit" class="btn btn-primary">Enregistrer</button>
|
||||
{{ form_end(form) }}
|
||||
</form>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
Loading…
Reference in New Issue