Skip to content

Commit

Permalink
IBX-2829: Added option to configure fields for register form (#40)
Browse files Browse the repository at this point in the history
* IBX-2829: Added option to remove fields from register form

* Replaced choice_loader with choices, as it seems symfony broke something

* Fixed CS

* Renamed `id` to `identifiers`

* fixed CS

* Aligned test with changes to limitation type

* Minor improvs after CR
  • Loading branch information
ViniTou authored Jun 23, 2022
1 parent b66c5ed commit c3cf31d
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ public function addSemanticConfig(NodeBuilder $nodeBuilder)
->end()
->end()
->end()
->arrayNode('form')
->info('User registration form configuration.')
->children()
->arrayNode('allowed_field_definitions_identifiers')
->requiresAtLeastOneElement()
->defaultValue(['user_account'])
->prototype('scalar')->end()
->end()
->end()
->end()
->end();
}
Expand Down Expand Up @@ -87,6 +96,14 @@ public function mapConfig(array &$scopeSettings, $currentScope, ContextualizerIn
$settings['templates']['confirmation']
);
}

if (!empty($settings['form']['allowed_field_definitions_identifiers'])) {
$contextualizer->setContextualParameter(
'user_registration.form.allowed_field_definitions_identifiers',
$currentScope,
$settings['form']['allowed_field_definitions_identifiers']
);
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/bundle/Resources/config/ezplatform_default_settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ parameters:
ibexa.site_access.config.default.user_registration.user_type_identifier: 'user'
ibexa.site_access.config.default.user_registration.templates.form: "@@IbexaContentForms/Content/content_edit.html.twig"
ibexa.site_access.config.default.user_registration.templates.confirmation: "@@IbexaUser/register/register_confirmation.html.twig"
ibexa.site_access.config.default.user_registration.form.allowed_field_definitions_identifiers:
- first_name
- last_name
- user_account

# Invitation
ibexa.site_access.config.default.user_invitation.templates.form: "@@IbexaUser/invitation/form.html.twig"
Expand Down
6 changes: 1 addition & 5 deletions src/lib/Form/Type/Invitation/SiteAccessChoiceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use Ibexa\Core\MVC\Symfony\SiteAccess\SiteAccessServiceInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\ChoiceList;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\OptionsResolver\OptionsResolver;

Expand All @@ -27,10 +26,7 @@ public function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setDefaults([
'choice_loader' => ChoiceList::lazy(
$this,
fn () => $this->siteAccessService->getAll(),
),
'choices' => $this->siteAccessService->getAll(),
'choice_label' => 'name',
'choice_name' => 'name',
'choice_value' => 'name',
Expand Down
27 changes: 27 additions & 0 deletions src/lib/Form/Type/UserRegisterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@

use Ibexa\ContentForms\Form\EventSubscriber\UserFieldsSubscriber;
use Ibexa\ContentForms\Form\Type\Content\BaseContentType;
use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
use Ibexa\User\Form\Data\UserRegisterData;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
Expand All @@ -21,6 +24,13 @@
*/
class UserRegisterType extends AbstractType
{
private ConfigResolverInterface $configResolver;

public function __construct(ConfigResolverInterface $configResolver)
{
$this->configResolver = $configResolver;
}

public function getName()
{
return $this->getBlockPrefix();
Expand All @@ -41,6 +51,23 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$builder
->add('register', SubmitType::class, ['label' => /** @Desc("Register") */ 'user.register_button'])
->addEventSubscriber(new UserFieldsSubscriber());

$builder->get('fieldsData')->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event): void {
$allowedFieldsId = $this
->configResolver
->getParameter('user_registration.form.allowed_field_definitions_identifiers');

$fieldsData = $event->getForm();
foreach ($fieldsData as $fieldData) {
if (!in_array($fieldData->getName(), $allowedFieldsId, true)) {
$fieldsData->remove($fieldData->getName());
}
}
},
-10
);
}

public function configureOptions(OptionsResolver $resolver)
Expand Down
6 changes: 3 additions & 3 deletions src/lib/Permission/UserPermissionsLimitationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
use Ibexa\Contracts\Core\Limitation\Type as SPILimitationTypeInterface;
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
use Ibexa\Contracts\Core\Repository\Exceptions\NotImplementedException;
use Ibexa\Contracts\Core\Repository\Values\Content\Content;
use Ibexa\Contracts\Core\Repository\Values\User\Limitation as APILimitationValue;
use Ibexa\Contracts\Core\Repository\Values\User\Role;
use Ibexa\Contracts\Core\Repository\Values\User\UserGroup;
use Ibexa\Contracts\Core\Repository\Values\User\UserReference as APIUserReference;
use Ibexa\Contracts\Core\Repository\Values\ValueObject;
use Ibexa\Core\Base\Exceptions\InvalidArgumentException;
Expand Down Expand Up @@ -154,7 +154,7 @@ public function evaluate(APILimitationValue $value, APIUserReference $currentUse
throw new InvalidArgumentException('$value', 'Must be of type: APISiteAccessLimitation');
}

if (!$object instanceof Role && !$object instanceof UserGroup) {
if (!$object instanceof Role && !$object instanceof Content) {
return self::ACCESS_ABSTAIN;
}

Expand All @@ -164,7 +164,7 @@ public function evaluate(APILimitationValue $value, APIUserReference $currentUse
return self::ACCESS_GRANTED;
}

if ($object instanceof UserGroup
if ($object instanceof Content
&& ($value->limitationValues['user_groups'] === null || in_array($object->id, $value->limitationValues['user_groups']))
) {
return self::ACCESS_GRANTED;
Expand Down
8 changes: 3 additions & 5 deletions tests/lib/Permission/UserPermissionsLimitationTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,9 @@ public function providerForTestEvaluate()
'user_groups' => [14, 18],
],
]),
'object' => new Content([
'versionInfo' => new VersionInfo([
'contentInfo' => new ContentInfo([
'id' => 14,
]),
'object' => new VersionInfo([
'contentInfo' => new ContentInfo([
'id' => 14,
]),
]),
'expected' => null,
Expand Down

0 comments on commit c3cf31d

Please sign in to comment.