Friday 15 May 2015

php - Symfony2 - Force User to complete profile -


I am struggling to not find a reusable and rigorous solution for this problem.

On my registration page, I ask the user as little information as possible to sign up, after the user completes that step, the user is created.

Before they can use other parts of the application, I want them to complete their profiles. (I know that it is alternatively better, but it is mandatory to use this case).

Already a flag is set on a false flag when I sign up, but I think it's a bad idea to complete this profile in every controller. The way.

Is there another oo method in Symphony by using an Event Subscriber or Custom Roll (USER_PENDING) to solve this problem? How would I set up additional firewalls for that role?

Anyway, I have already done it!

1) Make your listener first:

  & lt; Php namespace Sybio \ Bundle \ WebsiteBundle \ Services \ Listenener; Use Symfony \ Component \ Dependency Injection \ Container; Use Symfony \ Component \ Routing \ Router; Use Symfony \ Component \ HttpKernel \ Event \ GetResponseEvent; Use Symfony \ Component \ HttpFoundation \ RedirectResponse; Use Symfony \ Component \ HttpKernel \ HttpKernel; / ** * User listener: Redirect user based on your informations * / class UserListener {/ ** * @ container * / private $ container; / ** * @ Router * / Private $ Router; / ** * manufacturer * * @param container $ container * @param router router * / public function __construct (container $ container, router $ router) {$ this- & gt; Container = $ Container; $ This- & gt; Router = $ router; } Public Function onKernelRequest (GetResponseEvent $ event) {if ($ this-> container-> get ('security.context') -> getToken () & amp; amp; $-> this-> Container- & gt; get ('security.context') - & gt; isGranted ('ROLE_PENDING') & amp; $ event & gt; getRequest () -> attributes- & gt; mill ('_ = 'Sybio \ bundle \ WebsiteBundle \ Controller! \ UserController :: fullInformationAction' & amp; $ event & gt; getRequest () - & gt; get, attributes-> ('_controller') ! = '\ WebsiteBundle Controller \ SecurityController \ :: logoutAction' Sybio & Bundle; & amp; HttpKernel :: MASTER_REQUEST == $ Event & gt; getRequestType ()) {$ Event & gt; SetResponse) (New RedirectResponse generated ($ this-> gt router- ('sybiowebsite_user_complete_information'));   

Note:

  1. Check that the user Is logged in and then there is ROLE_PENDING (which means that incomplete profile)
  2. Check if the user is not already in action, where he can complete his profile, otherwise you will have an unlimited redirect loop !
  3. Allow user to logout (optional, of course)
  4. Check Whether you are in the Master's request, otherwise it will also be implemented for ESI, action (tribder in the render_controller) etc ... and it will crash!

    If all the terms are true , The listener redirects to the full profile page!

    2) Register your listener:

      # services.yml or config.yml Sybio .user_listener: Category: \ WebsiteBundle \ Sybio \ bundle services \ Listener \ UserListener Logic: - @service_container - @router Tags: - {name: kernel.event_listener, event: kernel.request, method: on KernelRequest}   

    3) The last step is to define that a user profile is incomplete and give it ROLE_PENDING to do so:

      // unit - & gt; User.php: / ** * Get Roles (Security) * * Return Array * / Public Function getRoles () {$ roles = array (); // Manage your roles ... if (! $ This-> Email) {// or you want $ roles [] = 'ROLE_PENDING'; } Return $ Roles; }   

    That's it!

No comments:

Post a Comment