Skip to content

Instantly share code, notes, and snippets.

@sergeylunev
Created October 12, 2012 19:25
Show Gist options
  • Save sergeylunev/3880989 to your computer and use it in GitHub Desktop.
Save sergeylunev/3880989 to your computer and use it in GitHub Desktop.
<?php
namespace App\DefaultBundle\Listener;
use Doctrine\Common\Annotations\Reader;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use App\DefaultBundle\Annotation\MinAccess;
class MinAccessListener
{
private $reader;
private $security;
private $routing;
private $session;
public function __construct(Reader $reader, $security, $routing, $session)
{
$this->reader = $reader;
$this->security = $security;
$this->routing = $routing;
$this->session = $session;
}
public function onKernelController(FilterControllerEvent $event)
{
if (!is_array($controller = $event->getController())) {
return;
}
$method = new \ReflectionMethod($controller[0], $controller[1]);
foreach ($this->reader->getMethodAnnotations($method) as $annotation) {
if ($annotation instanceof MinAccess) {
if (!$annotation->execute($this->security, $this->session, $this->routing)) {
$this->session->setFlash('pre_organisation', 'common.message.pre_organisation');
return new RedirectResponse($this->routing->generate('address_edit'));
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment