Bonjour,
Je vais un backend en Symfony 5.2 avec Api Platform. J'ai créé un voter pour autoriser sur condition les utilisateurs à effectuer les opérations qui correspondent à leurs permissions sur l'entité Business. Hier tout marchait bien. Quand dans le voter je fais dd($attribute) ou dd($subject), j'ai le résultat correspondant. Aujourd'hui je ne comprends pas ce qui s'est passé et mes voters ne marchent plus. $subject renvoie maintenant null. Voici mes codes:
// Business.php
BusinessVoter.php
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 <?php /** * @ApiResource( * collectionOperations={ * "post"={ * "denormalization_context"={ * "groups"={"post-business"} * }, * "security"="is_granted('ROLE_CREATE_REALESTATE_BUSINESS', object)" * } * } * ) */ class Business { /** * @ORM\Id() * @ORM\GeneratedValue() * @ORM\Column(type="integer") * @Groups({"get-business"}) */ private $id; /** * @ORM\Column(type="string", length=255) * @Groups({"get-business"}) */ private $name; // ..... }
Je remercie d'avance pour vos coups de pousse pour résoudre ce problème.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 <?php namespace App\Security\Voter; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Authorization\Voter\Voter; use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Security\Core\Security; use App\Repository\BusinessAdminRepository; class BusinessVoter extends Voter { private $security; private $adminRepository; public function __construct(Security $security, BusinessAdminRepository $adminRepository) { $this->security = $security; $this->adminRepository = $adminRepository; } protected function supports($attribute, $subject) { dd($subject); return in_array($attribute, [ "ROLE_CREATE_REALESTATE_BUSINESS", // ... ]) && $subject instanceof \App\Entity\Business; } protected function voteOnAttribute($attribute, $subject, TokenInterface $token) { // dd($subject); $user = $token->getUser(); // if the user is anonymous, do not grant access if (!$user instanceof UserInterface) { return false; } // ... (check conditions and return true to grant permission) ... switch ($attribute) { case 'ROLE_CREATE_REALESTATE_BUSINESS': if($this->security->isGranted(strtoupper($subject->getBusinessType()->getMinimumRole()))) { return true; } return false; break; // ... } return false; } }
Partager