src/FMT/Application/Voter/TransactionVoter.php line 16

Open in your IDE?
  1. <?php
  2. /**
  3.  * Author: Anton Orlov
  4.  * Date: 16.05.2018
  5.  * Time: 10:30
  6.  */
  7. namespace FMT\Application\Voter;
  8. use FMT\Data\Entity\User;
  9. use FMT\Data\Entity\UserTransaction;
  10. use FMT\Domain\Service\PaymentManagerInterface;
  11. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  13. class TransactionVoter extends Voter
  14. {
  15.     const CAN_VIEW_TRANSACTION "VIEW_TRANSACTION";
  16.     const CAN_THANKS "CAN_THANKS";
  17.     const CAN_VIEW_RECEIPT "CAN_VIEW_RECEIPT";
  18.     /** @var array */
  19.     private static $methodMapping = [
  20.         self::CAN_VIEW_TRANSACTION => "canViewTransaction",
  21.         self::CAN_THANKS => "canThanks",
  22.         self::CAN_VIEW_RECEIPT => "canViewReceipt",
  23.     ];
  24.     /** @var PaymentManagerInterface */
  25.     private $manager;
  26.     public function __construct(PaymentManagerInterface $manager)
  27.     {
  28.         $this->manager $manager;
  29.     }
  30.     /**
  31.      * Determines if the attribute and subject are supported by this voter.
  32.      *
  33.      * @param string $attribute An attribute
  34.      * @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type
  35.      *
  36.      * @return bool True if the attribute and subject are supported, false otherwise
  37.      */
  38.     protected function supports($attribute$subject)
  39.     {
  40.         if (!($subject instanceof UserTransaction)) {
  41.             return false;
  42.         }
  43.         return array_key_exists($attributeself::$methodMapping);
  44.     }
  45.     /**
  46.      * Perform a single access check operation on a given attribute, subject and token.
  47.      * It is safe to assume that $attribute and $subject already passed the "supports()" method check.
  48.      *
  49.      * @param string $attribute
  50.      * @param UserTransaction $subject
  51.      * @param TokenInterface $token
  52.      *
  53.      * @return bool
  54.      */
  55.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  56.     {
  57.         $owner $token->getUser();
  58.         if (!array_key_exists($attributeself::$methodMapping)) {
  59.             return false;
  60.         }
  61.         if (!($owner instanceof User)) {
  62.             $owner null;
  63.         }
  64.         return call_user_func([$thisself::$methodMapping[$attribute]], $subject$owner);
  65.     }
  66.     /**
  67.      * @param UserTransaction $transaction
  68.      * @param User|null $owner
  69.      * @return bool
  70.      */
  71.     protected function canViewTransaction(UserTransaction $transactionUser $owner null)
  72.     {
  73.         if (!empty($owner) && $owner !== $transaction->getSender()) {
  74.             return false;
  75.         }
  76.         if (empty($owner) && $transaction->getSender() && $transaction->getSender()->isRegistered()) {
  77.             return false;
  78.         }
  79.         return true;
  80.     }
  81.     /**
  82.      * @param UserTransaction $transaction
  83.      * @param User|null $user
  84.      * @return bool
  85.      */
  86.     protected function canThanks(UserTransaction $transactionUser $user null)
  87.     {
  88.         $campaign $transaction->getCampaign();
  89.         if (empty($campaign)) {
  90.             return false;
  91.         }
  92.         return $user === $campaign->getUser();
  93.     }
  94.     /**
  95.      * @param UserTransaction $transaction
  96.      * @param User|null $user
  97.      * @return bool
  98.      */
  99.     protected function canViewReceipt(UserTransaction $transactionUser $user null)
  100.     {
  101.         if ($transaction->getSender() == $user || $transaction->getRecipient() == $user) {
  102.             return true;
  103.         }
  104.         return false;
  105.     }
  106. }