Skip to content
New issue

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

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

Already on GitHub? # to your account

EZP-28553: Adds Permission Awareness to all menus #189

Merged
merged 1 commit into from
Dec 15, 2017
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
* system:
* default: # configuration per siteaccess or siteaccess group
* location_ids:
* content: 2
* media: 43
* users: 5
* ```
Expand All @@ -39,7 +38,6 @@ public function addSemanticConfig(NodeBuilder $nodeBuilder)
->arrayNode('location_ids')
->info('System locations id configuration')
->children()
->scalarNode('content')->isRequired()->end()
->scalarNode('media')->isRequired()->end()
->scalarNode('users')->isRequired()->end()
->end()
Expand All @@ -56,7 +54,7 @@ public function mapConfig(array &$scopeSettings, $currentScope, ContextualizerIn
}

$settings = $scopeSettings['location_ids'];
$keys = ['content', 'media', 'users'];
$keys = ['media', 'users'];

foreach ($keys as $key) {
if (!isset($settings[$key]) || empty($settings[$key])) {
Expand Down
6 changes: 6 additions & 0 deletions src/bundle/Resources/config/services/menu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ services:
autoconfigure: true
public: true

#
# Menu Item Factory
#

EzSystems\EzPlatformAdminUi\Menu\MenuItemFactory: ~

#
# Menu Builders
#
Expand Down
15 changes: 5 additions & 10 deletions src/lib/Menu/AbstractBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
namespace EzSystems\EzPlatformAdminUi\Menu;

use EzSystems\EzPlatformAdminUi\Menu\Event\ConfigureMenuEvent;
use Knp\Menu\FactoryInterface;
use Knp\Menu\ItemInterface;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
Expand All @@ -19,17 +18,17 @@
*/
abstract class AbstractBuilder
{
/** @var FactoryInterface */
/** @var MenuItemFactory */
protected $factory;

/** @var EventDispatcherInterface */
protected $eventDispatcher;

/**
* @param FactoryInterface $factory
* @param MenuItemFactory $factory
* @param EventDispatcherInterface $eventDispatcher
*/
public function __construct(FactoryInterface $factory, EventDispatcherInterface $eventDispatcher)
public function __construct(MenuItemFactory $factory, EventDispatcherInterface $eventDispatcher)
{
$this->factory = $factory;
$this->eventDispatcher = $eventDispatcher;
Expand All @@ -41,13 +40,9 @@ public function __construct(FactoryInterface $factory, EventDispatcherInterface
*
* @return ItemInterface
*/
protected function createMenuItem(string $id, array $options): ItemInterface
protected function createMenuItem(string $id, array $options): ?ItemInterface
{
$defaults = [
'extras' => ['translation_domain' => 'menu'],
];

return $this->factory->createItem($id, array_merge_recursive($defaults, $options));
return $this->factory->createItem($id, $options);
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/lib/Menu/ContentRightSidebarBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use InvalidArgumentException;
use JMS\TranslationBundle\Model\Message;
use JMS\TranslationBundle\Translation\TranslationContainerInterface;
use Knp\Menu\FactoryInterface;
use Knp\Menu\ItemInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

Expand All @@ -37,11 +36,11 @@ class ContentRightSidebarBuilder extends AbstractBuilder implements TranslationC

/**
* @param PermissionResolver $permissionResolver
* @param FactoryInterface $factory
* @param MenuItemFactory $factory
* @param EventDispatcherInterface $eventDispatcher
*/
public function __construct(
FactoryInterface $factory,
MenuItemFactory $factory,
EventDispatcherInterface $eventDispatcher,
PermissionResolver $permissionResolver
) {
Expand Down
124 changes: 75 additions & 49 deletions src/lib/Menu/MainMenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@

use eZ\Publish\Core\MVC\ConfigResolverInterface;
use EzSystems\EzPlatformAdminUi\Menu\Event\ConfigureMenuEvent;
use InvalidArgumentException;
use JMS\TranslationBundle\Model\Message;
use JMS\TranslationBundle\Translation\TranslationContainerInterface;
use Knp\Menu\FactoryInterface;
use Knp\Menu\ItemInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

Expand Down Expand Up @@ -42,12 +40,12 @@ class MainMenuBuilder extends AbstractBuilder implements TranslationContainerInt
private $configResolver;

/**
* @param FactoryInterface $factory
* @param MenuItemFactory $factory
* @param EventDispatcherInterface $eventDispatcher
* @param ConfigResolverInterface $configResolver
*/
public function __construct(
FactoryInterface $factory,
MenuItemFactory $factory,
EventDispatcherInterface $eventDispatcher,
ConfigResolverInterface $configResolver
) {
Expand All @@ -66,45 +64,71 @@ protected function getConfigureEventName(): string
*
* @return ItemInterface
*
* @throws InvalidArgumentException
* @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
public function createStructure(array $options): ItemInterface
{
/** @var ItemInterface|ItemInterface[] $menu */
$menu = $this->factory->createItem('root');

$menu->setChildren([
self::ITEM_CONTENT => $this->createMenuItem(
self::ITEM_CONTENT => $this->factory->createItem(
self::ITEM_CONTENT,
[]
),
self::ITEM_ADMIN => $this->createMenuItem(
self::ITEM_ADMIN => $this->factory->createItem(
self::ITEM_ADMIN,
[]
),
]);

$menu[self::ITEM_CONTENT]->setChildren([
self::ITEM_CONTENT__CONTENT_STRUCTURE => $this->createMenuItem(
self::ITEM_CONTENT__CONTENT_STRUCTURE,
[
'route' => '_ezpublishLocation',
'routeParameters' => [
'locationId' => $this->configResolver->getParameter('location_ids.content'),
],
]
),
self::ITEM_CONTENT__MEDIA => $this->createMenuItem(
self::ITEM_CONTENT__MEDIA,
[
'route' => '_ezpublishLocation',
'routeParameters' => [
'locationId' => $this->configResolver->getParameter('location_ids.media'),
],
]
),
]);
$menu[self::ITEM_CONTENT]->setChildren($this->getContentMenuItems());
$menu[self::ITEM_ADMIN]->setChildren($this->getAdminMenuItems());

return $menu;
}

/**
* @return array
*/
private function getContentMenuItems(): array
{
$menuItems = [];

$rootContentId = $this->configResolver->getParameter('content.tree_root.location_id');
$rootMediaId = $this->configResolver->getParameter('location_ids.media');

$contentStructureItem = $this->factory->createLocationMenuItem(
self::ITEM_CONTENT__CONTENT_STRUCTURE,
$rootContentId,
['label' => self::ITEM_CONTENT__CONTENT_STRUCTURE]
);
$mediaItem = $this->factory->createLocationMenuItem(
self::ITEM_CONTENT__MEDIA,
$rootMediaId,
['label' => self::ITEM_CONTENT__MEDIA]
);

$menu[self::ITEM_ADMIN]->setChildren([
if (null !== $contentStructureItem) {
$menuItems[$contentStructureItem->getName()] = $contentStructureItem;
}

if (null !== $mediaItem) {
$menuItems[$mediaItem->getName()] = $mediaItem;
}

return $menuItems;
}

/**
* @return array
*/
private function getAdminMenuItems(): array
{
$menuItems = [
self::ITEM_ADMIN__SYSTEMINFO => $this->createMenuItem(
self::ITEM_ADMIN__SYSTEMINFO,
['route' => 'ezplatform.systeminfo']
Expand Down Expand Up @@ -143,30 +167,32 @@ public function createStructure(array $options): ItemInterface
]]
),
self::ITEM_ADMIN__CONTENT_TYPES => $this->createMenuItem(
self::ITEM_ADMIN__CONTENT_TYPES,
['route' => 'ezplatform.content_type_group.list', 'extras' => [
'routes' => [
'update' => 'ezplatform.content_type_group.update',
'view' => 'ezplatform.content_type_group.view',
'create' => 'ezplatform.content_type_group.create',
'content_type_add' => 'ezplatform.content_type.add',
'content_type_view' => 'ezplatform.content_type.view',
'content_type_edit' => 'ezplatform.content_type.edit',
],
]]
),
self::ITEM_ADMIN__USERS => $this->createMenuItem(
self::ITEM_ADMIN__USERS,
[
'route' => '_ezpublishLocation',
'routeParameters' => [
'locationId' => $this->configResolver->getParameter('location_ids.users'),
self::ITEM_ADMIN__CONTENT_TYPES,
['route' => 'ezplatform.content_type_group.list', 'extras' => [
'routes' => [
'update' => 'ezplatform.content_type_group.update',
'view' => 'ezplatform.content_type_group.view',
'create' => 'ezplatform.content_type_group.create',
'content_type_add' => 'ezplatform.content_type.add',
'content_type_view' => 'ezplatform.content_type.view',
'content_type_edit' => 'ezplatform.content_type.edit',
],
]
]]
),
]);
];

return $menu;
$rootUsersId = $this->configResolver->getParameter('location_ids.users');
$usersItem = $this->factory->createLocationMenuItem(
self::ITEM_ADMIN__USERS,
$rootUsersId,
['label' => self::ITEM_ADMIN__USERS]
);

if (null !== $usersItem) {
$menuItems[$usersItem->getName()] = $usersItem;
}

return $menuItems;
}

/**
Expand Down
77 changes: 77 additions & 0 deletions src/lib/Menu/MenuItemFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\EzPlatformAdminUi\Menu;

use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\PermissionResolver;
use Knp\Menu\FactoryInterface;
use Knp\Menu\ItemInterface;

class MenuItemFactory implements FactoryInterface
{
/** @var FactoryInterface */
protected $factory;

/** @var PermissionResolver */
private $permissionResolver;

/** @var LocationService */
private $locationService;

/**
* @param FactoryInterface $factory
* @param PermissionResolver $permissionResolver
* @param LocationService $locationService
*/
public function __construct(
FactoryInterface $factory,
PermissionResolver $permissionResolver,
LocationService $locationService
) {
$this->factory = $factory;
$this->permissionResolver = $permissionResolver;
$this->locationService = $locationService;
}

/**
* Creates Location menu item only when user has content:read permission.
*
* @param string $name
* @param int $locationId
* @param array $options
*
* @return ItemInterface|null
*/
public function createLocationMenuItem(string $name, int $locationId, array $options = []): ?ItemInterface
{
try {
$location = $this->locationService->loadLocation($locationId);
$contentInfo = $location->getContentInfo();
$canRead = $this->permissionResolver->canUser('content', 'read', $contentInfo);
} catch (\Exception $e) {
return null;
}

$defaults = [
'route' => '_ezpublishLocation',
'routeParameters' => [
'locationId' => $locationId,
],
];

return $this->createItem($name, array_merge_recursive($defaults, $options));
}

public function createItem($name, array $options = [])
{
$defaults = [
'extras' => ['translation_domain' => 'menu'],
];

return $this->factory->createItem($name, array_merge_recursive($defaults, $options));
}
}
5 changes: 2 additions & 3 deletions src/lib/Menu/TrashRightSidebarBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use InvalidArgumentException;
use JMS\TranslationBundle\Model\Message;
use JMS\TranslationBundle\Translation\TranslationContainerInterface;
use Knp\Menu\FactoryInterface;
use Knp\Menu\ItemInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

Expand All @@ -35,13 +34,13 @@ class TrashRightSidebarBuilder extends AbstractBuilder implements TranslationCon
private $trashService;

/**
* @param FactoryInterface $factory
* @param MenuItemFactory $factory
* @param EventDispatcherInterface $eventDispatcher
* @param PermissionResolver $permissionResolver
* @param TrashService $trashService
*/
public function __construct(
FactoryInterface $factory,
MenuItemFactory $factory,
EventDispatcherInterface $eventDispatcher,
PermissionResolver $permissionResolver,
TrashService $trashService
Expand Down
Loading