. */ namespace App\Security; use App\Entity\UserSystem\User; use Hslavich\OneloginSamlBundle\Security\Http\Authenticator\Token\SamlToken; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\Security\Core\Event\AuthenticationSuccessEvent; use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\Security\Core\Exception\CustomUserMessageAccountStatusException; use Symfony\Contracts\Translation\TranslatorInterface; class EnsureSAMLUserForSAMLLoginChecker implements EventSubscriberInterface { private TranslatorInterface $translator; public function __construct(TranslatorInterface $translator) { $this->translator = $translator; } public static function getSubscribedEvents() { return [ AuthenticationSuccessEvent::class => 'onAuthenticationSuccess', ]; } public function onAuthenticationSuccess(AuthenticationSuccessEvent $event) { $token = $event->getAuthenticationToken(); $user = $token->getUser(); //If we are using SAML, we need to check that the user is a SAML user. if ($token instanceof SamlToken) { if ($user instanceof User && !$user->isSAMLUser()) { throw new CustomUserMessageAccountStatusException($this->translator->trans('saml.error.cannot_login_local_user_per_saml', [], 'security')); } } else { //Ensure that you can not login locally with a SAML user (even if this should not happen, as the password is not set) if ($user instanceof User && $user->isSamlUser()) { throw new CustomUserMessageAccountStatusException($this->translator->trans('saml.error.cannot_login_saml_user_locally', [], 'security')); } } } }