<?php
namespace Jabber\RestApiBundle\EventListener;
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
use Jabber\RestApiBundle\Security\BasicAuthenticator;
class SecurityListener {
public function onKernelControllerArguments(ControllerArgumentsEvent $event) {
// get controller and request arguments
// $namedArguments = $event->getRequest()->attributes->all();
// $controllerArguments = $event->getArguments()[0];
// $attributes = $controllerArguments->get('attributes');
$this->AuthorizeRequest($event->getRequest()->attributes->get('_controller'), $event->getRequest());
$event->request = $this->ContentTypeCheck($event->getRequest());
//get_class_methods($controllerArguments);
//$params = $attributes->parameters;
// $allHeaders = $event->getRequest()->headers->all();
// // if (str_starts_with($controllerArguments[0]['attributes']['parameters']['_controller'], "Jabber\\RestApiBundle")) {
// $controller = $event->getRequest()->attributes->get('_controller');
// if (!empty($event->getRequest()->headers->get('authorization'))) {
// $foobar = "abc";
// }
// }
}
private function AuthorizeRequest($controller, $request) {
if (str_starts_with($controller, 'Jabber\RestApiBundle')) {
$auth = new BasicAuthenticator();
$auth->authenticate($request);
}
}
/**
* Converts JSON requests to parameters
*
* @param Request $request
* @return Request
*/
private function ContentTypeCheck($request) {
if ($request->getContentType() == 'json') {
$request->request->replace(json_decode($request->getContent(), true));
}
return $request;
}
}