<?php
/**
* This file is part of the Knitink Project package.
*
* Created by Globunet Soluciones Tecnologicas, SL. on 31/03/22. <info@globunet.com>
* Copyright (c) 2016 Knitink Technologies, SL. All rights reserved.
*/
namespace Knitink\UniverseBundle\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\HttpFoundation\Session\Session;
use Globunet\DinamicDatabaseBundle\Services\DatabaseConnector;
class LicenseVoter extends Voter
{
private $databaseConnector;
private $session;
public function __construct(DatabaseConnector $databaseConnector, Session $session)
{
$this->databaseConnector = $databaseConnector;
$this->session = $session;
}
protected function supports($attribute, $subject)
{
return ($subject instanceof Request || is_array($subject)) && $attribute === 'VOTER_KNITINK_UNIVERSE_LICENSE';
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
/**/
if(($user instanceof \Symfony\Component\Security\Core\User\User || $user instanceof \Knitink\CoreBundle\Entity\User)){
if(in_array("ROLE_SUPER_ADMIN", $user->getRoles()) || in_array("ROLE_KNK_UNIV_SUPER_ADMIN", $user->getRoles())){
return true;
}
}
if (!$user instanceof \Knitink\CoreBundle\Entity\User) {
return false;
}
/**/
return $this->checker($user, $attribute, $subject);
}
public function checker($user, $attribute, $subject, $em = null)
{
$bool = true;
if($subject instanceof Request){
$licenseType = $subject->getSession()->get("licenseType");
$pathName = $subject->attributes->get('_route');
}else{
$licenseType = $subject["licenseType"];
$pathName = $subject['route'];
}
$licenseTypes = ['BASIC', 'BASIC_PLUS'];
$paths = [];
// Visualizar Alertas
$paths = array_merge ( $paths , ['knk_univ_alert_log_list', 'knk_univ_alert_log_delete'] );
// Configurar Alertas
$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'] );
// Riego Autónomo
$paths = array_merge ( $paths , ['knk_univ_season_logging_index', 'knk_univ_season_list', 'knk_univ_irrigation_plan_list', 'knk_univ_irrigation_unit_dashboard'] );
// Mapa
//$paths = array_merge ( $paths , ['knk_univ_map'] );
$paths = array_merge ( $paths , ['knk_univ_map_player_render_popup', 'knk_univ_map_plot_render_popup', 'ROLE_KNK_UNIV_MAP_INTERACTIVE'] );
// Escritorio
$paths = array_merge ( $paths , ['knk_univ_widget'] );
// Programas
$paths = array_merge ( $paths , ['knk_univ_program_cedit', 'knk_univ_program_forecast'] );
// Tabla
$paths = array_merge ( $paths , ['knk_univ_state_logging_table'] );
// Tanques
$paths = array_merge ( $paths , ['knk_univ_tank_cedit'] );
// Parcelas
$paths = array_merge ( $paths , ['knk_univ_plot_cedit'] );
// Unidades
$paths = array_merge ( $paths , ['knk_univ_irrigation_unit_list'] );
$paths2 = ['knk_univ_state_logging_chart_list', 'knk_univ_state_logging_chart_volume_list', 'knk_univ_player_cedit'];
if(in_array($licenseType, $licenseTypes) && in_array($pathName, $paths)){
$bool = false;
}else if(in_array($licenseType, $licenseTypes) && in_array($pathName, $paths2)){
if(array_key_exists('sensor', $subject)){
$sensor = $subject['sensor'];
if($sensor != null && $sensor->getPlayer() && !$sensor->getPlayer()->getIsMaster()){
$bool = false;
}
} else if(array_key_exists('flowmeter', $subject)){
$flowmeter = $subject['flowmeter'];
if($flowmeter != null && $flowmeter->getPlayer() && !$flowmeter->getPlayer()->getIsMaster()){
$bool = false;
}
} else if(array_key_exists('player', $subject)){
$player = $subject['player'];
if($player != null && !$player->getIsMaster()){
$bool = false;
}
}
}
return $bool;
}
}