Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. <?php
  2. // src/Security/Authorization/Voter/AbstractResourceVoter.php
  3. namespace App\Security\Authorization\Voter;
  4.  
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7.  
  8. abstract class AbstractResourceVoter extends Voter
  9. {
  10. const CREATE = 'ACTION_POST';
  11.  
  12. const READ = 'ACTION_GET';
  13.  
  14. const EDIT = 'ACTION_PUT';
  15.  
  16. const DELETE = 'ACTION_DELETE';
  17.  
  18. protected function supports($attribute, $subject)
  19. {
  20. if(!in_array($attribute, [
  21. self::CREATE,
  22. self::READ,
  23. self::EDIT,
  24. self::DELETE,
  25. ]))
  26. {
  27. return false;
  28. }
  29.  
  30. return true;
  31. }
  32.  
  33. protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
  34. {
  35. $user = $token->getUser();
  36.  
  37. if(!$user instanceof User)
  38. {
  39. return false;
  40. }
  41.  
  42. switch($attribute)
  43. {
  44. case self::CREATE:
  45. return $this->canCreate($subject, $token);
  46. case self::READ:
  47. return $this->canView($subject, $token);
  48. case self::EDIT:
  49. return $this->canEdit($subject, $token);
  50. case self::DELETE:
  51. return $this->canDelete($subject, $token);
  52. }
  53.  
  54. throw new \LogicException('');
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement