I used SonataUser with FOSUser to manage my users and created a custom field company
to attach each one to a given company.
Now I'd simply need to give users the ability to manage only users attached to the same company:
user1 company1
user2 company1
user3 company2
user4 company2
Example: user1 should be able to list/edit only user1 & user2
Should I use ACLs ?
Can you point me to the right direction or tutorial to customize SonataUser for this purpose ?
Yes ACL is the way to go. create a CompanyVoter implementing VoterInterface and check if the user is on the same company inside it's vote() method.
the cookbook entry "How to implement your own Voter to blacklist IP Addresses" gives a good introduction.
change your access-decision-manager's strategy to 'unanimous'. This means if only one voter denies access (e.g. the CompanyVoter), access is not granted to the end user.
Now create your Voter
Finally register the voter as as a service
... now use it in your twig template
or in your controller ...
or using JMSSecurityExtraBundle ...
As I didn't need ACLs here, (only voters) I used the role security handler of sonata.
But I had issues using it because its default implementation of
isGranted()
doesn't pass the current object to the voter.So I had to extend it, check my monologue in this github issue for more detail.
By the way, my PR was accepted about this issue