Reputation: 1231
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
public function __construct()
{
parent::__construct();
// your own logic
$this->enabled = false;
//and
$this->setEnabled(false);
}
P.S Together with FOS i am using the SonataUserBundle.. This is my registerAction:
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
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. 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');
}
}
Upvotes: 4
Views: 3099
Reputation: 61
You can try using @ORM\HasLifecycleCallbacks()
in your user entity then you define a method, like:
/**
* @ORM\PrePersist
*/
public function yourMethod(){
$this->setEnabled(false);
}
This works for me.
Upvotes: 2
Reputation: 14136
The line $this->setEnabled = true;
should probably be $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:
$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.
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 :)
Upvotes: 1