src/Security/LineTypeGroupVoter.php line 16

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