diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index 6adbf61d6d..61b71930eb 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -2239,7 +2239,30 @@ 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',