<?php/** * apiNotificationsEnableController.php. * * @copyright 2016-2017 Oecko */namespace App\Controller\Api;use App\Entity\Notification;use App\Utils\ApiController;use Doctrine\Persistence\ManagerRegistry;use FOS\RestBundle\View\View;use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\Annotation\Route;/** * Api controller for user profile management. */#[Route(path: '/api')]class ApiBrowserNotificationsController extends ApiController{ public function __construct(private readonly ManagerRegistry $doctrine) { } #[Route(path: '/notifications/preferences', methods: ['POST'])] #[IsGranted('ROLE_NOTIFICATION_CENTER')] public function postNotificationsPreferences(Request $request): Response { $em = $this->doctrine->getManager(); $user = $this->getUser(); $notifType = $request->get('notifType'); $isChecked = $request->get('isChecked'); if (str_contains(strtolower((string) $notifType), 'tenant')) { if (!$user->hasRole('ROLE_DOC_ADMIN_SPACE')) { return new View('Accès non authorisé', Response::HTTP_FORBIDDEN); } } if (str_contains(strtolower((string) $notifType), 'publication')) { if (!$user->hasRole('ROLE_PUBLICPAGE')) { return new View('Accès non authorisé', Response::HTTP_FORBIDDEN); } } if (str_contains(strtolower((string) $notifType), 'privatemessage')) { if (!$user->hasRole('ROLE_READ_MESSAGE')) { return new View('Accès non authorisé', Response::HTTP_FORBIDDEN); } } if ($notifType) { if ('true' == $isChecked) { $user->addNotifType($notifType); } else { $user->removeNotifType($notifType); } } $em->persist($user); $em->flush(); return $this->res(['message' => 'Préférences enregistrées']); } /** * * @return Response */ #[Route(path: '/notifications')] #[IsGranted('ROLE_NOTIFICATION_CENTER')] public function getUserNotifications() { $notifications = $this->doctrine->getRepository(Notification::class)->findBy( ['recipient' => $this->getUser()], ['date' => 'ASC'] ); return $this->res($notifications); } /** * * @return Response */ #[Route(path: '/notifications/{id}', methods: ['DELETE'])] #[IsGranted('ROLE_NOTIFICATION_CENTER')] public function deleteNotif($id) { $notificationDelete = $this->doctrine->getRepository(Notification::class)->find($id); if (null === $notificationDelete) { return new $this->err('La notification n\'existe pas.'); } $em = $this->doctrine->getManager(); $em->remove($notificationDelete); $em->flush(); return $this->res(['message' => 'La notification a bien été supprimée']); }}