Skip to content

Commit b7720d7

Browse files
authored
Merge pull request #795 from utopia-php/ser-541-tag-4.5.2
2 parents f89e7b5 + 4200ccc commit b7720d7

File tree

4 files changed

+48
-13
lines changed

4 files changed

+48
-13
lines changed

src/Database/Validator/Roles.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,15 @@ protected function isValidRole(
237237
string $identifier,
238238
string $dimension
239239
): bool {
240-
$key = new Key();
241-
$label = new Label();
240+
$identifierValidator = match ($role) {
241+
self::ROLE_LABEL => new Label(),
242+
default => new Key(),
243+
};
244+
/**
245+
* For project-specific permissions, roles will be in the format `project-<projectId>-<role>`.
246+
* Template takes 9 characters, `projectId` and `role` can be upto 36 characters. In total, 81 characters.
247+
*/
248+
$dimensionValidator = new Key(maxLength: 81);
242249

243250
$config = self::CONFIG[$role] ?? null;
244251

@@ -264,14 +271,9 @@ protected function isValidRole(
264271
}
265272

266273
// Allowed and has an invalid identifier
267-
if ($allowed && !empty($identifier)) {
268-
if ($role === self::ROLE_LABEL && !$label->isValid($identifier)) {
269-
$this->message = 'Role "' . $role . '"' . ' identifier value is invalid: ' . $label->getDescription();
270-
return false;
271-
} elseif ($role !== self::ROLE_LABEL && !$key->isValid($identifier)) {
272-
$this->message = 'Role "' . $role . '"' . ' identifier value is invalid: ' . $key->getDescription();
273-
return false;
274-
}
274+
if ($allowed && !empty($identifier) && !$identifierValidator->isValid($identifier)) {
275+
$this->message = 'Role "' . $role . '"' . ' identifier value is invalid: ' . $identifierValidator->getDescription();
276+
return false;
275277
}
276278

277279
// Process dimension configuration
@@ -300,8 +302,8 @@ protected function isValidRole(
300302
return false;
301303
}
302304
// Allowed and dimension is not a valid key
303-
if (!$key->isValid($dimension)) {
304-
$this->message = 'Role "' . $role . '"' . ' dimension value is invalid: ' . $key->getDescription();
305+
if (!$dimensionValidator->isValid($dimension)) {
306+
$this->message = 'Role "' . $role . '"' . ' dimension value is invalid: ' . $dimensionValidator->getDescription();
305307
return false;
306308
}
307309
}

tests/unit/RoleTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ public function testOutputFromString(): void
4040
$this->assertEquals('123', $role->getIdentifier());
4141
$this->assertEquals('456', $role->getDimension());
4242

43+
$role = Role::parse('team:123/project-456-owner');
44+
$this->assertEquals('team', $role->getRole());
45+
$this->assertEquals('123', $role->getIdentifier());
46+
$this->assertEquals('project-456-owner', $role->getDimension());
47+
48+
$role = Role::parse('team:123/project-456');
49+
$this->assertEquals('team', $role->getRole());
50+
$this->assertEquals('123', $role->getIdentifier());
51+
$this->assertEquals('project-456', $role->getDimension());
52+
4353
$role = Role::parse('user:123/verified');
4454
$this->assertEquals('user', $role->getRole());
4555
$this->assertEquals('123', $role->getIdentifier());
@@ -76,6 +86,12 @@ public function testInputFromParameters(): void
7686
$role = new Role('team', '123', '456');
7787
$this->assertEquals('team:123/456', $role->toString());
7888

89+
$role = new Role('team', '123', 'project-456-owner');
90+
$this->assertEquals('team:123/project-456-owner', $role->toString());
91+
92+
$role = new Role('team', '123', 'project-456');
93+
$this->assertEquals('team:123/project-456', $role->toString());
94+
7995
$role = new Role('label', 'vip');
8096
$this->assertEquals('label:vip', $role->toString());
8197
}
@@ -100,6 +116,12 @@ public function testInputFromRoles(): void
100116
$role = Role::team(ID::custom('123'), '456');
101117
$this->assertEquals('team:123/456', $role->toString());
102118

119+
$role = Role::team(ID::custom('123'), 'project-456-owner');
120+
$this->assertEquals('team:123/project-456-owner', $role->toString());
121+
122+
$role = Role::team(ID::custom('123'), 'project-456');
123+
$this->assertEquals('team:123/project-456', $role->toString());
124+
103125
$role = Role::label('vip');
104126
$this->assertEquals('label:vip', $role->toString());
105127
}

tests/unit/Validator/PermissionsTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,12 @@ public function testInvalidPermissions(): void
289289
$this->assertEquals('Only one dimension can be provided', $object->getDescription());
290290
$this->assertFalse($object->isValid([Permission::read(Role::team(ID::custom('ab&cd3'), 'efgh'))]));
291291
$this->assertEquals('Role "team" identifier value is invalid: Parameter must contain at most 36 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char', $object->getDescription());
292+
$this->assertFalse($object->isValid([Permission::read(Role::team(ID::custom(str_repeat('a', 37)), 'efgh'))]));
293+
$this->assertEquals('Role "team" identifier value is invalid: Parameter must contain at most 36 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char', $object->getDescription());
292294
$this->assertFalse($object->isValid([Permission::read(Role::team(ID::custom('abcd'), 'ef*gh'))]));
293-
$this->assertEquals('Role "team" dimension value is invalid: Parameter must contain at most 36 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char', $object->getDescription());
295+
$this->assertEquals('Role "team" dimension value is invalid: Parameter must contain at most 81 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char', $object->getDescription());
296+
$this->assertFalse($object->isValid([Permission::read(Role::team(ID::custom('abcd'), str_repeat('a', 82)))]));
297+
$this->assertEquals('Role "team" dimension value is invalid: Parameter must contain at most 81 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char', $object->getDescription());
294298

295299
// Permission-list length must be valid
296300
$object = new Permissions(100);

tests/unit/Validator/RolesTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests\Unit\Validator;
44

55
use PHPUnit\Framework\TestCase;
6+
use Utopia\Database\Helpers\ID;
67
use Utopia\Database\Helpers\Role;
78
use Utopia\Database\Validator\Roles;
89

@@ -23,6 +24,12 @@ public function testValidRole(): void
2324
{
2425
$object = new Roles();
2526
$this->assertTrue($object->isValid([Role::users()->toString()]));
27+
$this->assertTrue($object->isValid([Role::users(Roles::DIMENSION_VERIFIED)->toString()]));
28+
$this->assertTrue($object->isValid([Role::users(Roles::DIMENSION_UNVERIFIED)->toString()]));
29+
$this->assertTrue($object->isValid([Role::team(ID::custom('696f34ea003d48edab8e'))->toString()]));
30+
$this->assertTrue($object->isValid([Role::team(ID::custom('696f34ea003d48edab8e'), 'project-696f34ea003d48edab8e-owner')->toString()]));
31+
$this->assertTrue($object->isValid([Role::team(ID::custom('696f34ea003d48edab8e'), 'project-696f34ea003d48edab8e')->toString()]));
32+
$this->assertTrue($object->isValid([Role::label('vip')->toString()]));
2633
}
2734

2835
public function testNotAnArray(): void

0 commit comments

Comments
 (0)