src/Security/Voter/OrderVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Order;
  4. use App\Entity\User;
  5. use App\Repository\CompanyRepository;
  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. use Symfony\Component\Security\Core\User\UserInterface;
  10. class OrderVoter extends Voter
  11. {
  12.     public const EDIT 'ORDER_EDIT';
  13.     public const VIEW 'ORDER_VIEW';
  14.     public const DELETE 'ORDER_DELETE';
  15.     public const NOT_ANONYMIZE 'ORDER_NOT_ANONYMIZE';
  16.     public const VIEW_MERCHANT 'VIEW_MERCHANT';
  17.     public const VIEW_INPROGRESS 'ORDER_VIEW_INPROGRESS';
  18.     /**
  19.      * Limitace na spolecnost pri nacitani v repositari
  20.      */
  21.     public const REPO_DONT_LIMIT_COMPANY 'ORDER_REPO_NO_LIMIT_COMPANY';
  22.     public const REPO_DONT_LIMIT_OWNER 'ORDER_REPO_NO_LIMIT_OWNER';
  23.     public const VIEW_ICONS_ORDER_LIST 'VIEW_ICONS_ORDER_LIST';
  24.     public const VIEW_INVOICING_MERCHANT 'VIEW_INVOICING_MERCHANT';
  25.     public const VIEW_INVOICING_EMITENT 'VIEW_INVOICING_EMITENT';
  26.     /**
  27.      * @var Security
  28.      */
  29.     protected $_security;
  30.     /**
  31.      * @var CompanyRepository
  32.      */
  33.     protected $_companyRepository;
  34.     /**
  35.      * @param Security $_security
  36.      * @param CompanyRepository $companyRepository
  37.      */
  38.     public function __construct(Security $_securityCompanyRepository $companyRepository)
  39.     {
  40.         $this->_security $_security;
  41.         $this->_companyRepository $companyRepository;
  42.     }
  43.     protected function supports(string $attribute$subject): bool
  44.     {
  45.         return in_array($attribute, [
  46.             self::EDIT,
  47.             self::VIEW,
  48.             self::REPO_DONT_LIMIT_COMPANY,
  49.             self::REPO_DONT_LIMIT_OWNER,
  50.             self::VIEW_INPROGRESS,
  51.             self::DELETE,
  52.             self::VIEW_MERCHANT,
  53.             self::NOT_ANONYMIZE,
  54.             self::VIEW_ICONS_ORDER_LIST,
  55.             self::VIEW_INVOICING_EMITENT,
  56.             self::VIEW_INVOICING_MERCHANT
  57.         ]);
  58.     }
  59.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  60.     {
  61.         if ($this->_security->isGranted(User::ROLE_ADMIN)) {
  62.             return true;
  63.         }
  64.         $user $token->getUser();
  65.         // if the user is anonymous, do not grant access
  66.         if (!$user instanceof UserInterface) {
  67.             return false;
  68.         }
  69.         $order $subject;
  70.         // ... (check conditions and return true to grant permission) ...
  71.         switch ($attribute) {
  72.             case self::EDIT:
  73.                 return $this->canEdit($user$order);
  74.             case self::VIEW:
  75.                 return $this->canView($user$order);
  76.             case self::REPO_DONT_LIMIT_COMPANY:
  77.                 return $this->dontLimitByCompany($user$order);
  78.             case self::REPO_DONT_LIMIT_OWNER:
  79.                 return $this->dontLimitByOwner($user$order);
  80.             case self::DELETE:
  81.                 return $this->canDelete($user$order);
  82.             case self::VIEW_MERCHANT:
  83.                 return $this->canViewMerchant($user$order);
  84.             case self::NOT_ANONYMIZE:
  85.                 return $this->isNotAnonymize($user$order);
  86.             case self::VIEW_ICONS_ORDER_LIST:
  87.                 return $this->viewIconsOrderList($user);
  88.             case self::VIEW_INPROGRESS:
  89.                 return $this->canViewInProgress($user$order);
  90.             case self::VIEW_INVOICING_EMITENT:
  91.                 return $this->isEmitentInvoicing($user);
  92.             case self::VIEW_INVOICING_MERCHANT:
  93.                 return $this->isMerchantInvoicing($user);
  94.         }
  95.         return false;
  96.     }
  97.     /**
  98.      * @param UserInterface $user
  99.      * @param Order|null $order
  100.      * @return false
  101.      */
  102.     protected function canViewMerchant(UserInterface $user$order): bool
  103.     {
  104.         if($user->hasRole(User::ROLE_COMPANY_ADMIN)){
  105.             return false;
  106.         }
  107.         if($user->hasRole(User::ROLE_COMPANY_USER)){
  108.             return false;
  109.         }
  110.         if($user->hasRole(User::ROLE_MERCHANT)){
  111.             return true;
  112.         }
  113.         return true;
  114.     }
  115.     /**
  116.      * @param UserInterface $user
  117.      * @param Order|null $order
  118.      * @return false
  119.      */
  120.     protected function isNotAnonymize(UserInterface $user, ?Order $order): bool
  121.     {
  122.         // pokud neni nastavena anonymiziace na uzivateli vracim true
  123.         if($user->isAnonymize() === false){
  124.             return true;
  125.         }
  126.         if($user->hasRole(User::ROLE_MERCHANT)){
  127.             if(isset($order) && $order->getEmail() == $user->getEmail()){
  128.                 return true;
  129.             }
  130.         }
  131.         // pokud je nastavena 1 na anonymized u uzivatele
  132.         return false;
  133.     }
  134.     /**
  135.      * @param User $user
  136.      * @return bool
  137.      */
  138.     protected function viewIconsOrderList(UserInterface $user): bool
  139.     {
  140.         if ($user->hasRole(User::ROLE_COMPANY_ADMIN)) {
  141.             return true;
  142.         }
  143.         if ($user->hasRole(User::ROLE_COMPANY_USER)) {
  144.             return true;
  145.         }
  146.         return false;
  147.     }
  148.     /**
  149.      * @param UserInterface $user
  150.      * @param Order $order
  151.      * @return false
  152.      */
  153.     protected function canDelete(UserInterface $userOrder $order)
  154.     {
  155.         if($order->isSigned() != false || $order->isPaid() != false || $order->isSent() != false){
  156.             return false;
  157.         }
  158.         if($user->hasRole(User::ROLE_COMPANY_ADMIN)){
  159.             return true;
  160.         }
  161.         if(($user->hasRole(User::ROLE_COMPANY_USER)) &&
  162.             $user->getId() == $order->getUser()->getId()
  163.         ){
  164.             return true;
  165.         }
  166.         if($user->hasRole(User::ROLE_MERCHANT)){
  167.             return false;
  168.         }
  169.         return false;
  170.     }
  171.     /**
  172.      * @param UserInterface $user
  173.      * @param Order $order
  174.      * @return false
  175.      */
  176.     protected function canEdit(UserInterface $userOrder $order)
  177.     {
  178.         $companyIds = [];
  179.         if(is_null($user->getCompany()) == false) {
  180.             $companyIds $this->_companyRepository->getConnectedCompanies($user->getCompany()->getId());
  181.         }
  182.         if ($user->hasRole(User::ROLE_COMPANY_ADMIN)) {
  183.             if (
  184.                 is_null($order->getCompany()) == false && is_null($user->getCompany()) == false &&
  185.                 in_array($order->getCompany()->getId(), $companyIds) === true
  186.             ) {
  187.                 return true;
  188.             }
  189.         }
  190.         if ($user->hasRole(User::ROLE_MERCHANT)) {
  191.             if (is_null($order->getCompany()) == false &&
  192.                 is_null($user->getCompany()) == false &&
  193.                 in_array($order->getCompany()->getId(), $companyIds) === true &&
  194.                 $order->getUser()->getId() == $user->getId()
  195.             ) {
  196.                 return true;
  197.             }
  198.         }
  199.         return false;
  200.     }
  201.     /**
  202.      * @param UserInterface $user
  203.      * @param Order $order
  204.      * @return false
  205.      */
  206.     protected function canView(UserInterface $userOrder $order)
  207.     {
  208.         $companyIds = [];
  209.         if(is_null($user->getCompany()) == false) {
  210.             $companyIds $this->_companyRepository->getConnectedCompanies($user->getCompany()->getId());
  211.         }
  212.         if (
  213.             $user->hasRole(User::ROLE_COMPANY_USER) ||
  214.             $user->hasRole(User::ROLE_COMPANY_ADMIN)
  215.         ) {
  216.             if (is_null($order->getCompany()) == false &&
  217.                 is_null($user->getCompany()) == false &&
  218.                 in_array($order->getCompany()->getId(), $companyIds) === true
  219.             ) {
  220.                 return true;
  221.             }
  222.         }
  223.         if($user->hasRole(User::ROLE_MERCHANT)){
  224.             if($order->getEmail() == $user->getEmail()){
  225.                 return true;
  226.             }
  227.         }
  228.         return false;
  229.     }
  230.     /**
  231.      * @param UserInterface $user
  232.      * @param Order|null $order
  233.      * @return false
  234.      */
  235.     protected function dontLimitByCompany(UserInterface $user$order)
  236.     {
  237.         if($user->hasRole(User::ROLE_MERCHANT)){
  238.             return true;
  239.         }
  240.         return false;
  241.     }
  242.     /**
  243.      * @param UserInterface $user
  244.      * @param Order|null $order
  245.      * @return false
  246.      */
  247.     protected function dontLimitByOwner(UserInterface $user$order)
  248.     {
  249.         // neomezuj pokud je admin || company_user || company_admin
  250.         // omezi pokud jsi
  251.         if(
  252.             $user->hasRole(User::ROLE_COMPANY_USER) ||
  253.             $user->hasRole(User::ROLE_COMPANY_ADMIN)
  254.         ){
  255.             return true;
  256.         }
  257.         return false;
  258.     }
  259.     protected function canViewInProgress(UserInterface $user$order)
  260.     {
  261.         if(
  262.             $user->hasRole(User::ROLE_MERCHANT) ||
  263.             $user->hasRole(User::ROLE_COMPANY_USER) ||
  264.             $user->hasRole(User::ROLE_COMPANY_ADMIN)
  265.         ){
  266.             return true;
  267.         }
  268.         return false;
  269.     }
  270.     protected function isMerchantInvoicing(UserInterface $user){
  271.         if(
  272.             $user->hasRole(User::ROLE_MERCHANT) &&
  273.             ($user->hasRole(User::ROLE_COMPANY_USER) == false && $user->hasRole(User::ROLE_COMPANY_ADMIN) == false)
  274.         ){
  275.             return true;
  276.         }
  277.         return false;
  278.     }
  279.     protected function isEmitentInvoicing(UserInterface $user){
  280.         if(
  281.             $user->hasRole(User::ROLE_COMPANY_USER) ||
  282.             $user->hasRole(User::ROLE_COMPANY_ADMIN)
  283.         ){
  284.             return true;
  285.         }
  286.         return false;
  287.     }
  288. }