- <?php
- namespace ForumBundle\Security;
- use Symfony\Component\Security\Core\Authorization\Voter\Voter;
- use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
- use CoreBundle\Entity\Tutor;
- use CoreBundle\Entity\Student;
- use CoreBundle\Entity\Post;
- class PostVoter extends Voter
- {
-     public function __construct()
-     {
-     }
-     protected function supports($attribute, $subject)
-     {
-         // if the attribute isn't one we support, return false
-         // osef view car on vérifie déjà qu'il a accès à la catégorie correspondante
-         if (! in_array($attribute, array('edit', 'delete'))) {
-             return false;
-         }
-         // only vote on Post objects inside this voter
-         if (! $subject instanceof Post) {
-             return false;
-         }
-         return true;
-     }
-     /**
-      * Droit d'accès à un post $subject
-      *
-      * {@inheritDoc}
-      * @see \Symfony\Component\Security\Core\Authorization\Voter\Voter::voteOnAttribute()
-      */
-     protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
-     {
-         $user = $token->getUser();
-         $post = $subject;
-         // les tables tutor et student n'ont pas évoluées ensemble, elles présentent pas mal de différences.
-         if ($user instanceof Tutor) {
-             // si c'est un admin, c'est réglé
-             if ($user->getAdmin()) {
-                 return true;
-             }
-             //sinon
-             // les tutors n'effacent rien
-             if ($attribute == 'delete') {
-                 return false;
-             }
-             // si c'est son post, ok (pour edit)
-             if ($post->getTutor() == $user) {
-                 return true;
-             }
-         } elseif ($user instanceof Student) {
-             // les étudiants n'effacent rien
-             if ($attribute == 'delete') {
-                 return false;
-             }
-             // si c'est son post, ok (pour edit)
-             if ($post->getStudent() == $user) {
-                return true;
-             }
-         }
-         return false;
-     }
- }