<?php
/*
* Este arquivo é parte do package Ambiente Bundle.
*
* (c) Laino Santos <lainosantos@recife.pe.gov.br>
*
* Para informações completas sobre copyright e licença, por favor ver o
* arquivo LICENCE distribuído juntamente com este código.
*/
namespace Emprel\Ambiente\Bundle\Event;
use Emprel\Ambiente\Bundle\Utils\Ambiente;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
/**
* Restringe acesso por ambiente.
*
* @package Emprel\Ambiente\Bundle\Event
* @author Laino Santos <lainosantos@recife.pe.gov.br>
*/
class RouteAmbienteListener
{
/**
* {@inheritdoc}
*/
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
$ambientes = $request->attributes->get('_ambientes_apenas', []);
$ambientesBlacklist = $request->attributes->get('_ambientes_blacklist', []);
if (is_array($ambientes) && count($ambientes) && !Ambiente::in($ambientes)) {
throw new AccessDeniedHttpException('Requisição não permitida para o ambiente.');
}
if (is_array($ambientesBlacklist) && count($ambientesBlacklist) && Ambiente::in($ambientesBlacklist)) {
throw new AccessDeniedHttpException('Requisição negada para o ambiente.');
}
}
}