src/Controller/UserController.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Component\HttpFoundation\Response;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use App\Entity\User;
  9. use App\Form\UserType;
  10. class UserController extends BaseController
  11. {
  12.     /**
  13.      * @Route("/login", name="login")
  14.      */
  15.     public function login(AuthenticationUtils $authenticationUtils): Response
  16.     {
  17.        if($this->isGranted('ROLE_USER')) return $this->redirectToRoute('root');
  18.        // get the login error if there is one
  19.        $error $authenticationUtils->getLastAuthenticationError();
  20.        // last username entered by the user
  21.        $lastUsername $authenticationUtils->getLastUsername();
  22.         return $this->render('login/login.html.twig', [
  23.            'last_username' => $lastUsername,
  24.            'error'         => $error,
  25.         ]);
  26.     }
  27.     /**
  28.      * @Route("/logout", name="logout", methods={"GET"})
  29.      */
  30.     public function logout(): void
  31.     {
  32.         // controller can be blank: it will never be called!
  33.         throw new \Exception('Don\'t forget to activate logout in security.yaml');
  34.     }
  35.     /**
  36.      * @Route("/confirm-logout", name="confirm_logout", methods={"GET"})
  37.      */
  38.     public function confirmLogout(): Response
  39.     {
  40.         return $this->render('login/logout.html.twig');
  41.     }
  42.     /**
  43.      * @Route("/profile", name="profile", methods={"GET"})
  44.      */
  45.     public function profile(): Response
  46.     {
  47.         return $this->render('login/profile.html.twig', [
  48.             'user' =>  $this->getUser(),
  49.         ]);
  50.     }
  51.     /**
  52.      * @Route("/users", name="users")
  53.      */
  54.     public function users()
  55.     {
  56.         $this->denyAccessUnlessGranted('ROLE_ADMIN');
  57.         $adminLevel $this->isGranted('ROLE_SUPER_ADMIN') ? 'ROLE_SUPER_ADMIN' 'ROLE_ADMIN';
  58.         return $this->render(
  59.             'pages/users.html.twig',
  60.             [
  61.                 'users' => $this->userModel->getAllUsers($adminLevel),
  62.             ]
  63.         );
  64.     }
  65.     /**
  66.      * @Route("/user/{id}", name="user_detail", defaults={"id"=null}, requirements={"id"="\d+"})
  67.      */
  68.     public function userDetail($idRequest $request)
  69.     {
  70.         $this->denyAccessUnlessGranted('ROLE_ADMIN'); // musim byt admin
  71.         $adminLevel $this->isGranted('ROLE_SUPER_ADMIN') ? 'ROLE_SUPER_ADMIN' 'ROLE_ADMIN';
  72.         // radsi si to osetrim:
  73.         $from $request->query->get('from');
  74.         if(!in_array($from, ['suppliers''providers'])) $from 'users';
  75.         if(is_null($id))
  76.         {
  77.             // NOVY UZIVATEL
  78.             // zde jeste specielni osetreni:
  79.             // pokud jsem jen admin, musim dostat v requestu suppliera nebo providera (navazane na uzivatele)
  80.             if($adminLevel == 'ROLE_ADMIN' && is_null($request->query->get('supplier_id')) && is_null($request->query->get('provider_id')))
  81.             {
  82.                 throw $this->createAccessDeniedException();
  83.             }
  84.             $user = new User;
  85.             $operation 'create';
  86.             // role podle todo, zda mam navazujici id:
  87.             switch(true)
  88.             {
  89.                 case !is_null($request->query->get('supplier_id')) :
  90.                     $user->setRoles(['ROLE_SUPPLIER']);
  91.                     $user->setSupplier($this->supplierModel->getSupplier($request->query->get('supplier_id')));
  92.                     break;
  93.                 case !is_null($request->query->get('provider_id')) :
  94.                     $user->setRoles(['ROLE_PROVIDER']);
  95.                     $user->setProvider($this->providerModel->getProvider($request->query->get('provider_id')));
  96.                     break;
  97.                 case $adminLevel == 'ROLE_SUPER_ADMIN':
  98.                     $user->setRoles(['ROLE_ADMIN']);
  99.                     break;
  100.             }
  101.         }
  102.         else
  103.         {
  104.             // editace
  105.             $user $this->userModel->getUser($adminLevel$id);
  106.             if(empty($user)) throw $this->createNotFoundException('Uživatel nenalezen');
  107.             $operation 'edit';
  108.         }
  109.         $role $this->userModel::USER_ROLE_NAMES[$user->getSingleRole()];
  110.         switch($user->getSingleRole())
  111.         {
  112.             case 'ROLE_SUPPLIER'$role .= ' ' $user->getSupplier()->getDescription(); break;
  113.             case 'ROLE_PROVIDER'$role .= ' ' $user->getProvider()->getDescription(); break;
  114.             default: break; // pro admina zustava jen admin
  115.         }
  116.         $form $this->createForm(UserType::class, $user, ['operation' => $operation'admin_level' => $adminLevel]);
  117.         $form->handleRequest($request);
  118.         if($form->isSubmitted() && $form->isValid())
  119.         {
  120.             // poresit prava
  121.             /*
  122.             if($adminLevel == 'ROLE_SUPER_ADMIN' && $form['roleStyle']->getData() == 'admin')
  123.             {
  124.                 $roles = array();
  125.                 $roles[] = 'ROLE_ADMIN';
  126.             }
  127.             else
  128.             {
  129.                 $roles = $form['roles']->getData();
  130.             }
  131.             $user->setRoles($roles);
  132.             */
  133.             if(is_null($id))
  134.             {
  135.                 // vytvareni noveho
  136.                 $result $this->userModel->createNewUser($user);
  137.                 if($result === true)
  138.                 {
  139.                     $this->addFlash('success''Uživatel vytvořen');
  140.                     return $this->redirectToRoute($from);
  141.                 }
  142.                 else
  143.                 {
  144.                     $this->addFlash('danger'$result);
  145.                 }
  146.             }
  147.             else
  148.             {
  149.                 // uprava stavajiciho
  150.                 $result $this->userModel->updateUser($user);
  151.                 if($result === true)
  152.                 {
  153.                     $this->addFlash('success''Uživatel upraven');
  154.                     return $this->redirectToRoute($from);
  155.                 }
  156.                 else
  157.                 {
  158.                     $this->addFlash('danger'$result);
  159.                 }
  160.             }
  161.         }
  162.         return $this->render(
  163.             'pages/user-detail.html.twig',
  164.             [
  165.                 'form'         => $form->createView(),
  166.                 'operation'    => $operation,
  167.                 'role'         => $role,
  168.                 'from'         => $from,
  169.             ]
  170.         );
  171.     }
  172.     /************************************************************************************************************************************************/
  173.     /* AJAX
  174.     /************************************************************************************************************************************************/
  175.     /**
  176.      * @Route("/ajax/user-toggle-block/{id}", name="user_toggle_block", requirements={"id"="\d+"}, methods={"POST"}, condition="request.isXmlHttpRequest()")
  177.      */
  178.     public function toggleUserBlock($id)
  179.     {
  180.         $this->denyAccessUnlessGranted('ROLE_ADMIN');
  181.         $adminLevel $this->isGranted('ROLE_SUPER_ADMIN') ? 'ROLE_SUPER_ADMIN' 'ROLE_ADMIN';
  182.         try
  183.         {
  184.             $stav $this->userModel->toggleUserBlock($adminLevel$id);
  185.         }
  186.         catch(\Exception $e)
  187.         {
  188.             return new JsonResponse(["code" => 500"result" => $e->getMessage()]);
  189.         }
  190.         return new JsonResponse(["code" => 200"result" => $stav '1' '0']);
  191.     }
  192. }