src/Modules/TangibleAsset/Security/AssetVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Modules\TangibleAsset\Security;
  3. use App\Modules\TangibleAsset\Entity\AssetDetails;
  4. use App\Modules\TangibleAsset\Service\AssetAccessService;
  5. use App\Modules\User\Entity\UserContract;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\Security;
  9. class AssetVoter extends Voter
  10. {
  11.     const CREATE_ASSET 'create_asset';
  12.     const UPDATE_ASSET 'update_asset';
  13.     const DELETE_ASSET 'delete_asset';
  14.     static $access = [
  15.         self::CREATE_ASSET,
  16.         self::UPDATE_ASSET,
  17.         self::DELETE_ASSET,
  18.     ];
  19.     private $security;
  20.     /**
  21.      * @var AssetAccessService
  22.      */
  23.     private $assetAccessService;
  24.     public function __construct(Security $securityAssetAccessService $assetAccessService)
  25.     {
  26.         $this->security $security;
  27.         $this->assetAccessService $assetAccessService;
  28.     }
  29.     /**
  30.      * Determines if the attribute and subject are supported by this voter.
  31.      *
  32.      * @param string $attribute An attribute
  33.      * @param mixed  $subject   The subject to secure, e.g. an object the user wants to access or any other PHP type
  34.      *
  35.      * @return bool True if the attribute and subject are supported, false otherwise
  36.      */
  37.     protected function supports($attribute$subject)
  38.     {
  39.         if (!in_array($attributeself::$access)) {
  40.             return false;
  41.         }
  42.         return true;
  43.     }
  44.     /**
  45.      * Perform a single access check operation on a given attribute, subject and token.
  46.      * It is safe to assume that $attribute and $subject already passed the "supports()" method check.
  47.      *
  48.      * @param string         $attribute
  49.      * @param mixed          $subject
  50.      *
  51.      * @param TokenInterface $token
  52.      *
  53.      * @return bool
  54.      */
  55.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  56.     {
  57.         $loginedUser $token->getUser();
  58.         if (!$loginedUser instanceof UserContract) {
  59.             // the user must be logged in; if not, deny access
  60.             return false;
  61.         }
  62.         $asset $subject;
  63.         switch ($attribute) {
  64.             case self::CREATE_ASSET:
  65.                 return $this->canCreate();
  66.             case self::UPDATE_ASSET:
  67.                 return $this->canUpdate($asset$loginedUser);
  68.             case self::DELETE_ASSET:
  69.                 return $this->canDelete($asset$loginedUser);
  70.         }
  71.     }
  72.     private function canCreate()
  73.     {
  74.         if ($this->security->isGranted('ROLE_CIRCLE_MEMBER_ADMINISTRATOR') ||
  75.             $this->security->isGranted('ROLE_PARTNER_ADMINISTRATOR')
  76.         ) {
  77.             return true;
  78.         }
  79.         return false;
  80.     }
  81.     private function canUpdate(AssetDetails $assetUserContract $loggedInUser)
  82.     {
  83.         if ($this->assetAccessService->userHasAccess($asset$loggedInUser)) {
  84.             return true;
  85.         }
  86.         return false;
  87.     }
  88.     private function canDelete(AssetDetails $assetUserContract $loggedInUser)
  89.     {
  90.         if ($this->security->isGranted('ROLE_ADMIN') ||
  91.             $this->assetAccessService->userHasAccess($asset$loggedInUser)
  92.         ) {
  93.             return true;
  94.         }
  95.         return false;
  96.     }
  97. }