V-Beta-1.0.0

Vision is out of alpha !
This commit is contained in:
Xbird
2022-02-02 17:46:29 +01:00
parent 797bf35b47
commit 9f22f5b1ee
2297 changed files with 278438 additions and 76 deletions

0
src/Controller/.gitignore vendored Normal file
View File

View File

@@ -0,0 +1,696 @@
<?php
namespace App\Controller;
use App\Entity\Rank;
use App\Entity\User;
use App\Entity\Group;
use App\Entity\SubGroup;
use App\Form\AdminRankType;
use App\Form\AdminUserType;
use App\Form\SearchBarType;
use App\Form\AdminGroupType;
use App\Form\AdminSubGroupType;
use App\Repository\UserRepository;
use App\Repository\GroupRepository;
use Symfony\Component\Finder\Finder;
use App\Repository\DocumentRepository;
use App\Repository\DirectoryRepository;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Mime\FileinfoMimeTypeGuesser;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
#[Route('/admin', name: 'admin_')]
class AdminController extends AbstractController
{
private Security $security;
public function __construct(Security $security)
{
$this->security = $security;
}
#[Route('/', name: 'index')]
public function index(
DocumentRepository $DocumentRepository,
UserRepository $UserRepository,
DirectoryRepository $DirectoryRepository,
GroupRepository $GroupRepository
): Response {
return $this->render('admin/index.html.twig', [
'controller_name' => 'AdminController',
'documents' => $DocumentRepository->findAll(),
'users' => $UserRepository->findAll(),
'directories' => $DirectoryRepository->findAll(),
'folders' => $DocumentRepository->list()->limitType("Folder")->getResult(),
'gangs' => $DocumentRepository->list()->limitType("Gang")->getResult(),
'groups' => $GroupRepository->findAll(),
]);
}
//-- users
#[Route('/user', name: 'user_list')]
public function user(PaginatorInterface $paginator, Request $request, UserRepository $UserRepository): Response
{
$searchForm = $this->createForm(SearchBarType::class);
$searchForm->handleRequest($request);
$pagination = $paginator->paginate(
$UserRepository->getAll()
->search(
(
$searchForm->isSubmitted()
&& $searchForm->isValid()
&& $searchForm->getData()['subject'] !== null
) ? $searchForm->getData()['subject'] : null,
true
)
->getResult(),
$request->query->getInt('page', 1)
);
return $this->render('admin/user.html.twig', [
'controller_name' => 'AdminController',
'searchForm' => $searchForm->createView(),
'pagination' => $pagination,
]);
}
#[Route('/user/view/{id}', name: 'user_view')]
public function userView(PaginatorInterface $paginator, User $User): Response
{
$pagination = $paginator->paginate($User->getDocuments());
return $this->render('admin/user_view.html.twig', [
'controller_name' => 'AdminController',
'user' => $User,
'pagination' => $pagination
]);
}
#[Route('/user/disable/{id}', name: 'user_disable')]
public function userDisable(Request $Request, User $User): Response
{
$User->setIsDesactivated(($User->getIsDesactivated() ? false : true));
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($User);
try {
$entityManager->flush();
if ($User->getIsDesactivated()) {
$this->addFlash('success', 'alert_success_desactivating_user');
} else {
$this->addFlash('success', 'alert_success_activating_user');
}
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('danger', 'alert_error_saving_user');
}
if (null != $Request->headers->get('referer')) {
return $this->redirect($Request->headers->get('referer'));
}
return $this->redirectToRoute('admin_user_view', ['id' => $User->getId()]);
}
#[Route('/user/edit/{id}', name: 'user_edit')]
public function userEdit(Request $request, User $User): Response
{
$currentUserGroup = $User->getMainGroup();
$form = $this->createForm(AdminUserType::class, $User);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if ($currentUserGroup != $User->getMainGroup()) {
//if a user change group, he lost his SubGroups
$User->getSubGroups()->clear();
}
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($User);
try {
$entityManager->flush();
$this->addFlash('success', 'alert_success_saving_user');
return $this->redirectToRoute('admin_user_edit', ['id' => $User->getId()]);
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('danger', 'alert_error_saving_user');
return $this->redirectToRoute('admin_user_edit', ['id' => $User->getId()]);
}
}
return $this->render('admin/user_edit.html.twig', [
'controller_name' => 'AdminController',
'user' => $User,
'form' => $form->createView()
]);
}
//-- groups
#[Route('/group', name: 'group_list')]
public function group(PaginatorInterface $paginator, Request $request, GroupRepository $GroupRepository): Response
{
$searchForm = $this->createForm(SearchBarType::class);
$searchForm->handleRequest($request);
$pagination = $paginator->paginate(
$GroupRepository->getAll()
->search(
(
$searchForm->isSubmitted()
&& $searchForm->isValid()
&& $searchForm->getData()['subject'] !== null
) ? $searchForm->getData()['subject'] : null
)->getResult(),
$request->query->getInt('page', 1)
);
return $this->render('admin/group.html.twig', [
'controller_name' => 'AdminController',
'searchForm' => $searchForm->createView(),
'pagination' => $pagination,
]);
}
#[Route('/group/view/{id}', name: 'group_view')]
public function groupView(Group $Group): Response
{
return $this->render('admin/group_view.html.twig', [
'controller_name' => 'AdminController',
'group' => $Group,
]);
}
#[Route('/group/documents/{id}', name: 'group_documents')]
public function groupDocuments(
PaginatorInterface $paginator,
Request $request,
DocumentRepository $DocumentRepository,
Group $Group
): Response {
$searchForm = $this->createForm(SearchBarType::class);
$searchForm->handleRequest($request);
$pagination = $paginator->paginate(
$DocumentRepository ->list()
->limitGroup($Group)
->search(
(
$searchForm->isSubmitted()
&& $searchForm->isValid()
&& $searchForm->getData()['subject'] !== null
) ? $searchForm->getData()['subject'] : null
)
->order(['createdAt' => 'DESC'])
->getResult(),
$request->query->getInt('page', 1)
);
return $this->render('admin/group_documents.html.twig', [
'controller_name' => 'AdminController',
'pagination' => $pagination,
'group' => $Group,
'searchForm' => $searchForm->createView(),
]);
}
#[Route('/group/add', name: 'group_add')]
public function groupAdd(Request $request): Response
{
$Group = new Group();
$form = $this->createForm(AdminGroupType::class, $Group);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($Group);
try {
$entityManager->flush();
$this->addFlash('success', 'alert_success_creating_group');
return $this->redirectToRoute('admin_group_view', ['id' => $Group->getId()]);
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('danger', 'alert_error_creating_group');
}
}
return $this->render('admin/group_add.html.twig', [
'controller_name' => 'AdminController',
'group' => $Group,
'form' => $form->createView()
]);
}
#[Route('/group/edit/{id}', name: 'group_edit')]
public function groupEdit(Request $request, Group $Group): Response
{
$form = $this->createForm(AdminGroupType::class, $Group);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($Group);
try {
$entityManager->flush();
$this->addFlash('success', 'alert_success_editing_group');
return $this->redirectToRoute('admin_group_view', ['id' => $Group->getId()]);
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('danger', 'alert_error_editing_group');
}
}
return $this->render('admin/group_edit.html.twig', [
'controller_name' => 'AdminController',
'group' => $Group,
'form' => $form->createView()
]);
}
#[Route('/group/delete/{id}', name: 'group_delete')]
public function groupDelete(Group $Group): Response
{
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($Group);
try {
$entityManager->flush();
$this->addFlash('success', 'alert_success_deleting_group');
return $this->redirectToRoute('admin_group_list');
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('danger', 'alert_error_deleting_group');
return $this->redirectToRoute('admin_group_view', ['id' => $Group->getId()]);
}
}
//-- Groups Ranks
#[Route('/group/rank/view/{id}', name: 'group_rank_view')]
public function groupRankView(Rank $Rank): Response
{
return $this->render('admin/group_rank_view.html.twig', [
'controller_name' => 'AdminController',
'rank' => $Rank,
'group' => $Rank->getMainGroup(),
]);
}
#[Route('/group/rank/edit/{id}', name: 'group_rank_edit')]
public function groupRankEdit(Request $request, Rank $Rank): Response
{
$form = $this->createForm(AdminRankType::class, $Rank);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($Rank);
try {
$entityManager->flush();
$this->addFlash('success', 'alert_success_editing_rank');
return $this->redirectToRoute('admin_group_rank_view', ['id' => $Rank->getId()]);
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('danger', 'alert_error_editing_rank');
}
}
return $this->render('admin/group_rank_edit.html.twig', [
'controller_name' => 'AdminController',
'rank' => $Rank,
'group' => $Rank->getMainGroup(),
'form' => $form->createView()
]);
}
#[Route('/group/rank/add/{id}', name: 'group_rank_add')]
public function groupRankAdd(Request $request, Group $Group): Response
{
$Rank = new Rank();
$Rank->setMainGroup($Group);
$form = $this->createForm(AdminRankType::class, $Rank);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($Rank);
try {
$entityManager->flush();
$this->addFlash('success', 'alert_success_creating_rank');
return $this->redirectToRoute('admin_group_rank_view', ['id' => $Rank->getId()]);
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('danger', 'alert_error_creating_rank');
}
}
return $this->render('admin/group_rank_add.html.twig', [
'controller_name' => 'AdminController',
'rank' => $Rank,
'group' => $Rank->getMainGroup(),
'form' => $form->createView()
]);
}
#[Route('/rank/copy/{id}', name: 'group_rank_copy')]
public function rankCopy(Rank $Rank): Response
{
$entityManager = $this->getDoctrine()->getManager();
$newrank = clone $Rank;
$newrank->setName($newrank->getName() . ' - copy');
$entityManager->persist($newrank);
try {
$entityManager->flush();
$this->addFlash('success', 'alert_success_copying_rank');
return $this->redirectToRoute('admin_group_rank_view', ['id' => $newrank->getId()]);
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('danger', 'alert_error_copying_rank');
return $this->redirectToRoute('admin_group_rank_view', ['id' => $Rank->getId()]);
}
}
#[Route('/rank/delete/{id}', name: 'group_rank_delete')]
public function rankDelete(Rank $Rank): Response
{
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($Rank);
try {
$entityManager->flush();
$this->addFlash('success', 'alert_success_deleting_rank');
return $this->redirectToRoute('admin_group_view', ['id' => $Rank->getMainGroup()->getId()]);
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('danger', 'alert_error_deleting_rank');
return $this->redirectToRoute('admin_group_rank_view', ['id' => $Rank->getId()]);
}
}
//-- Groups SubGroups
#[Route('/group/subgroup/view/{id}', name: 'group_subgroup_view')]
public function groupSubGroupView(SubGroup $SubGroup): Response
{
return $this->render('admin/group_subgroup_view.html.twig', [
'controller_name' => 'AdminController',
'subgroup' => $SubGroup,
'group' => $SubGroup->getMainGroup(),
]);
}
#[Route('/group/subgroup/edit/{id}', name: 'group_subgroup_edit')]
public function groupSubGroupEdit(Request $request, SubGroup $SubGroup): Response
{
$form = $this->createForm(AdminSubGroupType::class, $SubGroup);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($SubGroup);
try {
$entityManager->flush();
$this->addFlash('success', 'alert_success_editing_subgroup');
return $this->redirectToRoute('admin_group_subgroup_view', ['id' => $SubGroup->getId()]);
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('danger', 'alert_error_editing_subgroup');
}
}
return $this->render('admin/group_subgroup_edit.html.twig', [
'controller_name' => 'AdminController',
'subgroup' => $SubGroup,
'group' => $SubGroup->getMainGroup(),
'form' => $form->createView()
]);
}
#[Route('/group/subgroup/add/{id}', name: 'group_subgroup_add')]
public function groupSubGroupAdd(Request $request, Group $Group): Response
{
$SubGroup = new SubGroup();
$SubGroup->setMainGroup($Group);
$form = $this->createForm(AdminSubGroupType::class, $SubGroup);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($SubGroup);
try {
$entityManager->flush();
$this->addFlash('success', 'alert_success_creating_subgroup');
return $this->redirectToRoute('admin_group_subgroup_view', ['id' => $SubGroup->getId()]);
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('danger', 'alert_error_creating_subgroup');
}
}
return $this->render('admin/group_subgroup_add.html.twig', [
'controller_name' => 'AdminController',
'subgroup' => $SubGroup,
'group' => $SubGroup->getMainGroup(),
'form' => $form->createView()
]);
}
#[Route('/subgroup/delete/{id}', name: 'group_subgroup_delete')]
public function subgroupDelete(SubGroup $SubGroup): Response
{
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($SubGroup);
try {
$entityManager->flush();
$this->addFlash('success', 'alert_success_deleting_subgroup');
return $this->redirectToRoute('admin_group_view', ['id' => $SubGroup->getMainGroup()->getId()]);
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('danger', 'alert_error_deleting_subgroup');
return $this->redirectToRoute('admin_group_subgroup_view', ['id' => $SubGroup->getId()]);
}
}
//-- Logs
#[Route('/logs', name: 'logs')]
public function logs(Request $Request): Response
{
$finder = new Finder();
$finder->files()->in($this->getParameter('kernel.logs_dir'));
return $this->render('admin/logs.html.twig', [
'controller_name' => 'AdminController',
'files' => $finder
]);
}
#[Route('/logs/view/{filename}', name: 'logs_view')]
public function logsView(string $filename, Request $Request): Response
{
$filePath = $this->getParameter('kernel.logs_dir') . '/' . $filename;
$filesystem = new Filesystem();
if (!$filesystem->exists($filePath)) {
throw new NotFoundHttpException('error_log_file_not_found');
}
$content = preg_replace(
[
'/(?<=\[).+?(?=\])/'
],
[
"<span class=\"text-success\">$0</span>"
],
file_get_contents($filePath)
);
$content = str_replace(
['request.CRITICAL'],
["<span class=\"text-danger\">request.CRITICAL</span>"],
$content
);
return $this->render('admin/logs_view.html.twig', [
'controller_name' => 'AdminController',
'name' => $filename,
'path' => $filePath,
'content' => $content
]);
}
#[Route('/logs/delete/{filename}', name: 'logs_delete')]
public function logsDelete(string $filename, Request $Request): Response
{
$filePath = $this->getParameter('kernel.logs_dir') . '/' . $filename;
$filesystem = new Filesystem();
if (!$filesystem->exists($filePath)) {
throw new NotFoundHttpException('error_log_file_not_found');
}
try {
$filesystem->remove($filePath);
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('danger', 'alert_error_deleting_log');
$referer = $Request->headers->get('referer');
return $this->redirect($referer);
}
$this->addFlash('success', 'alert_success_deleting_log');
return $this->redirectToRoute('admin_logs');
}
#[Route('/logs/download/{filename}', name: 'logs_download')]
public function logsDownload(string $filename, Request $Request): Response
{
$filePath = $this->getParameter('kernel.logs_dir') . '/' . $filename;
$filesystem = new Filesystem();
if (!$filesystem->exists($filePath)) {
throw new NotFoundHttpException('error_log_file_not_found');
}
// This should return the file to the browser as response
$response = new BinaryFileResponse(
$filePath
);
// To generate a file download, you need the mimetype of the file
$mimeTypeGuesser = new FileinfoMimeTypeGuesser();
// Set the mimetype with the guesser or manually
if ($mimeTypeGuesser->isGuesserSupported()) {
// Guess the mimetype of the file according to the extension of the file
$response->headers->set(
'Content-Type',
$mimeTypeGuesser->guessMimeType(
$filePath
)
);
} else {
// Set the mimetype of the file manually, in this case for a text file is text/plain
$response->headers->set('Content-Type', 'text/plain');
}
// Set content disposition inline of the file
$response->setContentDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
$filename
);
return $response;
}
#[Route('/watchdog', name: 'watchdog')]
public function watchdog(
PaginatorInterface $paginator,
Request $Request,
EntityManagerInterface $EntityManagerInterface
): Response {
/**
* @var LogEntryRepository $logEntryRepository
*/
$logEntryRepository = $EntityManagerInterface->getRepository('Gedmo\Loggable\Entity\LogEntry');
$qb = $EntityManagerInterface->createQueryBuilder();
$qb->select('l')
->from('Gedmo\Loggable\Entity\LogEntry', 'l')
->orderBy('l.loggedAt', 'DESC');
$pagination = $paginator->paginate(
$qb->getQuery()->getResult(),
$Request->query->getInt('page', 1)
);
return $this->render('admin/watchdog.html.twig', [
'controller_name' => 'AdminController',
'pagination' => $pagination
]);
}
//-- Specials
#[Route('/adminmode', name: 'mode')]
public function adminMode(Request $Request): Response
{
/**
* @var User $user
* */
$user = $this->security->getUser();
$currentAdminMode = $user->getAdminmode();
$newAdminMode = !(is_null($currentAdminMode) || $currentAdminMode);
$user->setAdminmode($newAdminMode);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($user);
try {
$entityManager->flush();
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('danger', 'alert_error_switching_adminmode');
}
$referer = $Request->headers->get('referer');
return $this->redirect($referer);
}
}

View File

@@ -0,0 +1,108 @@
<?php
namespace App\Controller;
use App\Entity\Comment;
use App\Entity\Document;
use App\Form\CommentType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
#[Route('/comment', name: 'comment_')]
class CommentController extends AbstractController
{
#[Route('/create/{Document}', name: 'create')]
#[IsGranted('view', subject: 'Document', message: 'granted_not_allowed_comment')]
public function create(Document $Document, Request $request): Response
{
$Comment = new Comment($this->getUser());
$form = $this->createForm(CommentType::class, $Comment);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$Comment->setDocument($Document);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($Comment);
try {
$entityManager->flush();
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('danger', 'alert_error_creating_comment');
return $this->redirectToRoute('document_view', ['id' => $Document->getId()]);
}
$this->addFlash('success', 'alert_success_creating_comment');
return $this->redirectToRoute('document_view', ['id' => $Document->getId()]);
}
return $this->redirectToRoute('document_view', ['id' => $Document->getId()]);
}
#[Route('/delete/{id}', name: 'delete')]
#[IsGranted('delete', subject: 'Comment', message: 'granted_not_allowed_deleting_comment')]
public function delete(Comment $Comment): Response
{
if (!$this->IsGranted('view', $Comment->getDocument())) {
throw new AccessDeniedHttpException('granted_not_allowed_delete_comment_on_unallowed_document');
}
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($Comment);
try {
$entityManager->flush();
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('warning', 'alert_error_deleting_comment');
return $this->redirectToRoute('document_view', ['id' => $Comment->getDocument()->getId()]);
}
$this->addFlash('success', 'alert_success_deleting_comment');
return $this->redirectToRoute('document_view', ['id' => $Comment->getDocument()->getId()]);
}
#[Route('/edit/{id}', name: 'edit')]
#[IsGranted('edit', subject: 'Comment', message: 'granted_not_allowed_editing_comment')]
public function edit(Comment $Comment, Request $request): Response
{
if (!$this->IsGranted('view', $Comment->getDocument())) {
throw new AccessDeniedHttpException('granted_not_allowed_editing_comment_on_unallowed_document');
}
$form = $this->createForm(CommentType::class, $Comment);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($Comment);
try {
$entityManager->flush();
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('danger', 'alert_error_editing_comment');
return $this->redirectToRoute('document_view', ['id' => $Comment->getDocument()->getId()]);
}
$this->addFlash('success', 'alert_success_editing_comment');
return $this->redirectToRoute('document_view', ['id' => $Comment->getDocument()->getId()]);
}
return $this->render('comment/edit.html.twig', [
'controller_name' => 'CommentController',
'comment' => $Comment,
'form' => $form->createView()
]);
}
}

View File

@@ -0,0 +1,293 @@
<?php
namespace App\Controller;
use App\Entity\Directory;
use App\Form\DirectoryType;
use App\Form\SearchBarType;
use App\Repository\DocumentRepository;
use App\Repository\DirectoryRepository;
use Doctrine\ORM\EntityManagerInterface;
use App\Repository\CertificateRepository;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
#[Route('/directory', name: 'directory_')]
class DirectoryController extends AbstractController
{
#[Route('/', name: 'list')]
#[Route('/wanted', name: 'list_wanted')]
#[Route('/dead', name: 'list_dead')]
public function list(
PaginatorInterface $paginator,
Request $request,
DirectoryRepository $DirectoryRepository
): Response {
$searchForm = $this->createForm(SearchBarType::class);
$searchForm->handleRequest($request);
$req = $DirectoryRepository->list()
->search(
(
$searchForm->isSubmitted()
&& $searchForm->isValid()
&& $searchForm->getData()['subject'] !== null
) ? $searchForm->getData()['subject'] : null
);
if ($request->attributes->get('_route') == 'directory_list_wanted') {
$req->wanted();
}
if ($request->attributes->get('_route') == 'directory_list_dead') {
$req->dead();
} else {
$req->notDead();
}
$pagination = $paginator->paginate(
$req->getResult()
);
return $this->render('directory/list.html.twig', [
'controller_name' => 'DirectoryController',
'pagination' => $pagination,
'searchForm' => $searchForm->createView(),
'wanted' => ($request->attributes->get('_route') == 'directory_list_wanted'),
'dead' => ($request->attributes->get('_route') == 'directory_list_dead')
]);
}
#[Route('/view/{id}', name: 'view')]
public function view(Directory $Directory): Response
{
$entityManager = $this->getDoctrine()->getManager();
$DocumentList = [
'Certificate',
'Complaint',
'LicenceWithdrawal',
'Infringement',
'Jail',
'Bracelet',
'Criminal',
'Stolenvehicle',
'Medical'
];
$RenderArray = [
'controller_name' => 'DirectoryController',
'directory' => $Directory,
];
foreach ($DocumentList as $key => $dType) {
/**
* Use Certificate as example for IDE
* @var CertificateRepository $repo
*/
$repo = $entityManager->getRepository('App\Entity\\' . $dType);
$RenderArray[$dType] = $repo->listForUser($this->getUser())->limitDirectory($Directory)->getResult();
}
return $this->render('directory/view.html.twig', $RenderArray);
}
#[Route('/create', name: 'create')]
public function create(Request $request): Response
{
$Directory = new Directory();
if (!$this->IsGranted('create', $Directory)) {
throw new AccessDeniedHttpException('granted_not_allowed_creating_directory');
}
$form = $this->createForm(DirectoryType::class, $Directory);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($Directory);
try {
$entityManager->flush();
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('danger', 'alert_error_creating_directory');
return $this->redirectToRoute($request->getRequestUri());
}
$this->addFlash('success', 'alert_success_creating_directory');
return $this->redirectToRoute('directory_view', ['id' => $Directory->getId()]);
}
return $this->render('directory/create.html.twig', [
'controller_name' => 'DirectoryController',
'directory' => $Directory,
'form' => $form->createView()
]);
}
#[Route('/edit/{id}', name: 'edit')]
#[IsGranted('edit', subject: 'Directory', message: 'granted_not_allowed_editing_directory')]
public function edit(Request $request, Directory $Directory): Response
{
$form = $this->createForm(DirectoryType::class, $Directory);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($Directory);
try {
$entityManager->flush();
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('danger', 'alert_error_editing_directory');
return $this->redirectToRoute($request->getRequestUri());
}
$this->addFlash('success', 'alert_success_editing_directory');
return $this->redirectToRoute('directory_view', ['id' => $Directory->getId()]);
}
return $this->render('directory/edit.html.twig', [
'controller_name' => 'DirectoryController',
'directory' => $Directory,
'form' => $form->createView()
]);
}
#[Route('/delete/{id}', name: 'delete')]
#[IsGranted('delete', subject: 'Directory', message: 'granted_not_allowed_deleting_directory')]
public function delete(Directory $Directory): Response
{
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($Directory);
try {
$entityManager->flush();
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('warning', 'alert_error_deleting_directory');
return $this->redirectToRoute('directory_view', ['id' => $Directory->getId()]);
}
$this->addFlash('success', 'alert_success_deleting_directory');
return $this->redirectToRoute('directory_list');
}
#[Route('/merge/router', name: 'merge_router')]
public function directoryMergeRouter(Request $Request): Response
{
if (empty($Request->request->get('directory-main')) || is_null($Request->request->get('directory-main'))) {
return $this->redirectToRoute('directory_list'); //id not found = redirect to list
}
if (empty($Request->request->get('directory-child')) || is_null($Request->request->get('directory-child'))) {
return $this->redirectToRoute('directory_list'); //id not found = redirect to list
}
return $this->redirectToRoute(
'directory_merge',
[
'keeped' => $Request->request->get('directory-main'),
'deleted' => $Request->request->get('directory-child')
]
);
}
#[Route('/merge/{deleted}/{keeped}', name: 'merge')]
#[IsGranted('delete', subject: 'keeped', message: 'granted_not_allowed_merging_directory')]
public function merge(
Directory $deleted,
Directory $keeped,
Request $Request,
EntityManagerInterface $EntityManagerInterface
): Response {
$base = clone $keeped;
// 1 : merge info before validation for display
$classMetadata = $EntityManagerInterface->getClassMetadata(Directory::class);
foreach ($classMetadata->getFieldNames() as $key => $f) {
if (
is_null($classMetadata->getFieldValue($keeped, $f))
&& !is_null($classMetadata->getFieldValue($deleted, $f))
) {
$classMetadata->setFieldValue($keeped, $f, $classMetadata->getFieldValue($deleted, $f));
}
}
if (null != ($Request->request->get('validatemerge')) && !empty($Request->request->get('validatemerge'))) {
//after validation
// 2 : check foreing keys
$entityManager = $this->getDoctrine()->getManager();
foreach ($classMetadata->getAssociationMappings() as $key => $associationMapping) {
//get mappings of deleted entity
$mappings = $classMetadata->getFieldValue($deleted, $associationMapping['fieldName']);
if (is_a($mappings, 'Doctrine\ORM\PersistentCollection')) {
//if mapping is a Collection
foreach ($mappings as $mapping) {
if (is_object($mapping) && method_exists($mapping, 'setDirectory')) {
$mapping->setDirectory($keeped);
}
}
} else {
$entityManager->initializeObject($mappings);
if (is_object($mappings) && method_exists($mappings, 'addDirectory')) {
$mappings->addDirectory($keeped);
}
}
}
$entityManager->persist($keeped);
try {
$entityManager->flush();
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('danger', 'alert_error_merging_directory');
return $this->redirectToRoute('directory_view', ['id' => $deleted->getId()]);
}
$entityManager->refresh($deleted); //refresh entity after flushing, to avoid deleting mappings
$deletedId = $deleted->getId();
$entityManager->remove($deleted);
//dd($deleted);
try {
$entityManager->flush();
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('danger', 'alert_error_merging_directory');
return $this->redirectToRoute('directory_view', ['id' => $deletedId]);
}
$this->addFlash('success', 'alert_success_merging_directory');
return $this->redirectToRoute('directory_view', ['id' => $keeped->getId()]);
}
return $this->render('directory/merge.html.twig', [
'controller_name' => 'DirectoryController',
'baseDirectory' => $base,
'keepedDirectory' => $keeped,
'deletedDirectory' => $deleted
]);
}
}

View File

@@ -0,0 +1,361 @@
<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\TestType;
use App\Entity\Comment;
use App\Entity\Document;
use App\Entity\Directory;
use App\Form\CommentType;
use App\Form\SearchBarType;
use App\Repository\DocumentRepository;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Gedmo\Loggable\Entity\Repository\LogEntryRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
#[Route('/document', name: 'document_')]
class DocumentController extends AbstractController
{
private TokenStorageInterface $TokenStorage;
public function __construct(TokenStorageInterface $TokenStorage)
{
$this->TokenStorage = $TokenStorage;
}
#[Route('/archive/{id}', name: 'archive')]
#[IsGranted('archive', subject: 'Document', message: 'granted_not_allowed_archiving_document')]
public function archive(Document $Document, Request $Request): Response
{
$entityManager = $this->getDoctrine()->getManager();
$Document->setArchive(($Document->getArchive()) ? false : true);
$entityManager->persist($Document);
try {
$entityManager->flush();
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash(
'warning',
'alert_error_' . (($Document->getArchive()) ? 'archiving' : 'unarchiving') . '_document'
);
if (null != $Request->headers->get('referer')) {
return $this->redirect($Request->headers->get('referer'));
}
return $this->redirectToRoute('document_view', ['id' => $Document->getId()]);
}
$this->addFlash(
'success',
'alert_success_' . (($Document->getArchive()) ? 'archiving' : 'unarchiving') . '_document'
);
if (null != $Request->headers->get('referer')) {
return $this->redirect($Request->headers->get('referer'));
}
return $this->redirectToRoute('document_view', ['id' => $Document->getId()]);
}
#[Route('/view/{id}', name: 'view')]
#[IsGranted('view', subject: 'Document', message: 'granted_not_allowed_viewing_document')]
public function view(Document $Document, EntityManagerInterface $em): Response
{
$formComment = $this->createForm(
CommentType::class,
new Comment(),
[ 'action' => $this->generateUrl('comment_create', ['Document' => $Document->getId()])]
);
/**
* @var LogEntryRepository $logEntryRepository
*/
$logEntryRepository = $em->getRepository('Gedmo\Loggable\Entity\LogEntry');
$documentHistory = $logEntryRepository->getLogEntries($Document);
return $this->render('document/view.html.twig', [
'controller_name' => 'DocumentController',
'document' => $Document,
'history' => $documentHistory,
'formComment' => $formComment->createView()
]);
}
#[Route('/history/{id}', name: 'history')]
#[IsGranted('view', subject: 'Document', message: 'granted_not_allowed_viewing_document')]
public function history(Document $Document, EntityManagerInterface $em): Response
{
/**
* @var LogEntryRepository $logEntryRepository
*/
$logEntryRepository = $em->getRepository('Gedmo\Loggable\Entity\LogEntry');
$documentHistory = $logEntryRepository->getLogEntries($Document);
return $this->render('document/history.html.twig', [
'controller_name' => 'DocumentController',
'document' => $Document,
'history' => $documentHistory,
]);
}
#[Route('/create/{type}', name: 'create')]
#[Route('/create/{type}/directory/{directory?}', name: 'create_directory')]
#[Route('/create/{type}/user/{user?}', name: 'create_user')]
public function create(string $type, Request $request, Directory $directory = null, User $user = null): Response
{
$Document = $this->getDocumentByTypename($type);
if (!$this->IsGranted('create', $Document)) {
throw new AccessDeniedHttpException('granted_not_allowed_creating_document');
}
if (method_exists($Document, 'setDirectory')) {
if (is_null($directory)) {
throw new \Exception('exception_document_need_directory');
}
$Document->setDirectory($directory);
}
if (method_exists($Document, 'setUser')) {
if (is_null($user)) {
throw new \Exception('exception_document_need_user');
}
$Document->setUser($user);
}
$form = $this->getFormByDocumentType($Document);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/**
* @var User $loggedUser
*/
$loggedUser = $this->getUser();
//check subGroups
//check for subGroups addition and user not in thoses subgroups
foreach ($Document->getAllowedSubGroups() as $key => $subGroup) {
if (
!$loggedUser->getSubGroups()->contains($subGroup)
&& !$loggedUser->hasPermission('group_ignore_subgroups')
) {
$this->addFlash('danger', 'alert_danger_adding_subgroup_document');
$Document->removeAllowedSubGroup($subGroup);
}
}
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($Document);
try {
$entityManager->flush();
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('danger', 'alert_error_creating_document');
return $this->redirectToRoute($request->getRequestUri());
}
$this->addFlash('success', 'alert_success_creating_document');
return $this->redirectToRoute('document_view', ['id' => $Document->getId()]);
}
return $this->render('document/create.html.twig', [
'controller_name' => 'DocumentController',
'document' => $Document,
'directory' => $directory,
'form' => $form->createView(),
'type' => $type,
]);
}
#[Route('/edit/{id}', name: 'edit')]
#[IsGranted('edit', subject: 'Document', message: 'granted_not_allowed_editing_document')]
public function edit(Request $request, Document $Document): Response
{
$CurrentSubGroupsAlloweds = $Document->getAllowedSubGroups()->toArray();
$form = $this->getFormByDocumentType($Document);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/**
* @var User $User
*/
$User = $this->getUser();
//check subGroups
//check for missing subGroups
foreach ($CurrentSubGroupsAlloweds as $key => $subGroup) {
if (
!in_array($subGroup, $Document->getAllowedSubGroups()->toArray())
&& !$User->getSubGroups()->contains($subGroup)
&& !$User->hasPermission('group_ignore_subgroups')
) {
$this->addFlash('danger', 'alert_danger_removing_subgroup_document');
$Document->addAllowedSubGroup($subGroup);
}
}
//check for subGroups addition and user not in thoses subgroups
foreach ($Document->getAllowedSubGroups() as $key => $subGroup) {
if (
!in_array($subGroup, $CurrentSubGroupsAlloweds)
&& !$User->getSubGroups()->contains($subGroup)
&& !$User->hasPermission('group_ignore_subgroups')
) {
$this->addFlash('danger', 'alert_danger_adding_subgroup_document');
$Document->removeAllowedSubGroup($subGroup);
}
}
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($Document);
try {
$entityManager->flush();
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('danger', 'alert_error_editing_document');
return $this->redirectToRoute($request->getRequestUri());
}
$this->addFlash('success', 'alert_success_editing_document');
return $this->redirectToRoute('document_view', ['id' => $Document->getId()]);
}
return $this->render('document/edit.html.twig', [
'controller_name' => 'DocumentController',
'document' => $Document,
'form' => $form->createView()
]);
}
#[Route('/delete/{id}', name: 'delete')]
#[IsGranted('delete', subject: 'Document', message: 'granted_not_allowed_deleting_document')]
public function delete(Document $Document, Request $Request): Response
{
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($Document);
try {
$entityManager->flush();
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('warning', 'alert_error_deleting_document');
if (null != $Request->headers->get('referer')) {
return $this->redirect($Request->headers->get('referer'));
}
return $this->redirectToRoute('document_view', ['id' => $Document->getId()]);
}
$this->addFlash('success', 'alert_success_deleting_document');
/*if (null != $Request->headers->get('referer')) {
return $this->redirect($Request->headers->get('referer'));
}*/ //TODO: fix refere document when deleted (if path return view, send to list)
return $this->redirectToRoute('document_list', ['type' => $Document->getClassShort()]);
}
#[Route('/archives/{type}/{directory?}', name: 'list_archives')]
#[Route('/{type}/{directory?}', name: 'list')]
public function list(
PaginatorInterface $paginator,
Request $request,
DocumentRepository $DocumentRepository,
EntityManagerInterface $EntityManagerInterface,
Directory $directory = null,
string $type
): Response {
$searchForm = $this->createForm(SearchBarType::class);
$searchForm->handleRequest($request);
$documentTypeEntity = $this->getDocumentByTypename($type);
/**
* @var DocumentRepository $DocumentRepository
*/
$DocumentRepository = $EntityManagerInterface->getRepository(get_class($documentTypeEntity));
$archive = ($request->attributes->get('_route') == 'document_list_archives');
$pagination = $paginator->paginate(
$DocumentRepository ->listForUser($this->getUser(), $documentTypeEntity->getIsPublic())
->search((
$searchForm->isSubmitted()
&& $searchForm->isValid()
&& $searchForm->getData()['subject'] !== null
) ? $searchForm->getData()['subject'] : null)
->limitType($type)
->archive($archive)
->order(['createdAt' => 'DESC'])
->limitDirectory($directory)
->getResult(),
$request->query->getInt('page', 1)
);
return $this->render('document/list.html.twig', [
'controller_name' => 'DocumentController',
'pagination' => $pagination,
'count' => $pagination->getTotalItemCount(),
'searchForm' => $searchForm->createView(),
'documentTypeEntity' => $documentTypeEntity,
'type' => $documentTypeEntity->getClassShort(),
'archive' => $archive,
'directory' => $directory
]);
}
private function getFormByDocumentType(Document $Document)
{
if (!is_subclass_of($Document, 'App\Entity\Document')) {
throw new \Exception('exception_invalid_document_type');
}
$type = 'App\Form\\' . ucfirst(strtolower($Document->getClassShort())) . 'Type';
if (!class_exists($type)) {
throw new \Exception('exception_invalid_document_type');
return null;
}
$formtype = new $type($this->TokenStorage);
return $this->createForm(get_class($formtype), $Document);
}
private function getDocumentByTypename(string $type)
{
$type = 'App\Entity\\' . ucfirst(strtolower($type));
if (!class_exists($type)) {
throw new \Exception('exception_invalid_document_type');
return null;
}
$Document = new $type($this->getUser());
if (!is_subclass_of($Document, 'App\Entity\Document')) {
throw new \Exception('exception_invalid_document_type');
}
return $Document ;
}
}

View File

@@ -0,0 +1,205 @@
<?php
namespace App\Controller;
use App\Entity\Folder;
use App\Entity\Document;
use App\Entity\Directory;
use App\Form\SearchBarType;
use App\Repository\DocumentRepository;
use App\Repository\DirectoryRepository;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
#[Route('/folder/{id}', name: 'folder_')]
class FolderController extends AbstractController
{
#[Route('/directory', name: 'directory')]
#[Route('/directory/wanted', name: 'directory_wanted')]
#[Route('/directory/dead', name: 'directory_dead')]
public function directory(
PaginatorInterface $paginator,
Request $request,
Folder $Folder,
DirectoryRepository $DirectoryRepository
): Response {
$searchForm = $this->createForm(SearchBarType::class);
$searchForm->handleRequest($request);
$req = $DirectoryRepository->list()
->search(
(
$searchForm->isSubmitted()
&& $searchForm->isValid()
&& $searchForm->getData()['subject'] !== null
) ? $searchForm->getData()['subject'] : null
);
if ($request->attributes->get('_route') == 'folder_directory_wanted') {
$req->wanted();
}
if ($request->attributes->get('_route') == 'folder_directory_dead') {
$req->dead();
} else {
$req->notDead();
}
$pagination = $paginator->paginate(
$req->getResult()
);
return $this->render('folder/directory.html.twig', [
'controller_name' => 'FolderController',
'folder' => $Folder,
'pagination' => $pagination,
'searchForm' => $searchForm->createView(),
'wanted' => ($request->attributes->get('_route') == 'folder_directory_wanted'),
'dead' => ($request->attributes->get('_route') == 'folder_directory_dead')
]);
}
#[Route('/directory/add/{Directory}', name: 'directory_add')]
public function directoryAdd(
Request $request,
Folder $Folder,
Directory $Directory
): Response {
$Folder->addDirectory($Directory);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($Folder);
try {
$entityManager->flush();
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('danger', 'alert_error_adding_to_folder');
return $this->redirectToRoute($request->getRequestUri());
}
$this->addFlash('success', 'alert_success_adding_to_folder');
return $this->redirectToRoute('document_view', ['id' => $Folder->getId()]);
}
#[Route('/directory/remove/{Directory}', name: 'directory_remove')]
public function directoryRemove(
Request $request,
Folder $Folder,
Directory $Directory
): Response {
$Folder->removeDirectory($Directory);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($Folder);
try {
$entityManager->flush();
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('danger', 'alert_error_removing_from_folder');
return $this->redirectToRoute($request->getRequestUri());
}
$this->addFlash('success', 'alert_success_removing_from_folder');
return $this->redirectToRoute('document_view', ['id' => $Folder->getId()]);
}
// Documents
#[Route('/document', name: 'document')]
#[Route('/document/archive', name: 'document_archive')]
public function document(
PaginatorInterface $paginator,
Request $request,
Folder $Folder,
DocumentRepository $DocumentRepository
): Response {
$searchForm = $this->createForm(SearchBarType::class);
$searchForm->handleRequest($request);
$req = $DocumentRepository->list()
->search(
(
$searchForm->isSubmitted()
&& $searchForm->isValid()
&& $searchForm->getData()['subject'] !== null
) ? $searchForm->getData()['subject'] : null
);
if ($request->attributes->get('_route') == 'folder_document_archive') {
$req->archive();
}
$pagination = $paginator->paginate(
$req->getResult()
);
return $this->render('folder/document.html.twig', [
'controller_name' => 'FolderController',
'folder' => $Folder,
'pagination' => $pagination,
'searchForm' => $searchForm->createView(),
'archive' => ($request->attributes->get('_route') == 'folder_document_archive'),
]);
}
#[Route('/document/add/{Document}', name: 'document_add')]
public function documentAdd(
Request $request,
Folder $Folder,
Document $Document
): Response {
$Folder->addDocument($Document);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($Folder);
try {
$entityManager->flush();
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('danger', 'alert_error_adding_to_folder');
return $this->redirectToRoute($request->getRequestUri());
}
$this->addFlash('success', 'alert_success_adding_to_folder');
return $this->redirectToRoute('document_view', ['id' => $Folder->getId()]);
}
#[Route('/document/remove/{Document}', name: 'document_remove')]
public function documentRemove(
Request $request,
Folder $Folder,
Document $Document
): Response {
$Folder->removeDocument($Document);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($Folder);
try {
$entityManager->flush();
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('danger', 'alert_error_removing_from_folder');
return $this->redirectToRoute($request->getRequestUri());
}
$this->addFlash('success', 'alert_success_removing_from_folder');
return $this->redirectToRoute('document_view', ['id' => $Folder->getId()]);
}
}

View File

@@ -0,0 +1,192 @@
<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\MotdType;
use App\Form\EmployeeType;
use App\Repository\TemplateRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
#[Route('/group', name: 'group_')]
class GroupController extends AbstractController
{
#[Route('/', name: 'index')]
public function index(Request $request, TemplateRepository $TemplateRepository): Response
{
$group = $this->getUser()->getMainGroup();
if (!$this->IsGranted('administrate', $group)) {
throw new AccessDeniedHttpException('granted_not_allowed_administrate_group');
}
$form = $this->createForm(MotdType::class, $group);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if (!$this->IsGranted('motd', $group)) {
throw new AccessDeniedHttpException('granted_not_allowed_editing_motd_group');
}
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($group);
try {
$entityManager->flush();
$this->addFlash('success', 'alert_success_editing_motd');
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('danger', 'alert_error_editing_motd');
}
}
return $this->render('group/index.html.twig', [
'controller_name' => 'GroupController',
'formMOTD' => $form->createView(),
'group' => $group,
'templates' => $TemplateRepository->listForUser($this->getUser())->getResult()
]);
}
#[Route('/fire/{id}', name: 'fire')]
public function fire(User $User, Request $Request): Response
{
$group = $this->getUser()->getMainGroup();
if (!$this->IsGranted('fire', $group)) {
throw new AccessDeniedHttpException('granted_not_allowed_fire_employee');
}
/**
* @var User $currentUser
*/
$currentUser = $this->getUser();
if (
$User->getMainRank()->getPower() >= $currentUser->getMainRank()->getPower()
&& !$currentUser->getAdminMode()
) {
$this->addFlash('danger', 'alert_error_cant_fire_superior_or_same_rank');
return $this->redirectToRoute('group_index');
}
$entityManager = $this->getDoctrine()->getManager();
$User->setMainGroup(null);
$User->setMainRank(null);
$entityManager->persist($User);
try {
$entityManager->flush();
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('danger', 'alert_error_fire_employee');
if (null != $Request->headers->get('referer')) {
return $this->redirect($Request->headers->get('referer'));
}
return $this->redirectToRoute('group_index');
}
$this->addFlash('success', 'alert_success_fire_employee');
if (null != $Request->headers->get('referer')) {
return $this->redirect($Request->headers->get('referer'));
}
return $this->redirectToRoute('group_index');
}
#[Route('/employee/{id}', name: 'employee')]
public function employee(User $Employee, Request $Request): Response
{
$group = $this->getUser()->getMainGroup();
if (!$this->IsGranted('administrate', $group)) {
throw new AccessDeniedHttpException('granted_not_allowed_administrate_group');
}
//check if employee belong to user group
if ($Employee->getMainGroup() != $this->getUser()->getMainGroup()) {
throw new AccessDeniedHttpException('granted_not_allowed_administrate_other_group_employee');
}
$UserActualRank = $Employee->getMainRank();
/**
* @var User $currentUser
*/
$currentUser = $this->getUser();
$form = $this->createForm(EmployeeType::class, $Employee);
$form->handleRequest($Request);
if ($form->isSubmitted() && $form->isValid()) {
//check if rank is modified
if ($UserActualRank != $Employee->getMainRank()) {
if (!$this->IsGranted('rank', $group)) {
$this->addFlash('warning', 'alert_error_editing_employee_rank');
if (null != $Request->headers->get('referer')) {
return $this->redirect($Request->headers->get('referer'));
}
return $this->redirectToRoute('group_employee', ['id' => $Employee->getId()]);
}
if (
$Employee == $currentUser
) {
$this->addFlash('warning', 'alert_error_editing_employee_own_rank');
if (null != $Request->headers->get('referer')) {
return $this->redirect($Request->headers->get('referer'));
}
return $this->redirectToRoute('group_employee', ['id' => $Employee->getId()]);
}
if (
$UserActualRank != null
&& $UserActualRank->getPower() >= $currentUser->getMainRank()->getPower()
&& !$currentUser->getAdminMode()
) {
$this->addFlash('warning', 'alert_error_editing_employee_superior_or_same_rank');
if (null != $Request->headers->get('referer')) {
return $this->redirect($Request->headers->get('referer'));
}
return $this->redirectToRoute('group_employee', ['id' => $Employee->getId()]);
}
if (
$Employee->getMainRank()->getPower() >= $currentUser->getMainRank()->getPower()
&& !$currentUser->getAdminMode()
) {
$this->addFlash('warning', 'alert_error_setting_employee_superior_or_same_rank');
if (null != $Request->headers->get('referer')) {
return $this->redirect($Request->headers->get('referer'));
}
return $this->redirectToRoute('group_employee', ['id' => $Employee->getId()]);
}
}
//end check rank modified
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($Employee);
try {
$entityManager->flush();
$this->addFlash('success', 'alert_success_editing_employee');
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('danger', 'alert_error_editing_employee');
}
}
return $this->render('group/employee.html.twig', [
'controller_name' => 'GroupController',
'form' => $form->createView(),
'employee' => $Employee
]);
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace App\Controller;
use App\Repository\DocumentRepository;
use App\Repository\DirectoryRepository;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class HomeController extends AbstractController
{
#[Route('/', name: 'home')]
public function home(
DocumentRepository $DocumentRepository,
DirectoryRepository $DirectoryRepository
): Response {
return $this->render('home/home.html.twig', [
'documents' =>
$DocumentRepository ->listForUser($this->getUser())
->limit(10)
->order(['createdAt' => 'DESC'])
->getResult(),
'announces' =>
$DocumentRepository ->listForUser($this->getUser())
->limitType('announce')
->limit(3)
->order(['createdAt' => 'DESC'])
->getResult(),
'wanted' =>
$DirectoryRepository->list()->notDead()->wanted()->getResult(),
'controller_name' => 'HomeController',
]);
}
}

View File

@@ -0,0 +1,105 @@
<?php
namespace App\Controller;
use App\Form\MeType;
use App\Form\MePasswordType;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
#[Route('/me', name: 'me_')]
class MeController extends AbstractController
{
private Security $security;
public function __construct(Security $security)
{
$this->security = $security;
}
#[Route('/', name: 'index')]
public function index(PaginatorInterface $paginator, Request $request): Response
{
/**
* @var User $user
*/
$user = $this->security->getUser();
$form = $this->createForm(MeType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($user);
try {
$entityManager->flush();
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('danger', 'alert_error_editing_profile');
return $this->redirectToRoute($request->getRequestUri());
}
$this->addFlash('success', 'alert_success_editing_profile');
$request->getSession()->set('_locale', $user->getLocale());
return $this->redirectToRoute('me_index');
}
return $this->render('me/index.html.twig', [
'controller_name' => 'MeController',
'form' => $form->createView(),
'pagination' => $paginator->paginate($user->getDocuments())
]);
}
#[Route('/password', name: 'password')]
public function password(Request $request, UserPasswordHasherInterface $passwordEncoder): Response
{
/**
* @var User $user
*/
$user = $this->security->getUser();
$form = $this->createForm(MePasswordType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// encode the plain password
$user->setPassword(
$passwordEncoder->hashPassword(
$user,
$form->get('plainPassword')->getData()
)
);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($user);
try {
$entityManager->flush();
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('danger', 'alert_error_editing_password');
return $this->redirectToRoute($request->getRequestUri());
}
$this->addFlash('success', 'alert_success_editing_password');
$request->getSession()->set('_locale', $user->getLocale());
return $this->redirectToRoute('me_index');
}
return $this->render('me/password.html.twig', [
'controller_name' => 'MeController',
'form' => $form->createView(),
]);
}
}

View File

@@ -0,0 +1,75 @@
<?php
namespace App\Controller;
use App\Entity\Notification;
use App\Repository\NotificationRepository;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
#[Route('/notification', name: 'notification_')]
class NotificationController extends AbstractController
{
#[Route('/', name: 'list')]
public function list(
PaginatorInterface $paginator,
Request $request,
NotificationRepository $NotificationRepository
): Response {
return $this->render('notification/list.html.twig', [
'controller_name' => 'NotificationController',
'pagination' => $paginator->paginate(
$NotificationRepository->listForUser($this->getUser())
->order(['readed' => 'ASC', 'createdAt' => 'DESC'])
->getResult(),
$request->query->getInt('page', 1)
)
]);
}
#[Route('/delete/{id}', name: 'delete')]
#[IsGranted('delete', subject: 'Notification', message: 'granted_not_allowed_delete_notification')]
public function delete(Notification $Notification, Request $Request): Response
{
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($Notification);
try {
$entityManager->flush();
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('warning', 'alert_error_deleting_notification');
}
if (null != $Request->headers->get('referer')) {
return $this->redirect($Request->headers->get('referer'));
}
return $this->redirectToRoute('notification_list');
}
#[Route('/markread/{id}', name: 'markread')]
#[IsGranted('markread', subject: 'Notification', message: 'granted_not_allowed_markread_notification')]
public function markread(Notification $Notification, Request $Request): Response
{
$entityManager = $this->getDoctrine()->getManager();
$Notification->setReaded(true);
try {
$entityManager->flush();
} catch (\Throwable $th) {
if ($_ENV['APP_ENV'] === 'dev') {
throw $th; //DEBUG
}
$this->addFlash('warning', 'alert_error_markread_notification');
}
if (null != $Request->headers->get('referer')) {
return $this->redirect($Request->headers->get('referer'));
}
return $this->redirectToRoute('notification_list');
}
}

View File

@@ -0,0 +1,176 @@
<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\ChangePasswordFormType;
use App\Form\ResetPasswordRequestFormType;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use SymfonyCasts\Bundle\ResetPassword\ResetPasswordHelperInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use SymfonyCasts\Bundle\ResetPassword\Controller\ResetPasswordControllerTrait;
use SymfonyCasts\Bundle\ResetPassword\Exception\ResetPasswordExceptionInterface;
#[Route('/reset-password')]
class ResetPasswordController extends AbstractController
{
use ResetPasswordControllerTrait;
private ResetPasswordHelperInterface $resetPasswordHelper;
private TranslatorInterface $translator;
public function __construct(ResetPasswordHelperInterface $resetPasswordHelper, TranslatorInterface $translator)
{
$this->resetPasswordHelper = $resetPasswordHelper;
$this->translator = $translator;
}
/**
* Display & process form to request a password reset.
*/
#[Route('', name: 'app_forgot_password_request')]
public function request(Request $request, MailerInterface $mailer): Response
{
$form = $this->createForm(ResetPasswordRequestFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
return $this->processSendingPasswordResetEmail(
$form->get('email')->getData(),
$mailer
);
}
return $this->render('reset_password/request.html.twig', [
'requestForm' => $form->createView(),
]);
}
/**
* Confirmation page after a user has requested a password reset.
*/
#[Route('/check-email', name: 'app_check_email')]
public function checkEmail(): Response
{
// Generate a fake token if the user does not exist or someone hit this page directly.
// This prevents exposing whether or not a user was found with the given email address or not
if (null === ($resetToken = $this->getTokenObjectFromSession())) {
$resetToken = $this->resetPasswordHelper->generateFakeResetToken();
}
return $this->render('reset_password/check_email.html.twig', [
'resetToken' => $resetToken,
]);
}
/**
* Validates and process the reset URL that the user clicked in their email.
*/
#[Route('/reset/{token}', name: 'app_reset_password')]
public function reset(Request $request, UserPasswordHasherInterface $passEncod, string $token = null): Response
{
if ($token) {
// We store the token in session and remove it from the URL, to avoid the URL being
// loaded in a browser and potentially leaking the token to 3rd party JavaScript.
$this->storeTokenInSession($token);
return $this->redirectToRoute('app_reset_password');
}
$token = $this->getTokenFromSession();
if (null === $token) {
//No reset password token found in the URL or in the session.
throw $this->createNotFoundException('exception_no_reset_token_found');
}
try {
$user = $this->resetPasswordHelper->validateTokenAndFetchUser($token);
} catch (ResetPasswordExceptionInterface $e) {
//There was a problem validating your reset request
$this->addFlash(
'reset_password_error',
'alert_error_problem_validating_reset_request'
);
return $this->redirectToRoute('app_forgot_password_request');
}
// The token is valid; allow the user to change their password.
$form = $this->createForm(ChangePasswordFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// A password reset token should be used only once, remove it.
$this->resetPasswordHelper->removeResetRequest($token);
// Encode the plain password, and set it.
$encodedPassword = $passEncod->hashPassword(
$user,
$form->get('plainPassword')->getData()
);
$user->setPassword($encodedPassword);
$this->getDoctrine()->getManager()->flush();
// The session is cleaned up after the password has been changed.
$this->cleanSessionAfterReset();
$this->addFlash('success', 'alert_success_resetting_password');
return $this->redirectToRoute('home');
}
return $this->render('reset_password/reset.html.twig', [
'resetForm' => $form->createView(),
]);
}
private function processSendingPasswordResetEmail(string $emailFormData, MailerInterface $mailer): RedirectResponse
{
$user = $this->getDoctrine()->getRepository(User::class)->findOneBy([
'email' => $emailFormData,
]);
// Do not reveal whether a user account was found or not.
if (!$user) {
return $this->redirectToRoute('app_check_email');
}
try {
$resetToken = $this->resetPasswordHelper->generateResetToken($user);
} catch (ResetPasswordExceptionInterface $e) {
// If you want to tell the user why a reset email was not sent, uncomment
// the lines below and change the redirect to 'app_forgot_password_request'.
// Caution: This may reveal if a user is registered or not.
//
// $this->addFlash('reset_password_error', sprintf(
// 'There was a problem handling your password reset request - %s',
// $e->getReason()
// ));
return $this->redirectToRoute('app_check_email');
}
$email = (new TemplatedEmail())
//->from(new Address('noreply@vision.com', 'Vision'))
->to($user->getEmail())
->subject($this->translator->trans('email_title_password_reset_request'))
->htmlTemplate('_emails/resetpassword.html.twig')
->context([
'resetToken' => $resetToken,
])
;
$mailer->send($email);
// Store the token object in session for retrieval in check-email route.
$this->setTokenObjectInSession($resetToken);
return $this->redirectToRoute('app_check_email');
}
}

View File

@@ -0,0 +1,141 @@
<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\AuthenticateType;
use App\Security\EmailVerifier;
use App\Form\RegistrationFormType;
use App\Repository\UserRepository;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
class SecurityController extends AbstractController
{
private EmailVerifier $emailVerifier;
private TranslatorInterface $translator;
public function __construct(EmailVerifier $emailVerifier, TranslatorInterface $translator)
{
$this->emailVerifier = $emailVerifier;
$this->translator = $translator;
}
#[Route('/register', name: 'app_register')]
public function register(Request $request, UserPasswordHasherInterface $passwordEncoder): Response
{
if ($this->getUser()) {
return $this->redirectToRoute('home');
}
$user = new User();
$user->setLocale($this->getParameter('kernel.default_locale'));
$form = $this->createForm(RegistrationFormType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// encode the plain password
$user->setPassword(
$passwordEncoder->hashPassword(
$user,
$form->get('plainPassword')->getData()
)
);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($user);
$entityManager->flush();
// generate a signed url and email it to the user
$this->emailVerifier->sendEmailConfirmation(
'app_verify_email',
$user,
(new TemplatedEmail())
//->from(new Address('noreply@vision.com', 'Vision'))
->to($user->getEmail())
->subject($this->translator->trans('email_title_confirm_email'))
->htmlTemplate('_emails/register.html.twig')
);
$this->addFlash('success', 'alert_success_check_mailbox_validate_account');
return $this->redirectToRoute('home');
}
return $this->render('security/register.html.twig', [
'form' => $form->createView(),
]);
}
#[Route('/verify/email', name: 'app_verify_email')]
public function verifyUserEmail(Request $request, UserRepository $userRepository): Response
{
$id = $request->get('id');
if (null === $id) {
return $this->redirectToRoute('app_register');
}
$user = $userRepository->find($id);
if (null === $user) {
return $this->redirectToRoute('app_register');
}
// validate email confirmation link, sets User::isVerified=true and persists
try {
$this->emailVerifier->handleEmailConfirmation($request, $user);
} catch (VerifyEmailExceptionInterface $exception) {
$this->addFlash('verify_email_error', $exception->getReason());
return $this->redirectToRoute('app_register');
}
$this->addFlash('success', 'alert_success_email_verified');
return $this->redirectToRoute('app_login');
}
/**
* @Route("/login", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
if ($this->getUser()) {
return $this->redirectToRoute('home');
}
$form = $this->createForm(AuthenticateType::class);
// 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,
'form' => $form->createView()
]
);
}
/**
* @Route("/logout", name="app_logout")
*/
public function logout()
{
throw new \LogicException('Something goes wrong if you see this');
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace App\Controller;
use App\Entity\Document;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class ShareController extends AbstractController
{
#[Route('/share/{share}', name: 'share')]
public function share(Document $Document): Response
{
if (!$Document->getAllowShare()) {
throw new AccessDeniedHttpException('granted_not_allowed_viewing_document');
}
return $this->render('document/view.html.twig', [
'controller_name' => 'DocumentController',
'document' => $Document,
'shared' => true
]);
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace App\Controller;
use App\Entity\Template;
use App\Repository\TemplateRepository;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
#[Route('/template', name: 'template_')]
class TemplateController extends AbstractController
{
/** Class by Aurel **/
#[Route('/json/list', name: 'json_list')]
public function templateJsonList(TemplateRepository $TemplateRepository): Response
{
/**
* @var Array $templates
*/
$templates = $TemplateRepository->listForUser($this->getUser())->getResult();
$json = [];
foreach ($templates as $template) {
array_push($json, [
'title' => $template->getTitle(),
'description' => '',
'url' => $this->generateUrl('template_json_get', ['id' => $template->getId()])
]);
}
return $this->json($json);
}
#[Route('/json/{id}', name: 'json_get')]
public function templateJsonGet(Template $template): Response
{
return new Response($template->getContent());
}
}

View File

@@ -0,0 +1,79 @@
<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\SearchBarType;
use App\Repository\UserRepository;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
#[Route('/user', name: 'user_')]
class UserController extends AbstractController
{
#[Route('/', name: 'list')]
public function list(PaginatorInterface $paginator, Request $request, UserRepository $UserRepository): Response
{
$searchForm = $this->createForm(SearchBarType::class);
$searchForm->handleRequest($request);
$pagination = $paginator->paginate(
$UserRepository->getAll()
->search(
(
$searchForm->isSubmitted()
&& $searchForm->isValid()
&& $searchForm->getData()['subject'] !== null
) ? $searchForm->getData()['subject'] : null
)
->onlyValid()
->getResult(),
$request->query->getInt('page', 1)
);
return $this->render('user/list.html.twig', [
'controller_name' => 'UserController',
'pagination' => $pagination,
'searchForm' => $searchForm->createView()
]);
}
#[Route('/view/{id}', name: 'view')]
public function view(User $User): Response
{
return $this->render('user/view.html.twig', [
'controller_name' => 'UserController',
'user' => $User
]);
}
#[Route('/view/{id}/sanctions', name: 'view_sanctions')]
public function viewSanctions(PaginatorInterface $paginator, User $User): Response
{
$group = $this->getUser()->getMainGroup();
if (!$this->IsGranted('sanction', $group)) {
throw new AccessDeniedHttpException("granted_not_allowed_to_view_sanctions");
}
$pagination = $paginator->paginate($User->getSanctions());
return $this->render('user/sanctions.html.twig', [
'controller_name' => 'UserController',
'user' => $User,
'pagination' => $pagination
]);
}
#[Route('/view/{id}/documents', name: 'view_documents')]
public function viewDocuments(PaginatorInterface $paginator, User $User): Response
{
$pagination = $paginator->paginate($User->getDocuments());
return $this->render('user/documents.html.twig', [
'controller_name' => 'UserController',
'user' => $User,
'pagination' => $pagination
]);
}
}