Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
20.00% |
2 / 10 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| Movie1430Voter | |
20.00% |
2 / 10 |
|
0.00% |
0 / 2 |
32.09 | |
0.00% |
0 / 1 |
| supports | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
4.59 | |||
| voteOnAttribute | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Security\Voter; |
| 4 | |
| 5 | use App\Entity\Movie; |
| 6 | use DateTime; |
| 7 | use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
| 8 | use Symfony\Component\Security\Core\Authorization\Voter\Voter; |
| 9 | use Symfony\Component\Security\Core\User\UserInterface; |
| 10 | |
| 11 | class Movie1430Voter extends Voter |
| 12 | { |
| 13 | /** |
| 14 | * Est ce que moi (la classe) sait gérer ce droit ? |
| 15 | * |
| 16 | * @param string $attribute le nom du droit demandé |
| 17 | * @param $subject un objet pour contextualisé le droit |
| 18 | * @return boolean |
| 19 | */ |
| 20 | protected function supports(string $attribute, $subject): bool |
| 21 | { |
| 22 | // replace with your own logic |
| 23 | // https://symfony.com/doc/current/security/voters.html |
| 24 | // si le droit se nomme MOVIE_1430 |
| 25 | // ET |
| 26 | // le contexte est un objet Movie |
| 27 | if ($attribute === "MOVIE_1430" && ($subject instanceof Movie || $subject === null)){ |
| 28 | return true; |
| 29 | } else { |
| 30 | return false; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Est ce que tu as le droit ? |
| 36 | * |
| 37 | * @param string $attribute |
| 38 | * @param $subject |
| 39 | * @param TokenInterface $token |
| 40 | * @return boolean |
| 41 | */ |
| 42 | protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool |
| 43 | { |
| 44 | $user = $token->getUser(); |
| 45 | // if the user is anonymous, do not grant access |
| 46 | if (!$user instanceof UserInterface) {return false;} |
| 47 | // $user->getRoles(); |
| 48 | |
| 49 | $dateDuJour = new DateTime("now"); |
| 50 | // ? https://www.php.net/manual/fr/datetime.format.php |
| 51 | // $heure => 810, 1430 |
| 52 | $heure = $dateDuJour->format("Gi"); |
| 53 | if ($heure > 1500){ |
| 54 | // Tu n'a pas le droit |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | // tu as le droit |
| 59 | return true; |
| 60 | } |
| 61 | } |