简体   繁体   English

Symfony2 FOSUserBundle禁用,电子邮件确认和用户注册

[英]Symfony2 FOSUserBundle disable both, email confirmation and user on registration

I just want to make it that when the user registers. 我只想在用户注册时做到这一点。 He does not get any email. 他没有收到任何电子邮件。 Of course I can do that by making this: 当然,我可以做到这一点:

registration:
    confirmation:
        enabled: false 

However this makes a user automatically register with no confirmation. 然而,这使得用户自动注册而无需确认。

I want to make it so that when a User registers he has to wait until the admin enables the user. 我想这样做,以便当用户注册时他必须等到管理员启用用户。 That means i need to make the user be Disabled by default.. 这意味着我需要默认禁用用户..

I cant find any information regarding that. 我找不到任何有关这方面的信息。 Can someone help? 有人可以帮忙吗?

I tried this but it didnt work: 我试过这个,但它没有用:

User.php user.php的

  public function __construct()
{
    parent::__construct();
    // your own logic

        $this->enabled = false;
    //and
        $this->setEnabled(false);
    }

PS Together with FOS i am using the SonataUserBundle.. This is my registerAction: PS和FOS一起使用SonataUserBundle ..这是我的注册动作:

 public function registerAction()
    {
        $user = $this->container->get('security.context')->getToken()->getUser();
        if ($user instanceof UserInterface) {
            $this->container->get('session')->getFlashBag()->set('sonata_user_error', 'sonata_user_already_authenticated');
            $url = $this->container->get('router')->generate('sonata_user_profile_show');
            return new RedirectResponse($url);
        }
        $form = $this->container->get('sonata.user.registration.form');
        $formHandler = $this->container->get('sonata.user.registration.form.handler');
        $confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');

        $process = $formHandler->process($confirmationEnabled);
        if ($process) {
            $user = $form->getData();

            $authUser = false;
            if ($confirmationEnabled) {
                $this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
                $url = $this->container->get('router')->generate('fos_user_registration_check_email');
            } else {
                $authUser = true;
                $route = $this->container->get('session')->get('sonata_basket_delivery_redirect');

                if (null !== $route) {
                    $this->container->get('session')->remove('sonata_basket_delivery_redirect');
                    $url = $this->container->get('router')->generate($route);
                } else {
                    $url = $this->container->get('session')->get('sonata_user_redirect_url');
                }
            }

            $this->setFlash('fos_user_success', 'registration.flash.user_created');

            $response = new RedirectResponse($url);

            if ($authUser) {
                $this->authenticateUser($user, $response);
            }

            return $response;
        }

        $this->container->get('session')->set('sonata_user_redirect_url', $this->container->get('request')->headers->get('referer'));

        return $this->container->get('templating')->renderResponse('MpShopBundle:Frontend:registration.html.'.$this->getEngine(), array(
            'form' => $form->createView(),
        ));
    }

UPDATE UPDATE

The only way I can make this work is by enabling the email confirmation, but not sending the email to the user. 我能够完成这项工作的唯一方法是启用电子邮件确认,但不将电子邮件发送给用户。 This way a user cant confirm his account and has to wait until the admin will enable it. 这样,用户无法确认其帐户,并且必须等到管理员启用它。 But I dont whant to have such problems.. There should be a way to disable them normally. 但我不想有这样的问题..应该有一种方法来正常禁用它们。

FIXED 固定

I finally figured it out. 我终于弄明白了。 All you had to to is in the registerAction in the code where check if i need email confirmation, in the else statement to set it to enabled and update the user. 所有你必须在代码中的registerAction中检查我是否需要电子邮件确认,在else语句中将其设置为启用并更新用户。 If you dont update it it wont work. 如果你不更新它不会工作。

if ($process) {
            $user = $form->getData();

            $authUser = false;
            if ($confirmationEnabled) {
                $this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
                $url = $this->container->get('router')->generate('fos_user_registration_check_email');
            } else {
                $user->setEnabled(false);
                $userManager = $this->container->get('fos_user.user_manager');
                $userManager->updateUser($user);
                $route = $this->container->get('session')->get('sonata_basket_delivery_redirect');

                if (null !== $route) {
                    $this->container->get('session')->remove('sonata_basket_delivery_redirect');
                    $url = $this->container->get('router')->generate($route);
                } else {
                    $url = $this->container->get('session')->get('sonata_user_redirect_url');
                }
            }

You can try using @ORM\\HasLifecycleCallbacks() in your user entity then you define a method, like: 您可以尝试在用户实体中使用@ORM\\HasLifecycleCallbacks() ,然后定义一个方法,如:

/**
* @ORM\PrePersist
*/
public function yourMethod(){
    $this->setEnabled(false);
}

This works for me. 这适合我。

The line $this->setEnabled = true; $this->setEnabled = true; should probably be $this->setEnabled(true); 应该是$this->setEnabled(true); .

In any case, I don't think this will work because the user is enabled in \\FOS\\UserBundle\\Controller\\RegistrationController on lines 43 and 114 explicitly with: 在任何情况下,我认为这不会起作用,因为用户在第43114行的\\FOS\\UserBundle\\Controller\\RegistrationController明确启用:

$user->setEnabled(true);

Therefore I expect that there is no configuration setting for this, so you would have to override the RegistrationController with your own logic to set a user as disabled. 因此,我希望没有配置设置,因此您必须使用自己的逻辑覆盖RegistrationController以将用户设置为禁用。

This is pretty straightforward, here's the relevant documentation: 这非常简单,这是相关的文档:

There's also some information in the documentation about hooking into controllers using events, which could be a viable alternative approach: 文档中还有一些关于使用事件挂钩到控制器的信息,这可能是一种可行的替代方法:

Hope this helps :) 希望这可以帮助 :)

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

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