Skip to content
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

Doctrine 4 fix #13

Merged
merged 2 commits into from
Jul 22, 2024
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
30 changes: 21 additions & 9 deletions src/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function savePolicyLine($pType, array $rule)
$queryBuilder->setValue('v' . strval($key), '?')->setParameter($key + 1, $value);
}

return $queryBuilder->execute();
return $this->executeQuery($queryBuilder);
}

/**
Expand All @@ -147,8 +147,7 @@ public function savePolicyLine($pType, array $rule)
public function loadPolicy(Model $model): void
{
$queryBuilder = $this->connection->createQueryBuilder();
$query = $queryBuilder->select('p_type', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5')->from($this->policyTableName);
$stmt = method_exists($query, "executeQuery") ? $query->executeQuery() : $query->execute();
$stmt = $this->executeQuery($queryBuilder->select('p_type', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5')->from($this->policyTableName));

while ($row = $this->fetch($stmt)) {
$this->loadPolicyArray($this->filterRule($row), $model);
Expand Down Expand Up @@ -180,7 +179,7 @@ public function loadFilteredPolicy(Model $model, $filter): void
throw new \Exception('invalid filter type');
}

$stmt = $queryBuilder->from($this->policyTableName)->execute();
$stmt = $this->executeQuery($queryBuilder->from($this->policyTableName));
while ($row = $this->fetch($stmt)) {
$line = implode(', ', array_filter($row, function ($val) {
return '' != $val && !is_null($val);
Expand Down Expand Up @@ -255,7 +254,7 @@ public function addPolicies(string $sec, string $ptype, array $rules): void
$sql = 'INSERT INTO ' . $table . ' (' . implode(', ', $columns) . ')' .
' VALUES' . $valuesStr;

$this->connection->executeUpdate($sql, $values);
$this->connection->executeStatement($sql, $values);
}

/**
Expand All @@ -275,7 +274,7 @@ private function _removePolicy(Connection $conn, string $sec, string $ptype, arr
$queryBuilder->andWhere('v' . strval($key) . ' = ?')->setParameter($key + 1, $value);
}

$queryBuilder->delete($this->policyTableName)->execute();
$this->executeQuery($queryBuilder->delete($this->policyTableName));
}

/**
Expand Down Expand Up @@ -332,13 +331,13 @@ public function _removeFilteredPolicy(string $sec, string $ptype, int $fieldInde
$fieldIndex++;
}

$stmt = $queryBuilder->select(...$this->columns)->from($this->policyTableName)->execute();
$stmt = $this->executeQuery($queryBuilder->select(...$this->columns)->from($this->policyTableName));

while ($row = $this->fetch($stmt)) {
$removedRules[] = $this->filterRule($row);
}

$queryBuilder->delete($this->policyTableName)->execute();
$this->executeQuery($queryBuilder->delete($this->policyTableName));
});

return $removedRules;
Expand Down Expand Up @@ -384,7 +383,7 @@ public function updatePolicy(string $sec, string $ptype, array $oldRule, array $

$queryBuilder->update($this->policyTableName);

$queryBuilder->execute();
$this->executeQuery($queryBuilder);
}

/**
Expand Down Expand Up @@ -501,4 +500,17 @@ private function fetch($stmt)
return $stmt->fetch();
}

/**
* Calls correct query execution method depending on Doctrine version and
* returns the result.
*
* @param \Doctrine\DBAL\Query\QueryBuilder $query
*
* @return mixed
*/
private function executeQuery($query)
{
return method_exists($query, "executeQuery") ? $query->executeQuery() : $query->execute();
}

}
119 changes: 69 additions & 50 deletions tests/AdapterTest.php
Original file line number Diff line number Diff line change
@@ -1,50 +1,69 @@
<?php

namespace CasbinAdapter\DBAL\Tests;

use CasbinAdapter\DBAL\Adapter as DatabaseAdapter;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception;

class AdapterTest extends TestCase
{

/**
* @throws Exception
*/
public function testUpdateFilteredPolicies()
{
$this->initConfig();
$connConfig = new Configuration();
$logger = new DebugStackLogger();
$connConfig->setSQLLogger($logger);
$conn = DriverManager::getConnection(
$this->config,
$connConfig
);
$adapter = DatabaseAdapter::newAdapter($conn);
$conn->delete($adapter->policyTableName, ["1" => "1"]);
$conn->insert($adapter->policyTableName, ["p_type" => "p", "v0" => "alice", "v1" => "data", "v2" => "read", "v3" => "allow"]);
$conn->insert($adapter->policyTableName, ["p_type" => "p", "v0" => "alice", "v1" => "data1", "v2" => "write", "v3" => "allow"]);
$conn->insert($adapter->policyTableName, ["p_type" => "p", "v0" => "bob", "v1" => "data", "v2" => "write", "v3" => "allow"]);
$conn->insert($adapter->policyTableName, ["p_type" => "p", "v0" => "bob", "v1" => "data1", "v2" => "read", "v3" => "allow"]);

$newPolicies = [
["alice", "data", "read", "allow"],
["bob", "data", "read", "allow"]
];
$oldRules = $adapter->updateFilteredPolicies("p", "p", $newPolicies, 1, "data", null, "allow");
$this->assertEquals([
["p", "alice", "data", "read", "allow"],
["p", "bob", "data", "write", "allow"]
], $oldRules);

$stmt = $conn->createQueryBuilder()->from($adapter->policyTableName)->where('p_type = "p" and v1 = "data" and v3 = "allow"')->select("v0", "v1", "v2", "v3")->execute();
$result = method_exists($stmt, 'fetchAssociative') ? $stmt->fetchAllAssociative() : $stmt->fetchAll();

$result = array_map([$adapter, "filterRule"], $result);

$this->assertEquals($newPolicies, $result);
}
}
<?php

namespace CasbinAdapter\DBAL\Tests;

use CasbinAdapter\DBAL\Adapter as DatabaseAdapter;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Logging\Middleware as LoggingMiddleware;

class AdapterTest extends TestCase
{
/**
* @throws Exception
*/
public function testUpdateFilteredPolicies()
{
$this->initConfig();
$connConfig = new Configuration();
$this->configureLogger($connConfig);
$conn = DriverManager::getConnection(
$this->config,
$connConfig
);
$adapter = DatabaseAdapter::newAdapter($conn);
$conn->delete($adapter->policyTableName, ["1" => "1"]);
$conn->insert($adapter->policyTableName, ["p_type" => "p", "v0" => "alice", "v1" => "data", "v2" => "read", "v3" => "allow"]);
$conn->insert($adapter->policyTableName, ["p_type" => "p", "v0" => "alice", "v1" => "data1", "v2" => "write", "v3" => "allow"]);
$conn->insert($adapter->policyTableName, ["p_type" => "p", "v0" => "bob", "v1" => "data", "v2" => "write", "v3" => "allow"]);
$conn->insert($adapter->policyTableName, ["p_type" => "p", "v0" => "bob", "v1" => "data1", "v2" => "read", "v3" => "allow"]);

$newPolicies = [
["alice", "data", "read", "allow"],
["bob", "data", "read", "allow"]
];
$oldRules = $adapter->updateFilteredPolicies("p", "p", $newPolicies, 1, "data", null, "allow");
$this->assertEquals([
["p", "alice", "data", "read", "allow"],
["p", "bob", "data", "write", "allow"]
], $oldRules);

$query = $conn->createQueryBuilder()->from($adapter->policyTableName)->where('p_type = "p" and v1 = "data" and v3 = "allow"')->select("v0", "v1", "v2", "v3");
$stmt = method_exists($query, "executeQuery") ? $query->executeQuery() : $query->execute();
$result = method_exists($stmt, 'fetchAssociative') ? $stmt->fetchAllAssociative() : $stmt->fetchAll();

$result = array_map([$adapter, "filterRule"], $result);

$this->assertEquals($newPolicies, $result);
}

/**
*
* @param \Doctrine\DBAL\Configuration $connConfig
* @return void
*/
private function configureLogger($connConfig)
{
// Doctrine < 4.0
if(method_exists($connConfig, "setSQLLogger")) {
$connConfig->setSQLLogger(new DebugStackLogger());
}
// Doctrine >= 4.0
else {
$connConfig->setMiddlewares([
new LoggingMiddleware(new PsrLogger())
]);
}
}
}
16 changes: 16 additions & 0 deletions tests/PsrLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace CasbinAdapter\DBAL\Tests;

use Psr\Log\AbstractLogger;

class PsrLogger extends AbstractLogger
{
public function log($level, string|\Stringable $message, array $context = [])
{
foreach($context as $k => $v) {
$context[$k] = is_string($v) ? $v : json_encode($v);
}
printf(date('Y-m-d H:i:s') . '[SQL]: ' . strtr($message, $context) . PHP_EOL);
}
}
Loading
Loading