Skip to content

Commit 84c76a8

Browse files
authored
Merge pull request #37520 from nextcloud/fix/noid/fix-dbal-exception-handling
fix DBAL exception handling in setValues
2 parents 671d272 + 997c2a2 commit 84c76a8

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

lib/private/DB/Connection.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040
use Doctrine\DBAL\Configuration;
4141
use Doctrine\DBAL\Driver;
4242
use Doctrine\DBAL\Exception;
43-
use Doctrine\DBAL\Exception\ConstraintViolationException;
44-
use Doctrine\DBAL\Exception\NotNullConstraintViolationException;
4543
use Doctrine\DBAL\Platforms\MySQLPlatform;
4644
use Doctrine\DBAL\Platforms\OraclePlatform;
4745
use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
@@ -381,10 +379,10 @@ private function getType($value) {
381379
* @param array $values (column name => value)
382380
* @param array $updatePreconditionValues ensure values match preconditions (column name => value)
383381
* @return int number of new rows
384-
* @throws \Doctrine\DBAL\Exception
382+
* @throws \OCP\DB\Exception
385383
* @throws PreConditionNotMetException
386384
*/
387-
public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []) {
385+
public function setValues(string $table, array $keys, array $values, array $updatePreconditionValues = []): int {
388386
try {
389387
$insertQb = $this->getQueryBuilder();
390388
$insertQb->insert($table)
@@ -394,9 +392,15 @@ public function setValues($table, array $keys, array $values, array $updatePreco
394392
}, array_merge($keys, $values))
395393
);
396394
return $insertQb->executeStatement();
397-
} catch (NotNullConstraintViolationException $e) {
398-
throw $e;
399-
} catch (ConstraintViolationException $e) {
395+
} catch (\OCP\DB\Exception $e) {
396+
if (!in_array($e->getReason(), [
397+
\OCP\DB\Exception::REASON_CONSTRAINT_VIOLATION,
398+
\OCP\DB\Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION,
399+
])
400+
) {
401+
throw $e;
402+
}
403+
400404
// value already exists, try update
401405
$updateQb = $this->getQueryBuilder();
402406
$updateQb->update($table);

tests/lib/Security/CredentialsManagerTest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424

2525
namespace Test\Security;
2626

27+
use OCP\Security\ICredentialsManager;
28+
use OCP\Server;
29+
2730
/**
2831
* @group DB
2932
*/
@@ -32,7 +35,7 @@ class CredentialsManagerTest extends \Test\TestCase {
3235
* @dataProvider credentialsProvider
3336
*/
3437
public function testWithDB($userId, $identifier) {
35-
$credentialsManager = \OC::$server->getCredentialsManager();
38+
$credentialsManager = Server::get(ICredentialsManager::class);
3639

3740
$secrets = 'Open Sesame';
3841

@@ -45,7 +48,23 @@ public function testWithDB($userId, $identifier) {
4548
$this->assertSame(1, $removedRows);
4649
}
4750

48-
public function credentialsProvider() {
51+
/**
52+
* @dataProvider credentialsProvider
53+
*/
54+
public function testUpdate($userId, $identifier): void {
55+
$credentialsManager = Server::get(ICredentialsManager::class);
56+
57+
$secrets = 'Open Sesame';
58+
$secretsRev = strrev($secrets);
59+
60+
$credentialsManager->store($userId, $identifier, $secrets);
61+
$credentialsManager->store($userId, $identifier, $secretsRev);
62+
$received = $credentialsManager->retrieve($userId, $identifier);
63+
64+
$this->assertSame($secretsRev, $received);
65+
}
66+
67+
public function credentialsProvider(): array {
4968
return [
5069
[
5170
'alice',

0 commit comments

Comments
 (0)