Last active
December 18, 2015 02:28
-
-
Save eminetto/5710894 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Api\PreProcessor; | |
use Zend\Mvc\MvcEvent; | |
/** | |
* Responsável por fazer o pré-processamento das requisições da APi | |
* | |
* @category Api | |
* @package PreProcessor | |
* @author Elton Minetto<eminetto@coderockr.com> | |
*/ | |
class PreProcessor | |
{ | |
/** | |
* Executado no pré-processamento, antes de qualquer action | |
* Verifica se o usuário tem permissão de acessar o recurso | |
* | |
* @param MvcEvent $e | |
* @return null|\Zend\Http\PhpEnvironment\Response | |
*/ | |
public function process(MvcEvent $e) | |
{ | |
$this->configureEnvironment($e); | |
$routeMatch = $e->getRouteMatch(); | |
$routeName = $routeMatch->getMatchedRouteName(); | |
$module = $routeMatch->getParam('module', false); | |
//verifica se a entidade ou o service sendo invocados estão disponíveis | |
switch ($routeName) { | |
case 'restful': | |
$request = $routeMatch->getParam('entity', false); | |
break; | |
} | |
$moduleConfig = null; | |
switch ($routeName) { | |
case 'restful': | |
$moduleConfig = include __DIR__ . '/../../../../' . ucfirst($module) . '/config/entities.config.php'; | |
break; | |
} | |
if (! $moduleConfig) { | |
throw new \Exception("Caminho inválido"); | |
} | |
if (! isset($moduleConfig[$request])) { | |
throw new \Exception("Não permitido"); | |
} | |
//acesso requer um token válido e permissões de acesso | |
if ($moduleConfig[$request]['authorization'] == 1) { | |
$token = $e->getRequest()->getHeaders('Authorization'); | |
if (!$token) { | |
$response = $e->getResponse(); | |
$response->setStatusCode(401); | |
return $response; | |
} | |
return $this->checkAuthorization($token, $module . '.' . $request); | |
} | |
return true; | |
} | |
/** | |
* Executa o teste da autorização | |
* @param Auth $auth Serviço de auth | |
* @param Header $token Token enviado na requisição | |
* @param string $request Serviço sendo requisitado | |
* @return boolean | |
*/ | |
private function checkAuthorization($token, $request) | |
{ | |
/* definir a estratégia de autenticação e autorização*/ | |
return true; | |
} | |
/** | |
* Verifica se a api está sendo acessada de um ambiente de testes | |
* e configura o ambiente | |
* @param MvcEvent $e Evento | |
* @return void | |
*/ | |
private function configureEnvironment(MvcEvent $e) | |
{ | |
if ( !method_exists($e->getRequest(), 'getHeaders')) { | |
return; | |
} | |
$env = $e->getRequest()->getHeaders('Environment'); | |
if ($env) { | |
switch ($env->getFieldValue()) { | |
case 'testing': | |
putenv("ENV=testing"); | |
break; | |
case 'jenkins': | |
putenv("ENV=jenkins"); | |
break; | |
} | |
} | |
return; | |
} | |
}<?php | |
namespace Api\PreProcessor; | |
use Zend\Mvc\MvcEvent; | |
/** | |
* Responsável por fazer o pré-processamento das requisições da APi | |
* | |
* @category Api | |
* @package PreProcessor | |
* @author Elton Minetto<eminetto@coderockr.com> | |
*/ | |
class PreProcessor | |
{ | |
/** | |
* Executado no pré-processamento, antes de qualquer action | |
* Verifica se o usuário tem permissão de acessar o recurso | |
* | |
* @param MvcEvent $e | |
* @return null|\Zend\Http\PhpEnvironment\Response | |
*/ | |
public function process(MvcEvent $e) | |
{ | |
$this->configureEnvironment($e); | |
$routeMatch = $e->getRouteMatch(); | |
$routeName = $routeMatch->getMatchedRouteName(); | |
$module = $routeMatch->getParam('module', false); | |
//verifica se a entidade ou o service sendo invocados estão disponíveis | |
switch ($routeName) { | |
case 'restful': | |
$request = $routeMatch->getParam('entity', false); | |
break; | |
} | |
$moduleConfig = null; | |
switch ($routeName) { | |
case 'restful': | |
$moduleConfig = include __DIR__ . '/../../../../' . ucfirst($module) . '/config/entities.config.php'; | |
break; | |
} | |
if (! $moduleConfig) { | |
throw new \Exception("Caminho inválido"); | |
} | |
if (! isset($moduleConfig[$request])) { | |
throw new \Exception("Não permitido"); | |
} | |
//acesso requer um token válido e permissões de acesso | |
if ($moduleConfig[$request]['authorization'] == 1) { | |
$token = $e->getRequest()->getHeaders('Authorization'); | |
if (!$token) { | |
throw new \Exception("Token requirido"); | |
} | |
$this->checkAuthorization($token, $module . '.' . $request); | |
} | |
return true; | |
} | |
/** | |
* Executa o teste da autorização | |
* @param Auth $auth Serviço de auth | |
* @param Header $token Token enviado na requisição | |
* @param string $request Serviço sendo requisitado | |
* @return boolean | |
*/ | |
private function checkAuthorization($token, $request) | |
{ | |
/* definir a estratégia de autenticação e autorização*/ | |
return true; | |
} | |
/** | |
* Verifica se a api está sendo acessada de um ambiente de testes | |
* e configura o ambiente | |
* @param MvcEvent $e Evento | |
* @return void | |
*/ | |
private function configureEnvironment(MvcEvent $e) | |
{ | |
if ( !method_exists($e->getRequest(), 'getHeaders')) { | |
return; | |
} | |
$env = $e->getRequest()->getHeaders('Environment'); | |
if ($env) { | |
switch ($env->getFieldValue()) { | |
case 'testing': | |
putenv("ENV=testing"); | |
break; | |
case 'jenkins': | |
putenv("ENV=jenkins"); | |
break; | |
} | |
} | |
return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment