<?php
namespace Elements\Bundle\StyleLabBundle\EventSubscriber;
use Elements\Bundle\StyleLabBundle\Controller\AuthController;
use Elements\Bundle\StyleLabBundle\Security\User\User;
use JetBrains\PhpStorm\ArrayShape;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Security;
class UserSubscriber implements EventSubscriberInterface
{
private Security $security;
public function __construct(Security $security)
{
$this->security = $security;
} //: __construct
/**
* @param ControllerEvent $event
* @return void
*/
public function onKernelController(ControllerEvent $event): void
{
$controller = $event->getController();
// when a controller class defines multiple action methods, the controller
// is returned as [$controllerInstance, 'methodName']
if (is_array($controller)) {
$controller = $controller[0];
}
if ($controller instanceof AuthController) {
$user = $this->security->getUser();
$adminUser = false;
if ($user) {
if (($user instanceof User && $user->getUsername() == 'stylelab') || (method_exists($user, 'getUser') && (strpos($user->getUser()->getEmail(), '@elements.at') !== false))) {
$adminUser = true;
}
}
$event->getRequest()->attributes->set('isAdmin', $adminUser);
}
} //: onKernelController
/**
* @return string[]
*/
#[ArrayShape( [ KernelEvents::CONTROLLER => "string" ] )]
public static function getSubscribedEvents(): array
{
return [
KernelEvents::CONTROLLER => 'onKernelController',
];
} //: getSubscribedEvents
}