src/AdminBundle/Admin/ClientAdmin.php line 15

Open in your IDE?
  1. <?php
  2. namespace AdminBundle\Admin;
  3. use AdminBundle\AdminException;
  4. use CoreBundle\Entity\Dealer;
  5. use CoreBundle\Entity\User;
  6. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  7. use Sonata\AdminBundle\Datagrid\ListMapper;
  8. use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
  9. use Sonata\AdminBundle\Form\FormMapper;
  10. use Sonata\AdminBundle\Route\RouteCollectionInterface;
  11. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  12. class ClientAdmin extends BaseAdmin
  13. {
  14.     protected $baseRouteName 'client';
  15.     protected $baseRoutePattern 'core/client';
  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.         $requestPathInfo $request->getPathInfo();
  37.         /** @var User $user */
  38.         $user $this->getUser();
  39.         $roles $this->parameterBag->get('security.role_hierarchy.roles');
  40.         $roles array_keys($roles);
  41.         $roles array_combine($roles$roles);
  42.         if (!$user->hasRole('ROLE_SUPER_ADMIN')) {
  43.             throw new AdminException('Ви не маєте доступу');
  44.         }
  45.         $formMapper->with('Клієнт', ['class' => 'col-lg-6']);
  46.         $formMapper
  47.             ->add('name',null, ['label' => 'Имя''required' => true])
  48.             ->add('last_name',null, ['label' => 'Фамиия''required' => true])
  49.             ->add('username'null, ['label' => 'Логин''required' => true])
  50.             ->add('email',null, ['label' => 'E-mail''required' => true])
  51.             ->add('roles'ChoiceType::class, [
  52.                 'label' => 'Роль',
  53.                 'required' => true,
  54.                 'multiple' => true,
  55.                 'choices'  => $roles
  56.             ]);
  57.         if ($requestPathInfo === '/core/client/create') {
  58.             $formMapper->add('password'null, ['label' => 'Пароль''required' => true]);
  59.         }
  60.         $formMapper->end();
  61.     }
  62.     /**
  63.      * @param string $context
  64.      * @return ProxyQueryInterface
  65.      */
  66.     public function configureQuery($context 'list'): ProxyQueryInterface
  67.     {
  68.         $query parent::configureQuery($context);
  69.         $alias $query->getRootAliases()[0];
  70.         $query->andWhere($alias.'.roles like \'%ROLE_LOYALTY_USER%\' OR '
  71.             .$alias.'.roles like \'%ROLE_INSURANCE_USER%\''
  72.         );
  73.         $User $this->security->getUser();
  74.         if ($User->hasRole('ROLE_SUPER_ADMIN')) {
  75.             return $query;
  76.         }
  77.         $query->andWhere($alias.'.id = :id')->setParameter('id'$User->getId());
  78.         return $query;
  79.     }
  80.     protected function configureListFields(ListMapper $listMapper): void
  81.     {
  82.         $this->checkByRole(['ROLE_SUPER_ADMIN''ROLE_CONTENT_MANAGER','ROLE_DC_MANAGER']);
  83.         $dcList = [];
  84.         $dc $this->em->getRepository(Dealer::class)->findAll();
  85.         foreach ($dc as $item) {
  86.             $dcList[$item->getId()] = $item->getName();
  87.         }
  88.         $subDcList = [];
  89.         $subDc $this->em->getRepository(\ImporterBundle\Entity\Dealer::class)->findAll();
  90.         foreach ($subDc as $item) {
  91.             $subDcList[$item->getId()] = $item->getName();
  92.         }
  93.         $listMapper->addIdentifier('id')
  94.             ->add('fullName',null,['label' => 'Имя'])
  95.             ->add('username',null,['label' => 'Login'])
  96.             ->add('email')
  97.             //->add('roles')
  98.             ->add('dealer'ChoiceType::class, [
  99.                 'label' => 'ДЦ',
  100.                 'class' => Dealer::class,
  101.                 'choices' => $dcList,
  102.                 'editable' => true
  103.             ])
  104.             ->add('sub_dealer'ChoiceType::class, [
  105.                 'label' => 'Суб ДЦ',
  106.                 'class' => \ImporterBundle\Entity\Dealer::class,
  107.                 'choices' => $subDcList,
  108.                 'editable' => true,
  109.                 'admin_code' => 'admin.sub.contact',
  110.             ])
  111.             ->add('enabled'null, ['editable' => true])
  112.             ->add('_action''actions', [
  113.                 'label' => 'Действия',
  114.                 'actions' => [
  115.                     'edit' => [],
  116.                 ]
  117.             ])
  118.         ;
  119.     }
  120.     /**
  121.      * Створити нового користувача
  122.      *
  123.      * @param User $object
  124.      * @return void
  125.      * @throws AdminException
  126.      */
  127.     public function prePersist($object): void
  128.     {
  129.         /**
  130.          * @var User $user
  131.          */
  132.         $user $this->getUser();
  133.         if (!$user->hasRole('ROLE_SUPER_ADMIN')) {
  134.             throw new AdminException("У вас нету доступа");
  135.         }
  136.         if (empty($object->getName())) {
  137.             throw new AdminException("Не вказано поле Имя");
  138.         }
  139.         if (empty($object->getLastName())) {
  140.             throw new AdminException("Не вказано поле Фамиия");
  141.         }
  142.         if (empty($object->getUsername())) {
  143.             throw new AdminException("Не вказано поле Логин");
  144.         }
  145.         if (empty($object->getEmail())) {
  146.             throw new AdminException("Не вказано поле E-mail");
  147.         }
  148.         if (empty($object->getPassword())) {
  149.             throw new AdminException("Не вказано поле Пароль");
  150.         }
  151.         $model = new UserAdminModel($this->getEntityManager());
  152.         if (!empty($model->selectByLogin($object->getUsername()))) {
  153.             throw new AdminException("Логин вже використовується");
  154.         }
  155.         if (!empty($model->selectByEmail($object->getEmail()))) {
  156.             throw new AdminException("E-mail вже використовується");
  157.         }
  158.         $password $object->getPassword();
  159.         $password password_hash($passwordPASSWORD_BCRYPT, ['cost' => 13]);
  160.         $object->setEnabled(true);
  161.         $object->setPassword($password);
  162.         parent::prePersist($object);
  163.     }
  164. }