vendor/kematjaya/user-bundle/src/Subscriber/UserTypeSubscriber.php line 71

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of the user-bundle.
  4.  */
  5. namespace Kematjaya\UserBundle\Subscriber;
  6. use Kematjaya\UserBundle\Entity\DefaultUser;
  7. use Kematjaya\UserBundle\Repo\KmjUserRepoInterface;
  8. use Kematjaya\UserBundle\Entity\KmjUserInterface;
  9. use Symfony\Component\Form\FormEvent;
  10. use Symfony\Component\Form\FormEvents;
  11. use Symfony\Component\Form\FormError;
  12. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  13. use Symfony\Component\Form\Extension\Core\Type\TextType;
  14. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  15. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  16. use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface;
  17. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  18. use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
  19. /**
  20.  * @package Kematjaya\UserBundle\Subscriber
  21.  * @license https://opensource.org/licenses/MIT MIT
  22.  * @author  Nur Hidayatullah <kematjaya0@gmail.com>
  23.  */
  24. class UserTypeSubscriber implements UserTypeSubscriberInterface
  25. {
  26.     
  27.     /**
  28.      * 
  29.      * @var PasswordHasherFactoryInterface
  30.      */
  31.     private $encoderFactory;
  32.     
  33.     /**
  34.      * 
  35.      * @var TokenStorageInterface
  36.      */
  37.     private $tokenStorage;
  38.     
  39.     /**
  40.      * 
  41.      * @var RoleHierarchyInterface
  42.      */
  43.     private $roleHierarchy;
  44.     
  45.     /**
  46.      * 
  47.      * @var KmjUserRepoInterface
  48.      */
  49.     private $repo;
  50.     
  51.     public function __construct(KmjUserRepoInterface $repoTokenStorageInterface $tokenPasswordHasherFactoryInterface $encoderFactoryRoleHierarchyInterface $roleHierarchy)
  52.     { 
  53.         $this->encoderFactory $encoderFactory;
  54.         $this->tokenStorage $token;
  55.         $this->roleHierarchy $roleHierarchy;
  56.         $this->repo $repo;
  57.     }
  58.     
  59.     public static function getSubscribedEvents(): array
  60.     {
  61.         return [
  62.             FormEvents::POST_SET_DATA => 'postSetData',
  63.             FormEvents::POST_SUBMIT => 'postSubmit'
  64.         ];
  65.     }
  66.     public function postSetData(FormEvent $event)
  67.     {
  68.         $data $event->getData();
  69.         $form $event->getForm();
  70.         
  71.         $form
  72.             ->add('username'TextType::class, [
  73.                 'label' => 'username',
  74.                 'attr' => ['readonly' => (bool) $data->getId()]
  75.             ])
  76.             ->add('is_active'CheckboxType::class, [
  77.                 'label' => 'is_active'
  78.             ])
  79.             ->add('roles'ChoiceType::class, [
  80.                 'label' =>'roles',
  81.                 'multiple'  => true,
  82.                 'choices' => $this->getRoles()
  83.             ]);
  84.         
  85.         if ($data instanceof DefaultUser) {
  86.             $form
  87.                 ->remove('roles')
  88.                 ->add('single_role'ChoiceType::class, [
  89.                 'label' =>'roles',
  90.                 'choices' => $this->getRoles()
  91.             ]);
  92.         }
  93.         
  94.         if (null === $data->getId()) {
  95.             $form->add('password'PasswordType::class, [
  96.                 'label' => 'password'
  97.             ]);
  98.         }
  99.     }
  100.     
  101.     public function postSubmit(FormEvent $event)
  102.     {
  103.         $data $event->getData();
  104.         if (!$data instanceof KmjUserInterface) {
  105.             
  106.             return;
  107.         }
  108.         
  109.         if ($data->getId()) {
  110.             
  111.             return;
  112.         }
  113.         
  114.         $data->setUsername(trim($data->getUsername()));
  115.         $other $this->repo->findOneByUsernameAndActive($data->getUsername());
  116.         if ($other) {
  117.             $event->getForm()
  118.                     ->get('username')
  119.                     ->addError(new FormError(sprintf('username "%s" already used'$data->getUsername())));
  120.             return;
  121.         }
  122.         
  123.         $encoder $this->encoderFactory->getPasswordHasher($data);
  124.         $password $encoder->hash($data->getPassword());
  125.         
  126.         $data->setPassword($password);
  127.         $event->setData($data);
  128.     }
  129.     
  130.     public function getRoles():array
  131.     {
  132.         $roles = [];
  133.         if (!$this->tokenStorage->getToken()) {
  134.             
  135.             return $roles;
  136.         }
  137.         foreach ($this->roleHierarchy->getReachableRoleNames($this->tokenStorage->getToken()->getRoleNames()) as $role) {
  138.             $roles[$role] = $role;
  139.         }
  140.         
  141.         unset($roles[DefaultUser::ROLE_USER]);
  142.         
  143.         return $roles;
  144.     }
  145.     public function isSupported(string $className): bool 
  146.     {
  147.         $refflection = new \ReflectionClass($className);
  148.         
  149.         return $refflection->implementsInterface(KmjUserInterface::class);
  150.     }
  151. }