Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NodeBundle] Fix not checking on online status in some cases #551

Merged
merged 1 commit into from
Jul 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/Kunstmaan/NodeBundle/Event/SlugSecurityEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,83 @@
final class SlugSecurityEvent extends Event
{

private $node;

private $nodeTranslation;

private $entity;

private $request;

/**
* @return mixed
*/
public function getNode()
{
return $this->node;
}

/**
* @param mixed $node
*/
public function setNode($node)
{
$this->node = $node;

return $this;
}

/**
* @return mixed
*/
public function getNodeTranslation()
{
return $this->nodeTranslation;
}

/**
* @param mixed $nodeTranslation
*/
public function setNodeTranslation($nodeTranslation)
{
$this->nodeTranslation = $nodeTranslation;

return $this;
}

/**
* @return mixed
*/
public function getEntity()
{
return $this->entity;
}

/**
* @param mixed $entity
*/
public function setEntity($entity)
{
$this->entity = $entity;

return $this;
}

/**
* @return mixed
*/
public function getRequest()
{
return $this->request;
}

/**
* @param mixed $request
*/
public function setRequest($request)
{
$this->request = $request;

return $this;
}
}
5 changes: 5 additions & 0 deletions src/Kunstmaan/NodeBundle/EventListener/SlugListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public function onKernelController(FilterControllerEvent $event)

// Do security check by firing an event that gets handled by the SlugSecurityListener
$securityEvent = new SlugSecurityEvent();
$securityEvent->setNode($nodeTranslation->getNode())
->setEntity($entity)
->setRequest($request)
->setNodeTranslation($nodeTranslation);

$this->eventDispatcher->dispatch(Events::SLUG_SECURITY, $securityEvent);

// Set the right controller
Expand Down
27 changes: 18 additions & 9 deletions src/Kunstmaan/NodeBundle/EventListener/SlugSecurityListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@


use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionMap;
use Kunstmaan\NodeBundle\Event\SlugSecurityEvent;
use Kunstmaan\NodeBundle\Helper\NodeMenu;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\HttpFoundation\RequestStack;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\Security\Core\SecurityContextInterface;

/**
* Class SlugSecurityListener
* @package Kunstmaan\NodeBundle\EventListener
Expand Down Expand Up @@ -36,36 +40,41 @@ class SlugSecurityListener
protected $acl;

/**
* @param RequestStack $requestStack
* @param EntityManager $entityManager
* @param $securityContext
* @param $acl
*/
public function __construct(RequestStack $requestStack,EntityManager $entityManager, SecurityContext $securityContext, $acl)
public function __construct(EntityManager $entityManager, SecurityContext $securityContext, $acl)
{
$this->securityContext = $securityContext;
$this->request = $requestStack->getCurrentRequest();
$this->em = $entityManager;
$this->acl = $acl;
}

/**
*
*/
public function onSlugSecurityEvent()
public function onSlugSecurityEvent(SlugSecurityEvent $event)
{
$node = $this->request->attributes->get('_nodeTranslation')->getNode();
$node = $event->getNode();
$nodeTranslation = $event->getNodeTranslation();
$request = $event->getRequest();

/* @var SecurityContextInterface $securityContext */
if (false === $this->securityContext->isGranted(PermissionMap::PERMISSION_VIEW, $node)) {
throw new AccessDeniedException('You do not have sufficient rights to access this page.');
}

$locale = $this->request->attributes->get('_locale');
$preview = $this->request->attributes->get('preview');
$locale = $request->attributes->get('_locale');
$preview = $request->attributes->get('preview');

// check if the requested node is online, else throw a 404 exception (only when not previewing!)
if (!$preview && !$nodeTranslation->isOnline()) {
throw new NotFoundHttpException("The requested page is not online");
}

$nodeMenu = new NodeMenu($this->em, $this->securityContext, $this->acl, $locale , $node, PermissionMap::PERMISSION_VIEW, $preview);
$nodeMenu = new NodeMenu($this->em, $this->securityContext, $this->acl, $locale , $node, PermissionMap::PERMISSION_VIEW, $preview);

$this->request->attributes->set('_nodeMenu', $nodeMenu);
$request->attributes->set('_nodeMenu', $nodeMenu);
}
}
2 changes: 1 addition & 1 deletion src/Kunstmaan/NodeBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ services:

kunstmaan_node.slug.security.listener:
class: Kunstmaan\NodeBundle\EventListener\SlugSecurityListener
arguments: ["@request_stack" , "@doctrine.orm.entity_manager", "@security.context" , "@kunstmaan_admin.acl.helper" ]
arguments: ["@doctrine.orm.entity_manager", "@security.context" , "@kunstmaan_admin.acl.helper" ]
tags:
- { name: kernel.event_listener, event: kunstmaan_node.slug.security, method: onSlugSecurityEvent }

Expand Down