src/Voters/OrderVoter.php line 33

Open in your IDE?
  1. <?php
  2. /**
  3.  *
  4.  * (c) BonBonSlick
  5.  *
  6.  */
  7. declare(strict_types=1);
  8. /*
  9.  * Created by BonBonSlick
  10.  * Contacts: google it
  11.  * Date: 9/14/18
  12.  * Time: 12:00 AM
  13.  */
  14. namespace App\Voters;
  15. use App\Entity\Order\Order;
  16. use App\Entity\Role\Role;
  17. use App\Entity\User\User;
  18. use function get_class;
  19. use function in_array;
  20. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  21. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  22. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  23. use Symfony\Component\Security\Core\User\UserInterface;
  24. /**
  25.  * Class self
  26.  */
  27. final class OrderVoter extends Voter
  28. {
  29.     public const ADMIN_VIEW_SINGLE 1;
  30.     public const ADMIN_VIEW_PAID_LIST 3;
  31.     public const ADMIN_VIEW_UNPAID_LIST 7;
  32.     public const ADMIN_EDIT 5;
  33.     /**
  34.      * string[]
  35.      */
  36.     public const ADMIN_PERMS = [
  37.         self::ADMIN_VIEW_SINGLE => 'admin.view.single',
  38.         self::ADMIN_VIEW_PAID_LIST => 'admin.view.paid.list',
  39.         self::ADMIN_VIEW_UNPAID_LIST => 'admin.view.un.paid.list',
  40.         self::ADMIN_EDIT => 'admin.edit',
  41.     ];
  42.     /**
  43.      * @var AccessDecisionManagerInterface
  44.      */
  45.     private $decisionManager;
  46.     /**
  47.      * self constructor.
  48.      * 
  49.      * @param AccessDecisionManagerInterface $decisionManager
  50.      */
  51.     public function __construct(AccessDecisionManagerInterface $decisionManager)
  52.     {
  53.         $this->decisionManager $decisionManager;
  54.     }
  55.     /**
  56.      * {@inheritdoc}
  57.      */
  58.     protected function supports($attribute$subject): bool
  59.     {
  60.         // if the attribute isn't one we support, return false
  61.         if (false === in_array($attributeself::ADMIN_PERMStrue)) {
  62.             return false;
  63.         }
  64.         // only vote on Post objects inside this voter
  65.         if (get_class($subject) === Order::class) {
  66.             return true;
  67.         }
  68.         return false;
  69.     }
  70.     /**
  71.      * {@inheritdoc}
  72.      */
  73.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  74.     {
  75.         /** @var User $user */
  76.         $user $token->getUser();
  77.         $response false;
  78.         $isGranted false === $user instanceof UserInterface ||
  79.             $this->decisionManager->decide($token, [
  80.                 User::ROLE_ADMIN,
  81.                 User::ROLE_WRITER,
  82.                 User::ROLE_PREMIUM_WRITER,
  83.                 User::ROLE_EXPRESS_WRITER,
  84.             ]);
  85.         if (false === $isGranted) {
  86.             return $response;
  87.         }
  88.         switch ($attribute) {
  89.             case self::ADMIN_PERMS[self::ADMIN_VIEW_SINGLE]:
  90.                 $response $this->isAdminCanViewSingle($user);
  91.                 break;
  92.             case self::ADMIN_PERMS[self::ADMIN_VIEW_PAID_LIST]:
  93.                 $response $this->adminViewPaidOrderList($user);
  94.                 break;
  95.             case self::ADMIN_PERMS[self::ADMIN_VIEW_UNPAID_LIST]:
  96.                 $response $this->isAdminCanViewUnPadiOrderList($user);
  97.                 break;
  98.             case self::ADMIN_PERMS[self::ADMIN_EDIT]:
  99.                 $response $this->isAdminCanEdit($user);
  100.                 break;
  101.             default:
  102.                 return $response;
  103.         }
  104.         return $response;
  105.     }
  106.     /**
  107.      * @param User $user
  108.      *
  109.      * @return bool
  110.      */
  111.     private function isAdminCanViewSingle(User $user): bool
  112.     {
  113.         // if they can edit, they can view
  114.         if ($this->isAdminCanEdit($user)) {
  115.             return true;
  116.         }
  117.         if ($this->isUserInRole($user, [
  118.             User::ROLE_ADMIN,
  119.             User::ROLE_WRITER,
  120.             User::ROLE_PREMIUM_WRITER,
  121.             User::ROLE_EXPRESS_WRITER,
  122.         ])) {
  123.             return true;
  124.         }
  125.         return false;
  126.     }
  127.     /**
  128.      * @param User $user
  129.      *
  130.      * @return bool
  131.      */
  132.     private function adminViewPaidOrderList(User $user): bool
  133.     {
  134.         // if they can edit, they can view
  135.         if ($this->isAdminCanViewUnPadiOrderList($user)) {
  136.             return true;
  137.         }
  138.         if ($this->isUserInRole($user, [
  139.             User::ROLE_WRITER,
  140.             User::ROLE_PREMIUM_WRITER,
  141.             User::ROLE_EXPRESS_WRITER
  142.         ])) {
  143.             return true;
  144.         }
  145.         return false;
  146.     }
  147.     /**
  148.      * @param User $user
  149.      *
  150.      * @return bool
  151.      */
  152.     private function isAdminCanViewUnPadiOrderList(User $user): bool
  153.     {
  154.         if ($this->isUserInRole($user, [
  155.             User::ROLE_ADMIN,
  156.         ])) {
  157.             return true;
  158.         }
  159.         return false;
  160.     }
  161.     /**
  162.      * @param User $user
  163.      *
  164.      * @return bool
  165.      */
  166.     private function isAdminCanEdit(User $user): bool
  167.     {
  168.         if ($this->isUserInRole($user, [
  169.             User::ROLE_ADMIN,
  170.             User::ROLE_WRITER,
  171.             User::ROLE_PREMIUM_WRITER,
  172.             User::ROLE_EXPRESS_WRITER,
  173.         ])) {
  174.             return true;
  175.         }
  176.         return false;
  177.     }
  178.     /**
  179.      * @param User     $user
  180.      *
  181.      * @param string[] $roles
  182.      *
  183.      * @return bool
  184.      */
  185.     private function isUserInRole(User $user, array $roles): bool
  186.     {
  187.         /** @var Role $item */
  188.         foreach ($user->rolesCollection() as $item) {
  189.             if (in_array($item->constantKey(), $rolestrue)) {
  190.                 return true;
  191.             }
  192.         }
  193.         return false;
  194.     }
  195. }