dachcom-digital / pimcore-members

Pimcore Object, Asset and Document Restriction & Frontend Authentication
Other
54 stars 34 forks source link

only set Initial Groups if present #135

Closed dasraab closed 4 years ago

dasraab commented 4 years ago
Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no

only set the initial groups if initial groups are present in the config. this prevents the groups field from being emptied when setting the groups-field via for example a form.

solverat commented 4 years ago

thanks @dasraab!

dasraab commented 4 years ago

thanks for merging! now i'm struggling to get the dev-master version to run with pimcore 6.6.0. Seems like its not yet compatible.

i'm trying to set the Users Groups via Form input. The RegistrationFormType looks like this:

<?php

class RegistrationFormType extends AbstractType
{

    protected $class;

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

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', TextType::class, [
              'constraints' => [new NotBlank()],
              'label' => 'Name (*)',
              'required' => true
              ])

            ->add('groups', ChoiceType::class, [
              'label' => 'Ich bin',
              'required' => true,
              'attr' => [
                  'required' => 'true',
                  'class' => 'form-select w-full'
              ],
              'choices' => [
                  'bitte wählen' => false,
                  'Elternteil' => MembersGroup::getById(53),
                  'Lehrer' => MembersGroup::getById(54),
              ],
              'constraints' => [
                  new NotBlank(),
              ]
              ])

            ->add('submit', SubmitType::class, [
                'label' => 'members.registration.submit',
            ]);

        $builder->get('groups')
            ->addModelTransformer(new CallbackTransformer(
                function ($group) {
                    // transform the array to a string
                    if (is_object($group)) {
                        return $group->name;
                    } else {
                        return null;
                    }
                },
                function ($group) {
                    // transform the string back to an array
                    if (is_object($group)) {
                        return [$group];
                    } else {
                        return [];
                    }
                }
            ))
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => $this->class,
            'csrf_token_id' => 'registration'
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'members_user_registration';
    }
}

Saving the selected group doesn't seem to work with pimcore 6.6.0 anymore. the selected group gets lost somewhere in the process - if i dump the user right after form submit i can see that "groups" property is an array with the selected group but its not written into the db :/ do you have any idea where to look for the bug? thank you!

solverat commented 4 years ago

@dasraab sorry for the late response, this has been fixed within https://github.com/dachcom-digital/pimcore-members/issues/140

dasraab commented 4 years ago

thank you for the update!