Skip to content

Commit

Permalink
Merge branch 'master' into release-2015.09
Browse files Browse the repository at this point in the history
  • Loading branch information
Bertrand Dunogier committed Oct 22, 2015
2 parents 544c7c8 + 866fd0f commit 3a31e1a
Show file tree
Hide file tree
Showing 24 changed files with 763 additions and 15 deletions.
43 changes: 43 additions & 0 deletions bundle/DependencyInjection/Compiler/LimitationFormMapperPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* This file is part of the eZ RepositoryForms package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*
* @version //autogentag//
*/
namespace EzSystems\RepositoryFormsBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use LogicException;
use Symfony\Component\DependencyInjection\Reference;

/**
* Compiler pass to register Limitation form mappers.
*/
class LimitationFormMapperPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('ezrepoforms.limitation_form_mapper.registry')) {
return;
}

$registry = $container->findDefinition('ezrepoforms.limitation_form_mapper.registry');

foreach ($container->findTaggedServiceIds('ez.limitation.formMapper') as $id => $attributes) {
foreach ($attributes as $attribute) {
if (!isset($attribute['limitationType'])) {
throw new LogicException(
'ez.limitation.formMapper service tag needs a "limitationType" attribute to identify which LimitationType the mapper is for. None given.'
);
}

$registry->addMethodCall('addMapper', [new Reference($id), $attribute['limitationType']]);
}
}
}
}
2 changes: 2 additions & 0 deletions bundle/EzSystemsRepositoryFormsBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace EzSystems\RepositoryFormsBundle;

use EzSystems\RepositoryFormsBundle\DependencyInjection\Compiler\FieldTypeFormMapperPass;
use EzSystems\RepositoryFormsBundle\DependencyInjection\Compiler\LimitationFormMapperPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

Expand All @@ -20,5 +21,6 @@ public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new FieldTypeFormMapperPass());
$container->addCompilerPass(new LimitationFormMapperPass());
}
}
54 changes: 53 additions & 1 deletion bundle/Resources/config/role.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
parameters:
# Form types
ezrepoforms.policy.edit.form.class: EzSystems\RepositoryForms\Form\Type\Role\PolicyType
ezrepoforms.policy.limitation.form.class: EzSystems\RepositoryForms\Form\Type\Role\LimitationType

# Validators
ezrepoforms.validator.unique_role_identifier.class: EzSystems\RepositoryForms\Validator\Constraints\UniqueRoleIdentifierValidator
Expand All @@ -13,14 +14,29 @@ parameters:
ezrepoforms.form_processor.role.class: EzSystems\RepositoryForms\Form\Processor\RoleFormProcessor
ezrepoforms.form_processor.policy.class: EzSystems\RepositoryForms\Form\Processor\PolicyFormProcessor

# Limitation mappers
ezrepoforms.limitation_form_mapper.registry.class: EzSystems\RepositoryForms\Limitation\LimitationFormMapperRegistry
ezrepoforms.limitation.null.template: "EzSystemsRepositoryFormsBundle:Limitation:null_limitation_values.html.twig"
ezrepoforms.limitation.form_mapper.null.class: EzSystems\RepositoryForms\Limitation\Mapper\NullLimitationMapper
ezrepoforms.limitation.multiple_selection.template: "EzSystemsRepositoryFormsBundle:Limitation:base_limitation_values.html.twig"
ezrepoforms.limitation.form_mapper.siteaccess.class: EzSystems\RepositoryForms\Limitation\Mapper\SiteAccessLimitationMapper
ezrepoforms.limitation.form_mapper.contenttype.class: EzSystems\RepositoryForms\Limitation\Mapper\ContentTypeLimitationMapper
ezrepoforms.limitation.form_mapper.section.class: EzSystems\RepositoryForms\Limitation\Mapper\SectionLimitationMapper

services:
# Form types
ezrepoforms.policy.edit.form:
class: %ezrepoforms.policy.edit.form.class%
arguments: [%ezpublish.api.role.policy_map%, @translator]
arguments: [%ezpublish.api.role.policy_map%, @translator, @ezpublish.api.service.role]
tags:
- { name: form.type, alias: ezrepoforms_policy_edit }

ezrepoforms.policy.limitation.form:
class: %ezrepoforms.policy.limitation.form.class%
arguments: [@ezrepoforms.limitation_form_mapper.registry, @ezrepoforms.limitation.form_mapper.null]
tags:
- { name: form.type, alias: ezrepoforms_policy_limitation_edit }

# Validators
ezrepoforms.validator.unique_role_identifier:
class: %ezrepoforms.validator.unique_role_identifier.class%
Expand Down Expand Up @@ -49,3 +65,39 @@ services:
arguments: [@ezpublish.api.service.role]
tags:
- { name: kernel.event_subscriber }

ezrepoforms.limitation_form_mapper.registry:
class: %ezrepoforms.limitation_form_mapper.registry.class%

ezrepoforms.limitation.form_mapper.multiple_selection:
class: EzSystems\RepositoryForms\Limitation\Mapper\MultipleSelectionBasedMapper
abstract: true
calls:
- [setFormTemplate, [%ezrepoforms.limitation.multiple_selection.template%]]

ezrepoforms.limitation.form_mapper.siteaccess:
parent: ezrepoforms.limitation.form_mapper.multiple_selection
class: %ezrepoforms.limitation.form_mapper.siteaccess.class%
arguments: [%ezpublish.siteaccess.list%]
tags:
- { name: ez.limitation.formMapper, limitationType: SiteAccess }

ezrepoforms.limitation.form_mapper.null:
class: %ezrepoforms.limitation.form_mapper.null.class%
arguments: [%ezrepoforms.limitation.null.template%]
tags:
- { name: ez.limitation.formMapper, limitationType: "Null" }

ezrepoforms.limitation.form_mapper.contenttype:
parent: ezrepoforms.limitation.form_mapper.multiple_selection
class: %ezrepoforms.limitation.form_mapper.contenttype.class%
arguments: [@ezpublish.api.service.content_type]
tags:
- { name: ez.limitation.formMapper, limitationType: Class }

ezrepoforms.limitation.form_mapper.section:
parent: ezrepoforms.limitation.form_mapper.multiple_selection
class: %ezrepoforms.limitation.form_mapper.section.class%
arguments: [@ezpublish.api.service.section]
tags:
- { name: ez.limitation.formMapper, limitationType: Section }
40 changes: 38 additions & 2 deletions bundle/Resources/translations/ezrepoforms_role.en.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
<source>role.remove_draft</source>
<target>Cancel</target>
</trans-unit>
<trans-unit id="0b4cb733c3b00614be932928650a567f">
<trans-unit id="0b4cb733c3b00614be932928650a567f" resname="role.form.delete">
<source>role.form.delete</source>
<target>Delete</target>
</trans-unit>
<trans-unit id="565405ab5f15e4274e9edf5301d567b1">
<trans-unit id="565405ab5f15e4274e9edf5301d567b1" resname="role.create">
<source>role.create</source>
<target>Create a role</target>
</trans-unit>
Expand Down Expand Up @@ -50,6 +50,42 @@
<source>role.policy.form.delete</source>
<target>Delete policy</target>
</trans-unit>
<trans-unit id="157e46260f2070af0f4f00093c8ee07c">
<source>role.policy.limitation.none</source>
<target>No limitations available</target>
</trans-unit>
<trans-unit id="afbaa2b43495f534ce1d02c20a792bef">
<source>role.policy.save_and_add_limitation</source>
<target>Save and add limitations</target>
</trans-unit>
<trans-unit id="09ecb68c2da6cf95b2f61a9019b67e28">
<source>role.policy.available_limitations</source>
<target>Limitations</target>
</trans-unit>
<trans-unit id="39446b5a5e8148ddb6197abc65cf7e82">
<source>role.policy.save</source>
<target>Save</target>
</trans-unit>
<trans-unit id="d04fa11f7880f330c284bff3f44f42fb">
<source>Class</source>
<target>ContentType</target>
</trans-unit>
<trans-unit id="a23eb7e851c75169666f9bcc4539e4c3">
<source>Node</source>
<target>Location</target>
</trans-unit>
<trans-unit id="7ba51a99524d33986e1169814afd8584" resname="role.assignment.form.delete">
<source>role.assignment.form.delete</source>
<target>Delete assignment</target>
</trans-unit>
<trans-unit id="b4b711374bd2445bc8716c0b10c0d361">
<source>role.policy.limitation.any</source>
<target>Any</target>
</trans-unit>
<trans-unit id="8d4b6dfff06f5ca9afc003c3aea32402">
<source>role.policy.limitation.not_implemented</source>
<target>Limitation edition for '%limitationTypeIdentifier%' is not implemented yet.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{ form_label(form.limitationValues) }}
{{ form_errors(form.limitationValues) }}
{{ form_widget(form.limitationValues) }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% do form.setRendered() %}
{{ form_label(form) }}
<em>{{ "role.policy.limitation.not_implemented"|trans({"%limitationTypeIdentifier%": form.vars.data.identifier}, 'ezrepoforms_role') }}</em>
32 changes: 31 additions & 1 deletion lib/Data/Mapper/PolicyMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function mapToFormData(ValueObject $policyDraft, array $params = [])
'roleDraft' => $params['roleDraft'],
'initialRole' => $params['initialRole'],
'moduleFunction' => "{$policyDraft->module}|{$policyDraft->function}",
'limitationsData' => $this->generateLimitationList($policyDraft->getLimitations(), $params['availableLimitationTypes']),
]);
} else {
$data = new PolicyCreateData([
Expand All @@ -51,7 +52,7 @@ public function mapToFormData(ValueObject $policyDraft, array $params = [])
private function configureOptions(OptionsResolver $optionsResolver)
{
$optionsResolver
->setRequired(['roleDraft', 'initialRole'])
->setRequired(['roleDraft', 'initialRole', 'availableLimitationTypes'])
->setAllowedTypes('roleDraft', '\eZ\Publish\API\Repository\Values\User\RoleDraft')
->setAllowedTypes('initialRole', '\eZ\Publish\API\Repository\Values\User\Role');
}
Expand All @@ -60,4 +61,33 @@ private function isPolicyNew(PolicyDraft $policy)
{
return $policy->id === null;
}

/**
* Generates the limitation list from existing limitations (already configured for current policy) and
* available limitation types available for current policy (i.e. current module/function combination).
*
* @param \eZ\Publish\API\Repository\Values\User\Limitation[] $existingLimitations
* @param \eZ\Publish\SPI\Limitation\Type[] $availableLimitationTypes
*
* @return array|\eZ\Publish\API\Repository\Values\User\Limitation[]
*/
private function generateLimitationList(array $existingLimitations, array $availableLimitationTypes)
{
$limitations = [];
foreach ($existingLimitations as $limitation) {
$limitations[$limitation->getIdentifier()] = $limitation;
}

foreach ($availableLimitationTypes as $identifier => $limitationType) {
if (isset($limitations[$identifier])) {
continue;
}

$limitations[$identifier] = $limitationType->buildValue([]);
}

ksort($limitations);

return $limitations;
}
}
11 changes: 11 additions & 0 deletions lib/Data/Role/PolicyCreateData.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* @property-read \eZ\Publish\API\Repository\Values\User\PolicyDraft $policyDraft
* @property-read \eZ\Publish\API\Repository\Values\User\RoleDraft $roleDraft
* @property-read \eZ\Publish\API\Repository\Values\User\Role $initialRole
* @property-read \eZ\Publish\API\Repository\Values\User\Limitation[] $limitationsData
*/
class PolicyCreateData extends PolicyCreateStruct implements NewsnessCheckable
{
Expand All @@ -24,4 +25,14 @@ public function isNew()
{
return true;
}

public function getModule()
{
return $this->module;
}

public function getFunction()
{
return $this->function;
}
}
11 changes: 11 additions & 0 deletions lib/Data/Role/PolicyDataTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ trait PolicyDataTrait
*/
protected $initialRole;

/**
* List of limitations that were posted.
*
* @var \eZ\Publish\API\Repository\Values\User\Limitation[]
*/
protected $limitationsData;

/**
* Combination of module + function as a single string.
* Example: "content|read".
Expand All @@ -46,4 +53,8 @@ public function getId()
{
return $this->policyDraft ? $this->policyDraft->id : null;
}

abstract public function getModule();

abstract public function getFunction();
}
1 change: 1 addition & 0 deletions lib/Data/Role/PolicyUpdateData.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* @property-read \eZ\Publish\API\Repository\Values\User\PolicyDraft $policyDraft
* @property-read \eZ\Publish\API\Repository\Values\User\RoleDraft $roleDraft
* @property-read \eZ\Publish\API\Repository\Values\User\Role $initialRole
* @property-read \eZ\Publish\API\Repository\Values\User\Limitation[] $limitationsData
*/
class PolicyUpdateData extends PolicyUpdateStruct
{
Expand Down
11 changes: 10 additions & 1 deletion lib/Form/Processor/PolicyFormProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,16 @@ public function processUpdatePolicy(FormActionEvent $event)
$data->function = $function;
$this->roleService->addPolicyByRoleDraft($data->roleDraft, $data);
} else {
// TODO: Save limitations. It's not possible by design to update policy module/function.
// Only save limitations on update.
// It is not possible by design to update policy module/function.
foreach ($data->limitationsData as $limitation) {
// Add posted limitations as valid ones, recognized by RoleService.
if (!empty($limitation->limitationValues)) {
$data->addLimitation($limitation);
}
}

$this->roleService->updatePolicyByRoleDraft($data->roleDraft, $data->policyDraft, $data);
}
}

Expand Down
Loading

0 comments on commit 3a31e1a

Please sign in to comment.