Who goes there?

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

Topics covered: Firewallform_loginaccess_control

Scandal at the tavern: a customer found the /kitchen/new-dish page and added a “Pippin's Stew, free of charge” to the menu. The kitchen must be closed to the public: only Bruna gets in.

Symfony's Security component is built around a firewall (who are you?) and access rules (are you allowed?).

Your mission

In config/packages/security.yaml:

  1. Declare an in-memory user: bruna, role ROLE_CUISINIER (cuisinier = cook), password cauldron… hashed, never in plain text. The console computes it for you: security:hash-password cauldron.
  2. In the main firewall, enable form login (page app_connexion, connexion = login) and logout (route app_deconnexion, back to the home page).
  3. Reserve everything starting with /kitchen for the ROLE_CUISINIER role.

In SecuriteController::connexion() (Securite = security), pass the last username entered and the last login error to the template (AuthenticationUtils service), as dernier_identifiant (last username) and erreur (error). The login page and the navigation (Log in / Log out) are ready.

Reminder

You don't write the code that checks the password: the firewall intercepts the form sent to check_path, compares the hashed password, opens the session, then sends you back where you were going.