From 8bc484dfca8d0721c668ec398e1021ad4dbddbf7 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Fri, 19 Feb 2021 14:59:36 +0100 Subject: [PATCH] fix leftovers --- app/config/collections.php | 2 +- app/controllers/api/account.php | 22 +++++++++++-------- app/controllers/api/teams.php | 1 + app/controllers/api/users.php | 18 +++++++++------ app/workers/deletes.php | 10 ++++++++- .../Utopia/Response/Model/Session.php | 18 +++++++++++++++ 6 files changed, 53 insertions(+), 18 deletions(-) diff --git a/app/config/collections.php b/app/config/collections.php index 92e687cd17..d88bb849aa 100644 --- a/app/config/collections.php +++ b/app/config/collections.php @@ -307,7 +307,7 @@ $collections = [ '$collection' => Database::SYSTEM_COLLECTION_COLLECTIONS, '$id' => Database::SYSTEM_COLLECTION_SESSIONS, '$permissions' => ['read' => ['*']], - 'name' => 'Sessions', + 'name' => 'Session', 'structure' => true, 'rules' => [ [ diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index a81e38288a..d37e6f0476 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -1054,7 +1054,9 @@ App::delete('/v1/account/sessions/:sessionId') $sessions = $user->getAttribute('sessions', []); - foreach ($sessions as $session) { /** @var Document $session */ + foreach ($sessions as $session) { + /** @var Document $session */ + if (($sessionId == $session->getId())) { if (!$projectDB->deleteDocument($session->getId())) { throw new Exception('Failed to remove token from DB', 500); @@ -1121,10 +1123,12 @@ App::delete('/v1/account/sessions') /** @var Appwrite\Event\Event $events */ $protocol = $request->getProtocol(); - $tokens = $user->getAttribute('tokens', []); + $sessions = $user->getAttribute('sessions', []); - foreach ($tokens as $token) { /* @var $token Document */ - if (!$projectDB->deleteDocument($token->getId())) { + foreach ($sessions as $session) { + /** @var Document $session */ + + if (!$projectDB->deleteDocument($session->getId())) { throw new Exception('Failed to remove token from DB', 500); } @@ -1140,10 +1144,10 @@ App::delete('/v1/account/sessions') ; } - $token->setAttribute('current', false); + $session->setAttribute('current', false); - if ($token->getAttribute('secret') == Auth::hash(Auth::$secret)) { // If current session delete the cookies too - $token->setAttribute('current', true); + if ($session->getAttribute('secret') == Auth::hash(Auth::$secret)) { // If current session delete the cookies too + $session->setAttribute('current', true); $response ->addCookie(Auth::$cookieName.'_legacy', '', \time() - 3600, '/', Config::getParam('cookieDomain'), ('https' == $protocol), true, null) ->addCookie(Auth::$cookieName, '', \time() - 3600, '/', Config::getParam('cookieDomain'), ('https' == $protocol), true, Config::getParam('cookieSamesite')) @@ -1153,8 +1157,8 @@ App::delete('/v1/account/sessions') $events ->setParam('payload', $response->output(new Document([ - 'sum' => count($tokens), - 'sessions' => $tokens + 'sum' => count($sessions), + 'sessions' => $sessions ]), Response::MODEL_SESSION_LIST)) ; diff --git a/app/controllers/api/teams.php b/app/controllers/api/teams.php index 775d9b47a3..f75f072e76 100644 --- a/app/controllers/api/teams.php +++ b/app/controllers/api/teams.php @@ -327,6 +327,7 @@ App::post('/v1/teams/:teamId/memberships') 'registration' => \time(), 'reset' => false, 'name' => $name, + 'sessions' => [], 'tokens' => [], ], ['email' => $email]); } catch (Duplicate $th) { diff --git a/app/controllers/api/users.php b/app/controllers/api/users.php index b8e79ccb95..4766d0a5f9 100644 --- a/app/controllers/api/users.php +++ b/app/controllers/api/users.php @@ -430,11 +430,13 @@ App::delete('/v1/users/:userId/sessions/:sessionId') throw new Exception('User not found', 404); } - $tokens = $user->getAttribute('tokens', []); + $sessions = $user->getAttribute('sessions', []); - foreach ($tokens as $token) { /* @var $token Document */ - if ($sessionId == $token->getId()) { - if (!$projectDB->deleteDocument($token->getId())) { + foreach ($sessions as $session) { + /** @var Document $session */ + + if ($sessionId == $session->getId()) { + if (!$projectDB->deleteDocument($session->getId())) { throw new Exception('Failed to remove token from DB', 500); } @@ -474,10 +476,12 @@ App::delete('/v1/users/:userId/sessions') throw new Exception('User not found', 404); } - $tokens = $user->getAttribute('tokens', []); + $sessions = $user->getAttribute('sessions', []); - foreach ($tokens as $token) { /* @var $token Document */ - if (!$projectDB->deleteDocument($token->getId())) { + foreach ($sessions as $session) { + /** @var Document $session */ + + if (!$projectDB->deleteDocument($session->getId())) { throw new Exception('Failed to remove token from DB', 500); } } diff --git a/app/workers/deletes.php b/app/workers/deletes.php index 426dcf1ed6..cda60b78e5 100644 --- a/app/workers/deletes.php +++ b/app/workers/deletes.php @@ -112,13 +112,21 @@ class DeletesV1 protected function deleteUser(Document $document, $projectId) { $tokens = $document->getAttribute('tokens', []); - + foreach ($tokens as $token) { if (!$this->getProjectDB($projectId)->deleteDocument($token->getId())) { throw new Exception('Failed to remove token from DB'); } } + $sessions = $document->getAttribute('sessions', []); + + foreach ($sessions as $session) { + if (!$this->getProjectDB($projectId)->deleteDocument($session->getId())) { + throw new Exception('Failed to remove session from DB'); + } + } + // Delete Memberships $this->deleteByGroup([ '$collection='.Database::SYSTEM_COLLECTION_MEMBERSHIPS, diff --git a/src/Appwrite/Utopia/Response/Model/Session.php b/src/Appwrite/Utopia/Response/Model/Session.php index 4bc23ab79a..7db7fdee36 100644 --- a/src/Appwrite/Utopia/Response/Model/Session.php +++ b/src/Appwrite/Utopia/Response/Model/Session.php @@ -28,6 +28,24 @@ class Session extends Model 'default' => 0, 'example' => 1592981250, ]) + ->addRule('provider', [ + 'type' => self::TYPE_STRING, + 'description' => 'Session Provider.', + 'default' => 0, + 'example' => 1592981250, + ]) + ->addRule('providerUid', [ + 'type' => self::TYPE_STRING, + 'description' => 'Session Provider User ID.', + 'default' => 0, + 'example' => 1592981250, + ]) + ->addRule('providerToken', [ + 'type' => self::TYPE_STRING, + 'description' => 'Session Provider Token.', + 'default' => 0, + 'example' => 1592981250, + ]) ->addRule('ip', [ 'type' => self::TYPE_STRING, 'description' => 'IP in use when the session was created.',