src/Uniski/CommerceBundle/Security/BookingVoter.php line 10

Open in your IDE?
  1. <?php
  2. // src/CommerceBundle/Security/MaterialVoter.php
  3. namespace Uniski\CommerceBundle\Security;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Uniski\CommerceBundle\Entity\Booking;
  7. use Uniski\UserBundle\Entity\User;
  8. class BookingVoter extends Voter
  9. {
  10.     // these strings are just invented: you can use anything
  11.     const CANCEL        'cancel';
  12.     const VIEW          'view';
  13.     const PAY           'pay';
  14.     const APPLY_CANCEL  'apply_cancel';
  15.     protected function supports($attribute$subject)
  16.     {
  17.         // if the attribute isn't one we support, return false
  18.         if (!in_array($attribute, array(
  19.             self::CANCELself::VIEWself::PAYself::APPLY_CANCEL))) {
  20.             return false;
  21.         }
  22.         // only vote on Booking objects inside this voter
  23.         if (!$subject instanceof Booking) {
  24.             return false;
  25.         }
  26.         return true;
  27.     }
  28.     protected function voteOnAttribute($attribute$bookingTokenInterface $token)
  29.     {
  30.         $user $token->getUser();
  31.         if (!$user instanceof User) {
  32.             // the user must be logged in; if not, deny access
  33.             return false;
  34.         }
  35.         switch($attribute) {
  36.             case self::CANCEL:
  37.                 return $this->canCancel($booking$user);
  38.             case self::VIEW:
  39.                 return $this->canView($booking$user);
  40.             case self::PAY:
  41.                 return $this->canPay($booking$user);
  42.             case self::APPLY_CANCEL:
  43.                 return $this->canApplyCancel($booking$user);
  44.         }
  45.         throw new \LogicException('This code should not be reached!');
  46.     }
  47.     private function canCancel(Booking $bookingUser $user)
  48.     {
  49.         if (( $user->hasRole('ROLE_ADMIN') ||
  50.                $user->hasRole('ROLE_SALESMAN') ) &&
  51.              $booking->getStatus() != Booking::STATUS_DELETED) return true;
  52.         return $booking->getStatus() == Booking::STATUS_PRE_BOOKED &&
  53.                $booking->getUser()->getId() == $user->getId();
  54.     }
  55.     private function canApplyCancel(Booking $bookingUser $user)
  56.     {
  57.         if (( $user->hasRole('ROLE_ADMIN') ||
  58.               $user->hasRole('ROLE_SALESMAN') ) &&
  59.             $booking->getStatus() != Booking::STATUS_DELETED) return true;
  60.         return ($booking->getStatus() == Booking::STATUS_PAYED ||
  61.                 $booking->getStatus() == Booking::STATUS_HALF_PAYED) &&
  62.                $booking->getDateIn() > new \DateTime() &&
  63.                $booking->getUser()->getId() == $user->getId();
  64.     }
  65.     private function canView(Booking $bookingUser $user)
  66.     {
  67.         if ( $user->hasRole('ROLE_ADMIN') ||
  68.              $user->hasRole('ROLE_SALESMAN'))  return true;
  69.         else return $booking->getUser()->getId() == $user->getId();
  70.     }
  71.     private function canPay(Booking $bookingUser $user)
  72.     {
  73.         if ( $booking->getUser()->getId() != $user->getId() ) return false;
  74.         return $booking->getStatus() == Booking::STATUS_PENDING ||
  75.                $booking->getStatus() == Booking::STATUS_HALF_PAYED;
  76.     }
  77. }