205 lines
6.8 KiB
PHP
205 lines
6.8 KiB
PHP
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Entity\User;
|
|
use App\Form\UserForm;
|
|
use App\Service\UserOrganizationService;
|
|
use App\Service\UserService;
|
|
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;
|
|
|
|
#[Route(path: '/user', name: 'user_')]
|
|
class UserController extends AbstractController
|
|
{
|
|
private const NOT_FOUND = 'User not found';
|
|
public function __construct(
|
|
private readonly UserOrganizationService $userOrganizationService,
|
|
private readonly EntityManagerInterface $entityManager,
|
|
private readonly UserService $userService)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* GET /user - List all users (index/collection)
|
|
*/
|
|
#[Route('/', name: 'index', methods: ['GET'])]
|
|
public function index(EntityManagerInterface $entityManager): Response
|
|
{
|
|
if ($this->isGranted('ROLE_SUDALYS_ADMIN')) {
|
|
$users = $entityManager->getRepository(User::class)->getAllActiveUsers();
|
|
} else {
|
|
$users = 'Not Super Admin';
|
|
}
|
|
return $this->render('user/index.html.twig', [
|
|
'users' => $users,
|
|
'controller_name' => 'IndexController',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* GET /user/{id} - Show specific user (show/member)
|
|
*/
|
|
#[Route('/{id}', name: 'show', requirements: ['id' => '\d+'], methods: ['GET'])]
|
|
public function show(int $id, EntityManagerInterface $entityManager): Response
|
|
{
|
|
if (!$this->isGranted('ROLE_SUDALYS_ADMIN')) {
|
|
throw $this->createAccessDeniedException('Access denied');
|
|
}
|
|
|
|
$user = $entityManager->getRepository(User::class)->find($id);
|
|
if (!$user) {
|
|
throw $this->createNotFoundException(self::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(UserForm::class);
|
|
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
//Data is a User object. App\Form\NewUserForm is a form type that maps to User entity
|
|
$data = $form->getData();
|
|
// Handle user creation logic here
|
|
|
|
//FOR DEV PURPOSES ONLY
|
|
$data->setPictureUrl("");
|
|
$data->setPassword($this->userService->generateRandomPassword());
|
|
//FOR DEV PURPOSES ONLY
|
|
|
|
$this->entityManager->persist($data);
|
|
$this->entityManager->flush();
|
|
|
|
// Redirect to user index
|
|
return $this->redirectToRoute('user_index');
|
|
}
|
|
|
|
return $this->render('user/new.html.twig', [
|
|
'form' => $form->createView(),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* GET /user/{id}/edit - Show form to edit user
|
|
*/
|
|
#[Route('/{id}/edit', name: 'edit', requirements: ['id' => '\d+'], methods: ['GET', 'PUT', 'POST'])]
|
|
public function edit(int $id, EntityManagerInterface $entityManager, Request $request): Response
|
|
{
|
|
//Handle access control
|
|
if (!$this->isGranted('ROLE_SUDALYS_ADMIN')) {
|
|
throw $this->createAccessDeniedException('Access denied');
|
|
}
|
|
|
|
//Fetch user by ID and handle not found case
|
|
$user = $entityManager->getRepository(User::class)->find($id);
|
|
if (!$user) {
|
|
throw $this->createNotFoundException(self::NOT_FOUND);
|
|
}
|
|
|
|
//Create form for editing user
|
|
$form = $this->createForm(UserForm::class, $user);
|
|
|
|
//Handle form submission
|
|
$form->handleRequest($request);
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
//Persist changes to the user entity
|
|
$entityManager->persist($user);
|
|
$entityManager->flush();
|
|
|
|
//Redirect to user profile after successful edit
|
|
return $this->redirectToRoute('user_show', ['id' => $user->getId()]);
|
|
}
|
|
|
|
return $this->render('user/edit.html.twig', [
|
|
'form' => $form->createView(),
|
|
'user' => $user,
|
|
]);
|
|
}
|
|
|
|
|
|
/**
|
|
* DELETE /user/{id} - Delete user
|
|
*/
|
|
#[Route('/{id}', name: 'setDelete', requirements: ['id' => '\d+'], methods: ['POST'])]
|
|
public function setDelete(int $id, EntityManagerInterface $entityManager): Response
|
|
{
|
|
//This method is used to set a user as deleted without actually removing them from the database.
|
|
|
|
//Handle access control
|
|
if (!$this->isGranted('ROLE_SUDALYS_ADMIN')) {
|
|
throw $this->createAccessDeniedException('Access denied');
|
|
}
|
|
//Fetch user by ID and handle not found case
|
|
$user = $entityManager->getRepository(User::class)->find($id);
|
|
if (!$user) {
|
|
throw $this->createNotFoundException(self::NOT_FOUND);
|
|
}
|
|
|
|
// Handle user deletion logic
|
|
$user->setIsDeleted(true);
|
|
$entityManager->persist($user);
|
|
$entityManager->flush();
|
|
|
|
return $this->redirectToRoute('user_index');
|
|
}
|
|
|
|
/**
|
|
* DELETE /user/{id} - Delete user
|
|
*/
|
|
#[Route('/{id}', name: 'delete', requirements: ['id' => '\d+'], methods: ['DELETE'])]
|
|
public function delete(int $id, EntityManagerInterface $entityManager): Response
|
|
{
|
|
if (!$this->isGranted('ROLE_SUDALYS_ADMIN')) {
|
|
throw $this->createAccessDeniedException('Access denied');
|
|
}
|
|
|
|
$user = $entityManager->getRepository(User::class)->find($id);
|
|
if (!$user) {
|
|
throw $this->createNotFoundException(self::NOT_FOUND);
|
|
}
|
|
|
|
// Handle user deletion logic
|
|
$entityManager->remove($user);
|
|
$entityManager->flush();
|
|
|
|
return $this->redirectToRoute('user_index');
|
|
}
|
|
|
|
|
|
#[Route('/deactivate/{id}', name: 'deactivate', methods: ['GET'])]
|
|
public function userDeactivate(Request $request, 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(self::NOT_FOUND);
|
|
}
|
|
$user->setIsActive(false);
|
|
$entityManager->persist($user);
|
|
$entityManager->flush();
|
|
return $this->redirectToRoute('user_index');
|
|
}
|
|
return new Response('Unauthorized', Response::HTTP_UNAUTHORIZED);
|
|
}
|
|
|
|
|
|
|
|
}
|