Skip to content

Commit

Permalink
Use "twig" service instead of deprecated "templating" where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
phansys authored and OskarStark committed Aug 10, 2019
1 parent 7314904 commit d9406ff
Show file tree
Hide file tree
Showing 20 changed files with 194 additions and 142 deletions.
14 changes: 7 additions & 7 deletions src/Action/CheckEmailAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@

use Sonata\AdminBundle\Admin\Pool;
use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Twig\Environment;

final class CheckEmailAction
{
/**
* @var EngineInterface
* @var Environment
*/
private $templating;
private $twig;

/**
* @var UrlGeneratorInterface
Expand All @@ -49,13 +49,13 @@ final class CheckEmailAction
private $resetTtl;

public function __construct(
EngineInterface $templating,
Environment $twig,
UrlGeneratorInterface $urlGenerator,
Pool $adminPool,
TemplateRegistryInterface $templateRegistry,
int $resetTtl
) {
$this->templating = $templating;
$this->twig = $twig;
$this->urlGenerator = $urlGenerator;
$this->adminPool = $adminPool;
$this->templateRegistry = $templateRegistry;
Expand All @@ -71,10 +71,10 @@ public function __invoke(Request $request): Response
return new RedirectResponse($this->urlGenerator->generate('sonata_user_admin_resetting_request'));
}

return $this->templating->renderResponse('@SonataUser/Admin/Security/Resetting/checkEmail.html.twig', [
return new Response($this->twig->render('@SonataUser/Admin/Security/Resetting/checkEmail.html.twig', [
'base_template' => $this->templateRegistry->getTemplate('layout'),
'admin_pool' => $this->adminPool,
'tokenLifetime' => ceil($this->resetTtl / 3600),
]);
]));
}
}
14 changes: 7 additions & 7 deletions src/Action/LoginAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Sonata\AdminBundle\Admin\Pool;
use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
use Sonata\UserBundle\Model\UserInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -27,13 +26,14 @@
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
use Twig\Environment;

final class LoginAction
{
/**
* @var EngineInterface
* @var Environment
*/
private $templating;
private $twig;

/**
* @var UrlGeneratorInterface
Expand Down Expand Up @@ -71,15 +71,15 @@ final class LoginAction
private $csrfTokenManager;

public function __construct(
EngineInterface $templating,
Environment $twig,
UrlGeneratorInterface $urlGenerator,
AuthorizationCheckerInterface $authorizationChecker,
Pool $adminPool,
TemplateRegistryInterface $templateRegistry,
TokenStorageInterface $tokenStorage,
Session $session
) {
$this->templating = $templating;
$this->twig = $twig;
$this->urlGenerator = $urlGenerator;
$this->authorizationChecker = $authorizationChecker;
$this->adminPool = $adminPool;
Expand Down Expand Up @@ -126,14 +126,14 @@ public function __invoke(Request $request): Response
$csrfToken = $this->csrfTokenManager->getToken('authenticate')->getValue();
}

return $this->templating->renderResponse('@SonataUser/Admin/Security/login.html.twig', [
return new Response($this->twig->render('@SonataUser/Admin/Security/login.html.twig', [
'admin_pool' => $this->adminPool,
'base_template' => $this->templateRegistry->getTemplate('layout'),
'csrf_token' => $csrfToken,
'error' => $error,
'last_username' => (null === $session) ? '' : $session->get(Security::LAST_USERNAME),
'reset_route' => $this->urlGenerator->generate('sonata_user_admin_resetting_request'),
]);
]));
}

public function setCsrfTokenManager(CsrfTokenManagerInterface $csrfTokenManager): void
Expand Down
14 changes: 7 additions & 7 deletions src/Action/RequestAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@

use Sonata\AdminBundle\Admin\Pool;
use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Twig\Environment;

final class RequestAction
{
/**
* @var EngineInterface
* @var Environment
*/
private $templating;
private $twig;

/**
* @var UrlGeneratorInterface
Expand All @@ -50,13 +50,13 @@ final class RequestAction
private $templateRegistry;

public function __construct(
EngineInterface $templating,
Environment $twig,
UrlGeneratorInterface $urlGenerator,
AuthorizationCheckerInterface $authorizationChecker,
Pool $adminPool,
TemplateRegistryInterface $templateRegistry
) {
$this->templating = $templating;
$this->twig = $twig;
$this->urlGenerator = $urlGenerator;
$this->authorizationChecker = $authorizationChecker;
$this->adminPool = $adminPool;
Expand All @@ -69,9 +69,9 @@ public function __invoke(Request $request): Response
return new RedirectResponse($this->urlGenerator->generate('sonata_admin_dashboard'));
}

return $this->templating->renderResponse('@SonataUser/Admin/Security/Resetting/request.html.twig', [
return new Response($this->twig->render('@SonataUser/Admin/Security/Resetting/request.html.twig', [
'base_template' => $this->templateRegistry->getTemplate('layout'),
'admin_pool' => $this->adminPool,
]);
]));
}
}
14 changes: 7 additions & 7 deletions src/Action/ResetAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Psr\Log\NullLogger;
use Sonata\AdminBundle\Admin\Pool;
use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -30,15 +29,16 @@
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Exception\AccountStatusException;
use Symfony\Component\Translation\TranslatorInterface;
use Twig\Environment;

final class ResetAction
{
use LoggerAwareTrait;

/**
* @var EngineInterface
* @var Environment
*/
private $templating;
private $twig;

/**
* @var UrlGeneratorInterface
Expand Down Expand Up @@ -96,7 +96,7 @@ final class ResetAction
private $firewallName;

public function __construct(
EngineInterface $templating,
Environment $twig,
UrlGeneratorInterface $urlGenerator,
AuthorizationCheckerInterface $authorizationChecker,
Pool $adminPool,
Expand All @@ -109,7 +109,7 @@ public function __construct(
int $resetTtl,
string $firewallName
) {
$this->templating = $templating;
$this->twig = $twig;
$this->urlGenerator = $urlGenerator;
$this->authorizationChecker = $authorizationChecker;
$this->adminPool = $adminPool;
Expand Down Expand Up @@ -172,11 +172,11 @@ public function __invoke(Request $request, $token): Response
return $response;
}

return $this->templating->renderResponse('@SonataUser/Admin/Security/Resetting/reset.html.twig', [
return new Response($this->twig->render('@SonataUser/Admin/Security/Resetting/reset.html.twig', [
'token' => $token,
'form' => $form->createView(),
'base_template' => $this->templateRegistry->getTemplate('layout'),
'admin_pool' => $this->adminPool,
]);
]));
}
}
14 changes: 7 additions & 7 deletions src/Action/SendEmailAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@
use FOS\UserBundle\Util\TokenGeneratorInterface;
use Sonata\AdminBundle\Admin\Pool;
use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Twig\Environment;

final class SendEmailAction
{
/**
* @var EngineInterface
* @var Environment
*/
private $templating;
private $twig;

/**
* @var UrlGeneratorInterface
Expand Down Expand Up @@ -67,7 +67,7 @@ final class SendEmailAction
private $resetTtl;

public function __construct(
EngineInterface $templating,
Environment $twig,
UrlGeneratorInterface $urlGenerator,
Pool $adminPool,
TemplateRegistryInterface $templateRegistry,
Expand All @@ -76,7 +76,7 @@ public function __construct(
TokenGeneratorInterface $tokenGenerator,
int $resetTtl
) {
$this->templating = $templating;
$this->twig = $twig;
$this->urlGenerator = $urlGenerator;
$this->adminPool = $adminPool;
$this->templateRegistry = $templateRegistry;
Expand All @@ -93,11 +93,11 @@ public function __invoke(Request $request): Response
$user = $this->userManager->findUserByUsernameOrEmail($username);

if (null === $user) {
return $this->templating->renderResponse('@SonataUser/Admin/Security/Resetting/request.html.twig', [
return new Response($this->twig->render('@SonataUser/Admin/Security/Resetting/request.html.twig', [
'base_template' => $this->templateRegistry->getTemplate('layout'),
'admin_pool' => $this->adminPool,
'invalid_username' => $username,
]);
]));
}

if (null !== $user && !$user->isPasswordRequestNonExpired($this->resetTtl)) {
Expand Down
12 changes: 6 additions & 6 deletions src/EventListener/TwoFactorLoginSuccessHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
use FOS\UserBundle\Model\UserManagerInterface;
use Sonata\UserBundle\GoogleAuthenticator\Helper;
use Sonata\UserBundle\Model\User;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Twig\Environment;

/**
* Class TwoFactorLoginSuccessHandler is used for handling 2FA authorization for enabled roles and ips.
Expand All @@ -32,7 +32,7 @@
final class TwoFactorLoginSuccessHandler implements AuthenticationSuccessHandlerInterface
{
/**
* @var EngineInterface
* @var Environment
*/
private $engine;

Expand All @@ -52,7 +52,7 @@ final class TwoFactorLoginSuccessHandler implements AuthenticationSuccessHandler
private $urlGenerator;

public function __construct(
EngineInterface $engine,
Environment $engine,
Helper $helper,
UserManagerInterface $userManager,
UrlGeneratorInterface $urlGenerator = null // NEXT_MAJOR: make it mandatory.
Expand All @@ -66,7 +66,7 @@ public function __construct(
/**
* @return RedirectResponse|Response
*/
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
public function onAuthenticationSuccess(Request $request, TokenInterface $token): Response
{
/** @var $user User */
$user = $token->getUser();
Expand All @@ -79,15 +79,15 @@ public function onAuthenticationSuccess(Request $request, TokenInterface $token)
$qrCodeUrl = $this->googleAuthenticator->getUrl($user);
$this->userManager->updateUser($user);

return $this->engine->renderResponse(
return new Response($this->engine->render(
'@SonataUser/Admin/Security/login.html.twig',
[
'qrCodeUrl' => $qrCodeUrl,
'qrSecret' => $secret,
'base_template' => '@SonataAdmin/standard_layout.html.twig',
'error' => [],
]
);
));
} elseif ($needToHave2FA && $user->getTwoStepVerificationCode()) {
$request->getSession()->set($this->googleAuthenticator->getSessionKey($token), null);
}
Expand Down
Loading

0 comments on commit d9406ff

Please sign in to comment.