From 8647e1435e079eabaa659abe96b5924f3b1d1680 Mon Sep 17 00:00:00 2001 From: core23 Date: Mon, 5 Feb 2024 16:31:17 +0100 Subject: [PATCH 1/5] Fix symfony 7 compatibility --- .../SecurityIdentityRetrievalStrategyTest.php | 2 +- Voter/AclVoter.php | 28 ++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Tests/Domain/SecurityIdentityRetrievalStrategyTest.php b/Tests/Domain/SecurityIdentityRetrievalStrategyTest.php index deebb44..edc56f1 100644 --- a/Tests/Domain/SecurityIdentityRetrievalStrategyTest.php +++ b/Tests/Domain/SecurityIdentityRetrievalStrategyTest.php @@ -284,7 +284,7 @@ public function getRoles(): array return []; } - public function eraseCredentials() + public function eraseCredentials(): void { } diff --git a/Voter/AclVoter.php b/Voter/AclVoter.php index 14c5d56..af86b38 100644 --- a/Voter/AclVoter.php +++ b/Voter/AclVoter.php @@ -22,6 +22,30 @@ use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; +if (class_exists(\Symfony\Component\Security\Core\Security::class)) { + /** + * @internal + */ + trait AclVoterTrait + { + public function vote(TokenInterface $token, $subject, array $attributes) + { + return $this->doVote($token, $subject, $attributes); + } + } +} else { + /** + * @internal + */ + trait AclVoterTrait + { + public function vote(TokenInterface $token, mixed $subject, array $attributes): int + { + return $this->doVote($token, $subject, $attributes); + } + } +} + /** * This voter can be used as a base class for implementing your own permissions. * @@ -29,6 +53,8 @@ */ class AclVoter implements VoterInterface { + use AclVoterTrait; + private $aclProvider; private $permissionMap; private $objectIdentityRetrievalStrategy; @@ -51,7 +77,7 @@ public function supportsAttribute($attribute) return \is_string($attribute) && $this->permissionMap->contains($attribute); } - public function vote(TokenInterface $token, $subject, array $attributes) + private function doVote(TokenInterface $token, $subject, array $attributes) { foreach ($attributes as $attribute) { if (!$this->supportsAttribute($attribute)) { From 1247ccb591897ac545b1dac5dad20789d31ca9ed Mon Sep 17 00:00:00 2001 From: Jordi Date: Fri, 6 Sep 2024 10:15:47 +0200 Subject: [PATCH 2/5] Fix tests --- Tests/Dbal/AclProviderTest.php | 52 ++++++++++++++++++++++----- Tests/Dbal/MutableAclProviderTest.php | 20 ++++++++--- 2 files changed, 59 insertions(+), 13 deletions(-) diff --git a/Tests/Dbal/AclProviderTest.php b/Tests/Dbal/AclProviderTest.php index 87ff1bd..194ae8e 100644 --- a/Tests/Dbal/AclProviderTest.php +++ b/Tests/Dbal/AclProviderTest.php @@ -11,7 +11,9 @@ namespace Symfony\Component\Security\Acl\Tests\Dbal; +use Doctrine\DBAL\Configuration; use Doctrine\DBAL\DriverManager; +use Doctrine\DBAL\Schema\DefaultSchemaManagerFactory; use PHPUnit\Framework\TestCase; use Symfony\Component\Security\Acl\Dbal\AclProvider; use Symfony\Component\Security\Acl\Dbal\Schema; @@ -146,10 +148,19 @@ public function testFindAcl() protected function setUp(): void { - $this->connection = DriverManager::getConnection([ - 'driver' => 'pdo_sqlite', - 'memory' => true, - ]); + $configuration = new Configuration(); + + if (\method_exists($configuration, 'setSchemaManagerFactory')) { + $configuration->setSchemaManagerFactory(new DefaultSchemaManagerFactory()); + } + + $this->connection = DriverManager::getConnection( + [ + 'driver' => 'pdo_sqlite', + 'memory' => true, + ], + $configuration + ); // import the schema $schema = new Schema($this->getOptions()); @@ -160,27 +171,50 @@ protected function setUp(): void // populate the schema with some test data $insertClassStmt = $this->connection->prepare('INSERT INTO acl_classes (id, class_type) VALUES (?, ?)'); foreach ($this->getClassData() as $data) { - $insertClassStmt->executeStatement($data); + $insertClassStmt->bindValue(1, $data[0]); + $insertClassStmt->bindValue(2, $data[1]); + $insertClassStmt->executeStatement(); } $insertSidStmt = $this->connection->prepare('INSERT INTO acl_security_identities (id, identifier, username) VALUES (?, ?, ?)'); foreach ($this->getSidData() as $data) { - $insertSidStmt->executeStatement($data); + $insertSidStmt->bindValue(1, $data[0]); + $insertSidStmt->bindValue(2, $data[1]); + $insertSidStmt->bindValue(3, $data[2]); + $insertSidStmt->executeStatement(); } $insertOidStmt = $this->connection->prepare('INSERT INTO acl_object_identities (id, class_id, object_identifier, parent_object_identity_id, entries_inheriting) VALUES (?, ?, ?, ?, ?)'); foreach ($this->getOidData() as $data) { - $insertOidStmt->executeStatement($data); + $insertOidStmt->bindValue(1, $data[0]); + $insertOidStmt->bindValue(2, $data[1]); + $insertOidStmt->bindValue(3, $data[2]); + $insertOidStmt->bindValue(4, $data[3]); + $insertOidStmt->bindValue(5, $data[4]); + $insertOidStmt->executeStatement(); } $insertEntryStmt = $this->connection->prepare('INSERT INTO acl_entries (id, class_id, object_identity_id, field_name, ace_order, security_identity_id, mask, granting, granting_strategy, audit_success, audit_failure) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'); foreach ($this->getEntryData() as $data) { - $insertEntryStmt->executeStatement($data); + $insertEntryStmt->bindValue(1, $data[0]); + $insertEntryStmt->bindValue(2, $data[1]); + $insertEntryStmt->bindValue(3, $data[2]); + $insertEntryStmt->bindValue(4, $data[3]); + $insertEntryStmt->bindValue(5, $data[4]); + $insertEntryStmt->bindValue(6, $data[5]); + $insertEntryStmt->bindValue(7, $data[6]); + $insertEntryStmt->bindValue(8, $data[7]); + $insertEntryStmt->bindValue(9, $data[8]); + $insertEntryStmt->bindValue(10, $data[9]); + $insertEntryStmt->bindValue(11, $data[10]); + $insertEntryStmt->executeStatement(); } $insertOidAncestorStmt = $this->connection->prepare('INSERT INTO acl_object_identity_ancestors (object_identity_id, ancestor_id) VALUES (?, ?)'); foreach ($this->getOidAncestorData() as $data) { - $insertOidAncestorStmt->executeStatement($data); + $insertOidAncestorStmt->bindValue(1, $data[0]); + $insertOidAncestorStmt->bindValue(2, $data[1]); + $insertOidAncestorStmt->executeStatement(); } } diff --git a/Tests/Dbal/MutableAclProviderTest.php b/Tests/Dbal/MutableAclProviderTest.php index 2c34c15..c5a3807 100644 --- a/Tests/Dbal/MutableAclProviderTest.php +++ b/Tests/Dbal/MutableAclProviderTest.php @@ -11,8 +11,10 @@ namespace Symfony\Component\Security\Acl\Tests\Dbal; +use Doctrine\DBAL\Configuration; use Doctrine\DBAL\Connection; use Doctrine\DBAL\DriverManager; +use Doctrine\DBAL\Schema\DefaultSchemaManagerFactory; use PHPUnit\Framework\TestCase; use Symfony\Component\Security\Acl\Dbal\AclProvider; use Symfony\Component\Security\Acl\Dbal\MutableAclProvider; @@ -518,10 +520,20 @@ protected function callMethod($object, $method, array $args) protected function setUp(): void { - $this->connection = DriverManager::getConnection([ - 'driver' => 'pdo_sqlite', - 'memory' => true, - ]); + $configuration = new Configuration(); + + if (\method_exists($configuration, 'setSchemaManagerFactory')) { + $configuration->setSchemaManagerFactory(new DefaultSchemaManagerFactory()); + } + + $this->connection = DriverManager::getConnection( + [ + 'driver' => 'pdo_sqlite', + 'memory' => true, + ], + $configuration + ); + $this->connection->setNestTransactionsWithSavepoints(true); // import the schema $schema = new Schema($this->getOptions()); From c4c7ca070e732d78b0965e061e06ef08f8b85a26 Mon Sep 17 00:00:00 2001 From: Jordi Date: Fri, 6 Sep 2024 10:18:47 +0200 Subject: [PATCH 3/5] suppress psalm since it is not fixable --- Tests/Dbal/AclProviderTest.php | 3 +++ Tests/Dbal/MutableAclProviderTest.php | 3 +++ 2 files changed, 6 insertions(+) diff --git a/Tests/Dbal/AclProviderTest.php b/Tests/Dbal/AclProviderTest.php index 194ae8e..1a44332 100644 --- a/Tests/Dbal/AclProviderTest.php +++ b/Tests/Dbal/AclProviderTest.php @@ -150,6 +150,9 @@ protected function setUp(): void { $configuration = new Configuration(); + /** + * @psalm-suppress RedundantCondition Since we are compatibles with DBAL 2 and 3, we need to check if the method exists + */ if (\method_exists($configuration, 'setSchemaManagerFactory')) { $configuration->setSchemaManagerFactory(new DefaultSchemaManagerFactory()); } diff --git a/Tests/Dbal/MutableAclProviderTest.php b/Tests/Dbal/MutableAclProviderTest.php index c5a3807..8c2d569 100644 --- a/Tests/Dbal/MutableAclProviderTest.php +++ b/Tests/Dbal/MutableAclProviderTest.php @@ -522,6 +522,9 @@ protected function setUp(): void { $configuration = new Configuration(); + /** + * @psalm-suppress RedundantCondition Since we are compatibles with DBAL 2 and 3, we need to check if the method exists + */ if (\method_exists($configuration, 'setSchemaManagerFactory')) { $configuration->setSchemaManagerFactory(new DefaultSchemaManagerFactory()); } From 29379e673708ee0b16e4149aea87f6218a2f230a Mon Sep 17 00:00:00 2001 From: Jordi Date: Fri, 6 Sep 2024 10:20:50 +0200 Subject: [PATCH 4/5] fix cs issues --- .php-cs-fixer.dist.php | 1 + Tests/Dbal/AclProviderTest.php | 2 +- Tests/Dbal/MutableAclProviderTest.php | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 1ad24d0..1919fb6 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -7,6 +7,7 @@ '@Symfony' => true, '@Symfony:risky' => true, 'protected_to_private' => false, + 'phpdoc_to_comment' => ['ignored_tags' => ['psalm-suppress']], ]) ->setRiskyAllowed(true) ->setFinder( diff --git a/Tests/Dbal/AclProviderTest.php b/Tests/Dbal/AclProviderTest.php index 1a44332..e2d5b4d 100644 --- a/Tests/Dbal/AclProviderTest.php +++ b/Tests/Dbal/AclProviderTest.php @@ -153,7 +153,7 @@ protected function setUp(): void /** * @psalm-suppress RedundantCondition Since we are compatibles with DBAL 2 and 3, we need to check if the method exists */ - if (\method_exists($configuration, 'setSchemaManagerFactory')) { + if (method_exists($configuration, 'setSchemaManagerFactory')) { $configuration->setSchemaManagerFactory(new DefaultSchemaManagerFactory()); } diff --git a/Tests/Dbal/MutableAclProviderTest.php b/Tests/Dbal/MutableAclProviderTest.php index 8c2d569..d29a0d9 100644 --- a/Tests/Dbal/MutableAclProviderTest.php +++ b/Tests/Dbal/MutableAclProviderTest.php @@ -525,7 +525,7 @@ protected function setUp(): void /** * @psalm-suppress RedundantCondition Since we are compatibles with DBAL 2 and 3, we need to check if the method exists */ - if (\method_exists($configuration, 'setSchemaManagerFactory')) { + if (method_exists($configuration, 'setSchemaManagerFactory')) { $configuration->setSchemaManagerFactory(new DefaultSchemaManagerFactory()); } From 17f22558763389344879fdb46c335390f3623150 Mon Sep 17 00:00:00 2001 From: Jordi Sala Morales Date: Wed, 2 Oct 2024 18:17:13 +0200 Subject: [PATCH 5/5] Actualizar AclVoter.php Co-authored-by: Nicolas Grekas --- Voter/AclVoter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Voter/AclVoter.php b/Voter/AclVoter.php index af86b38..de41d3e 100644 --- a/Voter/AclVoter.php +++ b/Voter/AclVoter.php @@ -77,7 +77,7 @@ public function supportsAttribute($attribute) return \is_string($attribute) && $this->permissionMap->contains($attribute); } - private function doVote(TokenInterface $token, $subject, array $attributes) + private function doVote(TokenInterface $token, $subject, array $attributes): int { foreach ($attributes as $attribute) { if (!$this->supportsAttribute($attribute)) {