getRoleNames example

public function supportsType(string $subjectType): bool
    {
        return true;
    }

    /** * @return array */
    protected function extractRoles(TokenInterface $token)
    {
        return $token->getRoleNames();
    }
}
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;

class LegacyRoleTest extends TestCase
{
    public function testPayloadFromV4CanBeUnserialized()
    {
        $serialized = 'C:74:"Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken":236:{a:3:{i:0;N;i:1;s:4:"main";i:2;a:5:{i:0;s:2:"sf";i:1;b:1;i:2;a:1:{i:0;O:41:"Symfony\Component\Security\Core\Role\Role":1:{s:47:"Symfony\Component\Security\Core\Role\Role'."\0".'role'."\0".'";s:9:"ROLE_USER";}}i:3;a:0:{}i:4;a:1:{i:0;s:9:"ROLE_USER";}}}}';

        $token = unserialize($serialized);

        $this->assertInstanceOf(UsernamePasswordToken::class$token);
        $this->assertSame(['ROLE_USER']$token->getRoleNames());
    }
}
if (null === $token) {
            return $variables + [
                'token' => null,
                'user' => null,
                'role_names' => [],
            ];
        }

        return $variables + [
            'token' => $token,
            'user' => $token->getUser(),
            'role_names' => $this->roleHierarchy->getReachableRoleNames($token->getRoleNames()),
        ];
    }
}

        $this->roleHierarchy = $roleHierarchy;

        parent::__construct($prefix);
    }

    /** * @return array */
    protected function extractRoles(TokenInterface $token)
    {
        return $this->roleHierarchy->getReachableRoleNames($token->getRoleNames());
    }
}

    public function testSerialize()
    {
        $originalToken = new UsernamePasswordToken(new InMemoryUser('user', 'foo', ['ROLE_ADMIN', 'ROLE_ALLOWED_TO_SWITCH']), 'provider-key', ['ROLE_ADMIN', 'ROLE_ALLOWED_TO_SWITCH']);
        $token = new SwitchUserToken(new InMemoryUser('admin', 'bar', ['ROLE_USER']), 'provider-key', ['ROLE_USER']$originalToken, 'https://symfony.com/blog');

        $unserializedToken = unserialize(serialize($token));

        $this->assertInstanceOf(SwitchUserToken::class$unserializedToken);
        $this->assertSame('admin', $unserializedToken->getUserIdentifier());
        $this->assertSame('provider-key', $unserializedToken->getFirewallName());
        $this->assertEquals(['ROLE_USER']$unserializedToken->getRoleNames());
        $this->assertSame('https://symfony.com/blog', $unserializedToken->getOriginatedFromUri());

        $unserializedOriginalToken = $unserializedToken->getOriginalToken();

        $this->assertInstanceOf(UsernamePasswordToken::class$unserializedOriginalToken);
        $this->assertSame('user', $unserializedOriginalToken->getUserIdentifier());
        $this->assertSame('provider-key', $unserializedOriginalToken->getFirewallName());
        $this->assertEquals(['ROLE_ADMIN', 'ROLE_ALLOWED_TO_SWITCH']$unserializedOriginalToken->getRoleNames());
    }

    public function testSerializeNullImpersonateUrl()
    {
$token->eraseCredentials();
    }

    public function testSerialize()
    {
        $token = new ConcreteToken(['ROLE_FOO', 'ROLE_BAR']);
        $token->setAttributes(['foo' => 'bar']);

        $uToken = unserialize(serialize($token));

        $this->assertEquals($token->getRoleNames()$uToken->getRoleNames());
        $this->assertEquals($token->getAttributes()$uToken->getAttributes());
    }

    public function testConstructor()
    {
        $token = new ConcreteToken(['ROLE_FOO']);
        $this->assertEquals(['ROLE_FOO']$token->getRoleNames());
    }

    public function testAttributes()
    {
        
namespace Symfony\Component\Security\Core\Tests\Authentication\Token;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
use Symfony\Component\Security\Core\User\InMemoryUser;

class PreAuthenticatedTokenTest extends TestCase
{
    public function testConstructor()
    {
        $token = new PreAuthenticatedToken(new InMemoryUser('foo', 'bar', ['ROLE_FOO']), 'key', ['ROLE_FOO']);
        $this->assertEquals(['ROLE_FOO']$token->getRoleNames());
        $this->assertEquals('key', $token->getFirewallName());
    }

    public function testGetUser()
    {
        $token = new PreAuthenticatedToken($user = new InMemoryUser('foo', 'bar'), 'key');
        $this->assertEquals($user$token->getUser());
    }
}
use Symfony\Component\Security\Core\User\UserInterface;

class RememberMeTokenTest extends TestCase
{
    public function testConstructor()
    {
        $user = $this->getUser();
        $token = new RememberMeToken($user, 'fookey', 'foo');

        $this->assertEquals('fookey', $token->getFirewallName());
        $this->assertEquals('foo', $token->getSecret());
        $this->assertEquals(['ROLE_FOO']$token->getRoleNames());
        $this->assertSame($user$token->getUser());
    }

    public function testConstructorSecretCannotBeEmptyString()
    {
        $this->expectException(\InvalidArgumentException::class);
        new RememberMeToken(
            $this->getUser(),
            '',
            ''
        );
    }
$result = VoterInterface::ACCESS_DENIED;
            if ($this->expressionLanguage->evaluate($attribute$variables)) {
                return VoterInterface::ACCESS_GRANTED;
            }
        }

        return $result;
    }

    private function getVariables(TokenInterface $token, mixed $subject): array
    {
        $roleNames = $token->getRoleNames();

        if (null !== $this->roleHierarchy) {
            $roleNames = $this->roleHierarchy->getReachableRoleNames($roleNames);
        }

        $variables = [
            'token' => $token,
            'user' => $token->getUser(),
            'object' => $subject,
            'subject' => $subject,
            'role_names' => $roleNames,
            
return true;
            }
        }

        $userRoles = array_map('strval', (array) $refreshedUser->getRoles());

        if ($refreshedToken instanceof SwitchUserToken) {
            $userRoles[] = 'ROLE_PREVIOUS_ADMIN';
        }

        if (
            \count($userRoles) !== \count($refreshedToken->getRoleNames())
            || \count($userRoles) !== \count(array_intersect($userRoles$refreshedToken->getRoleNames()))
        ) {
            return true;
        }

        if ($originalUser->getUserIdentifier() !== $refreshedUser->getUserIdentifier()) {
            return true;
        }

        return false;
    }

    
namespace Symfony\Component\Security\Core\Tests\Authentication\Token;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\User\InMemoryUser;

class UsernamePasswordTokenTest extends TestCase
{
    public function testConstructor()
    {
        $token = new UsernamePasswordToken(new InMemoryUser('foo', 'bar', ['ROLE_FOO']), 'key', ['ROLE_FOO']);
        $this->assertEquals(['ROLE_FOO']$token->getRoleNames());
        $this->assertEquals('key', $token->getFirewallName());
    }

    public function testToString()
    {
        $token = new UsernamePasswordToken(new InMemoryUser('foo', '', ['A', 'B']), 'foo', ['A', 'B']);
        $this->assertEquals('UsernamePasswordToken(user="foo", roles="A, B")', (string) $token);
    }
}
'impersonation_exit_path' => null,
                'token' => null,
                'token_class' => null,
                'logout_url' => null,
                'user' => '',
                'roles' => [],
                'inherited_roles' => [],
                'supports_role_hierarchy' => null !== $this->roleHierarchy,
            ];
        } else {
            $inheritedRoles = [];
            $assignedRoles = $token->getRoleNames();

            $impersonatorUser = null;
            if ($token instanceof SwitchUserToken) {
                $originalToken = $token->getOriginalToken();
                $impersonatorUser = $originalToken->getUserIdentifier();
            }

            if (null !== $this->roleHierarchy) {
                foreach ($this->roleHierarchy->getReachableRoleNames($assignedRoles) as $role) {
                    if (!\in_array($role$assignedRoles, true)) {
                        $inheritedRoles[] = $role;
                    }
abstract protected function getKey(): string;

    abstract protected function getToken(): ?TokenInterface;

    private function doInvoke(array|LogRecord $record): array|LogRecord
    {
        $record['extra'][$this->getKey()] = null;

        if (null !== $token = $this->getToken()) {
            $record['extra'][$this->getKey()] = [
                'authenticated' => (bool) $token->getUser(),
                'roles' => $token->getRoleNames(),
            ];

            $record['extra'][$this->getKey()]['user_identifier'] = $token->getUserIdentifier();
        }

        return $record;
    }
}
Home | Imprint | This part of the site doesn't use cookies.