src/ForumBundle/Security/PostVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace ForumBundle\Security;
  3. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use CoreBundle\Entity\Tutor;
  6. use CoreBundle\Entity\Student;
  7. use CoreBundle\Entity\Post;
  8. class PostVoter extends Voter
  9. {
  10.     public function __construct()
  11.     {
  12.     }
  13.     protected function supports($attribute$subject)
  14.     {
  15.         // if the attribute isn't one we support, return false
  16.         // osef view car on vérifie déjà qu'il a accès à la catégorie correspondante
  17.         if (! in_array($attribute, array('edit''delete'))) {
  18.             return false;
  19.         }
  20.         // only vote on Post objects inside this voter
  21.         if (! $subject instanceof Post) {
  22.             return false;
  23.         }
  24.         return true;
  25.     }
  26.     /**
  27.      * Droit d'accès à un post $subject
  28.      *
  29.      * {@inheritDoc}
  30.      * @see \Symfony\Component\Security\Core\Authorization\Voter\Voter::voteOnAttribute()
  31.      */
  32.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  33.     {
  34.         $user $token->getUser();
  35.         $post $subject;
  36.         // les tables tutor et student n'ont pas évoluées ensemble, elles présentent pas mal de différences.
  37.         if ($user instanceof Tutor) {
  38.             // si c'est un admin, c'est réglé
  39.             if ($user->getAdmin()) {
  40.                 return true;
  41.             }
  42.             //sinon
  43.             // les tutors n'effacent rien
  44.             if ($attribute == 'delete') {
  45.                 return false;
  46.             }
  47.             // si c'est son post, ok (pour edit)
  48.             if ($post->getTutor() == $user) {
  49.                 return true;
  50.             }
  51.         } elseif ($user instanceof Student) {
  52.             // les étudiants n'effacent rien
  53.             if ($attribute == 'delete') {
  54.                 return false;
  55.             }
  56.             // si c'est son post, ok (pour edit)
  57.             if ($post->getStudent() == $user) {
  58.                return true;
  59.             }
  60.         }
  61.         return false;
  62.     }
  63. }