<?php
namespace App\Controller;
use App\Repository\UserRepository;
use App\Security\LoginFormAuthenticator;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
/**
* @Route("/{_locale}/auth")
*/
class SecurityController extends AbstractController
{
/**
* @Route("/login", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
// if ($this->getUser()) {
// return $this->redirectToRoute('target_path');
// }
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
/**
* @Route("/api-login", name="app_api_login")
*/
public function apiLogin(LoginFormAuthenticator $authenticator, GuardAuthenticatorHandler $guardAuthenticatorHandler, Request $request, UserRepository $userRepository): Response
{
if ($request->query->has('ucrd') && $request->query->has('signature')) {
parse_str(parse_url($request->getRequestUri(), PHP_URL_QUERY), $params);
$cipher = 'aes256';
$ucrd = openssl_decrypt($params['ucrd'], $cipher, $_ENV['APP_SECRET'], 0, $params['signature']);
if($ucrd!==false && null !== $ucrd){
$ucrdData=json_decode($ucrd,true);
if (isset($ucrdData['u']) && isset($ucrdData['p'])) {
$user = $userRepository->findOneBy(['email' => $ucrdData['u']]);
if ($user) {
// check Password
if ($user->getPassword() === $ucrdData['p']) {
$guardAuthenticatorHandler->authenticateUserAndHandleSuccess($user, $request, $authenticator, 'main');
$roles=$user->getRoles();
dd($roles);
return $this->redirectToRoute('index');
} else {
// TODO: Fasches Passwort
throw new \LogicException('Falsches Passwort ' . "\n" .$ucrdData['p'] . "\n" . $user->getPassword());
}
} else {
// TODO: User existiert nicht
throw new \LogicException('User ' . $ucrdData['u'] . ' existiert nicht');
}
}
}
}
// TODO: Fehlende Url Parameter
throw new \LogicException('Fehlende Url Parameter');
}
/**
* @Route("/logout", name="app_logout")
*/
public function logout()
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
}