diff --git a/ContentAwareFactory.php b/ContentAwareFactory.php index 9ba25aa8..c08d1d74 100644 --- a/ContentAwareFactory.php +++ b/ContentAwareFactory.php @@ -11,11 +11,12 @@ use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\Exception\RouteNotFoundException; use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\Security\Core\SecurityContextInterface; +use Symfony\Cmf\Bundle\CoreBundle\PublishWorkflow\PublishWorkflowChecker; use Psr\Log\LoggerInterface; use Symfony\Cmf\Bundle\MenuBundle\Voter\VoterInterface; -use Symfony\Cmf\Bundle\CoreBundle\PublishWorkflow\PublishWorkflowCheckerInterface; /** * This factory builds menu items from the menu nodes and builds urls based on @@ -52,9 +53,16 @@ class ContentAwareFactory extends RouterAwareFactory private $logger; /** - * @var PublishWorkflowCheckerInterface + * @var SecurityContextInterface */ - private $publishChecker; + private $securityContext; + + /** + * The permission to check for when doing the publish workflow check. + * + * @var string + */ + private $publishWorkflowPermission = PublishWorkflowChecker::VIEW_ATTRIBUTE; /** * Whether to return null or a MenuItem without any URL if no URL can be @@ -65,22 +73,23 @@ class ContentAwareFactory extends RouterAwareFactory private $allowEmptyItems; /** - * @param ContainerInterface $container to fetch the request in order to determine - * whether this is the current menu item * @param UrlGeneratorInterface $generator for the parent class * @param UrlGeneratorInterface $contentRouter to generate routes when * content is set + * @param SecurityContextInterface $securityContext the publish workflow + * checker to check if menu items are published. + * @param LoggerInterface $logger */ public function __construct( UrlGeneratorInterface $generator, UrlGeneratorInterface $contentRouter, - PublishWorkflowCheckerInterface $publishChecker, + SecurityContextInterface $securityContext, LoggerInterface $logger ) { parent::__construct($generator); $this->contentRouter = $contentRouter; - $this->publishChecker = $publishChecker; + $this->securityContext = $securityContext; $this->logger = $logger; $this->linkTypes = array('route', 'uri', 'content'); } @@ -107,6 +116,17 @@ public function setAllowEmptyItems($allowEmptyItems) $this->allowEmptyItems = $allowEmptyItems; } + /** + * What attribute to use in the publish workflow check. This typically + * is VIEW or VIEW_ANONYMOUS. + * + * @param string $attribute + */ + public function setPublishWorkflowPermission($attribute) + { + $this->publishWorkflowPermission = $attribute; + } + /** * Add a voter to decide on current item. * @@ -148,7 +168,7 @@ public function createFromNode(NodeInterface $node) } foreach ($node->getChildren() as $childNode) { - if (false === $this->publishChecker->checkIsPublished($childNode)) { + if (!$this->securityContext->isGranted($this->publishWorkflowPermission, $childNode)) { continue; } @@ -197,8 +217,8 @@ public function createItem($name, array $options = array()) case 'content': try { $options['uri'] = $this->contentRouter->generate( - $options['content'], - $options['routeParameters'], + $options['content'], + $options['routeParameters'], $options['routeAbsolute'] ); } catch (RouteNotFoundException $e) { diff --git a/Document/MenuNode.php b/Document/MenuNode.php index 09d0e087..a452f69c 100644 --- a/Document/MenuNode.php +++ b/Document/MenuNode.php @@ -3,7 +3,8 @@ namespace Symfony\Cmf\Bundle\MenuBundle\Document; use Knp\Menu\NodeInterface; -use Symfony\Cmf\Bundle\CoreBundle\PublishWorkflow\PublishWorkflowInterface; +use Symfony\Cmf\Bundle\CoreBundle\PublishWorkflow\PublishableWriteInterface; +use Symfony\Cmf\Bundle\CoreBundle\PublishWorkflow\PublishTimePeriodWriteInterface; /** * This class represents a menu node for the cmf. @@ -11,7 +12,7 @@ * @author Uwe Jäger * @author Daniel Leech */ -class MenuNode implements NodeInterface, PublishWorkflowInterface +class MenuNode implements NodeInterface, PublishTimePeriodWriteInterface, PublishableWriteInterface { /** * Id of this menu node. diff --git a/Resources/config/phpcr-menu.xml b/Resources/config/phpcr-menu.xml index 2ed22185..215a5c6f 100644 --- a/Resources/config/phpcr-menu.xml +++ b/Resources/config/phpcr-menu.xml @@ -28,7 +28,7 @@ - + %cmf_menu.allow_empty_items% diff --git a/Tests/Unit/ContentAwareFactoryTest.php b/Tests/Unit/ContentAwareFactoryTest.php index 77138bd5..56eccb01 100644 --- a/Tests/Unit/ContentAwareFactoryTest.php +++ b/Tests/Unit/ContentAwareFactoryTest.php @@ -7,17 +7,27 @@ class ContentAwareFactoryTest extends \PHPUnit_Framework_Testcase { + private $urlGenerator; + private $contentUrlGenerator; + private $securityContext; + private $logger; + + private $node1; + private $node2; + private $node3; + private $content; + public function setUp() { - $this->pwfc = $this->getMock( - 'Symfony\Cmf\Bundle\CoreBundle\PublishWorkflow\PublishWorkflowCheckerInterface' - ); $this->urlGenerator = $this->getMock( 'Symfony\Component\Routing\Generator\UrlGeneratorInterface' ); $this->contentUrlGenerator = $this->getMock( 'Symfony\Component\Routing\Generator\UrlGeneratorInterface' ); + $this->securityContext = $this->getMock( + 'Symfony\Component\Security\Core\SecurityContext' + ); $this->logger = $this->getMock( 'Psr\Log\LoggerInterface' ); @@ -25,7 +35,7 @@ public function setUp() $this->factory = new ContentAwareFactory( $this->urlGenerator, $this->contentUrlGenerator, - $this->pwfc, + $this->securityContext, $this->logger, false // refactore this empty items option ); @@ -77,9 +87,10 @@ public function testCreateFromNode($options) ->method('getChildren') ->will($this->returnValue(array())); - $mock = $this->pwfc->expects($this->at(0)) - ->method('checkIsPublished') - ->with($this->node2); + $mock = $this->securityContext->expects($this->at(0)) + ->method('isGranted') + ->with(array('VIEW', $this->node2)) + ; if ($options['node2_is_published']) { $mock->will($this->returnValue(true)); @@ -92,9 +103,10 @@ public function testCreateFromNode($options) $mock->will($this->returnValue(false)); } - $this->pwfc->expects($this->at(1)) - ->method('checkIsPublished') - ->with($this->node3); + $this->securityContext->expects($this->at(1)) + ->method('isGranted') + ->with(array('VIEW', $this->node3)) + ; $res = $this->factory->createFromNode($this->node1); $this->assertInstanceOf('Knp\Menu\MenuItem', $res); @@ -180,9 +192,9 @@ protected function prepareCreateItemTests($name, $options) is_null($options['route']) && is_null($options['content']) && !in_array($options['linkType'], array( - 'route', - 'uri', - 'content', + 'route', + 'uri', + 'content', '' )) ) { @@ -196,7 +208,7 @@ protected function prepareCreateItemTests($name, $options) } if ( - null == $options['linkType'] && + null == $options['linkType'] && empty($options['uri']) && !empty($options['route']) ) { @@ -212,7 +224,7 @@ protected function prepareCreateItemTests($name, $options) } if ( - null === $options['linkType'] && + null === $options['linkType'] && empty($options['uri']) && empty($options['route']) && !empty($options['content']) diff --git a/Tests/Unit/Document/MenuNodeTest.php b/Tests/Unit/Document/MenuNodeTest.php index d3a61dd8..7e2312ee 100644 --- a/Tests/Unit/Document/MenuNodeTest.php +++ b/Tests/Unit/Document/MenuNodeTest.php @@ -73,7 +73,7 @@ public function testAddChild() $this->assertSame($c2, $ret); } - public function testPublishWorkflowInterface() + public function testPublishTimePeriodInterface() { $startDate = new \DateTime('2013-01-01'); $endDate = new \DateTime('2013-02-01'); @@ -81,7 +81,7 @@ public function testPublishWorkflowInterface() $n = new MenuNode; $this->assertInstanceOf( - 'Symfony\Cmf\Bundle\CoreBundle\PublishWorkflow\PublishWorkflowInterface', + 'Symfony\Cmf\Bundle\CoreBundle\PublishWorkflow\PublishTimePeriodInterface', $n ); diff --git a/composer.json b/composer.json index d4524dc5..f3b9528e 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "doctrine/phpcr-bundle": "1.0.*", "doctrine/phpcr-odm": "1.0.*", "knplabs/knp-menu-bundle": "1.1.*", - "symfony-cmf/core-bundle": "1.0.*" + "symfony-cmf/core-bundle": "~1.0.0-beta2" }, "require-dev": { "symfony-cmf/testing": "1.0.*",