<?php
declare(strict_types=1);
/**
* @copyright Copyright ©️ 2020-2021 seidemann: solutions GmbH <info@seidemann.com> - All Rights Reserved.
*
* Unauthorized copying and/or distribution of this file, via any medium, is strictly prohibited.
*
* Proprietary and confidential.
*/
namespace Seidemann\Hanagud\Controller;
use Exception;
use Seidemann\Hanagud\Entity\HanagudUser;
use Seidemann\Hanagud\Http\ApiResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class AccountController extends AbstractController
{
#[Route(path: '/ping', name: 'ping', methods: ['GET', 'POST'])]
public function ping(): Response
{
$user = $this->getUser();
$response = [
'isLoggedIn' => $this->isGranted('ROLE_USER'),
];
if ($user instanceof UserInterface) {
$userJson = ['id' => $user->getUserIdentifier()];
if ($user instanceof HanagudUser) {
$userJson['articleNumber'] = $user->getArticleNumber();
$userJson['userId'] = $user->getId();
$userJson['UserGroup'] = $user->getUserGroup();
}
$response['user'] = $userJson;
}
return new ApiResponse($response);
}
#[Route(path: '/login', name: 'login', methods: ['GET', 'POST'])]
public function showLoginForm(AuthenticationUtils $authenticationUtils): Response
{
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('login/index.html.twig', [
'user' => $this->getUser(),
'is_logged_in' => $this->isGranted('ROLE_USER'),
'last_username' => $lastUsername,
'error' => $error,
]);
}
#[Route(path: '/logout', name: 'logout', methods: ['GET'])]
public function logout()
{
// controller can be blank: it will never be executed!
throw new Exception('Don\'t forget to activate logout in security.yaml');
}
}