Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用addFlash从实现Symfony 4 UserCheckerInterface的类重定向_Php_Symfony_Symfony4 - Fatal编程技术网

Php 如何使用addFlash从实现Symfony 4 UserCheckerInterface的类重定向

Php 如何使用addFlash从实现Symfony 4 UserCheckerInterface的类重定向,php,symfony,symfony4,Php,Symfony,Symfony4,我需要你的帮助。我通过电子邮件地址建立了一个账户激活系统,效果很好 目前,我设法拒绝访问,但我不知道如何使用addFlash进行重定向,并且不显示403通行证 你会知道的​​我怎么能做到 下面是我拒绝访问的方式,我使用UserChecker class UserChecker implements UserCheckerInterface { /** * Checks the user account before authentication. * * @

我需要你的帮助。我通过电子邮件地址建立了一个账户激活系统,效果很好

目前,我设法拒绝访问,但我不知道如何使用
addFlash
进行重定向,并且不显示403通行证

你会知道的​​我怎么能做到

下面是我拒绝访问的方式,我使用
UserChecker

class UserChecker implements UserCheckerInterface
{
    /**
     * Checks the user account before authentication.
     *
     * @param UserInterface $user
     * @return RedirectResponse
     */
    public function checkPreAuth(UserInterface $user)
    {
        if ($user->getIsActivated() === null || $user->getIsActivated() === false) {
            throw new HttpException(403, 'Access denied.');
        } else {
            return new RedirectResponse('account_login');
        }
    }

抛出将AccountStatusException扩展到UserCheckerInterface的异常。例如:

use Symfony\Component\Security\Core\Exception\DisabledException;

// ...
throw new DisabledException('User account is disabled.');
听下面这个例外:

// src/EventSubscriber/ExceptionSubscriber.php
namespace App\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class ExceptionSubscriber implements EventSubscriberInterface
{
    /**
     * @var FlashBagInterface
     */
    private $flashBag;

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

    public function __construct(SessionInterface $session, UrlGeneratorInterface $router) 
    {
        $this->flashBag = $session->getFlashBag();
        $this->router = $router;
    }

    public static function getSubscribedEvents()
    {
        // return the subscribed events, their methods and priorities
        return [
            KernelEvents::EXCEPTION => 'onException',
        ];
    }

    public function onException(ExceptionEvent $event)
    {
        // You get the exception object from the received event
        $exception = $event->getException();

        if (!$exception instanceof DisabledException) {
            return;
        }

        $this->flashBag->add(
            'warning',
            $exception->getMessage()
        );

        $response = new RedirectResponse($this->router->generate('homepage'));
        $event->setResponse($response);     
    }
}
如果使用default services.yaml,这就足够了

有关活动订阅服务器的详细信息:

如何在我的UserChecker中使用它?通过
注入?
否。抛出AccessDeniedException而不是HttpException。Symfony将捕获它并触发AccessDeniedHandler,而不是403响应。它不起作用<代码>如果($user->getIsActivate()==null | |$user->getIsActivate()==false){抛出新的AccessDeniedException();}否则{返回新的重定向响应('account_login');}参数1传递给Symfony\Component\Security\Core\Exception\AuthenticationException::setToken()必须在第130行的/var/www/Symfony/vendor/Symfony/Security http/Firewall/ExceptionListener.php中实现接口Symfony\Component\Security\Core\Authentication\Token\Token interface,给定空值。用户在身份验证之前没有身份验证令牌。AccessDeniedException假定您拒绝对没有权限的经过身份验证的用户进行访问。我编辑了我的答案以使用合适的异常。但它没有传入
onException()
函数