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: Note: If all the terms are true , The listener redirects to the full profile page! 2) Register your listener: 3) The last step is to define that a user profile is incomplete and give it ROLE_PENDING to do so: That's it!
& 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'));
# 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}
// 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; }
No comments:
Post a Comment