src/Controller/SecurityController.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Entity\Company;
  5. use App\Entity\CompanyGroup;
  6. use App\Repository\UserRepository;
  7. use App\Repository\CompanyRepository;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  14. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  15. class SecurityController extends AbstractController
  16. {
  17.     /**
  18.      * @Route("/login", name="login")
  19.      */
  20.     public function login(AuthenticationUtils $authenticationUtils): Response
  21.     {
  22.         $error $authenticationUtils->getLastAuthenticationError();
  23.         $lastUsername $authenticationUtils->getLastUsername();
  24.         return $this->render('@EasyAdmin/page/login.html.twig', [
  25.             // parameters usually defined in Symfony login forms
  26.             'error' => $error,
  27.             'last_username' => $lastUsername,
  28.             // OPTIONAL parameters to customize the login form:
  29.             // the translation_domain to use (define this option only if you are
  30.             // rendering the login template in a regular Symfony controller; when
  31.             // rendering it from an EasyAdmin Dashboard this is automatically set to
  32.             // the same domain as the rest of the Dashboard)
  33.             'translation_domain' => 'admin',
  34.             // the title visible above the login form (define this option only if you are
  35.             // rendering the login template in a regular Symfony controller; when rendering
  36.             // it from an EasyAdmin Dashboard this is automatically set as the Dashboard title)
  37.             'page_title' => 'Flota ABS',
  38.             // the string used to generate the CSRF token. If you don't define
  39.             // this parameter, the login form won't include a CSRF token
  40.             'csrf_token_intention' => 'authenticate',
  41.             // the URL users are redirected to after the login (default: '/admin')
  42.             'target_path' => $this->generateUrl('admin'),
  43.             // the label displayed for the username form field (the |trans filter is applied to it)
  44.             'username_label' => 'Podaj adres email',
  45.             // the label displayed for the password form field (the |trans filter is applied to it)
  46.             'password_label' => 'Podaj hasło',
  47.             // the label displayed for the Sign In form button (the |trans filter is applied to it)
  48.             'sign_in_label' => 'Zaloguj się',
  49.             // whether to enable or not the "remember me" checkbox (default: false)
  50.             'remember_me_enabled' => true,
  51.             // remember me name form field (default: '_remember_me')
  52.             'remember_me_parameter' => 'custom_remember_me_param',
  53.             // whether to check by default the "remember me" checkbox (default: false)
  54.             'remember_me_checked' => true,
  55.             // the label displayed for the remember me checkbox (the |trans filter is applied to it)
  56.             'remember_me_label' => 'Zapamiętaj mnie',
  57.         ]);
  58.     }
  59.     /**
  60.      * @Route("/logout", name="app_logout", methods={"GET"})
  61.      */
  62.     public function logout(): void
  63.     {
  64.         // controller can be blank: it will never be called!
  65.         throw new \Exception('Don\'t forget to activate logout in security.yaml');
  66.     }
  67.     /**
  68.      * @Route("/register", name="app_register", methods={"POST"})
  69.      */
  70.     public function register(Request $requestUserPasswordHasherInterface $passwordHasherUserRepository $userRepositoryEntityManagerInterface $entityManagerCompanyRepository $companyRepository): Response
  71.     {
  72.         $data json_decode(
  73.             $request->getContent(),
  74.             true
  75.         );
  76.         if (array_key_exists('email'$data) && array_key_exists('password'$data)) {
  77.             $user $userRepository->findOneByEmail($data['email']);
  78.             if ($user) {
  79.                 $response = new Response(
  80.                     json_encode(['error' => 'Użytkownik już istnieje']),
  81.                     Response::HTTP_OK,
  82.                     ['Content-type' => 'application/json']
  83.                 );
  84.                 return $response;
  85.             }
  86.             if (array_key_exists('companyCode'$data) && $data['companyCode']) {
  87.                 if ($data['password'] && $data['email'] && $data['companyCode']) {
  88.                     $company $companyRepository->findOneByCompanyCode($data['companyCode']);
  89.                     if ($company) {
  90.                         $user = new User();
  91.                         $user->setEmail($data['email']);
  92.                         $user->setFirstName($data['firstName']);
  93.                         $user->setLastName($data['lastName']);
  94.                         $user->setActive(false);
  95.                         $roles = ['ROLE_USER'];
  96.                         $user->setRoles($roles);
  97.                         $plaintextPassword $data['password'];
  98.                         // hash the password (based on the security.yaml config for the $user class)
  99.                         $hashedPassword $passwordHasher->hashPassword(
  100.                             $user,
  101.                             $plaintextPassword
  102.                         );
  103.                         $user->setPassword($hashedPassword);
  104.                         $user->setCompany($company);
  105.                         $entityManager->persist($user);
  106.                         $entityManager->flush();
  107.                     } else {
  108.                         $response = new Response(
  109.                             json_encode(['error' => 'Firma nie istnieje']),
  110.                             Response::HTTP_OK,
  111.                             ['Content-type' => 'application/json']
  112.                         );
  113.                         return $response;
  114.                     }
  115.                 }
  116.             } else
  117.             if (array_key_exists('company'$data) && $data['company']) {
  118.                 if ($data['password'] && $data['email'] && $data['company']) {
  119.                     $group = new CompanyGroup();
  120.                     $group->setName($data['company']);
  121.                     $entityManager->persist($group);
  122.                     
  123.                     $company = new Company($data['company']);
  124.                     $company->setName($data['company']);
  125.                     $company->setCompanyGroup($group);
  126.                     $company->setNip($data['companyNip']);
  127.                     $companyCode mt_rand(111111,999999);
  128.                     while($companyRepository->findOneByCompanyCode($companyCode)){
  129.                         $companyCode mt_rand(111111,999999);
  130.                     }
  131.                     $company->setCompanyCode($companyCode);
  132.                     $entityManager->persist($company);
  133.                     
  134.                     if ($company) {
  135.                         $user = new User();
  136.                         $user->setEmail($data['email']);
  137.                         $user->setFirstName($data['firstName']);
  138.                         $user->setLastName($data['lastName']);
  139.                         $user->setActive(true);
  140.                         $roles = ['ROLE_USER','ROLE_COMPANY_ADMIN','ROLE_GROUP_ADMIN'];
  141.                         $user->setRoles($roles);
  142.                         $user->setEmail($data['email']);
  143.                         $plaintextPassword $data['password'];
  144.                         // hash the password (based on the security.yaml config for the $user class)
  145.                         $hashedPassword $passwordHasher->hashPassword(
  146.                             $user,
  147.                             $plaintextPassword
  148.                         );
  149.                         $user->setPassword($hashedPassword);
  150.                         $user->setCompany($company);
  151.                         $entityManager->persist($user);
  152.                         $entityManager->flush();
  153.                     } else {
  154.                         $response = new Response(
  155.                             json_encode(['error' => 'Firma nie istnieje']),
  156.                             Response::HTTP_OK,
  157.                             ['Content-type' => 'application/json']
  158.                         );
  159.                         return $response;
  160.                     }
  161.                 }
  162.             }
  163.         } else {
  164.             $response = new Response(
  165.                 json_encode(['error' => 'Brak wymaganych danych']),
  166.                 Response::HTTP_OK,
  167.                 ['Content-type' => 'application/json']
  168.             );
  169.             return $response;
  170.         }
  171.         $ret = [];
  172.         if ($user){
  173.             $ret = ['id'=>$user->getId()];
  174.         }
  175.         $response = new Response(
  176.             json_encode($ret),
  177.             Response::HTTP_OK,
  178.             ['Content-type' => 'application/json']
  179.         );
  180.         return $response;
  181.     }
  182. }