187 lines
6.4 KiB
PHP
187 lines
6.4 KiB
PHP
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Entity\Company;
|
|
use App\Entity\User;
|
|
use App\Form\UserType;
|
|
use App\Repository\UserRepository;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
|
use Symfony\Component\Routing\Attribute\Route;
|
|
|
|
class UserController extends AbstractController
|
|
{
|
|
#[Route('/admin/user', name: 'app_admin_user')]
|
|
public function list(UserRepository $userRepository): Response
|
|
{
|
|
$users = $userRepository->findAll();
|
|
|
|
return $this->render('user/list.html.twig', [
|
|
'usemenu' => true,
|
|
'usesidebar' => true,
|
|
'title' => 'Liste des Utilisateurs',
|
|
'routesubmit' => 'app_admin_user_submit',
|
|
'routeupdate' => 'app_admin_user_update',
|
|
'users' => $users,
|
|
]);
|
|
}
|
|
|
|
#[Route('/admin/user/submit', name: 'app_admin_user_submit')]
|
|
public function submit(Request $request, UserPasswordHasherInterface $passwordHasher, EntityManagerInterface $em): Response
|
|
{
|
|
$user = new User();
|
|
|
|
$form = $this->createForm(UserType::class, $user, ['mode' => 'submit']);
|
|
$form->handleRequest($request);
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
$user = $form->getData();
|
|
$hashedPassword = $passwordHasher->hashPassword(
|
|
$user,
|
|
$user->getPassword()
|
|
);
|
|
$user->setPassword($hashedPassword);
|
|
|
|
$em->persist($user);
|
|
$em->flush();
|
|
|
|
return $this->redirectToRoute('app_admin_user');
|
|
}
|
|
|
|
return $this->render('user/edit.html.twig', [
|
|
'usemenu' => true,
|
|
'usesidebar' => true,
|
|
'title' => 'Création Utilisateur',
|
|
'routecancel' => 'app_admin_user',
|
|
'routedelete' => 'app_admin_user_delete',
|
|
'mode' => 'submit',
|
|
'form' => $form,
|
|
]);
|
|
}
|
|
|
|
#[Route('/admin/user/update/{id}', name: 'app_admin_user_update')]
|
|
public function update(int $id, Request $request, UserPasswordHasherInterface $passwordHasher, EntityManagerInterface $em): Response
|
|
{
|
|
$user = $em->getRepository(User::class)->find($id);
|
|
if (!$user) {
|
|
return $this->redirectToRoute('app_admin_user');
|
|
}
|
|
$hashedPassword = $user->getPassword();
|
|
|
|
$form = $this->createForm(UserType::class, $user, ['mode' => 'update']);
|
|
$form->handleRequest($request);
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
$user = $form->getData();
|
|
if ($user->getPassword()) {
|
|
$hashedPassword = $passwordHasher->hashPassword(
|
|
$user,
|
|
$user->getPassword()
|
|
);
|
|
}
|
|
$user->setPassword($hashedPassword);
|
|
$em->flush();
|
|
|
|
return $this->redirectToRoute('app_admin_user');
|
|
}
|
|
|
|
return $this->render('user/edit.html.twig', [
|
|
'usemenu' => true,
|
|
'usesidebar' => true,
|
|
'title' => 'Modification Utilisateur = '.$user->getUsername(),
|
|
'routecancel' => 'app_admin_user',
|
|
'routedelete' => 'app_admin_user_delete',
|
|
'mode' => 'update',
|
|
'form' => $form,
|
|
]);
|
|
}
|
|
|
|
#[Route('/admin/user/delete/{id}', name: 'app_admin_user_delete')]
|
|
public function delete(int $id, EntityManagerInterface $em): Response
|
|
{
|
|
$user = $em->getRepository(User::class)->find($id);
|
|
if (!$user) {
|
|
return $this->redirectToRoute('app_admin_user');
|
|
}
|
|
|
|
// Tentative de suppression
|
|
try {
|
|
$em->remove($user);
|
|
$em->flush();
|
|
} catch (\Exception $e) {
|
|
$this->addflash('error', $e->getMessage());
|
|
|
|
return $this->redirectToRoute('app_admin_user_update', ['id' => $id]);
|
|
}
|
|
|
|
return $this->redirectToRoute('app_admin_user');
|
|
}
|
|
|
|
#[Route('/user', name: 'app_user_profil')]
|
|
public function profil(Request $request, UserPasswordHasherInterface $passwordHasher, EntityManagerInterface $em): Response
|
|
{
|
|
$user = $em->getRepository(User::class)->find($this->getUser());
|
|
if (!$user) {
|
|
return $this->redirectToRoute('app_home');
|
|
}
|
|
$hashedPassword = $user->getPassword();
|
|
|
|
$form = $this->createForm(UserType::class, $user, ['mode' => 'profil']);
|
|
$form->handleRequest($request);
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
$user = $form->getData();
|
|
if ($user->getPassword()) {
|
|
$hashedPassword = $passwordHasher->hashPassword(
|
|
$user,
|
|
$user->getPassword()
|
|
);
|
|
}
|
|
$user->setPassword($hashedPassword);
|
|
|
|
$em->persist($user);
|
|
$em->flush();
|
|
|
|
return $this->redirectToRoute('app_home');
|
|
}
|
|
|
|
return $this->render('user/edit.html.twig', [
|
|
'usemenu' => true,
|
|
'usesidebar' => false,
|
|
'title' => 'Profil = '.$user->getUsername(),
|
|
'routecancel' => 'app_home',
|
|
'routedelete' => '',
|
|
'mode' => 'profil',
|
|
'form' => $form,
|
|
]);
|
|
}
|
|
|
|
#[Route('/admin/user/selectcompany', name: 'app_user_selectcompany')]
|
|
public function selectcompany(Request $request, EntityManagerInterface $em): JsonResponse
|
|
{
|
|
$id = $request->get('id');
|
|
|
|
$company = $em->getRepository(Company::class)->find($id);
|
|
if (!$company) {
|
|
return new JsonResponse(['status' => 'KO', 'message' => 'ID non fourni'], Response::HTTP_NOT_FOUND);
|
|
}
|
|
|
|
$user = $this->getUser();
|
|
if (!$user instanceof User) {
|
|
throw new \LogicException('L\'utilisateur actuel n\'est pas une instance de App\Entity\User.');
|
|
}
|
|
|
|
$companys = $user->getCompanys();
|
|
if (!$companys->contains($company)) {
|
|
return new JsonResponse(['status' => 'KO', 'message' => 'Compangnie non autorisée'], Response::HTTP_FORBIDDEN);
|
|
}
|
|
|
|
$user->setCompany($company);
|
|
$em->flush();
|
|
|
|
return new JsonResponse(['status' => 'OK', 'message' => 'Compangnie selectionnée'], Response::HTTP_OK);
|
|
}
|
|
}
|