diff --git a/src/Security/MailAccessChecker.php b/src/Security/MailAccessChecker.php new file mode 100644 index 0000000..aa5073a --- /dev/null +++ b/src/Security/MailAccessChecker.php @@ -0,0 +1,53 @@ +getRoles(); + + if (in_array('ROLE_CLIENT', $roles, true) && !in_array('ROLE_ADMIN', $roles, true)) { + throw new AccessDeniedException('Mail not accessible to clients'); + } + + if (!in_array('ROLE_USER', $roles, true) && !in_array('ROLE_ADMIN', $roles, true)) { + throw new AccessDeniedException('ROLE_USER required'); + } + } + + /** + * Verifie que l'utilisateur est ROLE_ADMIN. + * + * @throws AccessDeniedException + */ + public function ensureIsAdmin(?UserInterface $user): void + { + if (!$user instanceof User || !$this->authorizationChecker->isGranted('ROLE_ADMIN')) { + throw new AccessDeniedException('Admin only'); + } + } +}