src/Controller/AdminUsersController.php line 177

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use App\Forms\Admin\AdminUserForm;
  6. use App\Helpers\UsersHelper;
  7. use App\Helpers\EmailHelper;
  8. use Symfony\Component\Validator\Constraints\File;
  9. use Symfony\Component\Form\Extension\Core\Type\TextType;
  10. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  11. use Symfony\Component\Filesystem\Filesystem;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use App\Entity\Users;
  16. use Symfony\Component\String\Slugger\SluggerInterface;
  17. use Knp\Component\Pager\PaginatorInterface;
  18. use App\Managements\SettingsManagement;
  19. use Symfony\Contracts\Translation\TranslatorInterface;
  20. use App\Managements\PayersManagement;
  21. class AdminUsersController extends AbstractController
  22. {
  23.     public function __construct(
  24.         SluggerInterface $slugger,
  25.         TranslatorInterface $translator,
  26.         // $activeMenuParent = 'admin_users',
  27.         PayersManagement $payersManagement
  28.     ) {
  29.         $this->slugger $slugger;
  30.         $this->incompletePayers $payersManagement->loadIncomplete();
  31.         $this->translator $translator;
  32.         $this->activeMenuParent 'admin_users';
  33.     }
  34.     /**
  35.      * @Route("/admin/uzytkownicy", name="admin_users")
  36.      */
  37.     public function index(
  38.         SettingsManagement $settingsManagement,
  39.         PaginatorInterface $paginator
  40.         UsersHelper $usersHelper,
  41.         Request $request
  42.     ){
  43.         if(in_array($this->activeMenuParent$_SESSION['accessModules'])){
  44.             $title $this->translator->trans('Users');
  45.             $searchForm $this->usersFilterForm();
  46.             $searchForm->handleRequest($request);
  47.             if ($searchForm->isSubmitted() && $searchForm->isValid()) {
  48.                 $users $this->getDoctrine()->getRepository(Users::class)->getUsers($searchForm->getData());
  49.             } else {
  50.                 $users $this->getDoctrine()->getRepository(Users::class)->findAll();
  51.             }
  52.             $users $usersHelper->prepareForListing($users);
  53.             $users $paginator->paginate($users$request->query->getInt('page'1), $settingsManagement->value('itemsPerDashboardPage'));
  54.             return $this->render('Admin/Users/list.html.twig', [
  55.                 'accessModules' => $_SESSION['accessModules'],
  56.                 'incompletePayers' => $this->incompletePayers,
  57.                 'title' => $title,
  58.                 'activeMenuParent' => $this->activeMenuParent,
  59.                 'searchForm' => $searchForm->createView(),
  60.                 'users' => $users
  61.             ]);
  62.         }else{
  63.             return $this->redirectToRoute($_SESSION['accessModules'][0]);
  64.         }
  65.     }
  66.     /**
  67.      * @Route("/admin/uzytkownicy/edytuj/{id}", name="admin_edit_user")
  68.      */
  69.     public function edit(
  70.         $id,
  71.         Request $request
  72.     ) {
  73.         if(in_array($this->activeMenuParent$_SESSION['accessModules'])){
  74.             $title 'Edycja użytkownika';
  75.             $user $this->getDoctrine()->getRepository(Users::class)->find($id);
  76.             $form $this->createForm(AdminUserForm::class, $user);
  77.             $form->handleRequest($request);
  78.             if ($form->isSubmitted() && $form->isValid()) {
  79.                 $item $form->getData();                
  80.                 $item->setCreatedDate($item->getCreatedDate());
  81.                 if(strlen($item->getPassword()) > 20){
  82.                     $item->setPassword($item->getPassword());
  83.                 }else{
  84.                     $item->setPassword(password_hash($item->getPassword(), PASSWORD_BCRYPT, array("cost" => 15)));
  85.                 }
  86.                 $entityManager $this->getDoctrine()->getManager();
  87.                 $entityManager->persist($item);
  88.                 $entityManager->flush();
  89.                 
  90.                 $this->addFlash('success'$this->translator->trans('Successed updated'));
  91.                 return $this->redirectToRoute('admin_users');
  92.             }
  93.             return $this->render('Admin/Users/details.html.twig', [
  94.                 'accessModules' => $_SESSION['accessModules'],
  95.                 'incompletePayers' => $this->incompletePayers,
  96.                 'title' => $title,
  97.                 'activeMenuParent' => $this->activeMenuParent,
  98.                 'form' => $form->createView(),
  99.                 'user' => $user
  100.             ]);
  101.         }else{
  102.             return $this->redirectToRoute($_SESSION['accessModules'][0]);
  103.         }
  104.     }
  105.     /**
  106.      * @Route("/admin/uzytkownicy/dodaj", name="admin_add_user")
  107.      */
  108.     public function add(
  109.         Request $request
  110.     ) {
  111.         $title 'Dodaj użytkownika';
  112.         $user = new Users();
  113.         $form $this->createForm(AdminUserForm::class, $user);
  114.         $form->handleRequest($request);
  115.         if ($form->isSubmitted() && $form->isValid()) {
  116.             $user $form->getData();
  117.             $user->setCreatedDate(new \DateTime());
  118.             $user->setType('worker');
  119.             $user->setActive(1);
  120.             $user->setPassword(password_hash($user->getPassword(), PASSWORD_BCRYPT, array("cost" => 15)));
  121.             $entityManager $this->getDoctrine()->getManager();
  122.             $entityManager->persist($user);
  123.             $entityManager->flush();
  124.             $this->addFlash('success'$this->translator->trans('Success updated'));
  125.             return $this->redirectToRoute('admin_users');
  126.         }
  127.         return $this->render('Admin/Users/details.html.twig', [
  128.             'accessModules' => $_SESSION['accessModules'],
  129.             'incompletePayers' => $this->incompletePayers,
  130.             'title' => $title,
  131.             'activeMenuParent' => $this->activeMenuParent,
  132.             'form' => $form->createView(),
  133.             'user' => $user
  134.         ]);
  135.     }
  136.     /**
  137.      * @Route("/remind-password", name="admin_remind_password")
  138.      */
  139.     public function remindPassword(
  140.         Request $request,
  141.         EmailHelper $emailHelper
  142.     ) {
  143.         $title 'Dodawanie użytkownika';
  144.         $form $this->remindPasswordForm();
  145.         $form->handleRequest($request);
  146.         if ($form->isSubmitted() && $form->isValid()) {
  147.             $email $form->getData()['email'];
  148.             $user $this->getDoctrine()->getRepository(Users::class)->findOneBy(array('email' => $email));
  149.             $newpass $this->randomPassword(8);
  150.             $user->setPassword(password_hash($newpassPASSWORD_BCRYPT, array("cost" => 15)));
  151.             $entityManager $this->getDoctrine()->getManager();
  152.             $entityManager->persist($user);
  153.             $entityManager->flush();
  154.             $formData = array(
  155.                 'newpass' => $newpass,
  156.                 'clientEmail' => $email,
  157.                 'subject' => 'Resetowanie hasła | Freeline',
  158.                 'fromEmail' => 'Biuro | Freeline Agencja Interaktywna <biuro@freeline.pl>',
  159.                 'bcc' => 'biuro@freeline.pl'
  160.             );
  161.             $template "resetpass";
  162.             try {
  163.                 $emailHelper->sendNewPassword($formData$template);
  164.                 return $this->redirectToRoute('admin');
  165.             } catch (FileException $e) {
  166.             }
  167.         }
  168.         return $this->render('Admin/remindPassword.html.twig', [
  169.             'title' => $title,
  170.             "form" => $form->createView()
  171.         ]);
  172.     }
  173.     function usersFilterForm($fields null)
  174.     {
  175.         $Form $this->createFormBuilder();
  176.         $Form $Form->add('name'TextType::class, ['required' => false'label' => 'Name''attr' => ['class' => 'form-control']]);
  177.         if (!is_null($fields)) {
  178.             foreach ($fields as $key => $value) {
  179.                 if ($key == 'select') {
  180.                     $choices = [];
  181.                     $choices['Wybierz'] = 0;
  182.                     foreach ($value[1] as $choice) {
  183.                         $choices[$choice->getName()] = $choice->getId();
  184.                     }
  185.                     $Form $Form->add($value[0], ChoiceType::class, [
  186.                         'label' => ucwords($value[0]),
  187.                         'choices' => $choices,
  188.                         'attr' => ['class' => 'form-control']
  189.                     ]);
  190.                 }
  191.             }
  192.         }
  193.         $Form $Form->add('save'SubmitType::class, ['label' => 'Search''attr' => ['class' => 'btn btn-theme pull-right']]);
  194.         $Form $Form->getForm();
  195.         return $Form;
  196.     }
  197.     function remindPasswordForm($fields null)
  198.     {
  199.         $Form $this->createFormBuilder();
  200.         $Form $Form->add('email'TextType::class, ['required' => false'label' => 'E-mail''attr' => ['placeholder' => 'E-mail''class' => 'form-control']]);
  201.         $Form $Form->add('save'SubmitType::class, ['label' => 'Remind password''attr' => ['class' => 'btn btn-theme btn-block']]);
  202.         $Form $Form->getForm();
  203.         return $Form;
  204.     }
  205.     function randomPassword(
  206.         $length,
  207.         $keyspace '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  208.     ) {
  209.         $str '';
  210.         $max mb_strlen($keyspace'8bit') - 1;
  211.         if ($max 1) {
  212.             throw new Exception('$keyspace must be at least two characters long');
  213.         }
  214.         for ($i 0$i $length; ++$i) {
  215.             $str .= $keyspace[random_int(0$max)];
  216.         }
  217.         return $str;
  218.     }
  219. }