From 94bcc913e27a5375cadd06c693db29592c724062 Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Sun, 14 Jul 2013 17:20:36 +0200 Subject: [PATCH 1/2] adjust to publish workflow refactoring --- ContentAwareFactory.php | 23 +++++++++-------- Document/MenuNode.php | 5 ++-- Resources/config/phpcr-menu.xml | 2 +- Tests/Unit/ContentAwareFactoryTest.php | 34 +++++++++++++++++--------- Tests/Unit/Document/MenuNodeTest.php | 4 +-- composer.json | 2 +- 6 files changed, 43 insertions(+), 27 deletions(-) diff --git a/ContentAwareFactory.php b/ContentAwareFactory.php index 6a030572..302df8e6 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 @@ -47,9 +48,9 @@ class ContentAwareFactory extends RouterAwareFactory private $logger; /** - * @var PublishWorkflowCheckerInterface + * @var SecurityContextInterface */ - private $publishChecker; + private $securityContext; /** * Whether to return null or a MenuItem without any URL if no URL can be @@ -67,15 +68,15 @@ class ContentAwareFactory extends RouterAwareFactory * content is set */ public function __construct( - UrlGeneratorInterface $generator, - UrlGeneratorInterface $contentRouter, - PublishWorkflowCheckerInterface $publishChecker, + UrlGeneratorInterface $generator, + UrlGeneratorInterface $contentRouter, + SecurityContextInterface $securityContext = null, LoggerInterface $logger ) { parent::__construct($generator); $this->contentRouter = $contentRouter; - $this->publishChecker = $publishChecker; + $this->securityContext = $securityContext; $this->logger = $logger; } @@ -131,7 +132,9 @@ public function createFromNode(NodeInterface $node) } foreach ($node->getChildren() as $childNode) { - if (false === $this->publishChecker->checkIsPublished($childNode)) { + if ($this->securityContext + && ! $this->securityContext->isGranted(PublishWorkflowChecker::VIEW_ATTRIBUTE, $childNode) + ) { continue; } @@ -171,8 +174,8 @@ public function createItem($name, array $options = array(), NodeInterface $node if (empty($options['uri']) && empty($options['route'])) { 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 da8b427c..eeeca909 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 709d4896..6479f9ec 100644 --- a/Resources/config/phpcr-menu.xml +++ b/Resources/config/phpcr-menu.xml @@ -26,7 +26,7 @@ - + %cmf_menu.allow_empty_items% diff --git a/Tests/Unit/ContentAwareFactoryTest.php b/Tests/Unit/ContentAwareFactoryTest.php index fbcb28e1..44e705a6 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); @@ -155,7 +167,7 @@ public function testCreateItem($options) $this->assertNull($res); return; } - } + } } $this->assertInstanceOf('\Knp\Menu\MenuItem', $res); diff --git a/Tests/Unit/Document/MenuNodeTest.php b/Tests/Unit/Document/MenuNodeTest.php index 33b00133..57d1e33c 100644 --- a/Tests/Unit/Document/MenuNodeTest.php +++ b/Tests/Unit/Document/MenuNodeTest.php @@ -61,7 +61,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'); @@ -69,7 +69,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 3d93d5a9..b1d74eb3 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.*", From 33dd15aa8967eb2a00dace474437cd0aa53b31dd Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Mon, 15 Jul 2013 20:06:30 +0200 Subject: [PATCH 2/2] we have a no-op service rather than no publish workflow service if its disabled --- ContentAwareFactory.php | 29 +++++++++++++++++++++++------ Resources/config/phpcr-menu.xml | 2 +- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/ContentAwareFactory.php b/ContentAwareFactory.php index 1111724c..c08d1d74 100644 --- a/ContentAwareFactory.php +++ b/ContentAwareFactory.php @@ -57,6 +57,13 @@ class ContentAwareFactory extends RouterAwareFactory */ 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 * found for a MenuNode. @@ -66,16 +73,17 @@ 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, - SecurityContextInterface $securityContext = null, + SecurityContextInterface $securityContext, LoggerInterface $logger ) { @@ -108,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. * @@ -149,9 +168,7 @@ public function createFromNode(NodeInterface $node) } foreach ($node->getChildren() as $childNode) { - if ($this->securityContext - && ! $this->securityContext->isGranted(PublishWorkflowChecker::VIEW_ATTRIBUTE, $childNode) - ) { + if (!$this->securityContext->isGranted($this->publishWorkflowPermission, $childNode)) { continue; } diff --git a/Resources/config/phpcr-menu.xml b/Resources/config/phpcr-menu.xml index acd8303d..215a5c6f 100644 --- a/Resources/config/phpcr-menu.xml +++ b/Resources/config/phpcr-menu.xml @@ -28,7 +28,7 @@ - + %cmf_menu.allow_empty_items%