Skip to content

Symfony 5.3 compatibility #82

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

Merged
merged 1 commit into from
May 19, 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
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:
- php: '7.4'
deps: lowest
deprecations: max[self]=0
- php: '8.0'
deps: highest

steps:
- name: Checkout code
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion Domain/UserSecurityIdentity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
25 changes: 5 additions & 20 deletions Tests/Domain/SecurityIdentityRetrievalStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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'),
Expand All @@ -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'),
Expand All @@ -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 {
Expand Down
20 changes: 8 additions & 12 deletions Tests/Domain/UserSecurityIdentityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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')
Expand All @@ -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],
];
}
}
45 changes: 45 additions & 0 deletions Tests/Fixtures/Account.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Symfony\Component\Security\Acl\Tests\Fixtures;

use Symfony\Component\Security\Core\User\UserInterface;

final class Account implements UserInterface
{
/** @var string */
private $identifier;

public function __construct(string $identifier)
{
$this->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
{
}
}