src/Security/UserSettingVoter.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\User;
  8. use App\Entity\UserSetting;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  11. use LogicException;
  12. /**
  13.  * Class CustomerVoter.
  14.  */
  15. class UserSettingVoter 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 UserSetting) {
  36.             return false;
  37.         }
  38.         return true;
  39.     }
  40.     /**
  41.      * Vote on attribute.
  42.      *
  43.      * @param string         $attribute
  44.      * @param UserSetting    $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.         return $subject->getUser() && $subject->getUser()->getId() === $user->getId();
  57.     }
  58. }