Skip to content

Commit

Permalink
IBX-4032: Added limitation to prevent changing content ownership (#615)
Browse files Browse the repository at this point in the history
* IBX-4032: Added limitation to prevent changing content ownership

* fix: Add translations

* fix: Use constant defined in Core

* fix: Move constant to Contract

* fix: Code Review

* fix: Missing Translation

* fix: Code Review

* fix: Missing translations
  • Loading branch information
Nattfarinn committed Oct 28, 2022
1 parent ff70b9f commit 2ee5163
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 17 deletions.
9 changes: 9 additions & 0 deletions src/bundle/Resources/config/services/role_form_mappers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,15 @@ services:
- { name: ibexa.admin_ui.limitation.mapper.form, limitationType: Subtree }
- { name: ibexa.admin_ui.limitation.mapper.value, limitationType: Subtree }

Ibexa\AdminUi\Limitation\Mapper\ChangeOwnerLimitationMapper:
arguments:
$translator: '@translator'
calls:
- [setFormTemplate, ['%ibexa.content_forms.limitation.multiple_selection.template%']]
tags:
- { name: ibexa.admin_ui.limitation.mapper.form, limitationType: ChangeOwner }
- { name: ibexa.admin_ui.limitation.mapper.value, limitationType: ChangeOwner }

Ibexa\AdminUi\Limitation\Mapper\UserPermissionsLimitationMapper:
autowire: true
autoconfigure: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@
<note>The source node in most cases contains the sample message as written by the developer. If it looks like a dot-delimitted string such as "form.label.firstname", then the developer has not provided a default message.</note>
</header>
<body>
<trans-unit id="0ba4dd0a3e03edbb23ad81c27bb921449e811cdc" resname="policy.limitation.identifier.changeowner">
<source>Change Owner</source>
<target>Change Owner</target>
<note>key: policy.limitation.identifier.changeowner</note>
</trans-unit>
<trans-unit id="3742f2cc514848bd4a5eb03430e412d44f5d9ec2" resname="policy.limitation.identifier.class">
<source>Content Type</source>
<target>Content Type</target>
<note>key: policy.limitation.identifier.class</note>
</trans-unit>
<trans-unit id="57f04cb9d7b09f4314ca3855ee020325d3195ab3" resname="policy.limitation.identifier.fieldgroup">
<source>FieldGroup</source>
<target>FieldGroup</target>
<source>Field Group</source>
<target>Field Group</target>
<note>key: policy.limitation.identifier.fieldgroup</note>
</trans-unit>
<trans-unit id="76adf2a27f1ae0ab14b623729cd3f281a6e2c285" resname="policy.limitation.identifier.group">
Expand Down Expand Up @@ -67,13 +72,13 @@
<note>key: policy.limitation.identifier.parentowner</note>
</trans-unit>
<trans-unit id="1b6a38d05e399ed640e6497fcd79ac31e4aafd73" resname="policy.limitation.identifier.personalizationaccess">
<source>PersonalizationAccess</source>
<target>PersonalizationAccess</target>
<source>Personalization Access</source>
<target>Personalization Access</target>
<note>key: policy.limitation.identifier.personalizationaccess</note>
</trans-unit>
<trans-unit id="af73694f5c5e5868af7948fb04f0d6bfe7ee6ad4" resname="policy.limitation.identifier.producttype">
<source>ProductType</source>
<target>ProductType</target>
<source>Product Type</source>
<target>Product Type</target>
<note>key: policy.limitation.identifier.producttype</note>
</trans-unit>
<trans-unit id="bb77b27363dad566197a200290f0f0b18baa4705" resname="policy.limitation.identifier.section">
Expand All @@ -82,8 +87,8 @@
<note>key: policy.limitation.identifier.section</note>
</trans-unit>
<trans-unit id="659face270db8822838d3efd32b3a9b72a6dc5f6" resname="policy.limitation.identifier.segmentgroup">
<source>SegmentGroup</source>
<target>SegmentGroup</target>
<source>Segment Group</source>
<target>Segment Group</target>
<note>key: policy.limitation.identifier.segmentgroup</note>
</trans-unit>
<trans-unit id="548bc922c693c8259cbfd3354451a3035e2fa6f6" resname="policy.limitation.identifier.siteaccess">
Expand Down Expand Up @@ -112,23 +117,23 @@
<note>key: policy.limitation.identifier.taxonomy</note>
</trans-unit>
<trans-unit id="097f571a3c731c66c83230e465f4b3c9af811add" resname="policy.limitation.identifier.userpermissions">
<source>UserPermissions</source>
<target>UserPermissions</target>
<source>User Permissions</source>
<target>User Permissions</target>
<note>key: policy.limitation.identifier.userpermissions</note>
</trans-unit>
<trans-unit id="ae8d9836b92dd7d303929104953d877fa9c14fed" resname="policy.limitation.identifier.versionlock">
<source>VersionLock</source>
<target>VersionLock</target>
<source>Version Lock</source>
<target>Version Lock</target>
<note>key: policy.limitation.identifier.versionlock</note>
</trans-unit>
<trans-unit id="920f6a0525d6ba93ff83f46e22baa981ff0b483c" resname="policy.limitation.identifier.workflowstage">
<source>WorkflowStage</source>
<target>WorkflowStage</target>
<source>Workflow Stage</source>
<target>Workflow Stage</target>
<note>key: policy.limitation.identifier.workflowstage</note>
</trans-unit>
<trans-unit id="017b3370e919f545dbf4e889252c00419fde6fde" resname="policy.limitation.identifier.workflowtransition">
<source>WorkflowTransition</source>
<target>WorkflowTransition</target>
<source>Workflow Transition</source>
<target>Workflow Transition</target>
<note>key: policy.limitation.identifier.workflowtransition</note>
</trans-unit>
</body>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:jms="urn:jms:translation" version="1.2">
<file source-language="en" target-language="en" datatype="plaintext" original="not.available">
<header>
<tool tool-id="JMSTranslationBundle" tool-name="JMSTranslationBundle" tool-version="1.1.0-DEV"/>
<note>The source node in most cases contains the sample message as written by the developer. If it looks like a dot-delimitted string such as "form.label.firstname", then the developer has not provided a default message.</note>
</header>
<body>
<trans-unit id="0ba4dd0a3e03edbb23ad81c27bb921449e811cdc" resname="policy.limitation.identifier.changeowner">
<source>Change Owner</source>
<target state="new">Change Owner</target>
<note>key: policy.limitation.identifier.changeowner</note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:jms="urn:jms:translation" version="1.2">
<file source-language="en" target-language="en" datatype="plaintext" original="not.available">
<header>
<tool tool-id="JMSTranslationBundle" tool-name="JMSTranslationBundle" tool-version="1.1.0-DEV"/>
<note>The source node in most cases contains the sample message as written by the developer. If it looks like a dot-delimitted string such as "form.label.firstname", then the developer has not provided a default message.</note>
</header>
<body>
<trans-unit id="57d5902302017c54f7a0965ff6bcd69a3dc03458" resname="policy.limitation.change_owner.forbid">
<source>Forbid</source>
<target state="new">Forbid</target>
<note>key: policy.limitation.change_owner.forbid</note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
{% endapply %}
{% endblock %}

{% block ez_limitation_changeowner_value %}
{% apply spaceless %}
{{ values|join(', ') }}
{% endapply %}
{% endblock %}

{% block ez_limitation_parentowner_value %}
{% apply spaceless %}
{{ values|join(', ') }}
Expand Down
84 changes: 84 additions & 0 deletions src/lib/Limitation/Mapper/ChangeOwnerLimitationMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\AdminUi\Limitation\Mapper;

use Ibexa\AdminUi\Limitation\LimitationFormMapperInterface;
use Ibexa\AdminUi\Limitation\LimitationValueMapperInterface;
use Ibexa\AdminUi\Translation\Extractor\LimitationTranslationExtractor;
use Ibexa\Contracts\Core\Repository\Values\User\Limitation;
use Ibexa\Contracts\Core\Repository\Values\User\Limitation\ChangeOwnerLimitation;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

final class ChangeOwnerLimitationMapper implements LimitationValueMapperInterface, LimitationFormMapperInterface
{
private TranslatorInterface $translator;

private ?string $formTemplate = null;

public function __construct(
TranslatorInterface $translator
) {
$this->translator = $translator;
}

/**
* @return int[]
*/
public function mapLimitationValue(Limitation $limitation): array
{
return $limitation->limitationValues;
}

public function mapLimitationForm(FormInterface $form, Limitation $data): void
{
$options = [
'multiple' => true,
'expanded' => false,
'required' => false,
'label' => LimitationTranslationExtractor::identifierToLabel($data->getIdentifier()),
'choices' => array_flip($this->getSelectionChoices()),
];

$form->add('limitationValues', ChoiceType::class, $options);
}

public function getFormTemplate(): ?string
{
return $this->formTemplate;
}

/**
* @return int[]
*/
public function filterLimitationValues(Limitation $limitation): array
{
return $limitation->limitationValues;
}

/**
* @return array<?int, string>
*/
private function getSelectionChoices(): array
{
return [
ChangeOwnerLimitation::LIMITATION_VALUE_SELF => $this->translator->trans(/** @Desc("Forbid") */
'policy.limitation.change_owner.forbid',
[],
'ibexa_content_forms_role'
),
];
}

public function setFormTemplate(string $formTemplate): void
{
$this->formTemplate = $formTemplate;
}
}
18 changes: 17 additions & 1 deletion src/lib/Translation/Extractor/LimitationTranslationExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function extract()
$message = new Message\XliffMessage($id, self::MESSAGE_DOMAIN);
$message->setNew(false);
$message->setMeaning($limitationType);
$message->setDesc($limitationType);
$message->setDesc($this->getReadableName($limitationType));
$message->setLocaleString($limitationType);
$message->addNote('key: ' . $id);

Expand Down Expand Up @@ -87,6 +87,22 @@ private function getLimitationTypes(): array

return $limitationTypes;
}

private function getReadableName(string $input): string
{
$parts = preg_split(
'/(^[^A-Z]+|[A-Z][^A-Z]+)/',
$input,
-1,
PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
);

if (!is_array($parts)) {
return $input;
}

return implode(' ', $parts);
}
}

class_alias(LimitationTranslationExtractor::class, 'EzSystems\EzPlatformAdminUi\Translation\Extractor\LimitationTranslationExtractor');

0 comments on commit 2ee5163

Please sign in to comment.