Users registration - adding a ROLE

Discuss Symfony 2 bundles here

Moderators: tiagojsag, dcobalt

Users registration - adding a ROLE

Postby foxx » Thu Mar 28, 2013 12:47 am

I'm a beginner in sf2 and I have a following problem: I'm using FOSUserBundle for registering users. Everything works perfectly but now I want to assign some ROLE for users during the registration. The problems are:

1. I don't want users to choose it, I want to add one role.
2. I don't want them to see explicity field with this role (but it could appear in HTML as a hidden field)

My first try was to override the form. But if I add new field:
$builder->add('roles', 'hidden', array('data' => 'ROLE_DUDE'));
it isn't enogh because after submitting the form I get an error because setRoles() wants and array. I don't know how could I pass an array with hidden field so I had to give up.

Second try was with RegistrationListener but I failed as it was complicated and also I guess there have to be more simple way.

I failed because of I can't find this file: "All events can be found in the constants of the FOS\UserBundle\FOSUserEvents class." and I get the error: Error: Class 'FOS\UserBundle\FOSUserEvents' not found


I would appreciate any help in this problem.
foxx
Junior Member
 
Posts: 18
Joined: Mon Apr 06, 2009 1:07 am

Re: Users registration - adding a ROLE

Postby althaus » Thu Mar 28, 2013 5:01 pm

althaus
Faithful Member
 
Posts: 605
Joined: Tue Oct 18, 2011 2:20 pm
Location: Germany

Re: Users registration - adding a ROLE

Postby foxx » Thu Mar 28, 2013 5:24 pm

Thank you. So you suggesting overriding controllers instead of using events - ok.

But I also have further questions: I can see that my version of FOSUB is 1.3.1 and events are not supported. I installed the bundle using composer "php composer.phar update friendsofsymfony/user-bundle" - and this installed 1.3.1 version. Is this possible to install 2.0 version this way?

My Symfony version is 2.2 and I have read the documentation of bundle for 2.x version. I used the command above so I don't get why it installed older version of the bundle, not fully adequate for documentation...
foxx
Junior Member
 
Posts: 18
Joined: Mon Apr 06, 2009 1:07 am

Re: Users registration - adding a ROLE

Postby althaus » Wed Apr 03, 2013 6:59 pm

foxx wrote:Thank you. So you suggesting overriding controllers instead of using events - ok.

I wouldn't put that into stone... it really depends on your needs. I tend to modify FOSUserBundle a lot so I prefer the override.


Is this possible to install 2.0 version this way?

There's not 2.x of FOSUserBundle? Its "master" branch is always matching the current Symfony2 master. The tagged versions may have specific requirements for minimal Symfony versions.

What's getting installed by composer depends on your composer.json file.
althaus
Faithful Member
 
Posts: 605
Joined: Tue Oct 18, 2011 2:20 pm
Location: Germany

Re: Users registration - adding a ROLE

Postby foxx » Thu Apr 04, 2013 3:09 am

Line in my composer.json is, just like the docs says:
"friendsofsymfony/user-bundle": "*"
and this gets me version 1.3.1

After changing it to "friendsofsymfony/user-bundle": "2.*" I get:

php composer.phar update friendsofsymfony/user-bundle
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

Problem 1
- The requested package friendsofsymfony/user-bundle 2.* could not be found.

Potential causes:
- A typo in the package name
- The package is not available in a stable-enough version according to your minimum-stability setting
see <https://groups.google.com/d/topic/composer-dev/_g3ASeIFlrc/discussion> for more details.


Why can't I get 2.* version of FOSUserBundle?
foxx
Junior Member
 
Posts: 18
Joined: Mon Apr 06, 2009 1:07 am

Re: Users registration - adding a ROLE

Postby althaus » Fri Apr 05, 2013 5:14 pm

foxx wrote:Why can't I get 2.* version of FOSUserBundle?

I don't wanna repeat myself, but there is no 2.x version of that bundle.

What makes you wanna use 2.x? What docs state to do this?
althaus
Faithful Member
 
Posts: 605
Joined: Tue Oct 18, 2011 2:20 pm
Location: Germany

Re: Users registration - adding a ROLE

Postby foxx » Fri Apr 05, 2013 5:42 pm

Ok, I get it now. I guess I pick up this idea of 2.0 version from some posts, like "First thing I did was updating FOSUserBundle to 2.0.*@dev in composer.json, because I was using v1.3.1, which doesn't implement the FOSUserEvents class.".

And information that I was missing was how to install master version, answer is:

Code: Select all
"friendsofsymfony/user-bundle": "dev-master"


Thanks!
foxx
Junior Member
 
Posts: 18
Joined: Mon Apr 06, 2009 1:07 am

Re: Users registration - adding a ROLE

Postby foxx » Mon Apr 08, 2013 2:46 pm

althaus wrote:What makes you wanna use 2.x? What docs state to do this?


One more place that makes me thinking about 2.x version is FOSUserBundle documentation on GitHub:
https://github.com/FriendsOfSymfony/FOS ... r_email.md

Quote:
As of the version 2.0, the bundle provides a built-in user provider implementation (...)
foxx
Junior Member
 
Posts: 18
Joined: Mon Apr 06, 2009 1:07 am

Re: Users registration - adding a ROLE

Postby althaus » Tue Apr 09, 2013 6:13 pm

Great to hear you got your stuff working.

My guess for that 2.0 would be just a mistake in the documentation. The only versioning rule at the moment is that FOB 1.2.x is for Symfony 2.0.x.
althaus
Faithful Member
 
Posts: 605
Joined: Tue Oct 18, 2011 2:20 pm
Location: Germany


Return to Bundles discussion

Who is online

Users browsing this forum: No registered users and 1 guest