Skip to content

Commit

Permalink
Fix compatibility with Doctrine DBAL 3
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed Apr 23, 2021
1 parent 3cbe1ef commit 61fe3c7
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 91 deletions.
8 changes: 4 additions & 4 deletions Dbal/AclProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function findChildren(ObjectIdentityInterface $parentOid, $directChildren
$sql = $this->getFindChildrenSql($parentOid, $directChildrenOnly);

$children = [];
foreach ($this->connection->executeQuery($sql)->fetchAll() as $data) {
foreach ($this->connection->executeQuery($sql)->fetchAllAssociative() as $data) {
$children[] = new ObjectIdentity($data['object_identifier'], $data['class_type']);
}

Expand Down Expand Up @@ -384,7 +384,7 @@ protected function getSelectObjectIdentityIdSql(ObjectIdentityInterface $oid)
*/
final protected function retrieveObjectIdentityPrimaryKey(ObjectIdentityInterface $oid)
{
return $this->connection->executeQuery($this->getSelectObjectIdentityIdSql($oid))->fetchColumn();
return $this->connection->executeQuery($this->getSelectObjectIdentityIdSql($oid))->fetchOne();
}

/**
Expand Down Expand Up @@ -421,7 +421,7 @@ private function getAncestorIds(array $batch)
$sql = $this->getAncestorLookupSql($batch);

$ancestorIds = [];
foreach ($this->connection->executeQuery($sql)->fetchAll() as $data) {
foreach ($this->connection->executeQuery($sql)->fetchAllAssociative() as $data) {
// FIXME: skip ancestors which are cached
// Fix: Oracle returns keys in uppercase
$ancestorIds[] = reset($data);
Expand Down Expand Up @@ -506,7 +506,7 @@ private function hydrateObjectIdentities(Result $stmt, array $oidLookup, array $

// fetchAll() consumes more memory than consecutive calls to fetch(),
// but it is faster
foreach ($stmt->fetchAll(\PDO::FETCH_NUM) as $data) {
foreach ($stmt->fetchAllNumeric() as $data) {
list($aclId,
$objectIdentifier,
$parentObjectIdentityId,
Expand Down
48 changes: 24 additions & 24 deletions Dbal/MutableAclProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function createAcl(ObjectIdentityInterface $oid)
$this->createObjectIdentity($oid);

$pk = $this->retrieveObjectIdentityPrimaryKey($oid);
$this->connection->executeUpdate($this->getInsertObjectIdentityRelationSql($pk, $pk));
$this->connection->executeStatement($this->getInsertObjectIdentityRelationSql($pk, $pk));

$this->connection->commit();
} catch (\Exception $e) {
Expand Down Expand Up @@ -117,7 +117,7 @@ public function deleteAcl(ObjectIdentityInterface $oid)
*/
public function deleteSecurityIdentity(SecurityIdentityInterface $sid)
{
$this->connection->executeUpdate($this->getDeleteSecurityIdentityIdSql($sid));
$this->connection->executeStatement($this->getDeleteSecurityIdentityIdSql($sid));
}

/**
Expand Down Expand Up @@ -332,7 +332,7 @@ public function updateAcl(MutableAclInterface $acl)

// persist any changes to the acl_object_identities table
if (\count($sets) > 0) {
$this->connection->executeUpdate($this->getUpdateObjectIdentitySql($acl->getId(), $sets));
$this->connection->executeStatement($this->getUpdateObjectIdentitySql($acl->getId(), $sets));
}

$this->connection->commit();
Expand Down Expand Up @@ -370,7 +370,7 @@ public function updateAcl(MutableAclInterface $acl)
*/
public function updateUserSecurityIdentity(UserSecurityIdentity $usid, $oldUsername)
{
$this->connection->executeUpdate($this->getUpdateUserSecurityIdentitySql($usid, $oldUsername));
$this->connection->executeStatement($this->getUpdateUserSecurityIdentitySql($usid, $oldUsername));
}

/**
Expand Down Expand Up @@ -736,7 +736,7 @@ private function createObjectIdentity(ObjectIdentityInterface $oid)
{
$classId = $this->createOrRetrieveClassId($oid->getType());

$this->connection->executeUpdate($this->getInsertObjectIdentitySql($oid->getIdentifier(), $classId, true));
$this->connection->executeStatement($this->getInsertObjectIdentitySql($oid->getIdentifier(), $classId, true));
}

/**
Expand All @@ -750,13 +750,13 @@ private function createObjectIdentity(ObjectIdentityInterface $oid)
*/
private function createOrRetrieveClassId($classType)
{
if (false !== $id = $this->connection->executeQuery($this->getSelectClassIdSql($classType))->fetchColumn()) {
if (false !== $id = $this->connection->executeQuery($this->getSelectClassIdSql($classType))->fetchOne()) {
return $id;
}

$this->connection->executeUpdate($this->getInsertClassSql($classType));
$this->connection->executeStatement($this->getInsertClassSql($classType));

return $this->connection->executeQuery($this->getSelectClassIdSql($classType))->fetchColumn();
return $this->connection->executeQuery($this->getSelectClassIdSql($classType))->fetchOne();
}

/**
Expand All @@ -769,13 +769,13 @@ private function createOrRetrieveClassId($classType)
*/
private function createOrRetrieveSecurityIdentityId(SecurityIdentityInterface $sid)
{
if (false !== $id = $this->connection->executeQuery($this->getSelectSecurityIdentityIdSql($sid))->fetchColumn()) {
if (false !== $id = $this->connection->executeQuery($this->getSelectSecurityIdentityIdSql($sid))->fetchOne()) {
return $id;
}

$this->connection->executeUpdate($this->getInsertSecurityIdentitySql($sid));
$this->connection->executeStatement($this->getInsertSecurityIdentitySql($sid));

return $this->connection->executeQuery($this->getSelectSecurityIdentityIdSql($sid))->fetchColumn();
return $this->connection->executeQuery($this->getSelectSecurityIdentityIdSql($sid))->fetchOne();
}

/**
Expand All @@ -785,7 +785,7 @@ private function createOrRetrieveSecurityIdentityId(SecurityIdentityInterface $s
*/
private function deleteAccessControlEntries($oidPK)
{
$this->connection->executeUpdate($this->getDeleteAccessControlEntriesSql($oidPK));
$this->connection->executeStatement($this->getDeleteAccessControlEntriesSql($oidPK));
}

/**
Expand All @@ -795,7 +795,7 @@ private function deleteAccessControlEntries($oidPK)
*/
private function deleteObjectIdentity($pk)
{
$this->connection->executeUpdate($this->getDeleteObjectIdentitySql($pk));
$this->connection->executeStatement($this->getDeleteObjectIdentitySql($pk));
}

/**
Expand All @@ -805,7 +805,7 @@ private function deleteObjectIdentity($pk)
*/
private function deleteObjectIdentityRelations($pk)
{
$this->connection->executeUpdate($this->getDeleteObjectIdentityRelationsSql($pk));
$this->connection->executeStatement($this->getDeleteObjectIdentityRelationsSql($pk));
}

/**
Expand All @@ -814,12 +814,12 @@ private function deleteObjectIdentityRelations($pk)
private function regenerateAncestorRelations(AclInterface $acl)
{
$pk = $acl->getId();
$this->connection->executeUpdate($this->getDeleteObjectIdentityRelationsSql($pk));
$this->connection->executeUpdate($this->getInsertObjectIdentityRelationSql($pk, $pk));
$this->connection->executeStatement($this->getDeleteObjectIdentityRelationsSql($pk));
$this->connection->executeStatement($this->getInsertObjectIdentityRelationSql($pk, $pk));

$parentAcl = $acl->getParentAcl();
while (null !== $parentAcl) {
$this->connection->executeUpdate($this->getInsertObjectIdentityRelationSql($pk, $parentAcl->getId()));
$this->connection->executeStatement($this->getInsertObjectIdentityRelationSql($pk, $parentAcl->getId()));

$parentAcl = $parentAcl->getParentAcl();
}
Expand Down Expand Up @@ -854,8 +854,8 @@ private function updateNewFieldAceProperty($name, array $changes)

$objectIdentityId = 'classFieldAces' === $name ? null : $ace->getAcl()->getId();

$this->connection->executeUpdate($this->getInsertAccessControlEntrySql($classId, $objectIdentityId, $field, $i, $sid, $ace->getStrategy(), $ace->getMask(), $ace->isGranting(), $ace->isAuditSuccess(), $ace->isAuditFailure()));
$aceId = $this->connection->executeQuery($this->getSelectAccessControlEntryIdSql($classId, $objectIdentityId, $field, $i))->fetchColumn();
$this->connection->executeStatement($this->getInsertAccessControlEntrySql($classId, $objectIdentityId, $field, $i, $sid, $ace->getStrategy(), $ace->getMask(), $ace->isGranting(), $ace->isAuditSuccess(), $ace->isAuditFailure()));
$aceId = $this->connection->executeQuery($this->getSelectAccessControlEntryIdSql($classId, $objectIdentityId, $field, $i))->fetchOne();
$this->loadedAces[$aceId] = $ace;

$aceIdProperty = new \ReflectionProperty('Symfony\Component\Security\Acl\Domain\Entry', 'id');
Expand Down Expand Up @@ -889,7 +889,7 @@ private function updateOldFieldAceProperty($name, array $changes)
$ace = $old[$i];

if (!isset($currentIds[$ace->getId()])) {
$this->connection->executeUpdate($this->getDeleteAccessControlEntrySql($ace->getId()));
$this->connection->executeStatement($this->getDeleteAccessControlEntrySql($ace->getId()));
unset($this->loadedAces[$ace->getId()]);
}
}
Expand Down Expand Up @@ -926,8 +926,8 @@ private function updateNewAceProperty($name, array $changes)

$objectIdentityId = 'classAces' === $name ? null : $ace->getAcl()->getId();

$this->connection->executeUpdate($this->getInsertAccessControlEntrySql($classId, $objectIdentityId, null, $i, $sid, $ace->getStrategy(), $ace->getMask(), $ace->isGranting(), $ace->isAuditSuccess(), $ace->isAuditFailure()));
$aceId = $this->connection->executeQuery($this->getSelectAccessControlEntryIdSql($classId, $objectIdentityId, null, $i))->fetchColumn();
$this->connection->executeStatement($this->getInsertAccessControlEntrySql($classId, $objectIdentityId, null, $i, $sid, $ace->getStrategy(), $ace->getMask(), $ace->isGranting(), $ace->isAuditSuccess(), $ace->isAuditFailure()));
$aceId = $this->connection->executeQuery($this->getSelectAccessControlEntryIdSql($classId, $objectIdentityId, null, $i))->fetchOne();
$this->loadedAces[$aceId] = $ace;

$aceIdProperty = new \ReflectionProperty($ace, 'id');
Expand Down Expand Up @@ -959,7 +959,7 @@ private function updateOldAceProperty($name, array $changes)
$ace = $old[$i];

if (!isset($currentIds[$ace->getId()])) {
$this->connection->executeUpdate($this->getDeleteAccessControlEntrySql($ace->getId()));
$this->connection->executeStatement($this->getDeleteAccessControlEntrySql($ace->getId()));
unset($this->loadedAces[$ace->getId()]);
}
}
Expand Down Expand Up @@ -1005,6 +1005,6 @@ private function updateAce(\SplObjectStorage $aces, $ace)
$sets[] = sprintf('audit_failure = %s', $this->connection->getDatabasePlatform()->convertBooleans($propertyChanges['auditFailure'][1]));
}

$this->connection->executeUpdate($this->getUpdateAccessControlEntrySql($ace->getId(), $sets));
$this->connection->executeStatement($this->getUpdateAccessControlEntrySql($ace->getId(), $sets));
}
}
50 changes: 26 additions & 24 deletions Tests/Dbal/AclProviderBenchmarkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

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

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Acl\Dbal\AclProvider;
use Symfony\Component\Security\Acl\Dbal\Schema;
use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
Expand All @@ -20,10 +22,10 @@
/**
* @group benchmark
*/
class AclProviderBenchmarkTest extends \PHPUnit\Framework\TestCase
class AclProviderBenchmarkTest extends TestCase
{
/** @var \Doctrine\DBAL\Connection */
protected $con;
/** @var Connection */
protected $connection;
protected $insertClassStmt;
protected $insertSidStmt;
protected $insertOidAncestorStmt;
Expand All @@ -33,21 +35,21 @@ class AclProviderBenchmarkTest extends \PHPUnit\Framework\TestCase
protected function setUp(): void
{
try {
$this->con = DriverManager::getConnection([
$this->connection = DriverManager::getConnection([
'driver' => 'pdo_mysql',
'host' => 'localhost',
'user' => 'root',
'dbname' => 'testdb',
]);
$this->con->connect();
$this->connection->connect();
} catch (\Exception $e) {
$this->markTestSkipped('Unable to connect to the database: '.$e->getMessage());
}
}

protected function tearDown(): void
{
$this->con = null;
$this->connection = null;
}

public function testFindAcls()
Expand All @@ -56,8 +58,8 @@ public function testFindAcls()

// get some random test object identities from the database
$oids = [];
$stmt = $this->con->executeQuery('SELECT object_identifier, class_type FROM acl_object_identities o INNER JOIN acl_classes c ON c.id = o.class_id ORDER BY RAND() LIMIT 25');
foreach ($stmt->fetchAll() as $oid) {
$stmt = $this->connection->executeQuery('SELECT object_identifier, class_type FROM acl_object_identities o INNER JOIN acl_classes c ON c.id = o.class_id ORDER BY RAND() LIMIT 25');
foreach ($stmt->fetchAllAssociative() as $oid) {
$oids[] = new ObjectIdentity($oid['object_identifier'], $oid['class_type']);
}

Expand All @@ -75,22 +77,22 @@ public function testFindAcls()
*/
protected function generateTestData()
{
$sm = $this->con->getSchemaManager();
$sm = $this->connection->getSchemaManager();
$sm->dropAndCreateDatabase('testdb');
$this->con->exec('USE testdb');
$this->connection->exec('USE testdb');

// import the schema
$schema = new Schema($options = $this->getOptions());
foreach ($schema->toSql($this->con->getDatabasePlatform()) as $sql) {
$this->con->exec($sql);
foreach ($schema->toSql($this->connection->getDatabasePlatform()) as $sql) {
$this->connection->exec($sql);
}

// setup prepared statements
$this->insertClassStmt = $this->con->prepare('INSERT INTO acl_classes (id, class_type) VALUES (?, ?)');
$this->insertSidStmt = $this->con->prepare('INSERT INTO acl_security_identities (id, identifier, username) VALUES (?, ?, ?)');
$this->insertOidStmt = $this->con->prepare('INSERT INTO acl_object_identities (id, class_id, object_identifier, parent_object_identity_id, entries_inheriting) VALUES (?, ?, ?, ?, ?)');
$this->insertEntryStmt = $this->con->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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
$this->insertOidAncestorStmt = $this->con->prepare('INSERT INTO acl_object_identity_ancestors (object_identity_id, ancestor_id) VALUES (?, ?)');
$this->insertClassStmt = $this->connection->prepare('INSERT INTO acl_classes (id, class_type) VALUES (?, ?)');
$this->insertSidStmt = $this->connection->prepare('INSERT INTO acl_security_identities (id, identifier, username) VALUES (?, ?, ?)');
$this->insertOidStmt = $this->connection->prepare('INSERT INTO acl_object_identities (id, class_id, object_identifier, parent_object_identity_id, entries_inheriting) VALUES (?, ?, ?, ?, ?)');
$this->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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
$this->insertOidAncestorStmt = $this->connection->prepare('INSERT INTO acl_object_identity_ancestors (object_identity_id, ancestor_id) VALUES (?, ?)');

for ($i = 0; $i < 40000; ++$i) {
$this->generateAclHierarchy();
Expand Down Expand Up @@ -121,7 +123,7 @@ protected function chooseClassId()
static $id = 1000;

if (1000 === $id || ($id < 1500 && rand(0, 1))) {
$this->insertClassStmt->execute([$id, $this->getRandomString(rand(20, 100), 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\\_')]);
$this->insertClassStmt->executeStatement([$id, $this->getRandomString(rand(20, 100), 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\\_')]);
++$id;

return $id - 1;
Expand All @@ -134,17 +136,17 @@ protected function generateAcl($classId, $parentId, $ancestors)
{
static $id = 1000;

$this->insertOidStmt->execute([
$this->insertOidStmt->executeStatement([
$id,
$classId,
$this->getRandomString(rand(20, 50)),
$parentId,
rand(0, 1),
]);

$this->insertOidAncestorStmt->execute([$id, $id]);
$this->insertOidAncestorStmt->executeStatement([$id, $id]);
foreach ($ancestors as $ancestor) {
$this->insertOidAncestorStmt->execute([$id, $ancestor]);
$this->insertOidAncestorStmt->executeStatement([$id, $ancestor]);
}

$this->generateAces($classId, $id);
Expand All @@ -158,7 +160,7 @@ protected function chooseSid()
static $id = 1000;

if (1000 === $id || ($id < 11000 && rand(0, 1))) {
$this->insertSidStmt->execute([
$this->insertSidStmt->executeStatement([
$id,
$this->getRandomString(rand(5, 30)),
rand(0, 1),
Expand Down Expand Up @@ -201,7 +203,7 @@ protected function generateAces($classId, $objectId)
}

// id, cid, oid, field, order, sid, mask, granting, strategy, a success, a failure
$this->insertEntryStmt->execute([
$this->insertEntryStmt->executeStatement([
$id,
$classId,
rand(0, 5) ? $objectId : null,
Expand Down Expand Up @@ -262,6 +264,6 @@ protected function getStrategy()

protected function getProvider()
{
return new AclProvider($this->con, $this->getStrategy(), $this->getOptions());
return new AclProvider($this->connection, $this->getStrategy(), $this->getOptions());
}
}
Loading

0 comments on commit 61fe3c7

Please sign in to comment.