first commit
This commit is contained in:
0
src/Controller/.gitignore
vendored
Normal file
0
src/Controller/.gitignore
vendored
Normal file
38
src/Controller/HomeController.php
Normal file
38
src/Controller/HomeController.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\User;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
class HomeController extends AbstractController
|
||||
{
|
||||
#[Route('/', name: 'app_home')]
|
||||
public function home(Request $request): Response
|
||||
{
|
||||
$user = $this->getUser();
|
||||
if (!$user instanceof User) {
|
||||
throw new AccessDeniedException('Vous n\'avez pas accès à cette ressource.');
|
||||
}
|
||||
$projects = $user->getProjects();
|
||||
|
||||
return $this->render('home/home.html.twig', [
|
||||
'usemenu' => true,
|
||||
'usesidebar' => false,
|
||||
'projects' => $projects,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/admin', name: 'app_admin')]
|
||||
public function admin(): Response
|
||||
{
|
||||
return $this->render('home/blank.html.twig', [
|
||||
'usemenu' => true,
|
||||
'usesidebar' => true,
|
||||
]);
|
||||
}
|
||||
}
|
113
src/Controller/ProjectController.php
Normal file
113
src/Controller/ProjectController.php
Normal file
@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Project;
|
||||
use App\Entity\User;
|
||||
use App\Form\ProjectType;
|
||||
use App\Repository\ProjectRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
class ProjectController extends AbstractController
|
||||
{
|
||||
#[Route('/admin/project', name: 'app_admin_project')]
|
||||
public function list(ProjectRepository $projectRepository): Response
|
||||
{
|
||||
$projects = $projectRepository->findAll();
|
||||
|
||||
return $this->render('project/list.html.twig', [
|
||||
'usemenu' => true,
|
||||
'usesidebar' => true,
|
||||
'title' => 'Liste des Projets',
|
||||
'routesubmit' => 'app_admin_project_submit',
|
||||
'routeupdate' => 'app_admin_project_update',
|
||||
'projects' => $projects,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/admin/project/submit', name: 'app_admin_project_submit')]
|
||||
public function submit(Request $request, EntityManagerInterface $em): Response
|
||||
{
|
||||
$project = new Project();
|
||||
$project->addUser($this->getUser());
|
||||
|
||||
$form = $this->createForm(ProjectType::class, $project, ['mode' => 'submit']);
|
||||
$form->handleRequest($request);
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$em->persist($project);
|
||||
$em->flush();
|
||||
|
||||
return $this->redirectToRoute('app_admin_project');
|
||||
}
|
||||
|
||||
return $this->render('project/edit.html.twig', [
|
||||
'usemenu' => true,
|
||||
'usesidebar' => true,
|
||||
'title' => 'Création Projet',
|
||||
'routecancel' => 'app_admin_project',
|
||||
'routedelete' => 'app_admin_project_delete',
|
||||
'mode' => 'submit',
|
||||
'form' => $form,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/admin/project/update/{id}', name: 'app_admin_project_update')]
|
||||
public function update(int $id, Request $request, ProjectRepository $projectRepository, EntityManagerInterface $em): Response
|
||||
{
|
||||
$project = $projectRepository->find($id);
|
||||
if (!$project) {
|
||||
throw new NotFoundHttpException('La ressource demandée est introuvable.');
|
||||
}
|
||||
|
||||
$form = $this->createForm(ProjectType::class, $project, ['mode' => 'update']);
|
||||
$form->handleRequest($request);
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$em->flush();
|
||||
|
||||
return $this->redirectToRoute('app_admin_project');
|
||||
}
|
||||
|
||||
return $this->render('project/edit.html.twig', [
|
||||
'usemenu' => true,
|
||||
'usesidebar' => true,
|
||||
'title' => 'Modification Projet = '.$project->getTitle(),
|
||||
'routecancel' => 'app_admin_project',
|
||||
'routedelete' => 'app_admin_project_delete',
|
||||
'mode' => 'update',
|
||||
'form' => $form,
|
||||
'project' => $project,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/admin/project/delete/{id}', name: 'app_admin_project_delete')]
|
||||
public function delete(int $id, ProjectRepository $projectRepository, EntityManagerInterface $em): Response
|
||||
{
|
||||
$project = $projectRepository->find($id);
|
||||
if (!$project) {
|
||||
throw new NotFoundHttpException('La ressource demandée est introuvable.');
|
||||
}
|
||||
|
||||
$users = $em->getRepository(User::class)->findBy(['project' => $project]);
|
||||
foreach ($users as $user) {
|
||||
$user->setProject(null);
|
||||
$em->flush();
|
||||
}
|
||||
|
||||
// Tentative de suppression
|
||||
try {
|
||||
$em->remove($project);
|
||||
$em->flush();
|
||||
} catch (\Exception $e) {
|
||||
$this->addflash('error', $e->getMessage());
|
||||
|
||||
return $this->redirectToRoute('app_admin_project_update', ['id' => $id]);
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('app_admin_project');
|
||||
}
|
||||
}
|
32
src/Controller/SecurityController.php
Normal file
32
src/Controller/SecurityController.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
|
||||
|
||||
class SecurityController extends AbstractController
|
||||
{
|
||||
#[Route(path: '/login', name: 'app_login')]
|
||||
public function login(AuthenticationUtils $authenticationUtils): Response
|
||||
{
|
||||
// get the login error if there is one
|
||||
$error = $authenticationUtils->getLastAuthenticationError();
|
||||
|
||||
// last username entered by the user
|
||||
$lastUsername = $authenticationUtils->getLastUsername();
|
||||
|
||||
return $this->render('security/login.html.twig', [
|
||||
'last_username' => $lastUsername,
|
||||
'error' => $error,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route(path: '/logout', name: 'app_logout')]
|
||||
public function logout(): void
|
||||
{
|
||||
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
|
||||
}
|
||||
}
|
80
src/Controller/UploadController.php
Normal file
80
src/Controller/UploadController.php
Normal file
@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Service\ImageService;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
class UploadController extends AbstractController
|
||||
{
|
||||
private ImageService $imageService;
|
||||
|
||||
public function __construct(ImageService $imageService)
|
||||
{
|
||||
$this->imageService = $imageService;
|
||||
}
|
||||
|
||||
#[Route('/user/upload/crop01/{endpoint}', name: 'app_user_upload_crop01')]
|
||||
public function crop01(string $endpoint, Request $request): Response
|
||||
{
|
||||
$reportThumb = $request->get('reportThumb');
|
||||
|
||||
return $this->render('upload\crop01.html.twig', [
|
||||
'useheader' => false,
|
||||
'usemenu' => false,
|
||||
'usesidebar' => false,
|
||||
'endpoint' => $endpoint,
|
||||
'reportThumb' => $reportThumb,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/user/upload/crop02', name: 'app_user_upload_crop02')]
|
||||
public function crop02(Request $request): Response
|
||||
{
|
||||
$reportThumb = $request->get('reportThumb');
|
||||
$path = $request->get('path');
|
||||
$file = $request->get('file');
|
||||
$image = $this->getParameter('kernel.project_dir').'/public/'.$path.'/'.$file;
|
||||
$thumb = $this->getParameter('kernel.project_dir').'/public/'.$path.'/thumb_'.$file;
|
||||
|
||||
// Redimentionner
|
||||
$this->imageService->resizeImage($image, 700, 700);
|
||||
|
||||
// Construction du formulaire
|
||||
$form = $this->createFormBuilder()
|
||||
->add('submit', SubmitType::class, ['label' => 'Valider', 'attr' => ['class' => 'btn btn-success']])
|
||||
->add('x1', HiddenType::class)
|
||||
->add('y1', HiddenType::class)
|
||||
->add('x2', HiddenType::class)
|
||||
->add('y2', HiddenType::class)
|
||||
->add('w', HiddenType::class)
|
||||
->add('h', HiddenType::class)
|
||||
->getForm();
|
||||
|
||||
// Récupération des data du formulaire
|
||||
$form->handleRequest($request);
|
||||
$toReport = false;
|
||||
// Sur validation on généère la miniature croppée
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$data = $form->getData();
|
||||
$toReport = true;
|
||||
$this->imageService->cropImage($image, $thumb, $data['x1'], $data['y1'], $data['w'], $data['h'], 150, 150);
|
||||
}
|
||||
|
||||
return $this->render('upload\crop02.html.twig', [
|
||||
'useheader' => false,
|
||||
'usemenu' => false,
|
||||
'usesidebar' => false,
|
||||
'reportThumb' => $reportThumb,
|
||||
'image' => $path.'/'.$file,
|
||||
'thumb' => $path.'/thumb_'.$file,
|
||||
'form' => $form,
|
||||
'toReport' => $toReport,
|
||||
]);
|
||||
}
|
||||
}
|
191
src/Controller/UserController.php
Normal file
191
src/Controller/UserController.php
Normal file
@ -0,0 +1,191 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Project;
|
||||
use App\Entity\User;
|
||||
use App\Form\UserType;
|
||||
use App\Repository\UserRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
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', 'modeAuth' => $this->getParameter('modeAuth')]);
|
||||
$form->handleRequest($request);
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$user = $form->getData();
|
||||
$password = $user->getPassword();
|
||||
if ('CAS' === $this->getParameter('modeAuth')) {
|
||||
$password = Uuid::uuid4();
|
||||
}
|
||||
|
||||
$hashedPassword = $passwordHasher->hashPassword(
|
||||
$user,
|
||||
$password
|
||||
);
|
||||
$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', 'modeAuth' => $this->getParameter('modeAuth')]);
|
||||
$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', 'modeAuth' => $this->getParameter('modeAuth')]);
|
||||
$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_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('/user/selectproject', name: 'app_user_selectproject')]
|
||||
public function selectproject(Request $request, EntityManagerInterface $em): JsonResponse
|
||||
{
|
||||
$id = $request->get('id');
|
||||
|
||||
$project = $em->getRepository(Project::class)->find($id);
|
||||
if (!$project) {
|
||||
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.');
|
||||
}
|
||||
|
||||
$projects = $user->getProjects();
|
||||
if (!$projects->contains($project)) {
|
||||
return new JsonResponse(['status' => 'KO', 'message' => 'Projet non autorisée'], Response::HTTP_FORBIDDEN);
|
||||
}
|
||||
|
||||
$user->setProject($project);
|
||||
$em->flush();
|
||||
|
||||
return new JsonResponse(['status' => 'OK', 'message' => 'Projet selectionnée'], Response::HTTP_OK);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user