src/Security/MeasurementVoter.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\License;
  8. use App\Entity\Measurement;
  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 MeasurementVoter.
  14.  */
  15. class MeasurementVoter extends Voter
  16. {
  17.     const SHOW 'show';
  18.     const EDIT 'edit';
  19.     /**
  20.      * Supports.
  21.      *
  22.      * @param string  $attribute
  23.      * @param License $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::EDIT])) {
  31.             return false;
  32.         }
  33.         if (!$subject instanceof Measurement) {
  34.             return false;
  35.         }
  36.         return true;
  37.     }
  38.     /**
  39.      * Vote on attribute.
  40.      *
  41.      * @param string         $attribute
  42.      * @param Measurement    $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.         return $user->getActiveCustomer() && $user->getActiveCustomer()->getId() === $subject->getCustomer()->getId();
  58.     }
  59. }