简体   繁体   English

当商店用户在Sylius进行电子邮件确认时,如何收听事件?

[英]How to listen to the event when a shop user is doing email confirmation at Sylius?

After the registration of a shop user in Sylius as the base framework of my web application, the user would receive an email with a verification link. 在Sylius中将商店用户注册为我的Web应用程序的基本框架之后,该用户将收到一封包含验证链接的电子邮件。

After the user clicks on this event, the user will be enabled in the database due to some magic stuff that happens in the behind of Sylius. 用户单击此事件后,由于Sylius后面发生了一些不可思议的事情,因此将在数据库中启用该用户。

I want to catch this event for being able to add more actions on this step. 我想捕获此事件,以便能够在此步骤中添加更多操作。

Steps taken: 采取的步骤:

First, i've tried to find an event, that could be fired in case of a successfully user authentication. 首先,我试图找到一个事件,如果成功进行用户身份验证,则可能会触发该事件。

bin/console debug:event-dispatcher

"sylius.admin_user.post_create" event
"sylius.admin_user.post_update" event
"sylius.admin_user.pre_delete" event
"sylius.admin_user.pre_update" event
"sylius.cart_change" event
"sylius.channel.pre_delete" event
"sylius.customer.post_register" event
"sylius.customer.pre_register" event
"sylius.customer.pre_update" event
"sylius.menu.shop.account" event
"sylius.oauth_user.post_create" event
"sylius.oauth_user.post_update" event
"sylius.oauth_user.pre_delete" event
"sylius.order.post_address" event
"sylius.order.post_complete" event
"sylius.order.post_payment" event
"sylius.order.post_select_shipping" event
"sylius.order.pre_complete" event
"sylius.order_item.pre_create" event
"sylius.order_item.pre_delete" event
"sylius.order_item.pre_update" event
"sylius.product.initialize_update" event
"sylius.product.pre_create" event
"sylius.product.pre_update" event
"sylius.product_review.pre_create" event
"sylius.product_variant.initialize_update" event
"sylius.shipment.post_ship" event
"sylius.shop_user.post_create" event
"sylius.shop_user.post_update" event
"sylius.shop_user.pre_delete" event
"sylius.taxon.pre_create" event
"sylius.taxon.pre_update" event
"sylius.user.email_verification.token" event
"sylius.user.password_reset.request.pin" event
"sylius.user.password_reset.request.token" event
"sylius.user.post_email_verification" event
"sylius.user.pre_password_change" event
"sylius.user.pre_password_reset" event
"sylius.user.security.impersonate" event
"sylius.user.security.implicit_login" event

This will list many events but nothing indicating any email-verification process besides "sylius.user.email_verification.token", which is only responsible to create the final auth token. 这将列出许多事件,但除了“ sylius.user.email_verification.token”(仅负责创建最终的身份验证令牌)外,没有任何内容指示任何电子邮件验证过程。

Then i stepped through the code of Sylius and found the UserController and the action verifyAction , that sounds promiseful. 然后,我逐步浏览了Sylius的代码,发现了UserController和操作verifyAction ,这听起来很有希望。

Within this action, several events are dispatched: 在此操作中,将调度几个事件:

$eventDispatcher->dispatch(UserEvents::PRE_EMAIL_VERIFICATION, new GenericEvent($user));

and

$eventDispatcher->dispatch(UserEvents::POST_EMAIL_VERIFICATION, new GenericEvent($user));

These constants are defined as: 这些常量定义为:

public const PRE_EMAIL_VERIFICATION = 'sylius.user.pre_email_verification';
public const POST_EMAIL_VERIFICATION = 'sylius.user.post_email_verification';

However, both Event types aren't registered in the global event dispatcher of symfony, they both where not listed with debug:event-dispatcher as shown before and thus can't be listened to. 但是,这两种事件类型都未在symfony的全局事件分派器中注册,它们都没有在debug:event-dispatcher中列出,因此之前都未列出,因此无法监听。

We already have a custom EventLister that works well for the registration-process, but it won't catch any email-verification events: 我们已经有一个自定义EventLister,可以很好地用于注册过程,但是它不会捕获任何电子邮件验证事件:

/**
 * {@inheritdoc}
 */
public static function getSubscribedEvents()
{
    return [
        'sylius.customer.pre_register' => ['onCustomerPreRegister', 0],
        'sylius.customer.post_register' => ['onCustomerPostRegister', 0],
        UserEvents::POST_EMAIL_VERIFICATION => ['onPostEmailVerification', 0]
    ];
}

Then i tried to overwrite the UserController in an own Implementation overriding the original controller and duplicating the verifyAction -Method (despite the fact that this wouldn't be a good option for the long tail) 然后,我尝试在自己的实现中覆盖UserController,以覆盖原始控制器并复制verifyAction -Method (尽管事实并非如此,长尾巴不是一个好选择)

Still no success. 仍然没有成功。

While we have successfully overridden several controllers, the UserController seems to be on an island that can't be changed to another class: 虽然我们已经成功重写了多个控制器,但UserController似乎位于一个无法更改为另一个类的岛上:

Our services.yml looks like this: 我们的services.yml看起来像这样:

sylius.controller.shop_user:
    class: AppBundle\Controller\User\UserController
    public: true

sylius.controller.shop.homepage:
    class: AppBundle\Controller\Shop\HomepageController
    public: true

The custom controller for the Homepage is working well, while any entries for UserController will be always ignored. 主页的自定义控制器运行良好,而UserController的任何条目将始终被忽略。

Possible solutions: 可能的解决方案:

in short, i tried several approaches and are thinking about other one (even those that would be very ugly): 简而言之,我尝试了几种方法,并在考虑其他方法(甚至是那些非常丑陋的方法):

  1. listen to appropriate events -> no chance 听适当的事件->没有机会
  2. overwrite the UserController -> no chance or did it the wrong way 覆盖UserController->没有机会或做错了方法
  3. create a cronjob that will fetch the sylius_customer-table in an interval of 5 min or so to find customers that have enabled their account recently -> yeah, can be done but is ugly too 创建一个cronjob,它将在5分钟左右的间隔内获取sylius_customer-table,以查找最近启用了其帐户的客户->是的,可以做到,但也很丑陋

Something else? 还有吗

Any idea how to solve our issue? 任何想法如何解决我们的问题?

Tried code below, and successfully received a dump message after opening verification link (in my case it was http://localhost/app_dev.php/en_US/verify/mUJjxjiI0OjWhRC2 ) 尝试以下代码,并在打开验证链接后成功接收到转储消息(在我的情况下为http://localhost/app_dev.php/en_US/verify/mUJjxjiI0OjWhRC2

<?php

namespace AppBundle\EventListener;

use Sylius\Bundle\UserBundle\UserEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

class CustomVerificationSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return [
            UserEvents::POST_EMAIL_VERIFICATION => 'onVerification'
        ];
    }

    public function onVerification(GenericEvent $event)
    {
        dump('it works');
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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