aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArthur Schiwon <blizzz@arthur-schiwon.de>2023-04-03 11:30:14 +0200
committerGitHub <noreply@github.com>2023-04-03 11:30:14 +0200
commit84c76a89ece842b807249d88769bb9d3f375dc5c (patch)
tree3bc90f626a41a368668fa586a0f3c6e7f278f0bd
parent671d2726fa43ead10a31ecab7ad244b04bcc5cb1 (diff)
parent997c2a2a79a97527f2dd3979e3d815ee6ce9fb51 (diff)
downloadnextcloud-server-84c76a89ece842b807249d88769bb9d3f375dc5c.tar.gz
nextcloud-server-84c76a89ece842b807249d88769bb9d3f375dc5c.zip
Merge pull request #37520 from nextcloud/fix/noid/fix-dbal-exception-handling
fix DBAL exception handling in setValues
-rw-r--r--lib/private/DB/Connection.php18
-rw-r--r--tests/lib/Security/CredentialsManagerTest.php23
2 files changed, 32 insertions, 9 deletions
diff --git a/lib/private/DB/Connection.php b/lib/private/DB/Connection.php
index ceaffbcfa9a..85c6a72dfdb 100644
--- a/lib/private/DB/Connection.php
+++ b/lib/private/DB/Connection.php
@@ -40,8 +40,6 @@ use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Exception;
-use Doctrine\DBAL\Exception\ConstraintViolationException;
-use Doctrine\DBAL\Exception\NotNullConstraintViolationException;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
@@ -381,10 +379,10 @@ class Connection extends \Doctrine\DBAL\Connection {
* @param array $values (column name => value)
* @param array $updatePreconditionValues ensure values match preconditions (column name => value)
* @return int number of new rows
- * @throws \Doctrine\DBAL\Exception
+ * @throws \OCP\DB\Exception
* @throws PreConditionNotMetException
*/
- public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []) {
+ public function setValues(string $table, array $keys, array $values, array $updatePreconditionValues = []): int {
try {
$insertQb = $this->getQueryBuilder();
$insertQb->insert($table)
@@ -394,9 +392,15 @@ class Connection extends \Doctrine\DBAL\Connection {
}, array_merge($keys, $values))
);
return $insertQb->executeStatement();
- } catch (NotNullConstraintViolationException $e) {
- throw $e;
- } catch (ConstraintViolationException $e) {
+ } catch (\OCP\DB\Exception $e) {
+ if (!in_array($e->getReason(), [
+ \OCP\DB\Exception::REASON_CONSTRAINT_VIOLATION,
+ \OCP\DB\Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION,
+ ])
+ ) {
+ throw $e;
+ }
+
// value already exists, try update
$updateQb = $this->getQueryBuilder();
$updateQb->update($table);
diff --git a/tests/lib/Security/CredentialsManagerTest.php b/tests/lib/Security/CredentialsManagerTest.php
index 6f535c84275..bb4feb8b6a9 100644
--- a/tests/lib/Security/CredentialsManagerTest.php
+++ b/tests/lib/Security/CredentialsManagerTest.php
@@ -24,6 +24,9 @@ declare(strict_types=1);
namespace Test\Security;
+use OCP\Security\ICredentialsManager;
+use OCP\Server;
+
/**
* @group DB
*/
@@ -32,7 +35,7 @@ class CredentialsManagerTest extends \Test\TestCase {
* @dataProvider credentialsProvider
*/
public function testWithDB($userId, $identifier) {
- $credentialsManager = \OC::$server->getCredentialsManager();
+ $credentialsManager = Server::get(ICredentialsManager::class);
$secrets = 'Open Sesame';
@@ -45,7 +48,23 @@ class CredentialsManagerTest extends \Test\TestCase {
$this->assertSame(1, $removedRows);
}
- public function credentialsProvider() {
+ /**
+ * @dataProvider credentialsProvider
+ */
+ public function testUpdate($userId, $identifier): void {
+ $credentialsManager = Server::get(ICredentialsManager::class);
+
+ $secrets = 'Open Sesame';
+ $secretsRev = strrev($secrets);
+
+ $credentialsManager->store($userId, $identifier, $secrets);
+ $credentialsManager->store($userId, $identifier, $secretsRev);
+ $received = $credentialsManager->retrieve($userId, $identifier);
+
+ $this->assertSame($secretsRev, $received);
+ }
+
+ public function credentialsProvider(): array {
return [
[
'alice',