| 1 | use sqlx::PgPool; |
| 2 | use utopia_core::models::{Role, Workspace}; |
| 3 | use utopia_core::{AppError, AppResult}; |
| 4 | use uuid::Uuid; |
| 5 | |
| 6 | pub async fn list_for_user(pool: &PgPool, user_id: Uuid) -> AppResult<Vec<Workspace>> { |
| 7 | let rows = sqlx::query_as( |
| 8 | "SELECT w.* FROM workspaces w |
| 9 | JOIN memberships m ON m.workspace_id = w.id |
| 10 | WHERE m.user_id = $1 |
| 11 | ORDER BY w.created_at", |
| 12 | ) |
| 13 | .bind(user_id) |
| 14 | .fetch_all(pool) |
| 15 | .await?; |
| 16 | Ok(rows) |
| 17 | } |
| 18 | |
| 19 | /// 在用户所属组织内新建工作区,创建者为 owner。 |
| 20 | pub async fn create( |
| 21 | pool: &PgPool, |
| 22 | org_id: Uuid, |
| 23 | user_id: Uuid, |
| 24 | name: &str, |
| 25 | ) -> AppResult<Workspace> { |
| 26 | let mut tx = pool.begin().await?; |
| 27 | let ws: Workspace = |
| 28 | sqlx::query_as("INSERT INTO workspaces (id, org_id, name) VALUES ($1, $2, $3) RETURNING *") |
| 29 | .bind(Uuid::now_v7()) |
| 30 | .bind(org_id) |
| 31 | .bind(name) |
| 32 | .fetch_one(&mut *tx) |
| 33 | .await?; |
| 34 | sqlx::query("INSERT INTO memberships (user_id, workspace_id, role) VALUES ($1, $2, $3)") |
| 35 | .bind(user_id) |
| 36 | .bind(ws.id) |
| 37 | .bind(Role::Owner.as_str()) |
| 38 | .execute(&mut *tx) |
| 39 | .await?; |
| 40 | tx.commit().await?; |
| 41 | Ok(ws) |
| 42 | } |
| 43 | |
| 44 | pub async fn get(pool: &PgPool, id: Uuid) -> AppResult<Workspace> { |
| 45 | sqlx::query_as("SELECT * FROM workspaces WHERE id = $1") |
| 46 | .bind(id) |
| 47 | .fetch_optional(pool) |
| 48 | .await? |
| 49 | .ok_or(AppError::NotFound) |
| 50 | } |
| 51 | |
| 52 | /// 取用户在工作区中的角色;非成员返回 None。 |
| 53 | pub async fn role_of(pool: &PgPool, user_id: Uuid, workspace_id: Uuid) -> AppResult<Option<Role>> { |
| 54 | let role: Option<(String,)> = |
| 55 | sqlx::query_as("SELECT role FROM memberships WHERE user_id = $1 AND workspace_id = $2") |
| 56 | .bind(user_id) |
| 57 | .bind(workspace_id) |
| 58 | .fetch_optional(pool) |
| 59 | .await?; |
| 60 | Ok(role.and_then(|(r,)| Role::parse(&r))) |
| 61 | } |
| 62 | |
| 63 | /// 权限检查:非成员 → NotFound(不泄露存在性);角色不足 → Forbidden。 |
| 64 | pub async fn require_role( |
| 65 | pool: &PgPool, |
| 66 | user_id: Uuid, |
| 67 | workspace_id: Uuid, |
| 68 | min: Role, |
| 69 | ) -> AppResult<Role> { |
| 70 | match role_of(pool, user_id, workspace_id).await? { |
| 71 | None => Err(AppError::NotFound), |
| 72 | Some(r) if r >= min => Ok(r), |
| 73 | Some(_) => Err(AppError::Forbidden), |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | pub async fn rename(pool: &PgPool, id: Uuid, name: &str) -> AppResult<Workspace> { |
| 78 | sqlx::query_as("UPDATE workspaces SET name = $2 WHERE id = $1 RETURNING *") |
| 79 | .bind(id) |
| 80 | .bind(name) |
| 81 | .fetch_optional(pool) |
| 82 | .await? |
| 83 | .ok_or(AppError::NotFound) |
| 84 | } |
| 85 | |
| 86 | pub async fn delete(pool: &PgPool, id: Uuid) -> AppResult<()> { |
| 87 | let res = sqlx::query("DELETE FROM workspaces WHERE id = $1") |
| 88 | .bind(id) |
| 89 | .execute(pool) |
| 90 | .await?; |
| 91 | if res.rows_affected() == 0 { |
| 92 | return Err(AppError::NotFound); |
| 93 | } |
| 94 | Ok(()) |
| 95 | } |