diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9ec062c..1505d22 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,6 +18,8 @@ jobs: - php: '7.4' deps: lowest deprecations: max[self]=0 + - php: '8.0' + deps: highest steps: - name: Checkout code @@ -29,6 +31,10 @@ jobs: php-version: '${{ matrix.php }}' coverage: none + - name: Configure composer + if: "${{ matrix.deps == 'highest' }}" + run: composer config minimum-stability dev + - name: Composer install uses: ramsey/composer-install@v1 with: diff --git a/Domain/UserSecurityIdentity.php b/Domain/UserSecurityIdentity.php index 50796b5..7f3ca7f 100644 --- a/Domain/UserSecurityIdentity.php +++ b/Domain/UserSecurityIdentity.php @@ -54,7 +54,7 @@ public function __construct($username, $class) */ public static function fromAccount(UserInterface $user) { - return new self($user->getUsername(), ClassUtils::getRealClass($user)); + return new self(method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername(), ClassUtils::getRealClass($user)); } /** diff --git a/Tests/Domain/SecurityIdentityRetrievalStrategyTest.php b/Tests/Domain/SecurityIdentityRetrievalStrategyTest.php index 88b2afe..81e3ef8 100644 --- a/Tests/Domain/SecurityIdentityRetrievalStrategyTest.php +++ b/Tests/Domain/SecurityIdentityRetrievalStrategyTest.php @@ -16,11 +16,11 @@ use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity; use Symfony\Component\Security\Acl\Domain\SecurityIdentityRetrievalStrategy; use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity; +use Symfony\Component\Security\Acl\Tests\Fixtures\Account; use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface; use Symfony\Component\Security\Core\Authentication\Token\AbstractToken; use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken; use Symfony\Component\Security\Core\Role\RoleHierarchyInterface; -use Symfony\Component\Security\Core\User\UserInterface; class SecurityIdentityRetrievalStrategyTest extends TestCase { @@ -81,8 +81,8 @@ public function testGetSecurityIdentities($user, array $roles, string $authentic public function getSecurityIdentityRetrievalTests(): array { return [ - [$this->getAccount('johannes', 'FooUser'), ['ROLE_USER', 'ROLE_SUPERADMIN'], 'fullFledged', [ - new UserSecurityIdentity('johannes', 'FooUser'), + [new Account('johannes'), ['ROLE_USER', 'ROLE_SUPERADMIN'], 'fullFledged', [ + new UserSecurityIdentity('johannes', Account::class), new RoleSecurityIdentity('ROLE_USER'), new RoleSecurityIdentity('ROLE_SUPERADMIN'), new RoleSecurityIdentity('IS_AUTHENTICATED_FULLY'), @@ -103,8 +103,8 @@ public function getSecurityIdentityRetrievalTests(): array new RoleSecurityIdentity('IS_AUTHENTICATED_REMEMBERED'), new RoleSecurityIdentity('IS_AUTHENTICATED_ANONYMOUSLY'), ]], - [$this->getAccount('foo', 'FooBarUser'), ['ROLE_FOO'], 'rememberMe', [ - new UserSecurityIdentity('foo', 'FooBarUser'), + [new Account('foo'), ['ROLE_FOO'], 'rememberMe', [ + new UserSecurityIdentity('foo', Account::class), new RoleSecurityIdentity('ROLE_FOO'), new RoleSecurityIdentity('IS_AUTHENTICATED_REMEMBERED'), new RoleSecurityIdentity('IS_AUTHENTICATED_ANONYMOUSLY'), @@ -116,21 +116,6 @@ public function getSecurityIdentityRetrievalTests(): array ]; } - private function getAccount(string $username, string $class): UserInterface - { - $account = $this->getMockBuilder(UserInterface::class) - ->setMockClassName($class) - ->getMock() - ; - $account - ->expects($this->any()) - ->method('getUsername') - ->willReturn($username) - ; - - return $account; - } - private function getStrategy(array $roles, string $authenticationStatus): SecurityIdentityRetrievalStrategy { $roleHierarchy = new class($roles) implements RoleHierarchyInterface { diff --git a/Tests/Domain/UserSecurityIdentityTest.php b/Tests/Domain/UserSecurityIdentityTest.php index b014620..602a78b 100644 --- a/Tests/Domain/UserSecurityIdentityTest.php +++ b/Tests/Domain/UserSecurityIdentityTest.php @@ -13,6 +13,9 @@ use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity; use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity; +use Symfony\Component\Security\Acl\Model\SecurityIdentityInterface; +use Symfony\Component\Security\Acl\Tests\Fixtures\Account; +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; class UserSecurityIdentityTest extends \PHPUnit\Framework\TestCase { @@ -36,23 +39,16 @@ public function testConstructorWithProxy() /** * @dataProvider getCompareData */ - public function testEquals($id1, $id2, $equal) + public function testEquals(UserSecurityIdentity $id1, SecurityIdentityInterface $id2, bool $equal) { $this->assertSame($equal, $id1->equals($id2)); } - public function getCompareData() + public function getCompareData(): array { - $account = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface') - ->setMockClassName('USI_AccountImpl') - ->getMock(); - $account - ->expects($this->any()) - ->method('getUsername') - ->willReturn('foo') - ; + $account = new Account('foo'); - $token = $this->createMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); + $token = $this->createMock(TokenInterface::class); $token ->expects($this->any()) ->method('getUser') @@ -67,7 +63,7 @@ public function getCompareData() [new UserSecurityIdentity('bla', 'Foo'), new UserSecurityIdentity('blub', 'Foo'), false], [new UserSecurityIdentity('foo', 'Foo'), new RoleSecurityIdentity('foo'), false], [new UserSecurityIdentity('foo', 'Foo'), UserSecurityIdentity::fromToken($token), false], - [new UserSecurityIdentity('foo', 'USI_AccountImpl'), UserSecurityIdentity::fromToken($token), true], + [new UserSecurityIdentity('foo', Account::class), UserSecurityIdentity::fromToken($token), true], ]; } } diff --git a/Tests/Fixtures/Account.php b/Tests/Fixtures/Account.php new file mode 100644 index 0000000..373b1d3 --- /dev/null +++ b/Tests/Fixtures/Account.php @@ -0,0 +1,45 @@ +identifier = $identifier; + } + + public function getUserIdentifier(): string + { + return $this->identifier; + } + + public function getUsername(): string + { + return $this->getUserIdentifier(); + } + + public function getRoles(): array + { + return ['ROLE_USER']; + } + + public function getPassword(): ?string + { + return null; + } + + public function getSalt(): ?string + { + return null; + } + + public function eraseCredentials(): void + { + } +}