Skip to content

Commit da0d583

Browse files
authored
Merge pull request #743 from utopia-php/feat-mongodb
Passthru max UID
2 parents d41fd56 + 4a0aea1 commit da0d583

9 files changed

Lines changed: 50 additions & 41 deletions

File tree

src/Database/Database.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class Database
8080
public const MAX_DOUBLE = PHP_FLOAT_MAX;
8181
public const MAX_VECTOR_DIMENSIONS = 16000;
8282
public const MAX_ARRAY_INDEX_LENGTH = 255;
83+
public const MAX_UID_DEFAULT_LENGTH = 36;
8384

8485
// Global SRID for geographic coordinates (WGS84)
8586
public const DEFAULT_SRID = 4326;
@@ -5151,6 +5152,7 @@ public function updateDocuments(
51515152
$indexes,
51525153
$this->adapter->getIdAttributeType(),
51535154
$this->maxQueryValues,
5155+
$this->adapter->getMaxUIDLength(),
51545156
$this->adapter->getMinDateTime(),
51555157
$this->adapter->getMaxDateTime(),
51565158
$this->adapter->getSupportForAttributes()
@@ -6710,6 +6712,7 @@ public function deleteDocuments(
67106712
$indexes,
67116713
$this->adapter->getIdAttributeType(),
67126714
$this->maxQueryValues,
6715+
$this->adapter->getMaxUIDLength(),
67136716
$this->adapter->getMinDateTime(),
67146717
$this->adapter->getMaxDateTime(),
67156718
$this->adapter->getSupportForAttributes()
@@ -6912,6 +6915,7 @@ public function find(string $collection, array $queries = [], string $forPermiss
69126915
$indexes,
69136916
$this->adapter->getIdAttributeType(),
69146917
$this->maxQueryValues,
6918+
$this->adapter->getMaxUIDLength(),
69156919
$this->adapter->getMinDateTime(),
69166920
$this->adapter->getMaxDateTime(),
69176921
$this->adapter->getSupportForAttributes()
@@ -7143,6 +7147,7 @@ public function count(string $collection, array $queries = [], ?int $max = null)
71437147
$indexes,
71447148
$this->adapter->getIdAttributeType(),
71457149
$this->maxQueryValues,
7150+
$this->adapter->getMaxUIDLength(),
71467151
$this->adapter->getMinDateTime(),
71477152
$this->adapter->getMaxDateTime(),
71487153
$this->adapter->getSupportForAttributes()
@@ -7208,6 +7213,7 @@ public function sum(string $collection, string $attribute, array $queries = [],
72087213
$indexes,
72097214
$this->adapter->getIdAttributeType(),
72107215
$this->maxQueryValues,
7216+
$this->adapter->getMaxUIDLength(),
72117217
$this->adapter->getMinDateTime(),
72127218
$this->adapter->getMaxDateTime(),
72137219
$this->adapter->getSupportForAttributes()

src/Database/Validator/Key.php

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,11 @@
22

33
namespace Utopia\Database\Validator;
44

5+
use Utopia\Database\Database;
56
use Utopia\Validator;
67

78
class Key extends Validator
89
{
9-
protected bool $allowInternal = false; // If true, you keys starting with $ are allowed
10-
11-
/**
12-
* Maximum length for Key validation
13-
*/
14-
protected int $maxLength;
15-
16-
/**
17-
* @var string
18-
*/
1910
protected string $message;
2011

2112
/**
@@ -33,10 +24,10 @@ public function getDescription(): string
3324
/**
3425
* Expression constructor
3526
*/
36-
public function __construct(bool $allowInternal = false, int $maxLength = 255)
37-
{
38-
$this->allowInternal = $allowInternal;
39-
$this->maxLength = $maxLength;
27+
public function __construct(
28+
protected readonly bool $allowInternal = false,
29+
protected readonly int $maxLength = Database::MAX_UID_DEFAULT_LENGTH,
30+
) {
4031
$this->message = 'Parameter must contain at most ' . $this->maxLength . ' chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char';
4132
}
4233

@@ -46,7 +37,6 @@ public function __construct(bool $allowInternal = false, int $maxLength = 255)
4637
* Returns true if valid or false if not.
4738
*
4839
* @param $value
49-
*
5040
* @return bool
5141
*/
5242
public function isValid($value): bool
@@ -59,15 +49,14 @@ public function isValid($value): bool
5949
return false;
6050
}
6151

62-
// no leading special characters
52+
// No leading special characters
6353
$leading = \mb_substr($value, 0, 1);
6454
if ($leading === '_' || $leading === '.' || $leading === '-') {
6555
return false;
6656
}
6757

6858
$isInternal = $leading === '$';
6959

70-
7160
if ($isInternal && !$this->allowInternal) {
7261
return false;
7362
}
@@ -83,6 +72,7 @@ public function isValid($value): bool
8372
if (\preg_match('/[^A-Za-z0-9\_\-\.]/', $value)) {
8473
return false;
8574
}
75+
8676
// At most maxLength chars
8777
if (\mb_strlen($value) > $this->maxLength) {
8878
return false;

src/Database/Validator/Label.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
namespace Utopia\Database\Validator;
44

5+
use Utopia\Database\Database;
6+
57
class Label extends Key
68
{
7-
public function __construct(bool $allowInternal = false, int $maxLength = 255)
8-
{
9+
public function __construct(
10+
bool $allowInternal = false,
11+
int $maxLength = Database::MAX_UID_DEFAULT_LENGTH
12+
) {
913
parent::__construct($allowInternal, $maxLength);
1014
$this->message = 'Value must be a valid string between 1 and ' . $this->maxLength . ' chars containing only alphanumeric chars';
1115
}

src/Database/Validator/Queries/Documents.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Utopia\Database\Validator\Queries;
44

5-
use Exception;
65
use Utopia\Database\Database;
76
use Utopia\Database\Document;
87
use Utopia\Database\Validator\IndexedQueries;
@@ -16,18 +15,21 @@
1615
class Documents extends IndexedQueries
1716
{
1817
/**
19-
* Expression constructor
20-
*
2118
* @param array<mixed> $attributes
2219
* @param array<mixed> $indexes
2320
* @param string $idAttributeType
24-
* @throws Exception
21+
* @param int $maxValuesCount
22+
* @param \DateTime $minAllowedDate
23+
* @param \DateTime $maxAllowedDate
24+
* @param bool $supportForAttributes
25+
* @throws \Utopia\Database\Exception
2526
*/
2627
public function __construct(
2728
array $attributes,
2829
array $indexes,
2930
string $idAttributeType,
3031
int $maxValuesCount = 5000,
32+
int $maxUIDLength = 36,
3133
\DateTime $minAllowedDate = new \DateTime('0000-01-01'),
3234
\DateTime $maxAllowedDate = new \DateTime('9999-12-31'),
3335
bool $supportForAttributes = true
@@ -60,7 +62,7 @@ public function __construct(
6062
$validators = [
6163
new Limit(),
6264
new Offset(),
63-
new Cursor(),
65+
new Cursor($maxUIDLength),
6466
new Filter(
6567
$attributes,
6668
$idAttributeType,

src/Database/Validator/Query/Cursor.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
namespace Utopia\Database\Validator\Query;
44

5+
use Utopia\Database\Database;
56
use Utopia\Database\Document;
67
use Utopia\Database\Query;
78
use Utopia\Database\Validator\UID;
89

910
class Cursor extends Base
1011
{
12+
public function __construct(private readonly int $maxLength = Database::MAX_UID_DEFAULT_LENGTH)
13+
{
14+
}
15+
1116
/**
1217
* Is valid.
1318
*
@@ -33,7 +38,7 @@ public function isValid($value): bool
3338
$cursor = $cursor->getId();
3439
}
3540

36-
$validator = new UID();
41+
$validator = new UID($this->maxLength);
3742
if ($validator->isValid($cursor)) {
3843
return true;
3944
}

src/Database/Validator/UID.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
namespace Utopia\Database\Validator;
44

5+
use Utopia\Database\Database;
6+
57
class UID extends Key
68
{
79
/**
810
* Expression constructor
911
*/
10-
public function __construct(int $maxLength = 255)
12+
public function __construct(int $maxLength = Database::MAX_UID_DEFAULT_LENGTH)
1113
{
1214
parent::__construct(false, $maxLength);
1315
}

tests/unit/Validator/KeyTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ public function testValues(): void
6666
$this->assertEquals(false, $this->object->isValid('as+5dasdasdas'));
6767
$this->assertEquals(false, $this->object->isValid('as=5dasdasdas'));
6868

69-
// At most 255 chars
70-
$this->assertEquals(true, $this->object->isValid(str_repeat('a', 255)));
69+
// At most 36 chars
70+
$this->assertEquals(true, $this->object->isValid(str_repeat('a', 36)));
7171
$this->assertEquals(false, $this->object->isValid(str_repeat('a', 256)));
7272

7373
// Internal keys

tests/unit/Validator/LabelTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function testValues(): void
5959
$this->assertEquals(false, $this->object->isValid('as=5dasdasdas'));
6060

6161
// At most 255 chars
62-
$this->assertEquals(true, $this->object->isValid(str_repeat('a', 255)));
62+
$this->assertEquals(true, $this->object->isValid(str_repeat('a', 36)));
6363
$this->assertEquals(false, $this->object->isValid(str_repeat('a', 256)));
6464
}
6565
}

tests/unit/Validator/PermissionsTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -248,25 +248,25 @@ public function testInvalidPermissions(): void
248248
// team:$value, member:$value and user:$value must have valid Key for $value
249249
// No leading special chars
250250
$this->assertFalse($object->isValid([Permission::read(Role::user('_1234'))]));
251-
$this->assertEquals('Role "user" identifier value is invalid: Parameter must contain at most 255 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char', $object->getDescription());
251+
$this->assertEquals('Role "user" 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());
252252
$this->assertFalse($object->isValid([Permission::read(Role::team('-1234'))]));
253-
$this->assertEquals('Role "team" identifier value is invalid: Parameter must contain at most 255 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char', $object->getDescription());
253+
$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());
254254
$this->assertFalse($object->isValid([Permission::read(Role::member('.1234'))]));
255-
$this->assertEquals('Role "member" identifier value is invalid: Parameter must contain at most 255 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char', $object->getDescription());
255+
$this->assertEquals('Role "member" 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());
256256

257257
// No unsupported special characters
258258
$this->assertFalse($object->isValid([Permission::read(Role::user('12$4'))]));
259-
$this->assertEquals('Role "user" identifier value is invalid: Parameter must contain at most 255 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char', $object->getDescription());
259+
$this->assertEquals('Role "user" 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());
260260
$this->assertFalse($object->isValid([Permission::read(Role::user('12&4'))]));
261-
$this->assertEquals('Role "user" identifier value is invalid: Parameter must contain at most 255 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char', $object->getDescription());
261+
$this->assertEquals('Role "user" 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());
262262
$this->assertFalse($object->isValid([Permission::read(Role::user('ab(124'))]));
263-
$this->assertEquals('Role "user" identifier value is invalid: Parameter must contain at most 255 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char', $object->getDescription());
263+
$this->assertEquals('Role "user" 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());
264264

265-
// Shorter than 255 chars
265+
// Shorter than 36 chars
266266

267-
$this->assertTrue($object->isValid([Permission::read(Role::user(ID::custom(str_repeat('a', 255))))]));
267+
$this->assertTrue($object->isValid([Permission::read(Role::user(ID::custom(str_repeat('a', 36))))]));
268268
$this->assertFalse($object->isValid([Permission::read(Role::user(ID::custom(str_repeat('a', 256))))]));
269-
$this->assertEquals('Role "user" identifier value is invalid: Parameter must contain at most 255 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char', $object->getDescription());
269+
$this->assertEquals('Role "user" 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());
270270

271271
// Permission role must begin with one of: member, role, team, user
272272
$this->assertFalse($object->isValid(['update("memmber:1234")']));
@@ -278,7 +278,7 @@ public function testInvalidPermissions(): void
278278

279279
// Team permission
280280
$this->assertFalse($object->isValid([Permission::read(Role::team(ID::custom('_abcd')))]));
281-
$this->assertEquals('Role "team" identifier value is invalid: Parameter must contain at most 255 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char', $object->getDescription());
281+
$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());
282282
$this->assertFalse($object->isValid([Permission::read(Role::team(ID::custom('abcd/')))]));
283283
$this->assertEquals('Dimension must not be empty', $object->getDescription());
284284
$this->assertFalse($object->isValid([Permission::read(Role::team(ID::custom(''), 'abcd'))]));
@@ -288,9 +288,9 @@ public function testInvalidPermissions(): void
288288
$this->assertFalse($object->isValid([Permission::read(Role::team(ID::custom('abcd'), 'e/fgh'))]));
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'))]));
291-
$this->assertEquals('Role "team" identifier value is invalid: Parameter must contain at most 255 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char', $object->getDescription());
291+
$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());
292292
$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 255 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char', $object->getDescription());
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());
294294

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

0 commit comments

Comments
 (0)