src/Voters/ClinicsVoter.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Voters;
  3. use App\Entity\Clinic\Clinic;
  4. use App\Entity\Clinic\PriceTier\PriceTier;
  5. use App\Entity\Organisation\Organisation;
  6. use App\Entity\Person\Admin;
  7. use App\Entity\Person\SuperClinicAdmin;
  8. use App\Entity\Person\User;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. class ClinicsVoter extends OrganisationVoter
  12. {
  13.     const VIEW_EXTERNAL 'view_external';
  14.     const VIEW_CALENDAR 'view_calendar';
  15.     const VIEW_DAY_SHEET 'view_day_sheet';
  16.     const VIEW_DAY_SHEET_EDIT 'view_day_sheet_edit';
  17.     const DAY_SHEET_BY_TIER 'day_sheet_by_tier';
  18.     const CREATE_BOARD 'create_board';
  19.     const VIEW_STOCK 'view_stock';
  20.     const VIEW_STOCK_EDIT 'view_stock_edit';
  21.     const VIEW_EXTERNAL_INVOICE 'view_external_invoice';
  22.     const VIEW_CREATE_TEAM_MEMBER 'create_team_member';
  23.     const VIEW_NOTIFICATION 'notification';
  24.     const VIEW_NOTIFICATION_TWILIO 'notification_twilio';
  25.     const VIEW_PROSPECTS 'prospects';
  26.     const VIEW_WAITING_LIST 'waiting_list';
  27.     const VIEW_REPORT 'report';
  28.     const VIEW_LIBRARY 'library';
  29.     const REMOVE 'remove';
  30.     const ROOM 'room';
  31.     const EQUIPMENT 'equipment';
  32.     const ONLINE_BOOKING 'online_booking';
  33.     protected function supports($attribute$subject null)
  34.     {
  35.         return $subject instanceof Clinic && in_array(
  36.                 $attribute,
  37.                 [
  38.                     self::VIEW,
  39.                     self::CREATE,
  40.                     self::EDIT,
  41.                     self::VIEW_EXTERNAL,
  42.                     self::VIEW_STOCK,
  43.                     self::VIEW_STOCK_EDIT,
  44.                     self::VIEW_EXTERNAL_INVOICE,
  45.                     self::VIEW_CREATE_TEAM_MEMBER,
  46.                     self::VIEW_NOTIFICATION,
  47.                     self::VIEW_NOTIFICATION_TWILIO,
  48.                     self::VIEW_PROSPECTS,
  49.                     self::VIEW_WAITING_LIST,
  50.                     self::VIEW_REPORT,
  51.                     self::VIEW_LIBRARY,
  52.                     self::REMOVE,
  53.                     self::VIEW_DAY_SHEET,
  54.                     self::VIEW_DAY_SHEET_EDIT,
  55.                     self::CREATE_BOARD,
  56.                     self::VIEW_CALENDAR,
  57.                     self::ROOM,
  58.                     self::EQUIPMENT,
  59.                     self::DAY_SHEET_BY_TIER,
  60.                     self::ONLINE_BOOKING,
  61.                 ],
  62.                 true
  63.             );
  64.     }
  65.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  66.     {
  67.         $user $token->getUser();
  68.         /** @var Clinic $clinic */
  69.         $clinic $subject;
  70.         /** @var Organisation $organisation */
  71.         $organisation $clinic->getOrganisation();
  72.         if (!$user instanceof UserInterface) {
  73.             return false;
  74.         }
  75.         switch ($attribute) {
  76.             case self::VIEW:
  77.                 if ($user instanceof Admin) {
  78.                     return true;
  79.                 }
  80.                 if ($user instanceof SuperClinicAdmin) {
  81.                     return parent::voteOnAttribute(self::VIEW$organisation$token);
  82.                 }
  83.                 return $user->getClinic()->getId() === $clinic->getId();
  84.                 break;
  85.             case self::CREATE:
  86.                 if ($this->decisionManager->decide($token, [User::ROLE_SUB_STOCK])) {
  87.                     if ($clinic !== $user->getClinic() && ($user->isClinicAdmin() || $user->isClinicReceptionist())) {
  88.                         return false;
  89.                     }
  90.                     return parent::voteOnAttribute(self::VIEW$organisation$token);
  91.                 }
  92.                 break;
  93.             case self::VIEW_CREATE_TEAM_MEMBER:
  94.                 if ($this->decisionManager->decide($token, [User::ROLE_SUB_CREATE_USERS])) {
  95.                     if ($user instanceof Admin) {
  96.                         return true;
  97.                     }
  98.                     if ($user instanceof SuperClinicAdmin) {
  99.                         return parent::voteOnAttribute(self::VIEW$organisation$token);
  100.                     }
  101.                     return $user->getClinic()->getId() === $clinic->getId();
  102.                 }
  103.                 break;
  104.             case self::VIEW_PROSPECTS:
  105.                 if ($this->decisionManager->decide($token, [User::ROLE_SUB_LEAD_CAPTURE])) {
  106.                     if ($user instanceof Admin) {
  107.                         return false;
  108.                     }
  109.                     if ($user instanceof SuperClinicAdmin) {
  110.                         return parent::voteOnAttribute(self::VIEW$organisation$token);
  111.                     }
  112.                     return $user->getClinic()->getId() === $clinic->getId();
  113.                 }
  114.                 break;
  115.             case self::VIEW_WAITING_LIST:
  116.                 if ($this->decisionManager->decide($token, [User::ROLE_SUB_WAITING_LIST])) {
  117.                     if ($user instanceof Admin) {
  118.                         return false;
  119.                     }
  120.                     if ($user instanceof SuperClinicAdmin) {
  121.                         return parent::voteOnAttribute(self::VIEW$organisation$token);
  122.                     }
  123.                     return $user->getClinic()->getId() === $clinic->getId();
  124.                 }
  125.                 break;
  126.             case self::VIEW_REPORT:
  127.                 if ($this->decisionManager->decide($token, [User::ROLE_SUB_REPORTING])) {
  128.                     if ($user instanceof Admin) {
  129.                         return true;
  130.                     }
  131.                     if ($user instanceof SuperClinicAdmin) {
  132.                         return parent::voteOnAttribute(self::VIEW$organisation$token);
  133.                     }
  134.                     return $user->getClinic()->getId() === $clinic->getId();
  135.                 }
  136.                 break;
  137.             case self::VIEW_LIBRARY:
  138.                 if ($this->decisionManager->decide($token, [User::ROLE_SUB_LIBRARY])) {
  139.                     if ($user instanceof Admin) {
  140.                         return true;
  141.                     }
  142.                     if ($user instanceof SuperClinicAdmin) {
  143.                         return parent::voteOnAttribute(self::VIEW$organisation$token);
  144.                     }
  145.                     return $user->getClinic()->getId() === $clinic->getId();
  146.                 }
  147.                 break;
  148.             case self::EDIT:
  149.                 if ($this->decisionManager->decide($token, [User::ROLE_SUB_CLINIC_SETUP])) {
  150.                     if ($user instanceof Admin) {
  151.                         return true;
  152.                     }
  153.                     if ($user instanceof SuperClinicAdmin) {
  154.                         return parent::voteOnAttribute(self::VIEW$organisation$token);
  155.                     }
  156.                     return $user->getClinic()->getId() === $clinic->getId();
  157.                 }
  158.                 break;
  159.             case self::REMOVE:
  160.                 if ($this->decisionManager->decide($token, [User::ROLE_SUB_CLINIC_SETUP])) {
  161.                     if ($user instanceof Admin) {
  162.                         return true;
  163.                     }
  164.                     if ($user instanceof SuperClinicAdmin) {
  165.                         return parent::voteOnAttribute(self::VIEW$organisation$token);
  166.                     }
  167.                     return false;
  168.                 }
  169.                 break;
  170.             case self::VIEW_NOTIFICATION:
  171.                 if ($this->decisionManager->decide($token, [User::TYPE_SUPER_CLINIC_ADMINUser::ROLE_CLINIC_ADMIN])) {
  172.                     if ($user instanceof Admin) {
  173.                         return true;
  174.                     }
  175.                     if ($user instanceof SuperClinicAdmin) {
  176.                         return parent::voteOnAttribute(self::VIEW$organisation$token);
  177.                     }
  178.                     return $user->getClinic()->getId() === $clinic->getId();
  179.                 }
  180.                 break;
  181.             case self::VIEW_NOTIFICATION_TWILIO:
  182.                 if ($this->decisionManager->decide($token, [User::ROLE_SUB_CLINIC_SETUP])) {
  183.                     if ($user instanceof Admin) {
  184.                         return true;
  185.                     }
  186.                     if ($user instanceof SuperClinicAdmin) {
  187.                         return parent::voteOnAttribute(self::VIEW$organisation$token);
  188.                     }
  189.                     return $user->getClinic()->getId() === $clinic->getId();
  190.                 }
  191.                 break;
  192.             case self::VIEW_EXTERNAL:
  193.                 if ($user instanceof Admin) {
  194.                     return false;
  195.                 }
  196.                 if ($user instanceof SuperClinicAdmin) {
  197.                     return parent::voteOnAttribute(self::VIEW$organisation$token);
  198.                 }
  199.                 return $user->getClinic()->getId() === $clinic->getId();
  200.                 break;
  201.             case self::VIEW_CALENDAR:
  202.                 if ($user instanceof Admin) {
  203.                     return false;
  204.                 }
  205.                 if ($user instanceof SuperClinicAdmin) {
  206.                     return parent::voteOnAttribute(self::VIEW$organisation$token);
  207.                 }
  208.                 if ($user->isCoordinator()) {
  209.                     return false;
  210.                 }
  211.                 return $user->getClinic()->getId() === $clinic->getId();
  212.                 break;
  213.             case self::VIEW_DAY_SHEET:
  214.                 if ($user instanceof Admin) {
  215.                     return false;
  216.                 }
  217.                 if ($user instanceof SuperClinicAdmin) {
  218.                     return parent::voteOnAttribute(self::VIEW$organisation$token);
  219.                 }
  220.                 return ($this->decisionManager->decide($token, [User::ROLE_CLINIC_ADMINUser::ROLE_PRACTITIONERUser::ROLE_RECEPTIONISTUser::ROLE_BOOKERUser::ROLE_COORDINATOR]))
  221.                     && $user->getClinic()->getId() === $clinic->getId();
  222.                 break;
  223.             case self::VIEW_DAY_SHEET_EDIT:
  224.                 if ($user instanceof Admin) {
  225.                     return false;
  226.                 }
  227.                 if ($user instanceof SuperClinicAdmin) {
  228.                     return parent::voteOnAttribute(self::VIEW$organisation$token);
  229.                 }
  230.                 return ($this->decisionManager->decide($token, [User::ROLE_CLINIC_ADMINUser::ROLE_RECEPTIONISTUser::ROLE_BOOKERUser::ROLE_COORDINATOR]))
  231.                     && $user->getClinic()->getId() === $clinic->getId();
  232.                 break;
  233.             case self::VIEW_STOCK:
  234.                 if ($this->decisionManager->decide($token, [User::ROLE_SUB_STOCK])) {
  235.                     if ($user instanceof SuperClinicAdmin) {
  236.                         return parent::voteOnAttribute(self::VIEW$organisation$token);
  237.                     }
  238.                     return $user->getClinic()->getId() === $clinic->getId();
  239.                 }
  240.                 break;
  241.             case self::VIEW_STOCK_EDIT:
  242.                 if ($this->decisionManager->decide($token, [User::ROLE_SUB_STOCK])) {
  243.                     if ($user instanceof SuperClinicAdmin) {
  244.                         return parent::voteOnAttribute(self::VIEW$organisation$token);
  245.                     }
  246.                     if ($user->isBooker() || $user->isCoordinator()) {
  247.                         return false;
  248.                     }
  249.                     return $user->getClinic()->getId() === $clinic->getId();
  250.                 }
  251.                 break;
  252.             case self::VIEW_EXTERNAL_INVOICE:
  253.                 if ($user instanceof Admin) {
  254.                     return false;
  255.                 }
  256.                 if ($user instanceof SuperClinicAdmin) {
  257.                     return parent::voteOnAttribute(self::VIEW$organisation$token);
  258.                 }
  259.                 return ($this->decisionManager->decide($token, [User::ROLE_SUB_INVOICE])) && $user->getClinic()->getId() === $clinic->getId();
  260.                 break;
  261.             case self::CREATE_BOARD:
  262.                 if ($user instanceof Admin) {
  263.                     return false;
  264.                 }
  265.                 if ($user instanceof SuperClinicAdmin) {
  266.                     return parent::voteOnAttribute(self::VIEW$organisation$token);
  267.                 }
  268.                 return ($this->decisionManager->decide($token, [User::ROLE_CLINIC_ADMIN])) && $user->getClinic()->getId() === $clinic->getId();
  269.                 break;
  270.             case self::ROOM:
  271.                 if ($clinic->getPriceTier()->getType() !== PriceTier::TYPE_STARTER) {
  272.                     return true;
  273.                 }
  274.                 return false;
  275.                 break;
  276.             case self::EQUIPMENT:
  277.                 if ($clinic->getPriceTier()->getType() !== PriceTier::TYPE_STARTER) {
  278.                     return true;
  279.                 }
  280.                 return false;
  281.                 break;
  282.             case self::DAY_SHEET_BY_TIER:
  283.                 if ($clinic->getPriceTier()->getType() === PriceTier::TYPE_ENTERPRISE) {
  284.                     return true;
  285.                 }
  286.                 return false;
  287.                 break;
  288.             case self::ONLINE_BOOKING:
  289.                 if ($clinic->getPriceTier()->getType() !== PriceTier::TYPE_STARTER) {
  290.                     return true;
  291.                 }
  292.                 return false;
  293.                 break;
  294.         }
  295.         return false;
  296.     }
  297. }