src/AdminBundle/Admin/UserAdmin.php line 17

Open in your IDE?
  1. <?php
  2. namespace AdminBundle\Admin;
  3. use CoreBundle\Entity\Dealer;
  4. use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
  5. use AdminBundle\AdminException;
  6. use CoreBundle\Entity\User;
  7. use Sonata\AdminBundle\Datagrid\ListMapper;
  8. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  9. use Sonata\AdminBundle\Form\FormMapper;
  10. use Sonata\AdminBundle\Route\RouteCollectionInterface;
  11. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  12. use Symfony\Component\Form\FormEvent;
  13. use Symfony\Component\Form\FormEvents;
  14. class UserAdmin extends BaseAdmin
  15. {
  16.     public function configureRoutes(RouteCollectionInterface $collection): void
  17.     {
  18.         $collection->remove('delete');
  19.         $collection->remove('view');
  20.     }
  21.     protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
  22.     {
  23.         $datagridMapper->add('id');
  24.         $datagridMapper->add('username');
  25.         $datagridMapper->add('email');
  26.         $datagridMapper->add('name');
  27.         $datagridMapper->add('last_name');
  28.     }
  29.     /**
  30.      * @param FormMapper $formMapper
  31.      * @throws AdminException
  32.      */
  33.     protected function configureFormFields(FormMapper $formMapper): void
  34.     {
  35.         $request $this->getRequest();
  36.         /** @var User $user */
  37.         $user $this->getUser();
  38.         $roles $this->parameterBag->get('security.role_hierarchy.roles');
  39.         $roles array_keys($roles);
  40.         $roles array_combine($roles$roles);
  41.         if (!$user->hasRole('ROLE_SUPER_ADMIN')) {
  42.             throw new AdminException('Ви не маєте доступу');
  43.         }
  44.         $dc $this->getEntityManager()
  45.             ->getRepository(Dealer::class)
  46.             ->findAll();
  47.         $dealerChoices = [];
  48.         foreach ($dc as $dealer) {
  49.             $dealerChoices[$dealer->getName()] = $dealer->getId();
  50.         }
  51.         $formMapper->with('Пользователь', ['class' => 'col-lg-6']);
  52.         $formMapper
  53.             ->add('name',null, ['label' => 'Имя''required' => true])
  54.             ->add('last_name',null, ['label' => 'Фамиия''required' => true])
  55.             ->add('username'null, ['label' => 'Логин''required' => true])
  56.             ->add('email',null, ['label' => 'E-mail''required' => true])
  57.             ->add('change_dealer_list'ChoiceType::class, [
  58.                 'label' => 'На які ДЦ може переключатися користувач',
  59.                 'required' => false,
  60.                 'multiple' => true,
  61.                 'expanded' => false,
  62.                 'choices'  => $dealerChoices,
  63.             ])
  64.             ->add('roles'ChoiceType::class, [
  65.                 'label' => 'Роль',
  66.                 'required' => true,
  67.                 'multiple' => true,
  68.                 'choices'  => $roles
  69.             ]);
  70.             $formMapper->add('password'null, ['label' => 'Пароль''required' => true]);
  71.         $formMapper->getFormBuilder()->addEventListener(
  72.             FormEvents::PRE_SUBMIT,
  73.             function (FormEvent $event): void {
  74.                 $data $event->getData();
  75.                 $form $event->getForm();
  76.                 if ($data['password'] !== $form->get('password')->getData()) {
  77.                     $data['password'] = password_hash($data['password'], PASSWORD_BCRYPT, ['cost' => 13]);
  78.                 }
  79.                 $event->setData($data);
  80.             }
  81.         );
  82.         $formMapper->end();
  83.     }
  84.     /**
  85.      * @param string $context
  86.      * @return ProxyQueryInterface
  87.      */
  88.     public function configureQuery($context 'list'): ProxyQueryInterface
  89.     {
  90.         $query parent::configureQuery($context);
  91.         $alias $query->getRootAliases()[0];
  92.         $query->andWhere($alias.'.roles like \'%ROLE_ADMIN%\' OR '
  93.             .$alias.'.roles like \'%ROLE_INSURANCE_AVARKOM%\' OR '
  94.             .$alias.'.roles like \'%ROLE_CORP_SALES_MANAGER%\' OR '
  95.             .$alias.'.roles like \'%ROLE_CREDIT_MANAGER%\' OR '
  96.             .$alias.'.roles like \'%ROLE_ACQUIRING_ADMIN%\' OR '
  97.             .$alias.'.roles like \'%ROLE_INSURANCE_ADMIN%\' OR '
  98.             .$alias.'.roles like \'%ROLE_DC_MANAGER%\' OR '
  99.             .$alias.'.roles like \'%ROLE_TICKET%\' OR '
  100.             .$alias.'.roles like \'%ROLE_AUTOMARKET_MANAGER%\' OR '
  101.             .$alias.'.roles like \'%ROLE_CONTENT_MANAGER%\' OR '
  102.             .$alias.'.roles like \'%ROLE_YAMAHA_SUB_DEALER%\' OR '
  103.             .$alias.'.roles like \'%ROLE_SUPER_ADMIN%\''
  104.         );
  105.         $User $this->getUser();
  106.         if($User?->hasRole("ROLE_SUPER_ADMIN")) {
  107.             return $query;
  108.         }
  109.         $query->andWhere($alias.'.id = :id')->setParameter('id'$User->getId());
  110.         return $query;
  111.     }
  112.     protected function configureListFields(ListMapper $listMapper): void
  113.     {
  114.         $this->checkByRole(['ROLE_SUPER_ADMIN''ROLE_CONTENT_MANAGER','ROLE_DC_MANAGER']);
  115.         $dcList = [];
  116.         $dc $this->getEntityManager()->getRepository(Dealer::class)->findAll();
  117.         foreach ($dc as $item) {
  118.             $dcList[$item->getId()] = $item->getName();
  119.         }
  120.         $subDcList = [];
  121.         $subDc $this->getEntityManager()->getRepository(\ImporterBundle\Entity\Dealer::class)->findAll();
  122.         foreach ($subDc as $item) {
  123.             $subDcList[$item->getId()] = $item->getName();
  124.         }
  125.         $listMapper->addIdentifier('id')
  126.             ->add('fullName',null,['label' => 'Имя'])
  127.             ->add('username',null,['label' => 'Login'])
  128.             ->add('email');
  129.             if($this->getUser()->hasRole("ROLE_SUPER_ADMIN")) {
  130.                 $listMapper
  131.                     ->add('dealer''choice', [
  132.                         'label' => 'ДЦ',
  133.                         'class' => Dealer::class,
  134.                         'editable' => true,
  135.                         'choices' => $dcList
  136.                     ])
  137.                     ->add('sub_dealer''choice', [
  138.                         'label' => 'Суб ДЦ',
  139.                         'class' => \ImporterBundle\Entity\Dealer::class,
  140.                         'editable' => true,
  141.                         'admin_code' => 'admin.sub.contact',
  142.                         'choices' => $subDcList,
  143.                     ]);
  144.             } else {
  145.                 $listMapper
  146.                     ->add('dealer'null, [
  147.                         'label' => 'ДЦ',
  148.                         'class' => Dealer::class,
  149.                         'editable' => true
  150.                     ])
  151.                     ->add('sub_dealer'null, [
  152.                         'label' => 'Суб ДЦ',
  153.                         'class' => \ImporterBundle\Entity\Dealer::class,
  154.                         'editable' => true,
  155.                         'admin_code' => 'admin.sub.contact',
  156.                     ]);
  157.             }
  158.             $listMapper->add('enabled'null, ['editable' => true])
  159.             ->add('_action''actions', [
  160.                 'label' => 'Действия',
  161.                 'actions' => [
  162.                     'edit' => [],
  163.                 ]
  164.             ])
  165.         ;
  166.     }
  167.     /**
  168.      * Створити нового користувача
  169.      *
  170.      * @param User $object
  171.      * @throws AdminException
  172.      */
  173.     public function prePersist($object): void
  174.     {
  175.         /**
  176.          * @var User $user
  177.          */
  178.         $user $this->getUser();
  179.         if (!$user->hasRole('ROLE_SUPER_ADMIN')) {
  180.             throw new AdminException("У вас нету доступа");
  181.         }
  182.         if (empty($object->getName())) {
  183.             throw new AdminException("Не вказано поле Имя");
  184.         }
  185.         if (empty($object->getLastName())) {
  186.             throw new AdminException("Не вказано поле Фамиия");
  187.         }
  188.         if (empty($object->getUsername())) {
  189.             throw new AdminException("Не вказано поле Логин");
  190.         }
  191.         if (empty($object->getEmail())) {
  192.             throw new AdminException("Не вказано поле E-mail");
  193.         }
  194.         if (empty($object->getPassword())) {
  195.             throw new AdminException("Не вказано поле Пароль");
  196.         }
  197.         $model = new UserAdminModel($this->getEntityManager());
  198.         if (!empty($model->selectByLogin($object->getUsername()))) {
  199.             throw new AdminException("Логин вже використовується");
  200.         }
  201.         if (!empty($model->selectByEmail($object->getEmail()))) {
  202.             throw new AdminException("E-mail вже використовується");
  203.         }
  204.         $password $object->getPassword();
  205.         $passwordInfo password_get_info($password);
  206.         if ($passwordInfo['algo'] == 0) {
  207.             $password password_hash($passwordPASSWORD_BCRYPT, ['cost' => 13]);
  208.         }
  209.         $object->setEnabled(true);
  210.         $object->setPassword($password);
  211.         parent::prePersist($object);
  212.     }
  213. }