Symfony 3 FOSUserBundle配置文件编辑的电子邮件确认

时间:2017-08-03 13:28:17

标签: symfony fosuserbundle

我尝试按照此论坛提供电子邮件授予配置文件编辑fos用户捆绑。  我创建文件

/src/AppBundle/EventListener.php

namespace AppBundle\EventListener;

use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\GetResponseUserEvent;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\Mailer\MailerInterface;
use FOS\UserBundle\Util\TokenGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class ChangeProfileListener implements EventSubscriberInterface
{
    private $mailer;
    private $tokenGenerator;
    private $router;
    private $session;
    private $tokenStorage;

    public function __construct(
        MailerInterface $mailer,
        TokenGeneratorInterface $tokenGenerator,
        UrlGeneratorInterface $router,
        SessionInterface $session, TokenStorageInterface $tokenStorage
    ) {
        $this->mailer = $mailer;
        $this->tokenGenerator = $tokenGenerator;
        $this->router = $router;
        $this->session = $session;
        $this->tokenStorage = $tokenStorage;
    }

    public static function getSubscribedEvents()
    {
        return array(
            FOSUserEvents::PROFILE_EDIT_INITIALIZE => 'onProfileEditInitialize',
            FOSUserEvents::PROFILE_EDIT_SUCCESS => 'onProfileEditSuccess',
        );
    }

    public function onProfileEditInitialize(GetResponseUserEvent $event)
    {
        // required, because when Success's event is called, session already contains new email
        $this->email = $event->getUser()->getEmail();
    }

    public function onProfileEditSuccess(FormEvent $event)
    {
        $user = $event->getForm()->getData();
        if ($user->getEmail() !== $this->email)
        {
            // disable user
            $user->setEnabled(false);

            // send confirmation token to new email
            $user->setConfirmationToken($this->tokenGenerator->generateToken());
            $this->mailer->sendConfirmationEmailMessage($user);

            // force user to log-out
            $this->tokenStorage->setToken();

            // redirect user to check email page
            $this->session->set('fos_user_send_confirmation_email/email', $user->getEmail());
            $url = $this->router->generate('fos_user_registration_check_email');
            $event->setResponse(new RedirectResponse($url));
        }
    }
}

在service.yml之后

parameters:
    #parameter_name: value
    oc_user.email_change.listener.class: AppBundle\EventListener\ChangeProfileListener

services:
    app.form.registration:
        class: AppBundle\Form\RegistrationType
        tags:
            - { name: form.type, alias: app_user_registration }
    app.form.profileedit:
        class: AppBundle\Form\ProfileType
        tags:
            - { name: form.type, alias: app_profile_edit }
...

    oc_user.email_change.listener:
        class: %oc_user.email_change.listener.class%
        arguments: ['@fos_user.mailer', '@fos_user.util.token_generator', '@router', '@session', '@security.token_storage']
        tags:
          - { name: kernel.event_subscriber }

但我总是有这个错误

(1/1)AutowiringFailedException 无法自动服务" AppBundle \ EventListener \ ChangeProfileListener":参数" $ mailer"方法" __ construct()"引用接口" FOS \ UserBundle \ Mailer \ MailerInterface"但是没有这样的服务。您应该将此接口别名为其中一个现有服务:" fos_user.mailer.default"," fos_user.mailer.twig_swift"," fos_user.mailer.noop&#34 ;

我也覆盖了表单,但它可以正常工作

可以帮助我??

我的配置文件

# app/config/config.yml
fos_user:
    db_driver: orm # other valid values are 'mongodb' and 'couchdb'
    firewall_name: main
    user_class: AppBundle\Entity\User
    from_email:
        address: "%mailer_user%"
        sender_name: "%mailer_user%"
    registration:
        form:

            type: AppBundle\Form\RegistrationType
            # if you are using Symfony < 2.8 you should use the type name instead
            # type: app_user_registration
        confirmation:
            enabled: true
    profile:
        form:
            type: AppBundle\Form\ProfileType
fos_user.mailer:
   alias: 'fos_user.mailer.default'

1 个答案:

答案 0 :(得分:1)

错误信息已经说明了:

  

您应该将此接口别名为其中一个现有服务:&#34; fos_user.mailer.default&#34;,&#34; fos_user.mailer.twig_swift&#34;,&#34; fos_user.mailer。空操作&#34;

配置中出现错误(标有--> <--的服务):

oc_user.email_change.listener:
    class: %oc_user.email_change.listener.class%
    arguments: [--> '@fos_user.mailer', <-- '@fos_user.util.token_generator', '@router', '@session', '@security.token_storage']
    tags:
      - { name: kernel.event_subscriber }

您可能必须在FOS UserBundle中启用通知功能:

# app/config/config.yml
fos_user:
    # ...
    registration:
        confirmation:
            enabled: true

如文档中所述:https://symfony.com/doc/current/bundles/FOSUserBundle/emails.html#registration-confirmation

如果这没有帮助,您可能想要在错误消息中引用上述服务之一,或者创建一个别名,指向其中一个服务,例如: fos_user.mailer.default

fos_user.mailer:
    alias: 'fos_user.mailer.default'

然后您可以按原样保留您的服务,每当您引用fos_user.mail时,它都将使用别名中引用的服务。

相关问题