Skip to content

Commit a074126

Browse files
committed
Project-specific roles
1 parent 8e6a033 commit a074126

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

src/Database/Validator/Roles.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,9 @@ protected function isValidRole(
237237
string $identifier,
238238
string $dimension
239239
): bool {
240-
$key = new Key();
241-
$label = new Label();
240+
$identifierValidator = new Key();
241+
$labelValidator = new Label();
242+
$dimensionValidator = new Key(maxLength: 60);
242243

243244
$config = self::CONFIG[$role] ?? null;
244245

@@ -265,11 +266,11 @@ protected function isValidRole(
265266

266267
// Allowed and has an invalid identifier
267268
if ($allowed && !empty($identifier)) {
268-
if ($role === self::ROLE_LABEL && !$label->isValid($identifier)) {
269-
$this->message = 'Role "' . $role . '"' . ' identifier value is invalid: ' . $label->getDescription();
269+
if ($role === self::ROLE_LABEL && !$labelValidator->isValid($identifier)) {
270+
$this->message = 'Role "' . $role . '"' . ' identifier value is invalid: ' . $labelValidator->getDescription();
270271
return false;
271-
} elseif ($role !== self::ROLE_LABEL && !$key->isValid($identifier)) {
272-
$this->message = 'Role "' . $role . '"' . ' identifier value is invalid: ' . $key->getDescription();
272+
} elseif ($role !== self::ROLE_LABEL && !$identifierValidator->isValid($identifier)) {
273+
$this->message = 'Role "' . $role . '"' . ' identifier value is invalid: ' . $identifierValidator->getDescription();
273274
return false;
274275
}
275276
}
@@ -300,8 +301,8 @@ protected function isValidRole(
300301
return false;
301302
}
302303
// Allowed and dimension is not a valid key
303-
if (!$key->isValid($dimension)) {
304-
$this->message = 'Role "' . $role . '"' . ' dimension value is invalid: ' . $key->getDescription();
304+
if (!$dimensionValidator->isValid($dimension)) {
305+
$this->message = 'Role "' . $role . '"' . ' dimension value is invalid: ' . $dimensionValidator->getDescription();
305306
return false;
306307
}
307308
}

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/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)