diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index dd3cf496ba..83e9af04b1 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -1041,11 +1041,17 @@ App::post('/v1/account/jwt') /** @var Utopia\Database\Document $user */ /** @var Utopia\Database\Database $dbForProject */ - $current = $dbForProject->findOne('sessions', [ - new Query('secret', Query::TYPE_EQUAL, [Auth::hash(Auth::$secret)]) - ]); - if (!$current) { + $sessions = $user->getAttribute('sessions', []); + $current = new Document(); + + foreach ($sessions as $session) { /** @var Utopia\Database\Document $session */ + if ($session->getAttribute('secret') == Auth::hash(Auth::$secret)) { // If current session delete the cookies too + $current = $session; + } + } + + if ($current->isEmpty()) { throw new Exception('No valid session found', 404, Exception::USER_SESSION_NOT_FOUND); } diff --git a/app/controllers/api/teams.php b/app/controllers/api/teams.php index 22705d3bf8..ce208b6deb 100644 --- a/app/controllers/api/teams.php +++ b/app/controllers/api/teams.php @@ -338,7 +338,7 @@ App::post('/v1/teams/:teamId/memberships') 'reset' => false, 'name' => $name, 'prefs' => new \stdClass(), - 'sessions' => [], + 'sessions' => null, 'tokens' => [], 'memberships' => [], 'search' => implode(' ', [$userId, $email, $name]), @@ -728,11 +728,10 @@ App::patch('/v1/teams/:teamId/memberships/:membershipId/status') ->setAttribute('$write', ['user:'.$user->getId()]) ); - $user->setAttribute('sessions', $session, Document::SET_TYPE_APPEND); + $dbForProject->deleteCachedDocument('users', $user->getId()); Authorization::setRole('user:'.$userId); - $user = $dbForProject->updateDocument('users', $user->getId(), $user); $membership = $dbForProject->updateDocument('memberships', $membership->getId(), $membership); $team = Authorization::skip(fn() => $dbForProject->updateDocument('teams', $team->getId(), $team->setAttribute('total', $team->getAttribute('total', 0) + 1))); diff --git a/app/workers/deletes.php b/app/workers/deletes.php index b8a44ebb82..007b9c2933 100644 --- a/app/workers/deletes.php +++ b/app/workers/deletes.php @@ -207,15 +207,13 @@ class DeletesV1 extends Worker */ $userId = $document->getId(); - $user = $this->getProjectDB($projectId)->getDocument('users', $userId); // Delete all sessions of this user from the sessions table and update the sessions field of the user record $this->deleteByGroup('sessions', [ new Query('userId', Query::TYPE_EQUAL, [$userId]) ], $this->getProjectDB($projectId)); - - $user->setAttribute('sessions', []); - $updated = $this->getProjectDB($projectId)->updateDocument('users', $userId, $user); + + $this->getProjectDB($projectId)->deleteCachedDocument('users', $userId); // Delete Memberships and decrement team membership counts $this->deleteByGroup('memberships', [ diff --git a/tests/e2e/Services/Teams/TeamsBaseServer.php b/tests/e2e/Services/Teams/TeamsBaseServer.php index 41dcc6c84c..5db4b628f6 100644 --- a/tests/e2e/Services/Teams/TeamsBaseServer.php +++ b/tests/e2e/Services/Teams/TeamsBaseServer.php @@ -204,7 +204,7 @@ trait TeamsBaseServer $this->assertEquals(1, $response['body']['total']); $this->assertIsInt($response['body']['total']); $this->assertIsInt($response['body']['dateCreated']); - + /** Delete User */ $user = $this->client->call(Client::METHOD_DELETE, '/users/' . $userUid, array_merge([