0

In Sylius (Symfony3 bundle), I have customized the register form to add some fields, in particular 'type of account' (pro or private). According to the type, some functionalities will not be enabled. In order to do that, I was thinking about giving users different roles.

As the authentication is made by Sylius, I was wondering how to override the default behavior to set the role according to the type data ?

Thanks for your help !

wyllyjon
  • 505
  • 1
  • 5
  • 20

2 Answers2

0

Sylius has no built-in roles or rbac system - whole security configuration is done with standard Symfony security system. So if you need to differentiate functionalities based on User role, just base on $roles parameter from User model, and override Sylius security configuration with your custom firewalls, as it's said in Symfony tutorial. Hope it will help ;)

Zaleslaw
  • 161
  • 2
  • Tks Zaleslaw, but I don't understand : the ShopUser is a Sylius Entity, so I should be able to give it a role defined by myself when is created, no ? – wyllyjon Jan 18 '17 at 12:53
  • Yes, `ShopUser` is an entity, so you should be able to set it's by, for example, well defined `ChoiceType` field in form. If you want to modify it's default roles, you should override `ShopUser` entity in your app (it has one 'ROLE_USER' role by default). Just think about ``$roles`` property as any other property, make a field for it and choose it in form ;) – Zaleslaw Jan 18 '17 at 20:50
  • I am not sure I understand, but I don't want the role to be chosen in the form by the user. I already have the 'type' field in the form, and, according to the value selected, I grant some role to the user. In the register form, the model used is "Customer", and ShopUser has a "Customer" attribute. What I have done is override ShopUser, override setCustomer, and inside that method, test the 'type' attribute and call a 'addRole'. But I still have the default role and not the new ones. I don't see which other method to override to correctly add my roles – wyllyjon Jan 19 '17 at 09:32
0

What I have done :

In Sylius, there is an event sylius.customer.post_register fired after registration. I have created a listener (defined in services.yml) :

app.registration_listener:
        class: AppBundle\EventListener\RegistrationListener
        tags:
            - { name: kernel.event_listener, event: sylius.customer.post_register, method: setUserRole }
        arguments:
            - "@sylius.manager.shop_user"

The ShopUserManager is passed as an argument to the setUserRole method.

public function __construct(ObjectManager $userManager) {
        $this->userManager = $userManager;
    }

In the listener, I get the $user object as the 'subject' of the event :

public function setUserRole(GenericEvent $event)
    {
        $customer = $event->getSubject();
        $user = $customer->getUser();
        ....
        $this->userManager->persist($user);
        $this->userManager->flush();
    }

Then I can modify the $user (add my role) and save it with the ShopUserManager.

wyllyjon
  • 505
  • 1
  • 5
  • 20