From 5fc751a9dd83e2fd8ce11c76b811471dbead9f41 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sun, 6 Feb 2022 19:17:19 +0400 Subject: [PATCH] feat: update error codes in the teams API --- app/config/errors.php | 27 ++++++++++++++++++++++++++- app/controllers/api/teams.php | 18 +++++++++--------- src/Appwrite/Extend/Exception.php | 9 +++++++-- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/app/config/errors.php b/app/config/errors.php index 6c964e3c3..5b90bfa17 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -127,6 +127,11 @@ return [ 'description' => 'The current user session could not be found.', 'statusCode' => 404, ], + Exception::USER_UNAUTHORIZED => [ + 'name' => Exception::USER_UNAUTHORIZED, + 'description' => 'The current user is not authorized to perform the requested action.', + 'statusCode' => 401, + ], Exception::USER_AUTH_METHOD_UNSUPPORTED => [ 'name' => Exception::USER_AUTH_METHOD_UNSUPPORTED, 'description' => 'The requested authentication method is either disabled or unsupported.', @@ -176,14 +181,34 @@ return [ 'description' => 'Team with the requested ID could not be found.', 'statusCode' => 404, ], + Exception::TEAM_DELETION_FAILED => [ + 'name' => Exception::TEAM_DELETION_FAILED, + 'description' => 'Failed to delete team from the database.', + 'statusCode' => 500, + ], + Exception::TEAM_INVITATION_ALREADY_EXISTS => [ + 'name' => Exception::TEAM_INVITATION_ALREADY_EXISTS, + 'description' => 'The current user already has an invitation to this team.', + 'statusCode' => 409, + ], + Exception::TEAM_INVITE_NOT_FOUND => [ + 'name' => Exception::TEAM_INVITE_NOT_FOUND, + 'description' => 'The requested invitation could not be found.', + 'statusCode' => 409, + ], + - /** Membership */ Exception::MEMBERSHIP_NOT_FOUND => [ 'name' => Exception::MEMBERSHIP_NOT_FOUND, 'description' => 'Membership with the requested ID could not be found.', 'statusCode' => 404, ], + Exception::MEMBERSHIP_DELETION_FAILED => [ + 'name' => Exception::MEMBERSHIP_DELETION_FAILED, + 'description' => 'Failed to delete membership from the database.', + 'statusCode' => 500, + ], /** Avatars */ Exception::AVATAR_SET_NOT_FOUND => [ diff --git a/app/controllers/api/teams.php b/app/controllers/api/teams.php index 823b9881a..bc38a28bd 100644 --- a/app/controllers/api/teams.php +++ b/app/controllers/api/teams.php @@ -232,12 +232,12 @@ App::delete('/v1/teams/:teamId') // TODO delete all members individually from the user object foreach ($memberships as $membership) { if (!$dbForProject->deleteDocument('memberships', $membership->getId())) { - throw new Exception('Failed to remove membership for team from DB', 500); + throw new Exception('Failed to remove membership for team from DB', 500, Exception::MEMBERSHIP_DELETION_FAILED); } } if (!$dbForProject->deleteDocument('teams', $teamId)) { - throw new Exception('Failed to remove team from DB', 500); + throw new Exception('Failed to remove team from DB', 500, Exception::TEAM_DELETION_FAILED); } $deletes @@ -348,7 +348,7 @@ App::post('/v1/teams/:teamId/memberships') $isOwner = Authorization::isRole('team:'.$team->getId().'/owner');; if (!$isOwner && !$isPrivilegedUser && !$isAppUser) { // Not owner, not admin, not app (server) - throw new Exception('User is not allowed to send invitations for this team', 401); + throw new Exception('User is not allowed to send invitations for this team', 401, Exception::USER_UNAUTHORIZED); } $secret = Auth::tokenGenerator(); @@ -370,7 +370,7 @@ App::post('/v1/teams/:teamId/memberships') try { $membership = Authorization::skip(fn() => $dbForProject->createDocument('memberships', $membership)); } catch (Duplicate $th) { - throw new Exception('User has already been invited or is already a member of this team', 409); + throw new Exception('User has already been invited or is already a member of this team', 409, Exception::TEAM_INVITATION_ALREADY_EXISTS); } $team->setAttribute('sum', $team->getAttribute('sum', 0) + 1); $team = Authorization::skip(fn() => $dbForProject->updateDocument('teams', $team->getId(), $team)); @@ -383,7 +383,7 @@ App::post('/v1/teams/:teamId/memberships') try { $membership = $dbForProject->createDocument('memberships', $membership); } catch (Duplicate $th) { - throw new Exception('User has already been invited or is already a member of this team', 409); + throw new Exception('User has already been invited or is already a member of this team', 409, Exception::TEAM_INVITATION_ALREADY_EXISTS); } } @@ -568,7 +568,7 @@ App::patch('/v1/teams/:teamId/memberships/:membershipId') $isOwner = Authorization::isRole('team:'.$team->getId().'/owner');; if (!$isOwner && !$isPrivilegedUser && !$isAppUser) { // Not owner, not admin, not app (server) - throw new Exception('User is not allowed to modify roles', 401); + throw new Exception('User is not allowed to modify roles', 401, Exception::USER_UNAUTHORIZED); } // Update the roles @@ -743,7 +743,7 @@ App::delete('/v1/teams/:teamId/memberships/:membershipId') $membership = $dbForProject->getDocument('memberships', $membershipId); if ($membership->isEmpty()) { - throw new Exception('Invite not found', 404); + throw new Exception('Invite not found', 404, Exception::TEAM_INVITE_NOT_FOUND); } if ($membership->getAttribute('teamId') !== $teamId) { @@ -753,7 +753,7 @@ App::delete('/v1/teams/:teamId/memberships/:membershipId') $user = $dbForProject->getDocument('users', $membership->getAttribute('userId')); if ($user->isEmpty()) { - throw new Exception('User not found', 404); + throw new Exception('User not found', 404, Exception::USER_NOT_FOUND); } $team = $dbForProject->getDocument('teams', $teamId); @@ -765,7 +765,7 @@ App::delete('/v1/teams/:teamId/memberships/:membershipId') try { $dbForProject->deleteDocument('memberships', $membership->getId()); } catch (AuthorizationException $exception) { - throw new Exception('Unauthorized permissions', 401, Exception::UNAUTHORIZED_SCOPE); + throw new Exception('Unauthorized permissions', 401, Exception::USER_UNAUTHORIZED); } catch (\Exception $exception) { throw new Exception('Failed to remove membership from DB', 500); } diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index d0cf5ec65..d102c7a3f 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -40,6 +40,7 @@ class Exception extends \Exception const USER_IP_NOT_WHITELISTED = 'user_ip_not_whitelisted'; const USER_SESSION_ALREADY_EXISTS = 'user_session_already_exists'; const USER_SESSION_NOT_FOUND = 'user_session_not_found'; + const USER_UNAUTHORIZED = 'user_unauthorized'; const USER_ANONYMOUS_CONSOLE_PROHIBITED = 'user_anonymous_console_prohibited'; /** OAuth **/ @@ -52,10 +53,14 @@ class Exception extends \Exception const OAUTH_MISSING_USER_ID = 'oauth_missing_user_id'; /** Teams */ - const TEAM_NOT_FOUND = 'team_not_found'; + const TEAM_NOT_FOUND = 'team_not_found'; + const TEAM_DELETION_FAILED = 'team_deletion_failed'; + const TEAM_INVITATION_ALREADY_EXISTS = 'team_invitation_already_exists'; + const TEAM_INVITE_NOT_FOUND = 'team_invite_not_found'; /** Membership */ - const MEMBERSHIP_NOT_FOUND = 'membership_not_found'; + const MEMBERSHIP_NOT_FOUND = 'membership_not_found'; + const MEMBERSHIP_DELETION_FAILED = 'membership_deletion_failed'; /** Avatars */ const AVATAR_SET_NOT_FOUND = 'avatar_set_not_found';