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