Skip to content
Advertisement

There is a way to load roles from a specific table in Symfony?

I have an app that manages all of its users inside one entity (USER), implementing the UserInterface, as recommended. Linked to it, I got the CUSTOMER and the EMPLOYEE entities: each one storing specific infos about the user.

You may ask why I’m doing like this: an EMPLOYEE can be a CUSTOMER in many WORKPLACES and may work in many WORKPLACES. In order to avoid repetition, the common info got centralized in USER and the attribute Roles in USER is always blank in db.

The goal here is to load my roles based on specific data stored in another entity (WORKPLACE) when an EMPLOYEE logs in.

My great question may live in the UserRepository: when symfony calls the repository looking for the user entered on the login form, doctrine delivers it with no roles. I put my roles and return the User to the Authenticator (the vanilla one, I havent touched it yet). After the end of every requery, Symfony checks for the user but then doctrine loads the ROLE_CLIENT (in other words – no privileges).

What I already tried and failed

The entities

JavaScript

Workplace entity

The booleans store the privileges i want to get

JavaScript

Employee Entity

JavaScript

An example USER tuple from db

JavaScript

My last resource is asking here, after some long and wasted work hours, what should I do (or what i did wrong)?

I’ve seen some other threads asking similar questions, but they’re outdated (im using the version 5): Dynamic roles in symfony Symfony 2 – Loading roles from database Symfony2 – Dynamic Role Management How to update roles in security token in Symfony 4 without re-logging in Symfony User Logout After Role Change

PS: Someone tried this approach (https://github.com/symfony/symfony/issues/12025#issuecomment-562004005) and got success in symfony5?

Advertisement

Answer

Well, turns out that the best solution for my case was manipulate the User Repository. Doctrine uses them to operate the db so I just coded my rules inside and let the framework do the rest.

The algorithm is something like the shown below:

  • Find the user using the provided $id
  • If he/she works somewhere, load the ROLE constants into the User entity
  • If he/she does not have a job, load the profile as a common user
JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement