GroupOwnedOrPublicValidator.php 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace Muzich\CoreBundle\Validator;
  3. use Symfony\Component\Validator\ConstraintValidator;
  4. use Symfony\Component\Validator\Constraint;
  5. use Doctrine\ORM\EntityManager;
  6. use Symfony\Component\Security\Core\SecurityContext;
  7. class GroupOwnedOrPublicValidator extends ConstraintValidator
  8. {
  9. private $entityManager;
  10. private $security_context;
  11. public function __construct(EntityManager $entityManager, SecurityContext $security_context)
  12. {
  13. $this->entityManager = $entityManager;
  14. $this->security_context = $security_context;
  15. }
  16. /**
  17. * Ce n'est valide que si
  18. * * Le groupe est open
  19. * * Le groupe appartient a l'user
  20. * * On a pas précisé de groupe
  21. *
  22. * @param int $value
  23. * @param Constraint $constraint
  24. * @return boolean
  25. */
  26. public function isValid($value, Constraint $constraint)
  27. {
  28. if ($value)
  29. {
  30. $user = $user = $this->security_context->getToken()->getUser();
  31. $group = $this->entityManager->getRepository('MuzichCoreBundle:Group')
  32. ->findOneById($value)
  33. ;
  34. if (!$group)
  35. {
  36. //$this->setMessage('Le groupe est invalide'); UPGRADE 2.1
  37. $this->context->addViolation('group_invalid');
  38. return false;
  39. }
  40. if (!$group->userCanAddElement($user))
  41. {
  42. //$this->setMessage('Le groupe est invalide'); UPGRADE 2.1
  43. $this->context->addViolation('group_invalid');
  44. return false;
  45. }
  46. }
  47. return true;
  48. }
  49. }