Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. namespace App\EventListener;
  2.  
  3. use App\Entity\Counting;
  4. use App\Entity\User;
  5. use Doctrine\ORM\Event\PrePersistEventArgs;
  6. use Symfony\Component\Security\Core\Security;
  7.  
  8. class CountingEventListener
  9. {
  10. /**
  11. * @var Security
  12. */
  13. private $security;
  14.  
  15. /**
  16. * @param Security $security
  17. */
  18. public function __construct(Security $security)
  19. {
  20. $this->security = $security;
  21. }
  22.  
  23. public function prePersist(Counting $counting, PrePersistEventArgs $event)
  24. {
  25. if (null === $user = $this->security->getUser()) {
  26. return;
  27. }
  28.  
  29. if ($this->security->isGranted('ROLE_ADMIN')) {
  30. return;
  31. }
  32.  
  33. if ($this->security->isGranted('ROLE_MANAGER')) {
  34. $counting->setManager($user);
  35.  
  36. return;
  37. }
  38. if ($this->security->isGranted('ROLE_USER')) {
  39. $counting->setPolicyHolder($user);
  40. }
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement