Skip to content

Instantly share code, notes, and snippets.

@ajgarlag
Created May 21, 2020 06:51
Show Gist options
  • Save ajgarlag/43ec84816d72bdd513dc238c0dba6de4 to your computer and use it in GitHub Desktop.
Save ajgarlag/43ec84816d72bdd513dc238c0dba6de4 to your computer and use it in GitHub Desktop.
<?php
/* composer.json
{
"require": {
"symfony/security-core": ">=3.4"
}
}
*/
require 'vendor/autoload.php';
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\User\UserInterface;
class User implements UserInterface
{
private const USERNAME = 'dummy';
private const ROLE_DUMMY = 'ROLE_DUMMY';
public function getUsername()
{
return self::USERNAME;
}
public function getRoles()
{
return [self::ROLE_DUMMY];
}
public function getPassword()
{
return null;
}
public function getSalt()
{
return null;
}
public function eraseCredentials()
{
return;
}
}
$user = new User();
$token = new UsernamePasswordToken($user, null, uniqid(), ['ROLE_EXTRA']);
assert($token->isAuthenticated());
echo 'Token is authenticated.' . PHP_EOL;
$token->setUser($user);
echo 'After setting the same user instance into the token,' . PHP_EOL;
echo ' the token should be still authenticated;' . PHP_EOL;
if ($token->isAuthenticated()) {
echo ' and token is still authenticated.' . PHP_EOL;
echo 'So you are using symfony/symfony <= 4.3' . PHP_EOL;
} else {
echo ' but token is not authenticated anymore.' . PHP_EOL;
echo 'So you are using symfony/symfony >= 4.4' . PHP_EOL;
}
assert($token->isAuthenticated());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment