2

I have a Symfony 2.8 based project, I installed Sonata Admin Bundle and User Bundle and all is working perfectly.

Now I have another constraint that I want to add. When I user sign up, I don't want to give him access immediately to the application. Instead, the admin should be able to check the user datain before, and then grant him the access.

Example:

PersonA sign up giving his email and other data but he shouldn't be able to login. When the admin checks the data that PersonA has entered and sees that he can be allowed to access the application, he can grant him that possibility and therefor PersonA can login freely.

I'm not well accustomed to the Sonata bundles world, so I'm open to any ideas you can give me.

Thanks a lot !

Auranx
  • 51
  • 1
  • 7

1 Answers1

2

Add the following in your User entity :

public function __construct()
{
    parent::__construct();
    $this->enabled = false;
}

If it doesn't work, add this in your Resources/config/doctrine/User.orm.xml

    <!-- ... -->
    <lifecycle-callbacks>
        <lifecycle-callback type="prePersist" method="disable" />
    </lifecycle-callbacks>

Or if it's User.orm.yml :

lifecycleCallbacks:
    prePersist: [disable]

And add the following in your User entity :

public function disable()
{
    $this->setEnabled(false);
}

Hope it works.

chalasr
  • 12,971
  • 4
  • 40
  • 82
  • Thanks for the quick answer. But I think that I'm missing something. I went to Application\Sonata\UserBundle\Entity\User and I added the given property, when running the doctrine command it says that there's nothing to change, and in fact, enabled is already in the fos_user_user table, and even if I added the $enabled = false; the registred user is enabled by default. Did I overrid the $enabled property in the right place ? – Auranx Feb 02 '16 at 21:56
  • Yes I have the Resources/config/doctrine folder and no @ORM\Entity – Auranx Feb 02 '16 at 22:02
  • See my edit, try first the construct solution, and then the mapping :) – chalasr Feb 02 '16 at 22:11
  • I've added more changes :) Sorry for the number of try. The last will be override the User admin and call setEnabled on prePersist – chalasr Feb 02 '16 at 22:27
  • Thanks a lot @chalasr, it's working now. But I still have a tiny problem :D .. When the user sign up, he's directly logged in after that, even if the account is disabled, but once he signs out, he can never login again until the account is enabled. What can I do to avoid thaht problem ? – Auranx Feb 02 '16 at 22:34
  • what about just starting the user with their basic `ROLE_USER` role that has very limited access, then have another role once the user is enabled that the admin sets? – Jason Roman Feb 02 '16 at 22:41
  • 1
    @Auranx For your automatic login, I think you have to override the default registration controller and method, and do the same as parent but removing the login part or make a manual logout/redirection. – chalasr Feb 02 '16 at 22:44
  • @JasonRoman You are right but he is using the registration form that is just overridden by sonata from FOSUB, also he have to do the same thing for the role but just a bit more complicated. Otherwise , he have to make an override of the registration method and call a setter manually. – chalasr Feb 02 '16 at 22:45
  • @JasonRoman : Thanks for the answer, but the needed constraint says that the user should not login at all. The solution proposed by chaslar fits my case perfectly, I just need now to disable the automatic login after a successful registration. – Auranx Feb 02 '16 at 22:45
  • 1
    @chalasr : Okey thank you, I will look into that separatly. Thank you for your effort and your time, it was very thoughtful of you. – Auranx Feb 02 '16 at 22:47