src/Security/LineTypeVoter.php line 17

Open in your IDE?
  1. <?php
  2. /**
  3.  * @license SILK SOFTWARE HOUSE SP Z O O
  4.  */
  5. namespace App\Security;
  6. use App\Entity\LineType;
  7. use App\Entity\LineTypeGroup;
  8. use App\Entity\User;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  11. /**
  12.  * Class LineTypeVoter.
  13.  */
  14. class LineTypeVoter extends Voter
  15. {
  16.     const SHOW 'show';
  17.     const NEW = 'new';
  18.     const EDIT 'edit';
  19.     const DELETE 'delete';
  20.     /**
  21.      * Supports.
  22.      *
  23.      * @param string $attribute
  24.      * @param object $subject
  25.      *
  26.      * @return bool
  27.      */
  28.     protected function supports($attribute$subject)
  29.     {
  30.         // if the attribute isn't one we support, return false
  31.         if (!in_array($attribute, [self::SHOWself::NEW, self::EDITself::DELETE])) {
  32.             return false;
  33.         }
  34.         return $subject instanceof LineType;
  35.     }
  36.     /**
  37.      * Vote on attribute.
  38.      *
  39.      * @param string         $attribute
  40.      * @param LineType       $subject
  41.      * @param TokenInterface $token
  42.      *
  43.      * @return bool
  44.      */
  45.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  46.     {
  47.         $user $token->getUser();
  48.         if (!$user instanceof User) {
  49.             // the user must be logged in; if not, deny access
  50.             return false;
  51.         }
  52.         if ($user->hasRole('ROLE_ADMIN')) {
  53.             return true;
  54.         }
  55.         if (!$subject->getId() && self::NEW === $attribute) {
  56.             return true;
  57.         }
  58.         return $subject->getLineTypeGroup() && $user->getActiveCustomer()
  59.             && $subject->getLineTypeGroup()->getCustomer()
  60.             && $subject->getLineTypeGroup()->getCustomer()->getId() === $user->getActiveCustomer()->getId();
  61.     }
  62. }