Skip to content

Commit

Permalink
fix remove user from team (#87)
Browse files Browse the repository at this point in the history
* update base style

* small style updates

* control delete modal manually

* add spacing for password copy button

* delete user if not part of any team
  • Loading branch information
amalshaji authored Aug 18, 2024
1 parent 2e1c18f commit b40a6a9
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 31 deletions.
22 changes: 17 additions & 5 deletions admin/apis/v1/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from schemas.user import AddUserToTeamResponseSchema, TeamUserSchemaForTeam
from services import team as team_service
from utils.exception import PermissionDenied

from tortoise.transactions import in_transaction

api = APIRouter(prefix="/team", tags=["team"])

Expand Down Expand Up @@ -58,22 +58,34 @@ async def add_user(
}


@api.delete("/users/{user_id}")
@api.delete("/users/{team_user_id}")
async def remove_user(
user_id: int,
team_user_id: int,
team_user: TeamUser = Depends(security.requires_admin),
):
team_user_to_delete = (
await TeamUser.filter()
.select_related("user")
.get_or_none(id=user_id, team=team_user.team)
.get_or_none(id=team_user_id, team=team_user.team)
)
if team_user_to_delete is None:
raise PermissionDenied("User not found in team")

if team_user_to_delete.user.is_superuser and not team_user.user.is_superuser:
raise PermissionDenied("Only superuser can remove superuser from team")

await TeamUser.filter(id=user_id).delete()
async with in_transaction() as connection:
await TeamUser.filter(id=team_user_id).using_db(connection).delete()

if (
not await TeamUser.filter(user=team_user_to_delete.user)
.using_db(connection)
.exists()
):
await (
User.filter(id=team_user_to_delete.user.id)
.using_db(connection)
.delete()
)

return {"status": "ok"}
3 changes: 2 additions & 1 deletion admin/tests/api_tests/test_team.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from models.user import TeamUser
from models.user import TeamUser, User
from tests import TestClient
from tortoise.contrib import test

Expand Down Expand Up @@ -39,6 +39,7 @@ async def test_remove_team_user(self):
)
assert resp.status_code == 200
assert await TeamUser.filter(id=team_user_to_delete.id).first() is None
assert await User.filter(id=team_user_to_delete.user.id).first() is None

async def test_remove_superuserteam_user_by_admin_user(self):
team_user_to_delete = await TeamUserFactory.create(
Expand Down
49 changes: 30 additions & 19 deletions admin/web/src/app.pcss
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,40 @@
@layer base {
:root {
--background: 0 0% 100%;
--foreground: 0 0% 3.9%;
--foreground: 222.2 84% 4.9%;
--card: 0 0% 100%;
--card-foreground: 0 0% 3.9%;
--card-foreground: 222.2 84% 4.9%;
--popover: 0 0% 100%;
--popover-foreground: 0 0% 3.9%;
--primary: 0 0% 9%;
--primary-foreground: 0 0% 98%;
--secondary: 0 0% 96.1%;
--secondary-foreground: 0 0% 9%;
--muted: 0 0% 96.1%;
--muted-foreground: 0 0% 45.1%;
--accent: 0 0% 96.1%;
--accent-foreground: 0 0% 9%;
--destructive: 0 72.22% 50.59%;
--destructive-foreground: 0 0% 98%;
--border: 0 0% 89.8%;
--input: 0 0% 89.8%;
--ring: 0 0% 3.9%;
--radius: 0.3rem;
--popover-foreground: 222.2 84% 4.9%;
--primary: 221.2 83.2% 53.3%;
--primary-foreground: 210 40% 98%;
--secondary: 210 40% 96.1%;
--secondary-foreground: 222.2 47.4% 11.2%;
--muted: 210 40% 96.1%;
--muted-foreground: 215.4 16.3% 46.9%;
--accent: 210 40% 96.1%;
--accent-foreground: 222.2 47.4% 11.2%;
--destructive: 0 84.2% 60.2%;
--destructive-foreground: 210 40% 98%;
--border: 214.3 31.8% 91.4%;
--input: 214.3 31.8% 91.4%;
--ring: 221.2 83.2% 53.3%;
--radius: 0.75rem;
--chart-1: 12 76% 61%;
--chart-2: 173 58% 39%;
--chart-3: 197 37% 24%;
--chart-4: 43 74% 66%;
--chart-5: 27 87% 67%;
}

.dark {
--background: 222.2 84% 4.9%;
--foreground: 210 40% 98%;
--card: 222.2 84% 4.9%;
--card-foreground: 210 40% 98%;
--popover: 222.2 84% 4.9%;
--popover-foreground: 210 40% 98%;
--primary: 210 40% 98%;
--primary: 217.2 91.2% 59.8%;
--primary-foreground: 222.2 47.4% 11.2%;
--secondary: 217.2 32.6% 17.5%;
--secondary-foreground: 210 40% 98%;
Expand All @@ -44,6 +50,11 @@
--destructive-foreground: 210 40% 98%;
--border: 217.2 32.6% 17.5%;
--input: 217.2 32.6% 17.5%;
--ring: 212.7 26.8% 83.9%;
--ring: 224.3 76.3% 48%;
--chart-1: 220 70% 50%;
--chart-2: 160 60% 45%;
--chart-3: 30 80% 55%;
--chart-4: 280 65% 60%;
--chart-5: 340 75% 55%;
}
}
4 changes: 2 additions & 2 deletions admin/web/src/lib/components/ConnectionType.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</script>

{#if type === "http"}
<Badge variant="outline" class="border-blue-300 font-normal">HTTP</Badge>
<Badge variant="secondary" class="border-blue-300 font-normal">HTTP</Badge>
{:else}
<Badge variant="outline" class="border-green-300 font-normal">TCP</Badge>
<Badge variant="secondary" class="border-green-300 font-normal">TCP</Badge>
{/if}
6 changes: 4 additions & 2 deletions admin/web/src/lib/components/users/delete.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { toast } from "svelte-sonner";
export let user: TeamUser;
let deleteOpen = false;
const team = getContext("team") as string;
Expand All @@ -25,6 +26,7 @@
if (response.ok) {
users.update((users) => users.filter((u) => u.id !== user.id));
isLoading = false;
deleteOpen = false;
toast.success("User removed from team");
}
} catch (err) {
Expand All @@ -40,8 +42,8 @@
);
</script>

<AlertDialog.Root>
<AlertDialog.Trigger asChild let:builder>
<AlertDialog.Root bind:open={deleteOpen}>
<AlertDialog.Trigger let:builder>
<Button
builders={[builder]}
variant="ghost"
Expand Down
2 changes: 1 addition & 1 deletion admin/web/src/lib/components/users/invite-user.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
<AlertDialog.Description>
<div class="mt-4 space-y-4">
<div class="sm:col-span-3 space-y-2">
<div class="flex items-center">
<div class="flex items-center space-x-1">
<Input
readonly
class="text-black"
Expand Down
2 changes: 1 addition & 1 deletion admin/web/src/lib/components/users/user-email.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<span>{email}</span>
<span
>{#if is_superuser}
<Badge class="rounded-full font-light">superuser</Badge>
<Badge>Superuser</Badge>
{/if}
</span>
</p>

0 comments on commit b40a6a9

Please sign in to comment.