简体   繁体   中英

Redirect users based on attribute

Alright so what am I trying to do is that I check if user status is "pending" and if so, I'd redirect him to "/pending" page.

Now I need this check on almost the entire website.

I tried with the decision manager but was unable to redirect, any other way to do this?

This should be called only for logged users

security.yaml

access_decision_manager:
        service: App\Security\StatusAuthenticator

And the StatusAuthenticator

<?php

namespace App\Security;


use App\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;

class StatusAuthenticator implements AccessDecisionManagerInterface
{
    /**
     * @param TokenInterface $token
     * @param array $attributes
     * @param null $object
     * @return bool|void
     */
    public function decide(TokenInterface $token, array $attributes, $object = null)
    {
        if($token->getUser()->getStatus() == User::USER_STATUS_PENDING) {
            // Needs to be redirected to /pending
            return false;
        }

        return true;
    }
}

Since you need to "check this on almost the entire website", you can use an EventListener that will fire on every request and there you can check if you have an authenticated user and their status.

// src/EventListener/PendingUserListener.php
namespace App\EventListener;

use App\Entity\User;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;

class PendingUserListener implements EventSubscriberInterface
{
    /**
     * @var Security
     */
    private $security;

    /**
     * @var UrlGeneratorInterface
     */
    private $urlGenerator;    

    public function __construct(Security $security, UrlGeneratorInterface $urlGenerator)
    {
        $this->security = $security;
        $this->urlGenerator = $urlGenerator;
    }

    public static function getSubscribedEvents()
    {
        return [ KernelEvents::REQUEST => 'onKernelRequest' ];
    }

    public function onKernelRequest(RequestEvent $event)
    {
        $pending_route = 'pending';
        $user = $this->security->getUser();

        if (!$event->isMasterRequest()) {
            return;
        }

        if (!$user instanceof UserInterface) {
            return;
        }

        // Check if the requested page is 'pending', prevent redirect loops
        if ($pending_route === $event->getRequest()->get('_route')) {
            return;
        }

        // RedirectResponse expects a full url, generate from route name
        if (User::USER_STATUS_PENDING == $user->getStatus()) {
            $event->setResponse(
                new RedirectResponse($this->urlGenerator->generate($pending_route))
            );
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM