From 523d98b9fe6a24e93f6dce3abbe0a7159b22214f 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 28609c27236917cdca42d7ef9bb2180dbcb638df Mon Sep 17 00:00:00 2001 From: Christoph Quadt Date: Wed, 8 May 2024 10:03:09 +0200 Subject: [PATCH 2/5] Fixed deprecations in tests. --- Tests/Dbal/AclProviderTest.php | 90 ++++++++++++++++++++++----- Tests/Dbal/MutableAclProviderTest.php | 20 ++++-- 2 files changed, 91 insertions(+), 19 deletions(-) diff --git a/Tests/Dbal/AclProviderTest.php b/Tests/Dbal/AclProviderTest.php index 87ff1bd..cc9308e 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,16 @@ public function testFindAcl() protected function setUp(): void { - $this->connection = DriverManager::getConnection([ + $configuration = new Configuration(); + $configuration->setSchemaManagerFactory(new DefaultSchemaManagerFactory()); + + $this->connection = DriverManager::getConnection( + [ 'driver' => 'pdo_sqlite', - 'memory' => true, - ]); + 'memory' => true + ], + $configuration + ); // import the schema $schema = new Schema($this->getOptions()); @@ -157,30 +165,84 @@ protected function setUp(): void $this->connection->executeStatement($sql); } - // populate the schema with some test data - $insertClassStmt = $this->connection->prepare('INSERT INTO acl_classes (id, class_type) VALUES (?, ?)'); + $qb = $this->connection->createQueryBuilder(); + foreach ($this->getClassData() as $data) { - $insertClassStmt->executeStatement($data); + $qb + ->insert('acl_classes') + ->values( + [ + 'id' => '?', + 'class_type' => '?', + ] + ) + ->setParameters($data) + ->executeQuery(); } - $insertSidStmt = $this->connection->prepare('INSERT INTO acl_security_identities (id, identifier, username) VALUES (?, ?, ?)'); foreach ($this->getSidData() as $data) { - $insertSidStmt->executeStatement($data); + $qb + ->insert('acl_security_identities') + ->values( + [ + 'id' => '?', + 'identifier' => '?', + 'username' => '?', + ] + ) + ->setParameters($data) + ->executeQuery(); } - $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); + $qb + ->insert('acl_object_identities') + ->values( + [ + 'id' => '?', + 'class_id' => '?', + 'object_identifier' => '?', + 'parent_object_identity_id' => '?', + 'entries_inheriting' => '?', + ] + ) + ->setParameters($data) + ->executeQuery(); } - $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); + $qb + ->insert('acl_entries') + ->values( + [ + 'id' => '?', + 'class_id' => '?', + 'object_identity_id' => '?', + 'field_name' => '?', + 'ace_order' => '?', + 'security_identity_id' => '?', + 'mask' => '?', + 'granting' => '?', + 'granting_strategy' => '?', + 'audit_success' => '?', + 'audit_failure' => '?', + ] + ) + ->setParameters($data) + ->executeQuery(); } - $insertOidAncestorStmt = $this->connection->prepare('INSERT INTO acl_object_identity_ancestors (object_identity_id, ancestor_id) VALUES (?, ?)'); foreach ($this->getOidAncestorData() as $data) { - $insertOidAncestorStmt->executeStatement($data); + $qb + ->insert('acl_object_identity_ancestors') + ->values( + [ + 'object_identity_id' => '?', + 'ancestor_id' => '?', + ] + ) + ->setParameters($data) + ->executeQuery(); } } diff --git a/Tests/Dbal/MutableAclProviderTest.php b/Tests/Dbal/MutableAclProviderTest.php index 2c34c15..77e9061 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; @@ -23,6 +25,7 @@ use Symfony\Component\Security\Acl\Domain\PermissionGrantingStrategy; use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity; use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity; +use Symfony\Component\Security\Acl\Exception\AclAlreadyExistsException; use Symfony\Component\Security\Acl\Exception\AclNotFoundException; use Symfony\Component\Security\Acl\Exception\ConcurrentModificationException; use Symfony\Component\Security\Acl\Model\AuditableEntryInterface; @@ -59,7 +62,7 @@ public static function assertAceEquals(EntryInterface $a, EntryInterface $b) public function testCreateAclThrowsExceptionWhenAclAlreadyExists() { - $this->expectException(\Symfony\Component\Security\Acl\Exception\AclAlreadyExistsException::class); + $this->expectException(AclAlreadyExistsException::class); $provider = $this->getProvider(); $oid = new ObjectIdentity('123456', 'FOO'); @@ -518,10 +521,17 @@ protected function callMethod($object, $method, array $args) protected function setUp(): void { - $this->connection = DriverManager::getConnection([ - 'driver' => 'pdo_sqlite', - 'memory' => true, - ]); + $configuration = new Configuration(); + $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 69a190996543655d0a652dab6c7bd6eca994b858 Mon Sep 17 00:00:00 2001 From: Christoph Quadt Date: Wed, 8 May 2024 10:16:05 +0200 Subject: [PATCH 3/5] Migrated phpunit configuration to valid schema. --- phpunit.xml.dist | 59 ++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 14d4060..01d8c68 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,37 +1,36 @@ - - - - - - - - - ./Tests/ - - - - - - ./ - - ./Resources - ./Tests - ./vendor - - - - - - benchmark - - - - - + + + ./ + + + ./Resources + ./Tests + ./vendor + + + + + + + + ./Tests/ + + + + + benchmark + + + + + From 679a6b27d09202eced3d512c18a3663c47aba3f3 Mon Sep 17 00:00:00 2001 From: Christoph Quadt Date: Fri, 10 May 2024 10:15:57 +0200 Subject: [PATCH 4/5] Fixed codesniffer and made sure setting up the database is backwards compatible. --- Tests/Dbal/AclProviderTest.php | 6 ++++-- Tests/Dbal/MutableAclProviderTest.php | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Tests/Dbal/AclProviderTest.php b/Tests/Dbal/AclProviderTest.php index cc9308e..1b17598 100644 --- a/Tests/Dbal/AclProviderTest.php +++ b/Tests/Dbal/AclProviderTest.php @@ -149,12 +149,14 @@ public function testFindAcl() protected function setUp(): void { $configuration = new Configuration(); - $configuration->setSchemaManagerFactory(new DefaultSchemaManagerFactory()); + if (function_exists('setSchemaManagerFactory')) { + $configuration->setSchemaManagerFactory(new DefaultSchemaManagerFactory()); + } $this->connection = DriverManager::getConnection( [ 'driver' => 'pdo_sqlite', - 'memory' => true + 'memory' => true, ], $configuration ); diff --git a/Tests/Dbal/MutableAclProviderTest.php b/Tests/Dbal/MutableAclProviderTest.php index 77e9061..a8b4914 100644 --- a/Tests/Dbal/MutableAclProviderTest.php +++ b/Tests/Dbal/MutableAclProviderTest.php @@ -522,12 +522,14 @@ protected function callMethod($object, $method, array $args) protected function setUp(): void { $configuration = new Configuration(); - $configuration->setSchemaManagerFactory(new DefaultSchemaManagerFactory()); + if (function_exists('setSchemaManagerFactory')) { + $configuration->setSchemaManagerFactory(new DefaultSchemaManagerFactory()); + } $this->connection = DriverManager::getConnection( [ 'driver' => 'pdo_sqlite', - 'memory' => true + 'memory' => true, ], $configuration ); From dec61ce90d54515d7d5f0c8821ad82b02cc6a0bf Mon Sep 17 00:00:00 2001 From: Christoph Quadt Date: Fri, 10 May 2024 11:19:39 +0200 Subject: [PATCH 5/5] Added fqdn for php functions. --- Tests/Dbal/AclProviderTest.php | 2 +- Tests/Dbal/MutableAclProviderTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Tests/Dbal/AclProviderTest.php b/Tests/Dbal/AclProviderTest.php index 1b17598..63ca85c 100644 --- a/Tests/Dbal/AclProviderTest.php +++ b/Tests/Dbal/AclProviderTest.php @@ -149,7 +149,7 @@ public function testFindAcl() protected function setUp(): void { $configuration = new Configuration(); - if (function_exists('setSchemaManagerFactory')) { + if (\method_exists($configuration, 'setSchemaManagerFactory')) { $configuration->setSchemaManagerFactory(new DefaultSchemaManagerFactory()); } diff --git a/Tests/Dbal/MutableAclProviderTest.php b/Tests/Dbal/MutableAclProviderTest.php index a8b4914..47c8a2b 100644 --- a/Tests/Dbal/MutableAclProviderTest.php +++ b/Tests/Dbal/MutableAclProviderTest.php @@ -522,8 +522,8 @@ protected function callMethod($object, $method, array $args) protected function setUp(): void { $configuration = new Configuration(); - if (function_exists('setSchemaManagerFactory')) { - $configuration->setSchemaManagerFactory(new DefaultSchemaManagerFactory()); + if (\method_exists($configuration, 'setSchemaManagerFactory')) { + $configuration->setSchemaManagerFactory(new DefaultSchemaManagerFactory()); } $this->connection = DriverManager::getConnection(