To each their own dishes

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

Topics covered: VoterIsGranted with a subjectCurrentUser

Lugh, a new cook, “improved” Bruna's pie by adding anchovies. Bruna is furious. New rule in the kitchen: to each their own dishes. A cook only edits the dishes they invented, and the boss can edit everything.

A role is no longer enough: the right depends on the object (this particular dish) and on who is asking. That's the job of a Voter.

Your mission

  1. Every dish now remembers its creator (Plat::$createur, already mapped). In nouveauPlat(), make the logged-in staff member the new dish's creator.
  2. Write PlatVoter, which decides on the PLAT_MODIFIER attribute (“edit dish”) for a Plat:
    • nobody logged in: denied;
    • the boss: granted;
    • the dish's creator: granted;
    • everyone else: denied.
  3. Protect modifierPlat() with this voter, and only display “Edit” on the dish page if you are allowed to edit this dish.

The historic dishes on the menu have no creator: only the boss can edit them.

Reminder

A voter answers two questions: “do I handle this?” (supports) and, if so, “is it allowed?” (voteOnAttribute). Symfony asks every voter. The ones that don't feel concerned abstain.