2012-06-18

1. Quick overview

1.1 Goal

What I'm trying to achieve is a create/edit user tool. Editable fields are:

username (type: text)

plainPassword (type: password)

email (type: email)

groups (type: collection)

avoRoles (type: collection)

Note: the last property is not named $roles becouse my User class is extending FOSUserBundle's User class and overwriting roles brought more problems. To avoid them I simply decided to store my collection of roles under $avoRoles.

1.2 User Interface

My template consists of 2 sections:

User form

Table displaying $userRepository->findAllRolesExceptOwnedByUser($user);

Note: findAllRolesExceptOwnedByUser() is a custom repository function, returns a subset of all roles (those not yet assigned to $user).

1.3 Desired functionality

1.3.1 Add role:

1.3.2 Remove roles:

1.3.3 Save changes:

Note: ONLY existing Roles and Groups can be assigned to User. If for any reason they are not found the form should not validate.

2. Code

In this section I present/or shortly describe code behind this action. If description is not enough and you need to see the code just tell me and I'll paste it. I'm not pasteing it all in the first place to avoid spamming you with unnecessary code.

2.1 User class

My User class extends FOSUserBundle user class.

2.2 Role class

My Role class extends Symfony Security Component Core Role class.

2.3 Groups class

Since I've got the same problem with groups as with roles, I'm skipping them here. If I get roles working I know I can do the same with groups.

2.4 Controller

2.5 Custom repositories

It is not neccesary to post this since they work just fine - they return a subset of all Roles/Groups (those not assigned to user).

2.6 UserType

UserType:

2.7 RoleNameType

This form is supposed to render:

hidden Role ID

Role name (READ ONLY)

hidden module (READ ONLY)

hidden description (READ ONLY)

remove (x) button

Module and description are rendered as hidden fields, becouse when Admin removes a role from a User, that role should be added by jQuery to Roles Table - and this table has Module and Description columns.

3. Current/known Problems

3.1 Case 1: configuration as quoted above

The above configuration returns error:

But setter for ID should not be required.

First becouse I don't want to create a NEW role. I want just to create a relation between existing Role and User entities.

Even if I did want to create a new Role, it's ID should be auto-generated:

3.2 Case 2: added setter for ID property in Role entity

I think it's wrong, but I did it just to be sure. After adding this code to Role entity:

If I create new user and add a role, then SAVE... What happens is:

New user is created

New user has role with the desired ID assigned (yay!)

but that role's name is overwritten with empty string (bummer!)

Obviously, thats not what I want. I don't want to edit/overwrite roles. I just want to add a relation between them and the User.

3.3 Case 3: Workaround suggested by Jeppe

When I first encountered this problem I ended up with a workaround, the same that Jeppe suggested. Today (for other reasons) I had to remake my form/view and the workaround stopped working.

What changes in Case3 UserManagementController -> createAction:

Unfortunately this does not change anything.. the results are either CASE1 (with no ID setter) or CASE2 (with ID setter).

3.4 Case 4: as suggested by userfriendly

Adding cascade={"persist", "remove"} to mapping.

And changeing by_reference to false in FormType:

And keeping workaround code suggested in 3.3 did change something:

Association between user and role was not created

.. but Role entity's name was overwritten by empty string (like in 3.2)

So.. it did change something but in the wrong direction.

4. Versions

4.1 Symfony2 v2.0.15

4.2 Doctrine2 v2.1.7

4.3 FOSUserBundle version: 6fb81861d84d460f1d070ceb8ec180aac841f7fa

5. Summary

I've tried many diffrent approaches (above are only the most recent ones) and after hours spent on studying code, google'ing and looking for the answer I just couldn't get this working.

Any help will be greatly appreciated. If you need to know anything I'll post whatever part of code you need.

Show more