From 06e4920ee224b27bbcbdd1bda1adefb638a3e9a8 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Tue, 29 Jul 2025 11:57:57 +0530 Subject: [PATCH 1/7] fix: create email target when using email otp registration --- app/controllers/api/account.php | 26 +++++++++++++++++++++- tests/e2e/Services/Account/AccountBase.php | 6 ++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index 6adbf61d6d..df17051d6d 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -2239,7 +2239,31 @@ App::post('/v1/account/tokens/email') ]); $user->removeAttribute('$sequence'); - Authorization::skip(fn () => $dbForProject->createDocument('users', $user)); + $user = Authorization::skip(fn () => $dbForProject->createDocument('users', $user)); + + try { + $target = Authorization::skip(fn () => $dbForProject->createDocument('targets', new Document([ + '$permissions' => [ + Permission::read(Role::user($user->getId())), + Permission::update(Role::user($user->getId())), + Permission::delete(Role::user($user->getId())), + ], + 'userId' => $user->getId(), + 'userInternalId' => $user->getSequence(), + 'providerType' => MESSAGE_TYPE_EMAIL, + 'identifier' => $email, + ]))); + $user->setAttribute('targets', [...$user->getAttribute('targets', []), $target]); + } catch (Duplicate) { + $existingTarget = $dbForProject->findOne('targets', [ + Query::equal('identifier', [$email]), + ]); + if (!$existingTarget->isEmpty()) { + $user->setAttribute('targets', $existingTarget, Document::SET_TYPE_APPEND); + } + } + + $dbForProject->purgeCachedDocument('users', $user->getId()); } $tokenSecret = Auth::codeGenerator(6); diff --git a/tests/e2e/Services/Account/AccountBase.php b/tests/e2e/Services/Account/AccountBase.php index 7c83edf3e3..8813e2784f 100644 --- a/tests/e2e/Services/Account/AccountBase.php +++ b/tests/e2e/Services/Account/AccountBase.php @@ -39,6 +39,8 @@ trait AccountBase $this->assertEquals($response['body']['labels'], []); $this->assertArrayHasKey('accessedAt', $response['body']); $this->assertNotEmpty($response['body']['accessedAt']); + $this->assertArrayHasKey('targets', $response['body']); + $this->assertEquals($email, $response['body']['targets'][0]['identifier']); /** * Test for FAILURE @@ -159,7 +161,7 @@ trait AccountBase 'email' => 'otpuser@appwrite.io' ]); - $this->assertEquals(201, $response['headers']['status-code'], ); + $this->assertEquals(201, $response['headers']['status-code']); $this->assertNotEmpty($response['body']['$id']); $this->assertNotEmpty($response['body']['$createdAt']); $this->assertNotEmpty($response['body']['userId']); @@ -209,6 +211,8 @@ trait AccountBase $this->assertEquals($userId, $response['body']['$id']); $this->assertEquals($userId, $response['body']['$id']); $this->assertTrue($response['body']['emailVerification']); + $this->assertArrayHasKey('targets', $response['body']); + $this->assertEquals('otpuser@appwrite.io', $response['body']['targets'][0]['identifier']); $response = $this->client->call(Client::METHOD_POST, '/account/sessions/token', array_merge([ 'origin' => 'http://localhost', From 7ad01fcadae3edaad48ca009b15cbe7e8e1ee1c8 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Tue, 29 Jul 2025 12:23:06 +0530 Subject: [PATCH 2/7] linebreak --- app/controllers/api/account.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index df17051d6d..61b71930eb 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -2240,7 +2240,6 @@ App::post('/v1/account/tokens/email') $user->removeAttribute('$sequence'); $user = Authorization::skip(fn () => $dbForProject->createDocument('users', $user)); - try { $target = Authorization::skip(fn () => $dbForProject->createDocument('targets', new Document([ '$permissions' => [ From 3db9cd06d5e8aae55f912fa05e6f677a7adf51d6 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Tue, 29 Jul 2025 15:21:15 +0530 Subject: [PATCH 3/7] chore: filter certificates renewal task in maintenance by region --- src/Appwrite/Platform/Tasks/Maintenance.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Appwrite/Platform/Tasks/Maintenance.php b/src/Appwrite/Platform/Tasks/Maintenance.php index e6def793b5..4ffbcbbe8e 100644 --- a/src/Appwrite/Platform/Tasks/Maintenance.php +++ b/src/Appwrite/Platform/Tasks/Maintenance.php @@ -122,6 +122,15 @@ class Maintenance extends Action Console::info("[{$time}] Found " . \count($certificates) . " certificates for renewal, scheduling jobs."); foreach ($certificates as $certificate) { + $rule = $dbForPlatform->find('rules', [ + Query::equal('domain', [$certificate->getAttribute('domain')]), + Query::limit(1), + ]); + + if (!$rule[0] || $rule[0]->getAttribute('region') !== System::getEnv('_APP_REGION', 'default')) { + continue; + } + $queueForCertificate ->setDomain(new Document([ 'domain' => $certificate->getAttribute('domain') From 1731212ebbf1b69d753050f47538e4b177590a9c Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Tue, 29 Jul 2025 15:26:37 +0530 Subject: [PATCH 4/7] chore: improve check --- src/Appwrite/Platform/Tasks/Maintenance.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Appwrite/Platform/Tasks/Maintenance.php b/src/Appwrite/Platform/Tasks/Maintenance.php index 4ffbcbbe8e..aec6e78a6f 100644 --- a/src/Appwrite/Platform/Tasks/Maintenance.php +++ b/src/Appwrite/Platform/Tasks/Maintenance.php @@ -122,12 +122,11 @@ class Maintenance extends Action Console::info("[{$time}] Found " . \count($certificates) . " certificates for renewal, scheduling jobs."); foreach ($certificates as $certificate) { - $rule = $dbForPlatform->find('rules', [ + $rule = $dbForPlatform->findOne('rules', [ Query::equal('domain', [$certificate->getAttribute('domain')]), - Query::limit(1), ]); - if (!$rule[0] || $rule[0]->getAttribute('region') !== System::getEnv('_APP_REGION', 'default')) { + if ($rule->isEmpty() || $rule->getAttribute('region') !== System::getEnv('_APP_REGION', 'default')) { continue; } From 66786f20f0d6b4ddb58be775f6c86f9596f0d2f9 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Tue, 29 Jul 2025 16:53:55 +0530 Subject: [PATCH 5/7] chore: use md5 if enabled --- src/Appwrite/Platform/Tasks/Maintenance.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Appwrite/Platform/Tasks/Maintenance.php b/src/Appwrite/Platform/Tasks/Maintenance.php index aec6e78a6f..036e8783d4 100644 --- a/src/Appwrite/Platform/Tasks/Maintenance.php +++ b/src/Appwrite/Platform/Tasks/Maintenance.php @@ -122,9 +122,14 @@ class Maintenance extends Action Console::info("[{$time}] Found " . \count($certificates) . " certificates for renewal, scheduling jobs."); foreach ($certificates as $certificate) { - $rule = $dbForPlatform->findOne('rules', [ - Query::equal('domain', [$certificate->getAttribute('domain')]), - ]); + $domain = $certificate->getAttribute('domain'); + if (System::getEnv('_APP_RULES_FORMAT') === 'md5') { + $rule = $dbForPlatform->getDocument('rules', md5($domain)); + } else { + $rule = $dbForPlatform->findOne('rules', [ + Query::equal('domain', [$domain]), + ]); + } if ($rule->isEmpty() || $rule->getAttribute('region') !== System::getEnv('_APP_REGION', 'default')) { continue; From ccf0a23bdfd5eaa872a68be651f4d9057f900dda Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 30 Jul 2025 01:02:21 +1200 Subject: [PATCH 6/7] Remove sequence on incoming docs --- app/controllers/api/databases.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 3378d011c9..89b31a10db 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -3453,6 +3453,7 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/documents') } else { $relation->removeAttribute('$collectionId'); $relation->removeAttribute('$databaseId'); + $relation->removeAttribute('$sequence'); $relation->setAttribute('$collection', $relatedCollection->getId()); $type = Database::PERMISSION_UPDATE; } @@ -3485,6 +3486,9 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/documents') } } + // Remove sequence if set + unset($document['$sequence']); + // Assign a unique ID if needed, otherwise use the provided ID. $document['$id'] = $sourceId === 'unique()' ? ID::unique() : $sourceId; $document = new Document($document); @@ -4074,6 +4078,9 @@ App::patch('/v1/databases/:databaseId/collections/:collectionId/documents/:docum $permissions = $document->getPermissions() ?? []; } + // Remove sequence if set + unset($data['$sequence']); + $data['$id'] = $documentId; $data['$permissions'] = $permissions; $newDocument = new Document($data); @@ -4708,6 +4715,9 @@ App::patch('/v1/databases/:databaseId/collections/:collectionId/documents') } } + // Remove sequence if set + unset($data['$sequence']); + $documents = []; try { From 933f07c1758f733c852770315a33035292a91663 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 30 Jul 2025 01:18:08 +1200 Subject: [PATCH 7/7] Add missing removal --- app/controllers/api/databases.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 89b31a10db..45ca444c1c 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -4133,6 +4133,7 @@ App::patch('/v1/databases/:databaseId/collections/:collectionId/documents/:docum )); $relation->removeAttribute('$collectionId'); $relation->removeAttribute('$databaseId'); + $relation->removeAttribute('$sequence'); // Attribute $collection is required for Utopia. $relation->setAttribute( '$collection',