<?php
namespace App\Controller;
use App\Entity\User;
use App\Entity\Company;
use App\Entity\CompanyGroup;
use App\Repository\UserRepository;
use App\Repository\CompanyRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
class SecurityController extends AbstractController
{
/**
* @Route("/login", name="login")
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('@EasyAdmin/page/login.html.twig', [
// parameters usually defined in Symfony login forms
'error' => $error,
'last_username' => $lastUsername,
// OPTIONAL parameters to customize the login form:
// the translation_domain to use (define this option only if you are
// rendering the login template in a regular Symfony controller; when
// rendering it from an EasyAdmin Dashboard this is automatically set to
// the same domain as the rest of the Dashboard)
'translation_domain' => 'admin',
// the title visible above the login form (define this option only if you are
// rendering the login template in a regular Symfony controller; when rendering
// it from an EasyAdmin Dashboard this is automatically set as the Dashboard title)
'page_title' => 'Flota ABS',
// the string used to generate the CSRF token. If you don't define
// this parameter, the login form won't include a CSRF token
'csrf_token_intention' => 'authenticate',
// the URL users are redirected to after the login (default: '/admin')
'target_path' => $this->generateUrl('admin'),
// the label displayed for the username form field (the |trans filter is applied to it)
'username_label' => 'Podaj adres email',
// the label displayed for the password form field (the |trans filter is applied to it)
'password_label' => 'Podaj hasło',
// the label displayed for the Sign In form button (the |trans filter is applied to it)
'sign_in_label' => 'Zaloguj się',
// whether to enable or not the "remember me" checkbox (default: false)
'remember_me_enabled' => true,
// remember me name form field (default: '_remember_me')
'remember_me_parameter' => 'custom_remember_me_param',
// whether to check by default the "remember me" checkbox (default: false)
'remember_me_checked' => true,
// the label displayed for the remember me checkbox (the |trans filter is applied to it)
'remember_me_label' => 'Zapamiętaj mnie',
]);
}
/**
* @Route("/logout", name="app_logout", methods={"GET"})
*/
public function logout(): void
{
// controller can be blank: it will never be called!
throw new \Exception('Don\'t forget to activate logout in security.yaml');
}
/**
* @Route("/register", name="app_register", methods={"POST"})
*/
public function register(Request $request, UserPasswordHasherInterface $passwordHasher, UserRepository $userRepository, EntityManagerInterface $entityManager, CompanyRepository $companyRepository): Response
{
$data = json_decode(
$request->getContent(),
true
);
if (array_key_exists('email', $data) && array_key_exists('password', $data)) {
$user = $userRepository->findOneByEmail($data['email']);
if ($user) {
$response = new Response(
json_encode(['error' => 'Użytkownik już istnieje']),
Response::HTTP_OK,
['Content-type' => 'application/json']
);
return $response;
}
if (array_key_exists('companyCode', $data) && $data['companyCode']) {
if ($data['password'] && $data['email'] && $data['companyCode']) {
$company = $companyRepository->findOneByCompanyCode($data['companyCode']);
if ($company) {
$user = new User();
$user->setEmail($data['email']);
$user->setFirstName($data['firstName']);
$user->setLastName($data['lastName']);
$user->setActive(false);
$roles = ['ROLE_USER'];
$user->setRoles($roles);
$plaintextPassword = $data['password'];
// hash the password (based on the security.yaml config for the $user class)
$hashedPassword = $passwordHasher->hashPassword(
$user,
$plaintextPassword
);
$user->setPassword($hashedPassword);
$user->setCompany($company);
$entityManager->persist($user);
$entityManager->flush();
} else {
$response = new Response(
json_encode(['error' => 'Firma nie istnieje']),
Response::HTTP_OK,
['Content-type' => 'application/json']
);
return $response;
}
}
} else
if (array_key_exists('company', $data) && $data['company']) {
if ($data['password'] && $data['email'] && $data['company']) {
$group = new CompanyGroup();
$group->setName($data['company']);
$entityManager->persist($group);
$company = new Company($data['company']);
$company->setName($data['company']);
$company->setCompanyGroup($group);
$company->setNip($data['companyNip']);
$companyCode = mt_rand(111111,999999);
while($companyRepository->findOneByCompanyCode($companyCode)){
$companyCode = mt_rand(111111,999999);
}
$company->setCompanyCode($companyCode);
$entityManager->persist($company);
if ($company) {
$user = new User();
$user->setEmail($data['email']);
$user->setFirstName($data['firstName']);
$user->setLastName($data['lastName']);
$user->setActive(true);
$roles = ['ROLE_USER','ROLE_COMPANY_ADMIN','ROLE_GROUP_ADMIN'];
$user->setRoles($roles);
$user->setEmail($data['email']);
$plaintextPassword = $data['password'];
// hash the password (based on the security.yaml config for the $user class)
$hashedPassword = $passwordHasher->hashPassword(
$user,
$plaintextPassword
);
$user->setPassword($hashedPassword);
$user->setCompany($company);
$entityManager->persist($user);
$entityManager->flush();
} else {
$response = new Response(
json_encode(['error' => 'Firma nie istnieje']),
Response::HTTP_OK,
['Content-type' => 'application/json']
);
return $response;
}
}
}
} else {
$response = new Response(
json_encode(['error' => 'Brak wymaganych danych']),
Response::HTTP_OK,
['Content-type' => 'application/json']
);
return $response;
}
$ret = [];
if ($user){
$ret = ['id'=>$user->getId()];
}
$response = new Response(
json_encode($ret),
Response::HTTP_OK,
['Content-type' => 'application/json']
);
return $response;
}
}