Simpler Programmatic Login
Logging in users programmatically is a common need in many applications. That's
why in Symfony 6.2 we're adding a login()
method to the Security service.
On any service or controller, you can now do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
use Symfony\Bundle\SecurityBundle\Security;
// ...
class SomeService
{
public function __construct(
private Security $security,
) {
}
public function someMethod()
{
// fetch a UserInterface object somehow (e.g. from a database)
$user = ...
// login the user programmatically
$this->security->login($user);
// if you have many authenticators associated to the current firewall,
// you must pass explicitly the name of authenticator to use
$this->security->login($user, 'form_login');
$this->security->login($user, SomeApiKeyAuthenticator::class);
// ...
}
}
Custom Target URL When Impersonating Users
Similar to the feature that allows to configure the target URL after login,
in Symfony 6.2 we're adding a new feature to allow you configure the target
URL after impersonating a user. To do so, define the new target_route
option under the switch_user
option of your firewall:
1 2 3 4 5 6 7 8 9 10
# config/packages/security.yaml
security:
# ...
firewalls:
main:
# ...
switch_user:
# ...
target_route: 'app_user_dashboard'
Custom Lifetime for Login Links
When using login links to implement passwordless authentication, the lifetime
of those links is configured globally for all. In Symfony 6.2 we're adding a
feature so you can configure the lifetime per link. Use the third optional
argument of createLoginLink()
to override the global lifetime with a new
custom value (in seconds):
1 2 3
// this login link will have a lifetime of 60 seconds
$loginLinkDetails = $loginLinkHandler->createLoginLink($user, null, 60);
$loginLink = $loginLinkDetails->getUrl();
Multiple User Checkers per Firewall
User checkers allow you to define additional checks performed during the authentication of a user, to verify if the identified user is allowed to log in. You can only apply one user checker per firewall, which makes it harder to share logic.
Imagine an application that has two firewalls (e.g. API and traditional web login) and needs to apply these checkers: for both firewalls, check that the user account is not disabled; for the API firewall, check also that user has API access.
In Symfony 6.2 we're introducing a new "chained user checker" feature so you can
call multiple user checkers for a firewall. To do so, apply to each user checker
the tags corresponding to the firewall where it applies (tags follow the
pattern security.user_checker.<firewall name>
).
In Symfony 6.2, the previous example can be solved as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
namespace App\Security\User;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
use Symfony\Component\Security\Core\User\UserCheckerInterface;
#[AutoconfigureTag('security.user_checker.main', ['priority' => 10])]
#[AutoconfigureTag('security.user_checker.api', ['priority' => 10])]
final class DisabledAccountUserChecker implements UserCheckerInterface
{
// ...
}
#[AutoconfigureTag('security.user_checker.api', ['priority' => 5])]
final class ApiAccessAllowedUserChecker implements UserCheckerInterface
{
// ...
}
Hey, thanks for mentioning my PR :) before the final release, the config was renamed to target_route from target_url (in another PR) I think the config example should be updated accordingly, see https://github.com/symfony/symfony-docs/pull/17024 for ref cheers!
@Antoine thanks for reviewing the post. I've just updated it to fix the error that you reported. Cheers!
I really loved those improments especially the programatically login which was difficult in the past.
I think the last example can be improved using the AutoconfigureTag attribute, eg.:
#[AutoconfigureTag('security.user_checker.api', ['priority' => 5])]
@Jonathan you are right! I've just updated the post with your proposal. Thanks!
I've seen a number of devs over the years push code to programmatically log in when an authenticator would be appropriate. I hope making that easier doesn't encourage this bad habit.