<?php
namespace Social\UserBundle\Listener;
use Doctrine\ORM\EntityManagerInterface;
use Social\InternalBundle\Entity\PackagesList;
use Social\UserBundle\Entity\User;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
/**
* Class RegistrationListener
*
* @package Social\UserBundle\Listener
*/
class RegistrationListener
{
/**
* @var TokenStorageInterface $security_context
*/
private $security_context;
/**
* @var Router $router
*/
private $router;
/**
* @var AuthorizationCheckerInterface $authorizationChecker
*/
protected $authorizationChecker;
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* RegistrationListener constructor.
*
* @param TokenStorageInterface $security_context
* @param Router $router
* @param AuthorizationCheckerInterface $authorizationChecker
* @param EntityManagerInterface $entityManager
*/
public function __construct(
TokenStorageInterface $security_context,
Router $router,
AuthorizationCheckerInterface $authorizationChecker,
EntityManagerInterface $entityManager
) {
$this->security_context = $security_context;
$this->router = $router;
$this->authorizationChecker = $authorizationChecker;
$this->entityManager = $entityManager;
}
/**
* @param GetResponseEvent $event
*
* @return GetResponseEvent
*/
public function onKernelRequest(GetResponseEvent $event)
{
if (!$this->security_context->getToken()) {
return $event;
}
$user = $this->security_context->getToken()->getUser();
$route = $event->getRequest()->get('_route');
if (!$route) {
return $event;
}
if (!$user instanceof User) {
return $event;
}
// if ($event->getRequest()->isXmlHttpRequest()) {
// return $event;
// }
if (in_array(
$route,
[
'social_channel_authentication',
]
) || strpos($route, '_imagine') !== false
) {
return $event;
}
if ($user->isEnabled() == 0) {
$this->security_context->setToken(null);
}
if ($user->getFromLandingPage() && $user->isProfileCompleted() == false) {
$now = new \DateTime();
$createdDiff = $now->diff($user->getCreatedAt());
$minutesDiff = $createdDiff->i;
if ($minutesDiff <= 5) {
return $event;
}
}
$userStepsNotCompleted = [
1 => [
'routes_allowed' => [
'social_user_signup_step2',
],
'route_redirect' => 'social_user_signup_step2',
],
2 => [
'routes_allowed' => [
'social_user_signup_step3',
'social_frontend_search_location',
'social_frontend_search_country',
'social_frontend_upload_photo',
],
'route_redirect' => 'social_user_signup_step3',
],
3 => [
'routes_allowed' => [
'social_user_signup_step4',
],
'route_redirect' => 'social_user_signup_step4',
],
4 => [
'routes_allowed' => [
'fos_user_registration_check_email',
'social_registration_resend_confirmation_email',
'fos_user_registration_confirm',
'social_support',
'social_account',
],
'route_redirect' => 'fos_user_registration_check_email',
],
0 => [
'routes_allowed' => [
'fos_user_registration_check_email',
'social_registration_resend_confirmation_email',
'fos_user_registration_confirm',
'social_support',
],
'route_redirect' => 'fos_user_registration_check_email',
],
];
if ($user->getExtendPeriodSignupConfirmation() && $user->isProfileCompleted() == false) {
if ($user->isExtendedPeriodSignupConfirmationValid()) {
return $event;
} else {
if ($user->isProfileCompleted() == false) {
foreach ($userStepsNotCompleted as $step => $userStepNotCompleted) {
if ($user->getLastRegistrationStep() == $step) {
if (in_array($route, $userStepNotCompleted['routes_allowed'])) {
return $event;
}
$event->setResponse(new RedirectResponse($this->router->generate($userStepNotCompleted['route_redirect'])));
return $event;
}
}
}
}
}
$packageName = $user->getPackageName();
/** @var PackagesList $packageList */
$packageList = $this->entityManager->getRepository(PackagesList::class)->findOneBy(['name' => $packageName]);
if ($packageList->getValue() > 0 && $user->getHasAgreedToWaiveRights() == false && !in_array($route,
['social_confirm_package_usage', 'social_terms_conditions', 'social_accept_package_usage', 'social_frontend_search_location', 'social_frontend_check_location_exist', 'social_user_signup_step3'])) {
$event->setResponse(new RedirectResponse($this->router->generate('social_confirm_package_usage')));
}
/**
* on frontend, allow only ROLE_USER or ROLE_PREVIOUS_ADMIN
*/
$isAdminRoute = strpos($route, 'admin') !== false;
if ($this->authorizationChecker->isGranted('ROLE_SONATA_ADMIN')) {
if ($isAdminRoute == false && $route != 'social_channel_authentication' && !in_array($route,
['social_frontend_impersonate'])) {
$event->setResponse(new RedirectResponse($this->router->generate('sonata_admin_dashboard')));
}
}
return $event;
}
}