Skip to content

Commit

Permalink
EZP-29742: Implement permissions for Content/Create in Content item view
Browse files Browse the repository at this point in the history
  • Loading branch information
mikadamczyk committed Nov 13, 2018
1 parent 997b274 commit 51d7a63
Show file tree
Hide file tree
Showing 19 changed files with 648 additions and 80 deletions.
17 changes: 13 additions & 4 deletions src/bundle/Controller/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,34 @@
use EzSystems\EzPlatformAdminUi\Form\Data\Content\Draft\ContentEditData;
use EzSystems\EzPlatformAdminUi\Form\Factory\FormFactory;
use eZ\Publish\API\Repository\PermissionResolver;
use Symfony\Component\HttpFoundation\Response;

class DashboardController extends Controller
{
/** @var \EzSystems\EzPlatformAdminUi\Form\Factory\FormFactory */
protected $formFactory;

/** @var \eZ\Publish\API\Repository\PermissionResolver */
private $permissionResolver;

/**
* @param \EzSystems\EzplatformAdminUi\Form\Factory\FormFactory $formFactory
* @param \EzSystems\EzPlatformAdminUi\Form\Factory\FormFactory $formFactory
* @param \eZ\Publish\API\Repository\PermissionResolver $permissionResolver
*/
public function __construct(FormFactory $formFactory, PermissionResolver $permissionResolver)
{
public function __construct(
FormFactory $formFactory,
PermissionResolver $permissionResolver
) {
$this->formFactory = $formFactory;
$this->permissionResolver = $permissionResolver;
}

public function dashboardAction()
/**
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
*/
public function dashboardAction(): Response
{
$editForm = $this->formFactory->contentEdit(
new ContentEditData()
Expand Down
8 changes: 4 additions & 4 deletions src/bundle/Controller/SectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
use EzSystems\EzPlatformAdminUi\Form\SubmitHandler;
use EzSystems\EzPlatformAdminUi\Notification\NotificationHandlerInterface;
use EzSystems\EzPlatformAdminUi\UI\Service\PathService;
use EzSystems\EzPlatformAdminUi\Util\PermissionUtil;
use EzSystems\EzPlatformAdminUi\Util\PermissionUtilInterface;
use EzSystems\EzPlatformAdminUiBundle\View\EzPagerfantaView;
use EzSystems\EzPlatformAdminUiBundle\View\Template\EzPagerfantaTemplate;
use Pagerfanta\Adapter\ArrayAdapter;
Expand Down Expand Up @@ -80,7 +80,7 @@ class SectionController extends Controller
/** @var \eZ\Publish\API\Repository\PermissionResolver */
private $permissionResolver;

/** @var \EzSystems\EzPlatformAdminUi\Util\PermissionUtil */
/** @var \EzSystems\EzPlatformAdminUi\Util\PermissionUtilInterface */
private $permissionUtil;

/** @var int */
Expand All @@ -99,7 +99,7 @@ class SectionController extends Controller
* @param \eZ\Publish\API\Repository\LocationService $locationService
* @param \EzSystems\EzPlatformAdminUi\UI\Service\PathService $pathService
* @param \eZ\Publish\API\Repository\PermissionResolver $permissionResolver
* @param \EzSystems\EzPlatformAdminUi\Util\PermissionUtil $permissionUtil
* @param \EzSystems\EzPlatformAdminUi\Util\PermissionUtilInterface $permissionUtil
* @param int $defaultPaginationLimit
*/
public function __construct(
Expand All @@ -115,7 +115,7 @@ public function __construct(
LocationService $locationService,
PathService $pathService,
PermissionResolver $permissionResolver,
PermissionUtil $permissionUtil,
PermissionUtilInterface $permissionUtil,
int $defaultPaginationLimit
) {
$this->notificationHandler = $notificationHandler;
Expand Down
29 changes: 25 additions & 4 deletions src/bundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,15 @@ services:
EzSystems\EzPlatformAdminUi\Form\DataMapper\:
resource: '../../../lib/Form/DataMapper'

EzSystems\EzPlatformAdminUi\Form\Type\Language\LanguageChoiceType:
arguments:
$siteAccessLanguages: '$languages$'

EzSystems\EzPlatformAdminUi\Form\Type\Policy\PolicyChoiceType:
arguments:
$policyMap: "%ezpublish.api.role.policy_map%"

EzSystems\EzPlatformAdminUi\Form\Type\Content\Draft\ContentCreateType:
arguments:
$choiceListProvider: '@EzSystems\EzPlatformAdminUi\Form\Type\ChoiceList\Provider\PermissionAwareContentTypeChoiceListProvider'
$languageChoiceListProvider: '@EzSystems\EzPlatformAdminUi\Form\Type\ChoiceList\Provider\PermissionAwareLanguageChoiceListProvider'

EzSystems\EzPlatformAdminUi\UI\Dataset\DatasetFactory:
arguments:
$userContentTypeIdentifier: '$user_content_type_identifier$'
Expand Down Expand Up @@ -151,3 +152,23 @@ services:
- {name: kernel.event_subscriber, priority: -250}

EzSystems\EzPlatformAdminUiBundle\Templating\Twig\UserPreferencesGlobalExtension: ~

EzSystems\EzPlatformAdminUi\Form\Type\ChoiceList\Provider\ContentTypeChoiceListProvider: ~

EzSystems\EzPlatformAdminUi\Form\Type\ChoiceList\Provider\LanguageChoiceListProvider:
arguments:
$siteAccessLanguages: '$languages$'

EzSystems\EzPlatformAdminUi\Form\Type\ChoiceList\Provider\PermissionAwareContentTypeChoiceListProvider:
arguments:
$decorated: '@EzSystems\EzPlatformAdminUi\Form\Type\ChoiceList\Provider\ContentTypeChoiceListProvider'
$module: 'content'
$function: 'create'

EzSystems\EzPlatformAdminUi\Form\Type\ChoiceList\Provider\PermissionAwareLanguageChoiceListProvider:
arguments:
$decorated: '@EzSystems\EzPlatformAdminUi\Form\Type\ChoiceList\Provider\LanguageChoiceListProvider'
$module: 'content'
$function: 'create'


3 changes: 3 additions & 0 deletions src/bundle/Resources/config/services/utils.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ services:

EzSystems\EzPlatformAdminUi\Util\:
resource: "../../../lib/Util"

EzSystems\EzPlatformAdminUi\Util\PermissionUtilInterface:
alias: EzSystems\EzPlatformAdminUi\Util\PermissionUtil
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
<div class="ez-extra-actions__header">{{ 'content.create.choose_content_type'|trans|desc('Create your content') }}</div>
<div class="ez-extra-actions__content">
{{ form_start(form, { 'action': path('ezplatform.content.create'), 'attr': { 'autocomplete': 'off' } }) }}
{% if form.language.vars.choices|length == 1 %}
{{ form_widget(form.language, {'attr': {'hidden': true}}) }}
{% else %}
<div class="ez-extra-actions__section-header">{{ 'content.create.select_language'|trans|desc('Select language') }}</div>
<div class="ez-extra-actions__section-content">
<div class="ez-extra-actions__section-header">{{ 'content.create.select_language'|trans|desc('Select language') }}</div>
<div class="ez-extra-actions__section-content">
{% if form.language.vars.choices|length == 1 %}
{{ form_widget(form.language, {'attr': {'disabled': true}}) }}
{% else %}
{{ form_widget(form.language) }}
</div>
{% endif %}
{% endif %}
</div>
<div class="ez-extra-actions__section-header">{{ 'content.create.select_content_type'|trans|desc('Select Content Type') }}</div>
<div class="ez-extra-actions__section-content ez-extra-actions__section-content--content-type">
<div class="ez-instant-filter">
Expand Down
7 changes: 5 additions & 2 deletions src/bundle/Resources/views/dashboard/dashboard.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
<div class="container ez-dashboard__header">
<h1 class="py-3">{{ 'my.dashboard'|trans|desc('My dashboard') }}</h1>
<div class="ez-dashboard__create">
<button type="button" class="btn btn-primary ez-btn--cotf-create" data-udw-config="{{ ez_udw_config('create', {}) }}" {% if not can_create_content %}disabled{% endif %}>
<svg class="ez-icon ez-icon--medium ez-icon--light">
<button
class="btn btn-primary ez-btn--cotf-create"
data-udw-config="{{ ez_udw_config('create', {'type': 'content_create'}) }}"
{% if not can_create_content %}disabled{% endif %}>
<svg class="ez-icon ez-icon--medium ez-icon--light ez-icon-create">
<use xlink:href="{{ asset('bundles/ezplatformadminui/img/ez-icons.svg') }}#create"></use>
</svg>
<span>{{ 'dashboard.create'|trans|desc('Create') }}</span>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?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\Form\Type\ChoiceList\Provider;

interface ChoiceListProviderInterface
{
public function getChoiceList(): array;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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\Form\Type\ChoiceList\Provider;

use eZ\Publish\API\Repository\ContentTypeService;

class ContentTypeChoiceListProvider implements ChoiceListProviderInterface
{
/** @var \eZ\Publish\API\Repository\ContentTypeService */
protected $contentTypeService;

/**
* @param \eZ\Publish\API\Repository\ContentTypeService $contentTypeService
*/
public function __construct(ContentTypeService $contentTypeService)
{
$this->contentTypeService = $contentTypeService;
}

/**
* @return array
*/
public function getChoiceList(): array
{
$contentTypes = [];
$contentTypeGroups = $this->contentTypeService->loadContentTypeGroups();
foreach ($contentTypeGroups as $contentTypeGroup) {
$contentTypes[$contentTypeGroup->identifier] = $this->contentTypeService->loadContentTypes($contentTypeGroup);
}

return $contentTypes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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\Form\Type\ChoiceList\Provider;

use eZ\Publish\API\Repository\LanguageService;

class LanguageChoiceListProvider implements ChoiceListProviderInterface
{
/** @var \eZ\Publish\API\Repository\LanguageService */
protected $languageService;

/** @var array */
protected $siteAccessLanguages;

/**
* @param \eZ\Publish\API\Repository\LanguageService $languageService
* @param array $siteAccessLanguages
*/
public function __construct(LanguageService $languageService, array $siteAccessLanguages)
{
$this->languageService = $languageService;
$this->siteAccessLanguages = $siteAccessLanguages;
}

/**
* @return array
*/
public function getChoiceList(): array
{
$saLanguages = [];
$languagesByCode = [];

foreach ($this->languageService->loadLanguages() as $language) {
if ($language->enabled) {
$languagesByCode[$language->languageCode] = $language;
}
}

foreach ($this->siteAccessLanguages as $languageCode) {
if (!isset($languagesByCode[$languageCode])) {
continue;
}

$saLanguages[] = $languagesByCode[$languageCode];
unset($languagesByCode[$languageCode]);
}

return array_merge($saLanguages, array_values($languagesByCode));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?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\Form\Type\ChoiceList\Provider;

use eZ\Publish\API\Repository\PermissionResolver;
use eZ\Publish\API\Repository\Values\User\Limitation\ContentTypeLimitation;
use EzSystems\EzPlatformAdminUi\Util\PermissionUtilInterface;
use eZ\Publish\API\Repository\Values\ContentType\ContentType;

class PermissionAwareContentTypeChoiceListProvider implements ChoiceListProviderInterface
{
/** @var \EzSystems\EzPlatformAdminUi\Form\Type\ChoiceList\Provider\ChoiceListProviderInterface */
private $decorated;

/** @var \eZ\Publish\API\Repository\PermissionResolver */
private $permissionResolver;

/** @var \EzSystems\EzPlatformAdminUi\Util\PermissionUtilInterface */
private $permissionUtil;

/** @var string */
private $module;

/** @var string */
private $function;

/**
* @param \eZ\Publish\API\Repository\PermissionResolver $permissionResolver
* @param \EzSystems\EzPlatformAdminUi\Util\PermissionUtilInterface $permissionUtil
* @param \EzSystems\EzPlatformAdminUi\Form\Type\ChoiceList\Provider\ContentTypeChoiceListProvider $decorated
* @param string $module
* @param string $function
*/
public function __construct(
PermissionResolver $permissionResolver,
PermissionUtilInterface $permissionUtil,
ContentTypeChoiceListProvider $decorated,
string $module,
string $function
) {
$this->decorated = $decorated;
$this->permissionResolver = $permissionResolver;
$this->permissionUtil = $permissionUtil;
$this->module = $module;
$this->function = $function;
}

/**
* @return array
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
*/
public function getChoiceList(): array
{
$hasAccess = $this->permissionResolver->hasAccess($this->module, $this->function);
if (!is_bool($hasAccess)) {
$restrictedContentTypesIds = $this->permissionUtil->getRestrictions($hasAccess, ContentTypeLimitation::class);
}

$contentTypesGroups = $this->decorated->getChoiceList();

if (empty($restrictedContentTypesIds)) {
return $contentTypesGroups;
}

foreach ($contentTypesGroups as $group => $contentTypes) {
$contentTypesGroups[$group] = array_filter($contentTypes, function (ContentType $contentType) use ($restrictedContentTypesIds) {
return in_array($contentType->id, $restrictedContentTypesIds);
});
}

return $contentTypesGroups;
}
}
Loading

0 comments on commit 51d7a63

Please sign in to comment.