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

EZP-31783: Fixed embedding Content with read permissions #1441

Merged
merged 5 commits into from
Sep 24, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?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.
*/
declare(strict_types=1);

namespace EzSystems\EzPlatformAdminUi\UniversalDiscovery\Event\Subscriber;

use eZ\Publish\API\Repository\ContentTypeService;
use eZ\Publish\API\Repository\PermissionResolver;
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
use eZ\Publish\API\Repository\Values\User\Limitation\ContentTypeLimitation;
use EzSystems\EzPlatformAdminUi\Permission\PermissionCheckerInterface;
use EzSystems\EzPlatformAdminUi\UniversalDiscovery\Event\ConfigResolveEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class RichTextEmbedAllowedContentTypes implements EventSubscriberInterface
{
/** @var string[] */
private $restrictedContentTypesIdentifiers;

/**
* @param \eZ\Publish\API\Repository\PermissionResolver $permissionResolver
* @param \EzSystems\EzPlatformAdminUi\Permission\PermissionCheckerInterface $permissionChecker
* @param \eZ\Publish\API\Repository\ContentTypeService $contentTypeService
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
*/
public function __construct(
PermissionResolver $permissionResolver,
PermissionCheckerInterface $permissionChecker,
ContentTypeService $contentTypeService
) {
$this->restrictedContentTypesIdentifiers = $this->getRestrictedContentTypesIdentifiers(
$permissionResolver,
$permissionChecker,
$contentTypeService
);
}

/**
* @param \eZ\Publish\API\Repository\PermissionResolver $permissionResolver
* @param \EzSystems\EzPlatformAdminUi\Permission\PermissionCheckerInterface $permissionChecker
* @param \eZ\Publish\API\Repository\ContentTypeService $contentTypeService
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
*/
private function getRestrictedContentTypesIdentifiers(
PermissionResolver $permissionResolver,
PermissionCheckerInterface $permissionChecker,
ContentTypeService $contentTypeService
): array {
$access = $permissionResolver->hasAccess('content', 'read');
if (!\is_array($access)) {
return [];
}

$restrictedContentTypesIds = $permissionChecker->getRestrictions($access, ContentTypeLimitation::class);

if (empty($restrictedContentTypesIds)) {
return [];
}

$restrictedContentTypes = $contentTypeService->loadContentTypeList($restrictedContentTypesIds);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tested this with weak user with no permissions to content type/read (or class/read legacy speaking)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is deployed on a few large projects. And no issues were reported there. But I`m not sure exactly this case was tested. I hope your QA will check all edge cases (like this).


return array_values(array_map(function (ContentType $contentType): string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any reason not to:

Suggested change
return array_values(array_map(function (ContentType $contentType): string {
return array_values(array_map(static function (ContentType $contentType): string {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 0e3c6cf.

return $contentType->identifier;
}, (array)$restrictedContentTypes));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please re-format this code, it's not readable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, can you please suggest a more readable option?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

        return array_values(
            array_map(static function (ContentType $contentType): string {
                return $contentType->identifier;
            },
            iterator_to_array($restrictedContentTypes)
        );

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: 502ce9c

}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array
{
return [
ConfigResolveEvent::NAME => ['onUdwConfigResolve', -10],
];
}

/**
* @param \EzSystems\EzPlatformAdminUi\UniversalDiscovery\Event\ConfigResolveEvent $event
*/
public function onUdwConfigResolve(ConfigResolveEvent $event): void
{
$config = $event->getConfig();

if (!in_array($event->getConfigName(), ['richtext_embed', 'richtext_embed_image'])) {
return;
}

$config['allowed_content_types'] = !empty($this->restrictedContentTypesIdentifiers) ? $this->restrictedContentTypesIdentifier : null;

$event->setConfig($config);
}
}