Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirect to login page if unauthenticated #3014

Merged
merged 5 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/packages/security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ security:
pattern: ^/
user_checker: Bolt\Security\UserChecker
switch_user: { role: CAN_SWITCH_USER }
entry_point: Bolt\Security\AuthenticationEntryPointRedirector

custom_authenticators:
- Bolt\Security\LoginFormAuthenticator
Expand Down
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ parameters:
message: '#Property Bolt\\Entity\\(.*)::\$id is never written, only read#'
path: %currentWorkingDirectory%/src/Entity/*

# False positive: Call to an undefined method Symfony\Component\HttpFoundation\Session\SessionInterface::getFlashBag().
-
message: '#Symfony\\Component\\HttpFoundation\\Session\\SessionInterface::getFlashBag#'
path: %currentWorkingDirectory%/src/Security/AuthenticationEntryPointRedirector

includes:
- vendor/phpstan/phpstan-symfony/extension.neon
- vendor/phpstan/phpstan-doctrine/extension.neon
Expand Down
27 changes: 27 additions & 0 deletions src/Security/AuthenticationEntryPointRedirector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Bolt\Security;

use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;

class AuthenticationEntryPointRedirector implements AuthenticationEntryPointInterface
{
private $urlGenerator;

public function __construct(UrlGeneratorInterface $urlGenerator)
{
$this->urlGenerator = $urlGenerator;
}

public function start(Request $request, AuthenticationException $authException = null)
{
// add a custom flash message and redirect to the login page
$request->getSession()->getFlashBag()->add('warning', 'You have to login in order to access this page.');

return new RedirectResponse($this->urlGenerator->generate('bolt_login'));
}
}
4 changes: 4 additions & 0 deletions templates/security/login.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
</div>

</div>

<div class="admin__notifications">
{{ include('@bolt/_partials/_flash_messages.html.twig') }}
</div>
{% endblock %}


1 change: 1 addition & 0 deletions yaml-migrations/m_2021-12-10-security_1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ add:
main:
custom_authenticators:
- Bolt\Security\LoginFormAuthenticator
entry_point: Bolt\Security\AuthenticationEntryPointRedirector