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: $permissions returns as object rather than array #281

Merged
merged 5 commits into from
Jun 9, 2023
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
2 changes: 1 addition & 1 deletion src/Database/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function getCollection(): string
*/
public function getPermissions(): array
{
return \array_unique($this->getAttribute('$permissions', []));
return \array_values(\array_unique($this->getAttribute('$permissions', [])));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Helpers/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public static function aggregate(?array $permissions, array $allowed = Database:
}
}
}
return $mutated;
return \array_values(\array_unique($mutated));
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/Database/PermissionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,5 +284,23 @@ public function testAggregation(): void

$parsed = Permission::aggregate($permissions, [Database::PERMISSION_UPDATE, Database::PERMISSION_DELETE]);
$this->assertEquals(['update("any")', 'delete("any")'], $parsed);

$permissions = [
'read("any")',
'read("user:123")',
'read("user:123")',
'write("user:123")',
'update("user:123")',
'delete("user:123")'
];

$parsed = Permission::aggregate($permissions, Database::PERMISSIONS);
$this->assertEquals([
'read("any")',
'read("user:123")',
'create("user:123")',
'update("user:123")',
'delete("user:123")',
], $parsed);
}
}
42 changes: 42 additions & 0 deletions tests/Database/Validator/PermissionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,46 @@ public function testInvalidPermissions(): void
$this->assertFalse($object->isValid($permissions));
$this->assertEquals('You can only provide up to 100 permissions.', $object->getDescription());
}

/*
* Test for checking duplicate methods input. The getPermissions should return an a list array
*/
public function testDuplicateMethods(): void
{
$validator = new Permissions();

$user = ID::unique();

$document = new Document([
'$id' => uniqid(),
'$collection' => uniqid(),
'$permissions' => [
Permission::read(Role::any()),
Permission::read(Role::user($user)),
Permission::read(Role::user($user)),
Permission::write(Role::user($user)),
Permission::update(Role::user($user)),
Permission::delete(Role::user($user)),
],
'title' => 'This is a test.',
'list' => [
'one'
],
'children' => [
new Document(['name' => 'x']),
new Document(['name' => 'y']),
new Document(['name' => 'z']),
]
]);
$this->assertTrue($validator->isValid($document->getPermissions()));
$permissions = $document->getPermissions();
$this->assertEquals(5, count($permissions));
$this->assertEquals([
'read("any")',
'read("user:' . $user . '")',
'write("user:' . $user . '")',
'update("user:' . $user . '")',
'delete("user:' . $user . '")',
], $permissions);
}
}