简体   繁体   中英

FOSUserBundle: how to allow registration, confirmation but not activation?

I'd like the user to register, confirm it's email, but being activated manually by an administrator .

Thanks to this page I found the FOSUserEvents::REGISTRATION_CONFIRMED which is called right after clicking on the confirmation link in the email.

Now I'd like to disable the account (see below).

class RegistrationListener implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return array(            
            FOSUserEvents::REGISTRATION_CONFIRMED => 'onRegistrationCompleted'
        );
    }

    public function onRegistrationCompleted(UserEvent $event) {
        // registration completed
        // TODO: disable the user. How?
    }
}

Or is there any configuration that I missed?

Any ideas?

Thanks in advance!

As I can see, inside FOS\\UserBundle\\Controller\\RegistrationController:: confirmAction() user is enabled:

/**
 * Receive the confirmation token from user email provider, login the user.
 *
 * @param Request $request
 * @param string  $token
 *
 * @return Response
 */
public function confirmAction(Request $request, $token)
{
    /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
    $userManager = $this->get('fos_user.user_manager');

    ...

    $user->setConfirmationToken(null);
    $user->setEnabled(true);

    $event = new GetResponseUserEvent($user, $request);
    $dispatcher->dispatch(FOSUserEvents::REGISTRATION_CONFIRM, $event);

    $userManager->updateUser($user);
    ...

    $dispatcher->dispatch(FOSUserEvents::REGISTRATION_CONFIRMED, new FilterUserResponseEvent($user, $request, $response));

    return $response;
}

I can think of two things you can do to disable it.

1) write an event listener, that will react on FOSUserEvents::REGISTRATION_CONFIRMED and disable the user => http://symfony.com/doc/master/bundles/FOSUserBundle/controller_events.html

2) override RegistrationController => https://symfony.com/doc/current/bundles/FOSUserBundle/overriding_controllers.html

I prefer first option.

class RegistrationListener implements EventSubscriberInterface
{
    /** @var EntityManager */
    private $em;


    /**
     * @param EntityManager $em
     */
    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    public static function getSubscribedEvents()
    {
        return array(            
            FOSUserEvents::REGISTRATION_CONFIRMED => 'onRegistrationCompleted'
        );
    }

    public function onRegistrationCompleted(UserEvent $event) {
        // registration completed
        // TODO: disable the user. How?
        $user = $event->getUser();
        $user->setEnabled(false);

        $this->em->persist($user);
        $this->em->flush();
    }
}

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