src/Knitink/UniverseBundle/Security/LicenseVoter.php line 18

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of the Knitink Project package.
  4.  *
  5.  * Created by Globunet Soluciones Tecnologicas, SL. on 31/03/22. <info@globunet.com>
  6.  * Copyright (c) 2016 Knitink Technologies, SL. All rights reserved.
  7.  */
  8. namespace Knitink\UniverseBundle\Security;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  12. use Symfony\Component\HttpFoundation\Session\Session;
  13. use Globunet\DinamicDatabaseBundle\Services\DatabaseConnector;
  14. class LicenseVoter extends Voter
  15. {
  16.     private $databaseConnector;
  17.     private $session;
  18.     public function __construct(DatabaseConnector $databaseConnectorSession $session)
  19.     {
  20.         $this->databaseConnector $databaseConnector;
  21.         $this->session $session;
  22.     }
  23.     protected function supports($attribute$subject)
  24.     {
  25.         return ($subject instanceof Request || is_array($subject)) && $attribute === 'VOTER_KNITINK_UNIVERSE_LICENSE';
  26.     }
  27.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  28.     {
  29.         $user $token->getUser();
  30.         /**/
  31.         if(($user instanceof \Symfony\Component\Security\Core\User\User || $user instanceof \Knitink\CoreBundle\Entity\User)){
  32.             if(in_array("ROLE_SUPER_ADMIN"$user->getRoles()) || in_array("ROLE_KNK_UNIV_SUPER_ADMIN"$user->getRoles())){
  33.                 return true;
  34.             }
  35.         }
  36.         if (!$user instanceof \Knitink\CoreBundle\Entity\User) {
  37.             return false;
  38.         }
  39.         /**/
  40.         return $this->checker($user$attribute$subject);
  41.     }
  42.     public function checker($user$attribute$subject$em null)
  43.     {
  44.         $bool true;
  45.         if($subject  instanceof Request){
  46.             $licenseType $subject->getSession()->get("licenseType");
  47.             $pathName $subject->attributes->get('_route');
  48.         }else{
  49.             $licenseType $subject["licenseType"];
  50.             $pathName $subject['route'];
  51.         }
  52.         $licenseTypes = ['BASIC''BASIC_PLUS'];
  53.         $paths = [];
  54.         // Visualizar Alertas
  55.         $paths array_merge $paths , ['knk_univ_alert_log_list''knk_univ_alert_log_delete'] );
  56.         // Configurar Alertas
  57.         $paths array_merge $paths , ['knk_univ_alert_config_list''knk_univ_alert_config_create''knk_univ_alert_config_show''knk_univ_alert_config_edit''knk_univ_alert_config_delete'] );
  58.         // Riego Autónomo
  59.         $paths array_merge $paths , ['knk_univ_season_logging_index''knk_univ_season_list''knk_univ_irrigation_plan_list''knk_univ_irrigation_unit_dashboard'] );
  60.         // Mapa
  61.         //$paths = array_merge ( $paths , ['knk_univ_map'] );
  62.         $paths array_merge $paths , ['knk_univ_map_player_render_popup''knk_univ_map_plot_render_popup''ROLE_KNK_UNIV_MAP_INTERACTIVE'] );
  63.         // Escritorio
  64.         $paths array_merge $paths , ['knk_univ_widget'] );
  65.         // Programas
  66.         $paths array_merge $paths , ['knk_univ_program_cedit''knk_univ_program_forecast'] );
  67.         // Tabla
  68.         $paths array_merge $paths , ['knk_univ_state_logging_table'] );
  69.         // Tanques
  70.         $paths array_merge $paths , ['knk_univ_tank_cedit'] );
  71.         // Parcelas
  72.         $paths array_merge $paths , ['knk_univ_plot_cedit'] );
  73.         // Unidades
  74.         $paths array_merge $paths , ['knk_univ_irrigation_unit_list'] );
  75.         $paths2 = ['knk_univ_state_logging_chart_list''knk_univ_state_logging_chart_volume_list''knk_univ_player_cedit'];
  76.         if(in_array($licenseType$licenseTypes) && in_array($pathName$paths)){
  77.             $bool false;
  78.         }else if(in_array($licenseType$licenseTypes) && in_array($pathName$paths2)){
  79.             if(array_key_exists('sensor'$subject)){
  80.                 $sensor $subject['sensor'];
  81.                 if($sensor != null && $sensor->getPlayer() && !$sensor->getPlayer()->getIsMaster()){
  82.                     $bool false;
  83.                 }
  84.             } else if(array_key_exists('flowmeter'$subject)){
  85.                 $flowmeter $subject['flowmeter'];
  86.                 if($flowmeter != null && $flowmeter->getPlayer() && !$flowmeter->getPlayer()->getIsMaster()){
  87.                     $bool false;
  88.                 }
  89.             } else if(array_key_exists('player'$subject)){
  90.                 $player $subject['player'];
  91.                 if($player != null && !$player->getIsMaster()){
  92.                     $bool false;
  93.                 }
  94.             }
  95.         }
  96.         return $bool;
  97.     }
  98. }