Skip to content

Commit 9ac7e09

Browse files
authored
Merge pull request #535 from utopia-php/auth_outside_transaction
Move auth outside transaction
2 parents 7961014 + 8353f1e commit 9ac7e09

File tree

1 file changed

+48
-39
lines changed

1 file changed

+48
-39
lines changed

src/Database/Database.php

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4052,6 +4052,15 @@ public function updateDocuments(string $collection, Document $updates, array $qu
40524052
throw new DatabaseException('Collection not found');
40534053
}
40544054

4055+
$documentSecurity = $collection->getAttribute('documentSecurity', false);
4056+
4057+
$authorization = new Authorization(self::PERMISSION_UPDATE);
4058+
$skipAuth = $authorization->isValid($collection->getUpdate());
4059+
4060+
if (!$skipAuth && !$documentSecurity && $collection->getId() !== self::METADATA) {
4061+
throw new AuthorizationException($authorization->getDescription());
4062+
}
4063+
40554064
$attributes = $collection->getAttribute('attributes', []);
40564065
$indexes = $collection->getAttribute('indexes', []);
40574066

@@ -4098,18 +4107,8 @@ public function updateDocuments(string $collection, Document $updates, array $qu
40984107
throw new StructureException($validator->getDescription());
40994108
}
41004109

4101-
$documents = $this->withTransaction(function () use ($collection, $queries, $batchSize, $updates, $limit, $cursor) {
4110+
$documents = $this->withTransaction(function () use ($collection, $queries, $batchSize, $updates, $limit, $cursor, $authorization, $skipAuth) {
41024111
$documents = [];
4103-
4104-
$documentSecurity = $collection->getAttribute('documentSecurity', false);
4105-
4106-
$authorization = new Authorization(self::PERMISSION_UPDATE);
4107-
$skipAuth = $authorization->isValid($collection->getUpdate());
4108-
4109-
if (!$skipAuth && !$documentSecurity && $collection->getId() !== self::METADATA) {
4110-
throw new AuthorizationException($authorization->getDescription());
4111-
}
4112-
41134112
$originalLimit = $limit;
41144113
$lastDocument = $cursor;
41154114

@@ -4121,15 +4120,19 @@ public function updateDocuments(string $collection, Document $updates, array $qu
41214120
$limit -= $batchSize;
41224121
}
41234122

4124-
$affectedDocuments = $this->silent(fn () => $this->find($collection->getId(), array_merge(
4125-
$queries,
4126-
empty($lastDocument) ? [
4127-
Query::limit($batchSize),
4128-
] : [
4129-
Query::limit($batchSize),
4130-
Query::cursorAfter($lastDocument),
4131-
]
4132-
), forPermission: Database::PERMISSION_UPDATE));
4123+
$new = [
4124+
Query::limit($batchSize)
4125+
];
4126+
4127+
if (! empty($lastDocument)) {
4128+
$new[] = Query::cursorAfter($lastDocument);
4129+
}
4130+
4131+
$affectedDocuments = $this->silent(fn () => $this->find(
4132+
$collection->getId(),
4133+
array_merge($new, $queries),
4134+
forPermission: Database::PERMISSION_UPDATE
4135+
));
41334136

41344137
if (empty($affectedDocuments)) {
41354138
break;
@@ -5359,6 +5362,14 @@ public function deleteDocuments(string $collection, array $queries = [], int $ba
53595362
throw new DatabaseException('Collection not found');
53605363
}
53615364

5365+
$documentSecurity = $collection->getAttribute('documentSecurity', false);
5366+
$authorization = new Authorization(self::PERMISSION_DELETE);
5367+
$skipAuth = $authorization->isValid($collection->getDelete());
5368+
5369+
if (!$skipAuth && !$documentSecurity && $collection->getId() !== self::METADATA) {
5370+
throw new AuthorizationException($authorization->getDescription());
5371+
}
5372+
53625373
$attributes = $collection->getAttribute('attributes', []);
53635374
$indexes = $collection->getAttribute('indexes', []);
53645375

@@ -5384,16 +5395,8 @@ public function deleteDocuments(string $collection, array $queries = [], int $ba
53845395
throw new DatabaseException("Cursor document must be from the same Collection.");
53855396
}
53865397

5387-
$documents = $this->withTransaction(function () use ($collection, $queries, $batchSize, $limit, $cursor) {
5388-
$documentSecurity = $collection->getAttribute('documentSecurity', false);
5389-
$authorization = new Authorization(self::PERMISSION_DELETE);
5390-
$skipAuth = $authorization->isValid($collection->getDelete());
5398+
$documents = $this->withTransaction(function () use ($collection, $queries, $batchSize, $limit, $cursor, $skipAuth, $authorization) {
53915399
$documents = [];
5392-
5393-
if (!$skipAuth && !$documentSecurity && $collection->getId() !== self::METADATA) {
5394-
throw new AuthorizationException($authorization->getDescription());
5395-
}
5396-
53975400
$originalLimit = $limit;
53985401
$lastDocument = $cursor;
53995402

@@ -5404,15 +5407,19 @@ public function deleteDocuments(string $collection, array $queries = [], int $ba
54045407
$limit -= $batchSize;
54055408
}
54065409

5407-
$affectedDocuments = $this->silent(fn () => $this->find($collection->getId(), array_merge(
5408-
$queries,
5409-
empty($lastDocument) ? [
5410-
Query::limit($batchSize),
5411-
] : [
5412-
Query::limit($batchSize),
5413-
Query::cursorAfter($lastDocument),
5414-
]
5415-
), forPermission: Database::PERMISSION_DELETE));
5410+
$new = [
5411+
Query::limit($batchSize)
5412+
];
5413+
5414+
if (! empty($lastDocument)) {
5415+
$new[] = Query::cursorAfter($lastDocument);
5416+
}
5417+
5418+
$affectedDocuments = $this->silent(fn () => $this->find(
5419+
$collection->getId(),
5420+
array_merge($new, $queries),
5421+
forPermission: Database::PERMISSION_DELETE
5422+
));
54165423

54175424
if (empty($affectedDocuments)) {
54185425
break;
@@ -5454,10 +5461,12 @@ public function deleteDocuments(string $collection, array $queries = [], int $ba
54545461
}
54555462

54565463
foreach (\array_chunk($documents, $batchSize) as $chunk) {
5457-
$this->adapter->deleteDocuments(
5464+
$callback = fn () => $this->adapter->deleteDocuments(
54585465
$collection->getId(),
54595466
array_map(fn ($document) => $document->getId(), $chunk)
54605467
);
5468+
5469+
$skipAuth ? $authorization->skip($callback) : $callback();
54615470
}
54625471

54635472
return $documents;

0 commit comments

Comments
 (0)