The staff register

Exercise 2 of 6 in chapter 4 · 200 XP · 5 objectives checked by tests

Topics covered: UserInterfaceEntity providerHashing

Bruna no longer works alone: Gorm wants to get into the kitchen too, and he has hired Pippin (to wait tables). Declare every staff member in security.yaml, and redeploy at every hire? Unthinkable. The accounts are going to live in the database.

To Symfony, a user is simply an object that implements UserInterface: an identifier and roles. And if it has a password, it also implements PasswordAuthenticatedUserInterface.

Your mission

  1. Turn the Employe entity (staff member) into a Symfony user with a password:
    • you log in with your email;
    • every logged-in staff member has at least the ROLE_USER role, on top of the stored roles.
  2. In security.yaml, replace the in-memory user with a Doctrine provider: staff members are Employe objects, looked up by their email.

The app:embaucher command (hire the staff, already written) creates three accounts, with passwords hashed by UserPasswordHasherInterface. It fails as long as Employe is not a user: run it again in the console afterwards, it displays the demo accounts.

Reminder

Symfony never stores a password in plain text. hashPassword() uses the algorithm from security.yaml (auto: bcrypt or argon2), and the firewall checks it at login.