vendor/nen/kennisbank-platform/src/Nen/Form/UserType.php line 29

Open in your IDE?
  1. <?php
  2. namespace Nen\Bundle\KennisbankPlatformBundle\Form;
  3. use App\Entity\User;
  4. use Doctrine\ORM\EntityRepository;
  5. use App\Entity\Company;
  6. use Nen\Bundle\KennisbankPlatformBundle\Form\Constraint\StrongPasswordConstraint;
  7. use Nen\Bundle\KennisbankPlatformBundle\Form\DataTransformer\RolesDataTransformer;
  8. use Nen\Bundle\KennisbankPlatformBundle\Service\PlatformUserRolesProvider;
  9. use Nen\Bundle\KennisbankPlatformBundle\Service\UserRolesProvider;
  10. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  11. use Symfony\Component\Form\AbstractType;
  12. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  13. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  14. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  15. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  16. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  17. use Symfony\Component\Form\Extension\Core\Type\TextType;
  18. use Symfony\Component\Form\FormBuilderInterface;
  19. use Symfony\Component\OptionsResolver\OptionsResolver;
  20. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  21. use Symfony\Component\Security\Core\Authorization\AccessDecisionManager;
  22. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  23. use Symfony\Component\Security\Core\Security;
  24. use Symfony\Component\Validator\Constraints\NotBlank;
  25. use Symfony\Component\Validator\Constraints\NotCompromisedPassword;
  26. class UserType extends AbstractType
  27. {
  28.     private UserRolesProvider $userRolesProvider;
  29.     private Security $security;
  30.     private AccessDecisionManagerInterface $accessDecisionManager;
  31.     public function __construct(UserRolesProvider $userRolesProviderSecurity $securityAccessDecisionManagerInterface $accessDecisionManager)
  32.     {
  33.         $this->userRolesProvider $userRolesProvider;
  34.         $this->security $security;
  35.         $this->accessDecisionManager $accessDecisionManager;
  36.     }
  37.     /**
  38.      * @param FormBuilderInterface $builder
  39.      * @param array $options
  40.      */
  41.     public function buildForm(FormBuilderInterface $builder, array $options)
  42.     {
  43.         $builder
  44.             ->add(
  45.                 'sex',
  46.                 ChoiceType::class,
  47.                 [
  48.                     'label' => 'Aanhef',
  49.                     'choices' => User::getSexes(),
  50.                     'expanded' => true,
  51.                     'multiple' => false,
  52.                     'label_attr' => [
  53.                         'class' => 'radio-inline',
  54.                     ],
  55.                 ]
  56.             )
  57.             ->add(
  58.                 'initials',
  59.                 null,
  60.                 [
  61.                     'label' => 'Voorletters',
  62.                     'attr' => [
  63.                         'placeholder' => 'Voorletters',
  64.                     ],
  65.                 ]
  66.             )
  67.             ->add(
  68.                 'firstname',
  69.                 null,
  70.                 [
  71.                     'label' => 'Voornaam',
  72.                     'attr' => [
  73.                         'placeholder' => 'Voornaam',
  74.                     ],
  75.                 ]
  76.             )
  77.             ->add(
  78.                 'lastnamePrefix',
  79.                 null,
  80.                 [
  81.                     'label' => 'Tussenvoegsel',
  82.                     'required' => false,
  83.                     'attr' => [
  84.                         'placeholder' => 'Tussenvoegsel',
  85.                     ],
  86.                 ]
  87.             )
  88.             ->add(
  89.                 'lastname',
  90.                 null,
  91.                 [
  92.                     'label' => 'Achternaam',
  93.                     'attr' => [
  94.                         'placeholder' => 'Achternaam',
  95.                     ],
  96.                 ]
  97.             )
  98.             ->add(
  99.                 'username',
  100.                 null,
  101.                 [
  102.                     'label' => 'E-mailadres',
  103.                     'attr' => [
  104.                         'placeholder' => 'E-mailadres',
  105.                     ],
  106.                 ]
  107.             )
  108.             ->add(
  109.                 'job',
  110.                 null,
  111.                 [
  112.                     'label' => 'Functie',
  113.                     'attr' => [
  114.                         'placeholder' => 'Functie',
  115.                     ],
  116.                 ]
  117.             );
  118.         $type $options['type'];
  119.         if ($type == 'register') {
  120.             $builder->add(
  121.                 'hash',
  122.                 RepeatedType::class,
  123.                 [
  124.                     // instead of being set onto the object directly,
  125.                     // this is read and encoded in the controller
  126.                     'type' => PasswordType::class,
  127.                     'mapped' => false,
  128.                     'first_options' => [
  129.                         'label' => 'Wachtwoord',
  130.                         'constraints' => [
  131.                             new NotBlank(
  132.                                 [
  133.                                     'message' => 'Ontbrekende waarde.',
  134.                                 ]
  135.                             ),
  136.                             new StrongPasswordConstraint(),
  137.                             new NotCompromisedPassword([
  138.                                 'message' => 'Het ingevulde wachtwoord is niet veilig genoeg. Deze voldoet niet aan de onderstaande voorwaarden of staat op de lijst van onveilige veelvoorkomende wachtwoorden.',
  139.                             ]),
  140.                         ],
  141.                     ],
  142.                     'second_options' => [
  143.                         'label' => 'Wachtwoord nogmaals',
  144.                         'constraints' => [
  145.                             new NotBlank(
  146.                                 [
  147.                                     'message' => 'Ontbrekende waarde.',
  148.                                 ]
  149.                             ),
  150.                         ],
  151.                     ],
  152.                 ]
  153.             )
  154.                 ->add(
  155.                     'license_code',
  156.                     TextType::class,
  157.                     [
  158.                         'required' => true,
  159.                         'mapped' => false,
  160.                     ]
  161.                 );
  162.         }
  163.         if ($type !== 'profile') {
  164.             $roles $this->userRolesProvider->getSelectableRolesForForm();
  165.             // When the form is in edit or registration mode check the roles of the current
  166.             // user and disable roles that cannot be set. Edit or registration mode is when
  167.             // the company manager is editing the users of their company.
  168.             if ($type === 'edit' || $type === 'registration' || $type === 'administration') {
  169.                 if (!$this->security->isGranted(PlatformUserRolesProvider::ROLE_CUSTOMER_SERVICE)) {
  170.                     $roles array_filter($roles, static function ($role) {
  171.                         return !in_array($role, [
  172.                             PlatformUserRolesProvider::ROLE_ADMINISTRATOR,
  173.                             PlatformUserRolesProvider::ROLE_CUSTOMER_SERVICE
  174.                         ], true);
  175.                     });
  176.                 }
  177.                 if (!$this->security->isGranted(PlatformUserRolesProvider::ROLE_ADMINISTRATOR)) {
  178.                     $roles array_filter($roles, static function ($role) {
  179.                         return !in_array($role, [
  180.                             PlatformUserRolesProvider::ROLE_ADMINISTRATOR,
  181.                             PlatformUserRolesProvider::ROLE_CUSTOMER_SERVICE
  182.                         ], true);
  183.                     });
  184.                 }
  185.             }
  186.             $userToken $builder->getData() !== null ? new UsernamePasswordToken($builder->getData(), 'none''main'$builder->getData()->getRoles()) : null;
  187.             $builder->add(
  188.                 'roles',
  189.                 ChoiceType::class,
  190.                 [
  191.                     'label' => 'Rollen',
  192.                     'choices' => $roles,
  193.                     'disabled' => $userToken !== null && $this->accessDecisionManager->decide($userToken, [PlatformUserRolesProvider::ROLE_CUSTOMER_SERVICE]),
  194.                     'expanded' => false,
  195.                     'multiple' => true,
  196.                 ]
  197.             );
  198.             $builder->get('roles')->addModelTransformer(new RolesDataTransformer());
  199. //            $builder->add(
  200. //                'role',
  201. //                ChoiceType::class,
  202. //                [
  203. //                    'label' => 'Rol',
  204. //                    'choices' => User::getUserRoles(),
  205. //                    'expanded' => false,
  206. //                    'multiple' => false,
  207. //                    'label_attr' => [
  208. //                        'class' => 'radio-inline',
  209. //                    ],
  210. //                ]
  211. //            )
  212. //                ->add(
  213. //                    'manager',
  214. //                    CheckboxType::class,
  215. //                    [
  216. //                        'label' => 'Bedrijfsbeheerder',
  217. //                        'required' => false,
  218. //                    ]
  219. //                );
  220.         }
  221.         if (in_array($type, ['registration''upgrade'])) {
  222.             $builder->add('company'CompanyType::class, ['license' => $options['license'], 'type' => $type]);
  223.         }
  224.         if ($type == 'administration') {
  225.             $builder->add(
  226.                 'company',
  227.                 EntityType::class,
  228.                 [
  229.                     // looks for choices from this entity
  230.                     'class' => Company::class,
  231.                     'label' => 'Organisatie',
  232.                     'placeholder' => 'Kies een organisatie',
  233.                     'query_builder' => function (EntityRepository $repository) {
  234.                         return $repository
  235.                             ->createQueryBuilder('c')
  236.                             ->orderBy('c.name''ASC');
  237.                     },
  238.                     // uses the User.username property as the visible option string
  239.                     'choice_label' => 'name',
  240.                 ]
  241.             )
  242.                 ->add(
  243.                     'ips',
  244.                     CollectionType::class,
  245.                     [
  246.                         'entry_type' => IpType::class,
  247.                         'allow_add' => true,
  248.                         'prototype' => true,
  249.                         'by_reference' => false,
  250.                         'label' => 'IP adressen',
  251.                     ]
  252.                 );
  253.         }
  254.     }
  255.     /**
  256.      * @param OptionsResolver $resolver
  257.      */
  258.     public function configureOptions(OptionsResolver $resolver)
  259.     {
  260.         $resolver->setDefaults(
  261.             [
  262.                 'data_class' => User::class,
  263.                 'license' => null,
  264.                 'type' => 'registration',
  265.             ]
  266.         );
  267.     }
  268. }