<?php
namespace Sintra\CMSCoreBundle\Controller;
use CustomerManagementFrameworkBundle\Security\OAuth\Exception\AccountNotLinkedException;
use Pimcore\Controller\FrontendController;
use Pimcore\Model\DataObject\Customer;
use Sintra\CMSCoreBundle\Form\LoginFormType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
/**
* @Route("/account")
*/
class AccountController extends FrontendController
{
/**
* @Route("/login", name="account-login")
*
* @param AuthenticationUtils $authenticationUtils
* @param SessionInterface $session
* @param Request $request
* @param UserInterface|null $user
*
* @return Response|RedirectResponse
*/
public function loginAction(
AuthenticationUtils $authenticationUtils,
SessionInterface $session,
Request $request,
UserInterface $user = null
) {
//redirect user to index page if logged in
if ($user && $this->isGranted('ROLE_USER')) {
return $this->redirectToRoute('home');
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// OAuth handling - the OAuth authenticator is configured to return to the login page on errors
// (see failure_path configuration) - therefore we can fetch the last authentication error
// here. If the error is an AccountNotLinkedException (as thrown by our user provider) save the
// OAuth token to the session and redirect to registration with a special key which can be used
// to load the token to prepopulate the registration form with account data.
if ($error instanceof AccountNotLinkedException) {
//TO-DO if needed
}
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
$formData = [
'_username' => $lastUsername
];
$form = $this->createForm(LoginFormType::class, $formData, [
'action' => $this->generateUrl('account-login'),
]);
$template = $this->getLoginTemplate();
$locale = $request->get("locale");
return $this->render($template, [
'form' => $form->createView(),
'error' => $error,
'hideBreadcrumbs' => true,
'locale' => $locale
]);
}
private function getLoginTemplate()
{
if (file_exists(PIMCORE_PROJECT_ROOT . "/templates/account/login.html.twig")) {
return "/account/login.html.twig";
}
return "@SintraCMSCore/account/login.html.twig";
}
}