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-4160: Enh. custom Limitation doc and examples (4.5+) #2195

Merged
merged 30 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
5c0c0f8
custom_policies.md: Move code to files
adriendupuis Oct 30, 2023
6641d9d
custom_policies.md: Fix style and typo
adriendupuis Oct 30, 2023
7adc742
custom_policies.md: Add <?php declare(strict_types=1); to PHP sample
adriendupuis Oct 30, 2023
8568ea0
custom_policies.md: Fix policies.yaml path & move its content to file
adriendupuis Oct 30, 2023
795fbbb
custom_policies.md: Fix style and typo
adriendupuis Oct 30, 2023
c08e5d1
custom_policies.md: Fix and move custom policies translations
adriendupuis Oct 30, 2023
41efde9
custom_policies.md: Add "Custom Limitation type" section
adriendupuis Oct 30, 2023
076afd5
custom_policies.md: Rework UI integration
adriendupuis Oct 30, 2023
14ae297
custom_policies.md: Add Limitation check example
adriendupuis Oct 30, 2023
e7f85eb
PHP CS Fixes
adriendupuis Oct 30, 2023
991c9af
Add missing custom_limitation_form.html.twig
adriendupuis Nov 6, 2023
4ece140
custom_policies.md: Fix path to ibexa_security.yaml example
adriendupuis Nov 6, 2023
886eba7
Update code_samples/back_office/limitation/src/Kernel.php
adriendupuis Nov 10, 2023
af249d8
append_to_services.yaml: Change concat strategy
adriendupuis Nov 10, 2023
657e4a6
Add forgotten custom_limitation_value.html.twig
adriendupuis Nov 10, 2023
0fae483
custom_policies.md: Add TranslationContainerInterface & translation:e…
adriendupuis Nov 10, 2023
8402891
Apply suggestions from code review
adriendupuis Mar 7, 2024
ea5117c
custom_policies.md: Remove gerund from headers
adriendupuis Mar 7, 2024
6554aef
Apply suggestions from code review
adriendupuis Mar 7, 2024
299dad7
…/limitation/config/append_to_services.yaml: rm new line
adriendupuis Mar 7, 2024
836a194
Apply suggestions from code review
adriendupuis Mar 7, 2024
0577511
return types
adriendupuis Mar 7, 2024
317ad17
return types
adriendupuis Mar 7, 2024
8f08a4a
Merge branch 'master' into IBX-4160_custom_limitation
adriendupuis Mar 7, 2024
9a36bd3
PHP CS Fixes
adriendupuis Mar 7, 2024
f89ce32
ezplatform_content_forms_policies → ibexa_content_forms_policies
adriendupuis Mar 7, 2024
99bc8dc
Update code_samples/back_office/limitation/src/Security/Limitation/Cu…
adriendupuis Mar 11, 2024
5f9ded1
Apply suggestions from code review
adriendupuis Mar 11, 2024
40020e8
custom_policies.md: Move "Extend existing Policies" to a note
adriendupuis Mar 11, 2024
fcb3706
custom_policies.md: Move "Extend existing Policies" note up
adriendupuis Mar 11, 2024
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
12 changes: 12 additions & 0 deletions code_samples/back_office/limitation/config/append_to_services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
services:
App\Security\Limitation\CustomLimitationType:
tags:
- { name: 'ibexa.permissions.limitation_type', alias: 'CustomLimitation' }

App\Security\Limitation\Mapper\CustomLimitationFormMapper:
tags:
- { name: 'ibexa.admin_ui.limitation.mapper.form', limitationType: 'CustomLimitation' }

App\Security\Limitation\Mapper\CustomLimitationValueMapper:
tags:
- { name: 'ibexa.admin_ui.limitation.mapper.value', limitationType: 'CustomLimitation' }
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ibexa:
system:
default:
limitation_value_templates:
- { template: '@ibexadesign/limitation/custom_limitation_value.html.twig', priority: 0 }
24 changes: 24 additions & 0 deletions code_samples/back_office/limitation/src/Kernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace App;

use App\Security\MyPolicyProvider;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;

class Kernel extends BaseKernel
{
use MicroKernelTrait;

protected function build(ContainerBuilder $container): void
{
// Retrieve "ibexa" container extension
/** @var \Ibexa\Bundle\Core\DependencyInjection\IbexaCoreExtension $IbexaExtension */
$ibexaExtension = $container->getExtension('ibexa');
// Add the policy provider
$ibexaExtension->addPolicyProvider(new MyPolicyProvider());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# src/Resources/config/policies.yaml
custom_module:
custom_function_1: ~
custom_function_2: [CustomLimitation]
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

namespace App\Security\Limitation;

use Ibexa\Contracts\Core\Limitation\Type;
use Ibexa\Contracts\Core\Repository\Exceptions\NotImplementedException;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\CriterionInterface;
use Ibexa\Contracts\Core\Repository\Values\User\Limitation;
use Ibexa\Contracts\Core\Repository\Values\User\UserReference;
use Ibexa\Contracts\Core\Repository\Values\ValueObject;
use Ibexa\Core\Base\Exceptions\InvalidArgumentType;
use Ibexa\Core\FieldType\ValidationError;

class CustomLimitationType implements Type
{
public function acceptValue(Limitation $limitationValue): void
{
if (!$limitationValue instanceof CustomLimitationValue) {
throw new InvalidArgumentType(
'$limitationValue',
FieldGroupLimitation::class,
$limitationValue
);
}
}

/** @return \Ibexa\Core\FieldType\ValidationError[] */
public function validate(Limitation $limitationValue): array
{
$validationErrors = [];
if (!array_key_exists('value', $limitationValue->limitationValues)) {
$validationErrors[] = new ValidationError("limitationValues['value'] is missing.");
} elseif (!is_bool($limitationValue->limitationValues['value'])) {
$validationErrors[] = new ValidationError("limitationValues['value'] is not a boolean.");
}

return $validationErrors;
}

public function buildValue(array $limitationValues): CustomLimitationValue
{
$value = false;
if (array_key_exists('value', $limitationValues)) {
$value = $limitationValues['value'];
} elseif (count($limitationValues)) {
$value = (bool)$limitationValues[0];
}

return new CustomLimitationValue(['limitationValues' => ['value' => $value]]);
}

/**
* @param \Ibexa\Contracts\Core\Repository\Values\ValueObject[]|null $targets
*
* @return bool|null
*/
public function evaluate(Limitation $value, UserReference $currentUser, ValueObject $object, array $targets = null): ?bool
{
if (!$value instanceof CustomLimitationValue) {
throw new InvalidArgumentException('$value', 'Must be of type: CustomLimitationValue');
}

if ($value->limitationValues['value']) {
return Type::ACCESS_GRANTED;
}

// If the limitation value is not set to `true`, then $currentUser, $object and/or $targets could be challenged to determine if the access is granted or not.
return Type::ACCESS_DENIED;
adriendupuis marked this conversation as resolved.
Show resolved Hide resolved
adriendupuis marked this conversation as resolved.
Show resolved Hide resolved
}

public function getCriterion(Limitation $value, UserReference $currentUser): CriterionInterface
{
throw new NotImplementedException(__METHOD__);
}

public function valueSchema(): void
{
throw new NotImplementedException(__METHOD__);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace App\Security\Limitation;

use Ibexa\Contracts\Core\Repository\Values\User\Limitation;

class CustomLimitationValue extends Limitation
{
public function getIdentifier(): string
{
return 'CustomLimitation';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace App\Security\Limitation\Mapper;

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

class CustomLimitationFormMapper implements LimitationFormMapperInterface
{
public function mapLimitationForm(FormInterface $form, Limitation $data): void
{
$form->add('limitationValues', CheckboxType::class, [
'label' => LimitationTranslationExtractor::identifierToLabel($data->getIdentifier()),
'required' => false,
'data' => $data->limitationValues['value'],
'property_path' => 'limitationValues[value]',
]);
}

public function getFormTemplate(): string
{
return '@ibexadesign/limitation/custom_limitation_form.html.twig';
}

public function filterLimitationValues(Limitation $limitation): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace App\Security\Limitation\Mapper;

use Ibexa\AdminUi\Limitation\LimitationValueMapperInterface;
use Ibexa\Contracts\Core\Repository\Values\User\Limitation;

class CustomLimitationValueMapper implements LimitationValueMapperInterface
{
public function mapLimitationValue(Limitation $limitation): bool
{
return $limitation->limitationValues['value'];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace App\Security;

use Ibexa\Bundle\Core\DependencyInjection\Security\PolicyProvider\YamlPolicyProvider;

class MyPolicyProvider extends YamlPolicyProvider
{
/** @returns string[] */
protected function getFiles(): array
{
return [
__DIR__ . '/../Resources/config/policies.yaml',
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{# templates/themes/admin/limitation/custom_limitation_form.html.twig #}
{{ form_label(form.limitationValues) }}
{{ form_widget(form.limitationValues) }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{# templates/themes/standard/limitation/custom_limitation_value.html.twig #}
{% block ez_limitation_customlimitation_value %}
<span style="color: {{ values ? 'green' : 'red' }};">{{ values ? 'Yes' : 'No' }}</span>
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
role.policy.custom_module: 'Custom module'
role.policy.custom_module.all_functions: 'Custom module / All functions'
role.policy.custom_module.custom_function_1: 'Custom module / Function #1'
role.policy.custom_module.custom_function_2: 'Custom module / Function #2'
adriendupuis marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
policy.limitation.identifier.customlimitation: 'Custom limitation'
Loading
Loading