src/Security/RecipeToSyncVoter.php line 18

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