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

IBX-715: Disabled buttons on the right sidebar if user has no permissions #1794

Merged
merged 8 commits into from
Aug 26, 2021
107 changes: 57 additions & 50 deletions src/lib/Menu/ContentRightSidebarBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

namespace EzSystems\EzPlatformAdminUi\Menu;

use eZ\Publish\API\Repository\ContentService;
use eZ\Publish\API\Repository\ContentTypeService;
use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\PermissionResolver;
use eZ\Publish\API\Repository\SearchService;
use eZ\Publish\API\Repository\Values\Content\Content;
Expand Down Expand Up @@ -55,21 +52,12 @@ class ContentRightSidebarBuilder extends AbstractBuilder implements TranslationC
/** @var \eZ\Publish\Core\MVC\ConfigResolverInterface */
private $configResolver;

/** @var \eZ\Publish\API\Repository\ContentTypeService */
private $contentTypeService;

/** @var \eZ\Publish\API\Repository\SearchService */
private $searchService;

/** @var \EzSystems\EzPlatformAdminUiBundle\Templating\Twig\UniversalDiscoveryExtension */
private $udwExtension;

/** @var \eZ\Publish\API\Repository\ContentService */
private $contentService;

/** @var \eZ\Publish\API\Repository\LocationService */
private $locationService;

/** @var \EzSystems\EzPlatformAdminUi\Permission\PermissionCheckerInterface */
private $permissionChecker;

Expand Down Expand Up @@ -98,11 +86,8 @@ public function __construct(
EventDispatcherInterface $eventDispatcher,
PermissionResolver $permissionResolver,
ConfigResolverInterface $configResolver,
ContentTypeService $contentTypeService,
SearchService $searchService,
UniversalDiscoveryExtension $udwExtension,
ContentService $contentService,
LocationService $locationService,
PermissionCheckerInterface $permissionChecker,
array $userContentTypeIdentifier,
array $userGroupContentTypeIdentifier
Expand All @@ -111,11 +96,8 @@ public function __construct(

$this->permissionResolver = $permissionResolver;
$this->configResolver = $configResolver;
$this->contentTypeService = $contentTypeService;
$this->searchService = $searchService;
$this->udwExtension = $udwExtension;
$this->contentService = $contentService;
$this->locationService = $locationService;
$this->permissionChecker = $permissionChecker;
$this->userContentTypeIdentifier = $userContentTypeIdentifier;
$this->userGroupContentTypeIdentifier = $userGroupContentTypeIdentifier;
Expand Down Expand Up @@ -153,6 +135,8 @@ public function createStructure(array $options): ItemInterface

$lookupLimitationsResult = $this->permissionChecker->getContentCreateLimitations($location);
$canCreate = $lookupLimitationsResult->hasAccess && $contentType->isContainer;
$rootLocation = $this->configResolver->getParameter('universal_discovery_widget_module.default_location_id');
$uwdConfig = $this->udwExtension->renderUniversalDiscoveryWidgetConfig('single_container');
$canEdit = $this->permissionResolver->canUser(
'content',
'edit',
Expand All @@ -177,7 +161,12 @@ public function createStructure(array $options): ItemInterface
$location->getContentInfo(),
[$location, $target]
);

$canHide = $this->permissionResolver->canUser(
'content',
'hide',
$content,
[$target]
);
$createAttributes = [
'class' => 'ez-btn--extra-actions ez-btn--create',
'data-actions' => 'create',
Expand All @@ -189,11 +178,29 @@ public function createStructure(array $options): ItemInterface
];
$copySubtreeAttributes = [
'class' => 'ez-btn--udw-copy-subtree',
'data-udw-config' => $this->udwExtension->renderUniversalDiscoveryWidgetConfig('single_container'),
'data-root-location' => $this->configResolver->getParameter(
'universal_discovery_widget_module.default_location_id'
),
'data-udw-config' => $uwdConfig,
'data-root-location' => $rootLocation,
];
$moveAttributes = [
'class' => 'btn--udw-move',
'data-udw-config' => $uwdConfig,
'data-root-location' => $rootLocation,
];
$copyAttributes = [
'class' => 'btn--udw-copy',
'data-udw-config' => $uwdConfig,
'data-root-location' => $rootLocation,
];
$createPolicies = $this->permissionResolver->hasAccess(
'content',
'create'
);
$manageLocationsPolicies = $this->permissionResolver->hasAccess(
'content',
'manage_locations'
);
$hasCreatePermission = !is_bool($createPolicies) || $createPolicies;
$hasManageLocationsPermission = !is_bool($manageLocationsPolicies) || $manageLocationsPolicies;
mateuszdebinski marked this conversation as resolved.
Show resolved Hide resolved

$copyLimit = $this->configResolver->getParameter(
'subtree_operations.copy_subtree.limit'
Expand Down Expand Up @@ -225,13 +232,9 @@ public function createStructure(array $options): ItemInterface
self::ITEM__MOVE,
[
'extras' => ['icon' => 'move'],
'attributes' => [
'class' => 'btn--udw-move',
'data-udw-config' => $this->udwExtension->renderUniversalDiscoveryWidgetConfig('single_container'),
'data-root-location' => $this->configResolver->getParameter(
'universal_discovery_widget_module.default_location_id'
),
],
'attributes' => $hasCreatePermission
alongosz marked this conversation as resolved.
Show resolved Hide resolved
? $moveAttributes
: array_merge($moveAttributes, ['disabled' => 'disabled']),
]
)
);
Expand All @@ -241,13 +244,9 @@ public function createStructure(array $options): ItemInterface
self::ITEM__COPY,
[
'extras' => ['icon' => 'copy'],
'attributes' => [
'class' => 'btn--udw-copy',
'data-udw-config' => $this->udwExtension->renderUniversalDiscoveryWidgetConfig('single_container'),
'data-root-location' => $this->configResolver->getParameter(
'universal_discovery_widget_module.default_location_id'
),
],
'attributes' => $hasCreatePermission && $hasManageLocationsPermission
mateuszdebinski marked this conversation as resolved.
Show resolved Hide resolved
? $copyAttributes
: array_merge($copyAttributes, ['disabled' => 'disabled']),
]
)
);
Expand All @@ -257,7 +256,7 @@ public function createStructure(array $options): ItemInterface
self::ITEM__COPY_SUBTREE,
[
'extras' => ['icon' => 'copy-subtree'],
'attributes' => $canCopySubtree
'attributes' => $canCopySubtree && $hasCreatePermission
mateuszdebinski marked this conversation as resolved.
Show resolved Hide resolved
? $copySubtreeAttributes
: array_merge($copySubtreeAttributes, ['disabled' => 'disabled']),
]
Expand All @@ -266,9 +265,9 @@ public function createStructure(array $options): ItemInterface
}

if ($content->getVersionInfo()->getContentInfo()->isHidden) {
$this->addRevealMenuItem($menu);
$this->addRevealMenuItem($menu, $canHide);
} else {
$this->addHideMenuItem($menu);
$this->addHideMenuItem($menu, $canHide);
}

if ($contentIsUser && $canDelete) {
Expand Down Expand Up @@ -369,17 +368,21 @@ private function addEditMenuItem(ItemInterface $menu, bool $contentIsUser, bool
/**
* @param \Knp\Menu\ItemInterface $menu
*/
private function addRevealMenuItem(ItemInterface $menu): void
private function addRevealMenuItem(ItemInterface $menu, bool $canHide): void
{
$attributes = [
'data-actions' => 'reveal',
'class' => 'ez-btn--reveal',
];

$menu->addChild(
$this->createMenuItem(
self::ITEM__REVEAL,
[
'extras' => ['icon' => 'reveal'],
'attributes' => [
'data-actions' => 'reveal',
'class' => 'ez-btn--reveal',
],
'attributes' => $canHide
? $attributes
: array_merge($attributes, ['disabled' => 'disabled']),
]
)
);
Expand All @@ -388,17 +391,21 @@ private function addRevealMenuItem(ItemInterface $menu): void
/**
* @param \Knp\Menu\ItemInterface $menu
*/
private function addHideMenuItem(ItemInterface $menu): void
private function addHideMenuItem(ItemInterface $menu, bool $canHide): void
{
$attributes = [
'data-actions' => 'hide',
'class' => 'ez-btn--hide',
];

$menu->addChild(
$this->createMenuItem(
self::ITEM__HIDE,
[
'extras' => ['icon' => 'hide'],
'attributes' => [
'data-actions' => 'hide',
'class' => 'ez-btn--hide',
],
'attributes' => $canHide
? $attributes
: array_merge($attributes, ['disabled' => 'disabled']),
]
)
);
Expand Down