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

Fix crud operations permission in update relationships #300

Merged
merged 11 commits into from
Aug 21, 2023
15 changes: 8 additions & 7 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -3245,16 +3245,16 @@ private function updateDocumentRelationships(Document $collection, Document $old
$removedDocuments = \array_diff($oldIds, $newIds);

foreach ($removedDocuments as $relation) {
$relation = $this->skipRelationships(fn () => $this->getDocument(
$relation = Authorization::skip(fn () => $this->skipRelationships(fn () => $this->getDocument(
$relatedCollection->getId(),
$relation
));
)));

$this->skipRelationships(fn () => $this->updateDocument(
Authorization::skip(fn () => $this->skipRelationships(fn () => $this->updateDocument(
$relatedCollection->getId(),
$relation->getId(),
$relation->setAttribute($twoWayKey, null)
));
)));
}

foreach ($value as $relation) {
Expand Down Expand Up @@ -3285,11 +3285,12 @@ private function updateDocumentRelationships(Document $collection, Document $old
$relation->setAttribute($twoWayKey, $document->getId())
);
} else {
$this->updateDocument(
// We need to skip the relationship when updating document so that comparison check does not fail
$this->skipRelationships(fn () => $this->updateDocument(
fanatic75 marked this conversation as resolved.
Show resolved Hide resolved
$relatedCollection->getId(),
$related->getId(),
$relation->setAttribute($twoWayKey, $document->getId())
);
));
}
} else {
throw new DatabaseException('Invalid value for relationship');
Expand Down Expand Up @@ -3368,7 +3369,7 @@ private function updateDocumentRelationships(Document $collection, Document $old
]);

foreach ($junctions as $junction) {
$this->deleteDocument($junction->getCollection(), $junction->getId());
Authorization::skip(fn () => $this->deleteDocument($junction->getCollection(), $junction->getId()));
}
}

Expand Down
61 changes: 61 additions & 0 deletions tests/Database/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -11843,6 +11843,67 @@ public function testCollectionPermissionsRelationshipsDeleteWorks(array $data):
));
}

public function testCreateRelationDocumentWithoutUpdatePermission(): void
{
if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) {
$this->expectNotToPerformAssertions();
return;
}

Authorization::cleanRoles();
Authorization::setRole(Role::user('a')->toString());

static::getDatabase()->createCollection('parentRelationTest', [], [], [
Permission::read(Role::user('a')),
Permission::create(Role::user('a')),
Permission::update(Role::user('a')),
Permission::delete(Role::user('a'))
]);
static::getDatabase()->createCollection('childRelationTest', [], [], [
Permission::create(Role::user('a')),
Permission::read(Role::user('a')),
]);
static::getDatabase()->createAttribute('parentRelationTest', 'name', Database::VAR_STRING, 255, false);
static::getDatabase()->createAttribute('childRelationTest', 'name', Database::VAR_STRING, 255, false);

static::getDatabase()->createRelationship(
collection: 'parentRelationTest',
relatedCollection: 'childRelationTest',
type: Database::RELATION_ONE_TO_MANY,
id: 'children'
);

// Create document with relationship with nested data
$parent = static::getDatabase()->createDocument('parentRelationTest', new Document([
'$id' => 'parent1',
'name' => 'Parent 1',
'children' => [
[
'$id' => 'child1',
'name' => 'Child 1',
],
],
]));
$doc = static::getDatabase()->skipRelationships(
fn () => static::getDatabase()->getDocument('childRelationTest', 'child1')
);
$this->assertEquals(1, \count($parent['children']));
$parent->setAttribute('children', [
[
'$id' => 'child1',
],
[
'$id' => 'child2',
],
]);
$updatedParent = static::getDatabase()->updateDocument('parentRelationTest', 'parent1', $parent);

$this->assertEquals(2, \count($updatedParent['children']));
fanatic75 marked this conversation as resolved.
Show resolved Hide resolved

static::getDatabase()->deleteCollection('parentRelationTest');
static::getDatabase()->deleteCollection('childRelationTest');
}

public function testLabels(): void
{
$this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->createCollection(
Expand Down