From ee243dedc6df28a64f15e0b274b7fa96f6428474 Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Sat, 18 Nov 2023 15:07:06 +0100 Subject: [PATCH] fix: leave workspace + instance api --- ...26a853da91d607ff9e57e10d0e5481e4d3848.json | 15 ++++ backend/windmill-api/openapi.yaml | 46 ++++++---- backend/windmill-api/src/users.rs | 24 +++++ backend/windmill-api/src/workspaces.rs | 28 +++++- backend/windmill-worker/src/worker.rs | 1 - .../src/lib/components/UserSettings.svelte | 2 +- .../settings/WorkspaceUserSettings.svelte | 2 +- .../components/sidebar/SidebarContent.svelte | 87 ++++++++++++++++--- .../components/sidebar/WorkspaceMenu.svelte | 30 ++++--- 9 files changed, 187 insertions(+), 48 deletions(-) create mode 100644 backend/.sqlx/query-00be497354f5375e9ccffb998d126a853da91d607ff9e57e10d0e5481e4d3848.json diff --git a/backend/.sqlx/query-00be497354f5375e9ccffb998d126a853da91d607ff9e57e10d0e5481e4d3848.json b/backend/.sqlx/query-00be497354f5375e9ccffb998d126a853da91d607ff9e57e10d0e5481e4d3848.json new file mode 100644 index 0000000000..bc967dc2f6 --- /dev/null +++ b/backend/.sqlx/query-00be497354f5375e9ccffb998d126a853da91d607ff9e57e10d0e5481e4d3848.json @@ -0,0 +1,15 @@ +{ + "db_name": "PostgreSQL", + "query": "DELETE FROM usr WHERE workspace_id = $1 AND email = $2", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Text", + "Text" + ] + }, + "nullable": [] + }, + "hash": "00be497354f5375e9ccffb998d126a853da91d607ff9e57e10d0e5481e4d3848" +} diff --git a/backend/windmill-api/openapi.yaml b/backend/windmill-api/openapi.yaml index a7ed5d8b90..31791d1294 100644 --- a/backend/windmill-api/openapi.yaml +++ b/backend/windmill-api/openapi.yaml @@ -731,6 +731,20 @@ paths: schema: type: string + /users/leave_instance: + post: + summary: leave instance + operationId: leaveInstance + tags: + - user + responses: + "200": + description: status + content: + text/plain: + schema: + type: string + /users/usage: get: summary: get current usage outside of premium workspaces @@ -827,22 +841,6 @@ paths: schema: $ref: "#/components/schemas/User" - /w/{workspace}/users/leave_workspace: - post: - summary: leave workspace - operationId: leaveWorkspace - tags: - - user - parameters: - - $ref: "#/components/parameters/WorkspaceId" - responses: - "200": - description: status - content: - text/plain: - schema: - type: string - /users/accept_invite: post: summary: accept invite to workspace @@ -1051,6 +1049,22 @@ paths: schema: type: string + /w/{workspace}/workspaces/leave: + post: + summary: leave workspace + operationId: leaveWorkspace + tags: + - workspace + parameters: + - $ref: "#/components/parameters/WorkspaceId" + responses: + "200": + description: status + content: + text/plain: + schema: + type: string + /w/{workspace}/users/whois/{username}: get: summary: whois diff --git a/backend/windmill-api/src/users.rs b/backend/windmill-api/src/users.rs index c1c20a931e..7411dbf73d 100644 --- a/backend/windmill-api/src/users.rs +++ b/backend/windmill-api/src/users.rs @@ -94,6 +94,7 @@ pub fn global_service() -> Router { "/tutorial_progress", post(update_tutorial_progress).get(get_tutorial_progress), ) + .route("/leave_instance", post(leave_instance)) // .route("/list_invite_codes", get(list_invite_codes)) // .route("/create_invite_code", post(create_invite_code)) // .route("/signup", post(signup)) @@ -1416,6 +1417,29 @@ async fn add_user_to_workspace<'c>( Ok(tx) } +async fn leave_instance( + Extension(db): Extension, + ApiAuthed { email, username, .. }: ApiAuthed, +) -> Result { + let mut tx = db.begin().await?; + sqlx::query!("DELETE FROM password WHERE email = $1", &email) + .execute(&mut *tx) + .await?; + + audit_log( + &mut *tx, + &username, + "workspaces.leave", + ActionKind::Delete, + "global", + Some(&email), + None, + ) + .await?; + tx.commit().await?; + + Ok(format!("Left instance",)) +} async fn update_workspace_user( ApiAuthed { username, is_admin, .. }: ApiAuthed, Extension(db): Extension, diff --git a/backend/windmill-api/src/workspaces.rs b/backend/windmill-api/src/workspaces.rs index 4924b2c617..2f4200fd2e 100644 --- a/backend/windmill-api/src/workspaces.rs +++ b/backend/windmill-api/src/workspaces.rs @@ -83,7 +83,8 @@ pub fn workspaced_service() -> Router { .route("/edit_copilot_config", post(edit_copilot_config)) .route("/get_copilot_info", get(get_copilot_info) ) .route("/edit_error_handler", post(edit_error_handler)) - .route("/edit_large_file_storage_config", post(edit_large_file_storage_config)); + .route("/edit_large_file_storage_config", post(edit_large_file_storage_config)) + .route("/leave", post(leave_workspace)); #[cfg(feature = "enterprise")] { @@ -1306,6 +1307,31 @@ async fn archive_workspace( Ok(format!("Archived workspace {}", &w_id)) } +async fn leave_workspace( + Extension(db): Extension, + Path(w_id): Path, + ApiAuthed { email, username, .. }: ApiAuthed, +) -> Result { + let mut tx = db.begin().await?; + sqlx::query!("DELETE FROM usr WHERE workspace_id = $1 AND email = $2", &w_id, &email) + .execute(&mut *tx) + .await?; + + audit_log( + &mut *tx, + &username, + "workspaces.leave", + ActionKind::Delete, + &w_id, + Some(&email), + None, + ) + .await?; + tx.commit().await?; + + Ok(format!("Left workspace {}", &w_id)) +} + async fn unarchive_workspace( Extension(db): Extension, Path(w_id): Path, diff --git a/backend/windmill-worker/src/worker.rs b/backend/windmill-worker/src/worker.rs index bc4f81c421..cf4523542e 100644 --- a/backend/windmill-worker/src/worker.rs +++ b/backend/windmill-worker/src/worker.rs @@ -1847,7 +1847,6 @@ async fn spawn_dedicated_worker( mpsc::channel::>(MAX_BUFFERED_DEDICATED_JOBS); let mut killpill_rx = killpill_rx.resubscribe(); let db = db.clone(); - let worker_dir = worker_dir.clone(); let base_internal_url = base_internal_url.to_string(); let worker_name = worker_name.to_string(); let job_completed_tx = job_completed_tx.clone(); diff --git a/frontend/src/lib/components/UserSettings.svelte b/frontend/src/lib/components/UserSettings.svelte index feba7c736c..1692cf2028 100644 --- a/frontend/src/lib/components/UserSettings.svelte +++ b/frontend/src/lib/components/UserSettings.svelte @@ -145,7 +145,7 @@ {/if}
-

Tokens

+

Tokens

+ + {:else} + +
+ {#if subItem.icon} + + {/if} + + {subItem.label} +
+
+ {/if}
{/each} @@ -203,3 +246,19 @@ + + { + leaveWorkspaceModal = false + }} + on:confirmed={() => { + leaveWorkspace() + }} +> +
+ Are you sure you want to leave this workspace? +
+
diff --git a/frontend/src/lib/components/sidebar/WorkspaceMenu.svelte b/frontend/src/lib/components/sidebar/WorkspaceMenu.svelte index 30ec00611b..b9f5c3e374 100644 --- a/frontend/src/lib/components/sidebar/WorkspaceMenu.svelte +++ b/frontend/src/lib/components/sidebar/WorkspaceMenu.svelte @@ -1,5 +1,5 @@