| 1 | use sqlx::PgPool; |
| 2 | use utopia_core::models::{MemberView, OrgUser, Role}; |
| 3 | use utopia_core::{AppError, AppResult}; |
| 4 | use uuid::Uuid; |
| 5 | |
| 6 | pub async fn list(pool: &PgPool, workspace_id: Uuid) -> AppResult<Vec<MemberView>> { |
| 7 | let rows = sqlx::query_as( |
| 8 | "SELECT m.user_id, u.email, u.display_name, m.role, u.is_admin |
| 9 | FROM memberships m JOIN users u ON u.id = m.user_id |
| 10 | -- 停用的人不再出现在成员列表里(见 `users.deactivated_at`)。成员关系那一行留着—— |
| 11 | -- 恢复账号时不必重新加回每一个工作区 |
| 12 | WHERE m.workspace_id = $1 AND u.deactivated_at IS NULL |
| 13 | ORDER BY m.created_at", |
| 14 | ) |
| 15 | .bind(workspace_id) |
| 16 | .fetch_all(pool) |
| 17 | .await?; |
| 18 | Ok(rows) |
| 19 | } |
| 20 | |
| 21 | /// 部署内全部用户(添加成员的选人器)。 |
| 22 | pub async fn org_users(pool: &PgPool, org_id: Uuid) -> AppResult<Vec<OrgUser>> { |
| 23 | let rows = sqlx::query_as( |
| 24 | "SELECT id, email, display_name, is_admin FROM users |
| 25 | WHERE org_id = $1 AND deactivated_at IS NULL ORDER BY created_at", |
| 26 | ) |
| 27 | .bind(org_id) |
| 28 | .fetch_all(pool) |
| 29 | .await?; |
| 30 | Ok(rows) |
| 31 | } |
| 32 | |
| 33 | pub async fn owner_count(pool: &PgPool, workspace_id: Uuid) -> AppResult<i64> { |
| 34 | let (n,): (i64,) = sqlx::query_as( |
| 35 | "SELECT count(*) FROM memberships WHERE workspace_id = $1 AND role = 'owner'", |
| 36 | ) |
| 37 | .bind(workspace_id) |
| 38 | .fetch_one(pool) |
| 39 | .await?; |
| 40 | Ok(n) |
| 41 | } |
| 42 | |
| 43 | pub async fn current_role( |
| 44 | pool: &PgPool, |
| 45 | workspace_id: Uuid, |
| 46 | user_id: Uuid, |
| 47 | ) -> AppResult<Option<Role>> { |
| 48 | crate::workspaces::role_of(pool, user_id, workspace_id).await |
| 49 | } |
| 50 | |
| 51 | /// 设置/添加成员角色(upsert)。防呆逻辑在 API 层。 |
| 52 | pub async fn set_role( |
| 53 | pool: &PgPool, |
| 54 | workspace_id: Uuid, |
| 55 | user_id: Uuid, |
| 56 | role: Role, |
| 57 | ) -> AppResult<()> { |
| 58 | // 目标用户必须存在于本组织,**而且在职**——否则能把一个已停用的账号 |
| 59 | // 加进工作区,它在成员列表里又看不见(那条查询过滤了停用的), |
| 60 | // 于是成了一条谁也发现不了的授权 |
| 61 | let exists: Option<(Uuid,)> = |
| 62 | sqlx::query_as("SELECT id FROM users WHERE id = $1 AND deactivated_at IS NULL") |
| 63 | .bind(user_id) |
| 64 | .fetch_optional(pool) |
| 65 | .await?; |
| 66 | if exists.is_none() { |
| 67 | return Err(AppError::NotFound); |
| 68 | } |
| 69 | sqlx::query( |
| 70 | "INSERT INTO memberships (user_id, workspace_id, role) VALUES ($1, $2, $3) |
| 71 | ON CONFLICT (user_id, workspace_id) DO UPDATE SET role = EXCLUDED.role", |
| 72 | ) |
| 73 | .bind(user_id) |
| 74 | .bind(workspace_id) |
| 75 | .bind(role.as_str()) |
| 76 | .execute(pool) |
| 77 | .await?; |
| 78 | Ok(()) |
| 79 | } |
| 80 | |
| 81 | pub async fn remove(pool: &PgPool, workspace_id: Uuid, user_id: Uuid) -> AppResult<()> { |
| 82 | let res = sqlx::query("DELETE FROM memberships WHERE workspace_id = $1 AND user_id = $2") |
| 83 | .bind(workspace_id) |
| 84 | .bind(user_id) |
| 85 | .execute(pool) |
| 86 | .await?; |
| 87 | if res.rows_affected() == 0 { |
| 88 | return Err(AppError::NotFound); |
| 89 | } |
| 90 | Ok(()) |
| 91 | } |
| 92 | |
| 93 | /// 已停用的账号。**没有这一条,恢复就够不着**——停用的人从所有列表里消失, |
| 94 | /// 管理员拿不到他的 id,而恢复接口要的正是那个 id。 |
| 95 | /// |
| 96 | /// 与 [`org_users`] 分成两个查询而不是加一个「含停用」的开关:读的人不一样 |
| 97 | /// (那个喂选人器,这个喂管理页的一小块),而一个布尔参数会让两处的调用点 |
| 98 | /// 都得先想一下自己要哪一种。 |
| 99 | pub async fn deactivated_users(pool: &PgPool, org_id: Uuid) -> AppResult<Vec<OrgUser>> { |
| 100 | Ok(sqlx::query_as( |
| 101 | "SELECT id, email, display_name, is_admin FROM users |
| 102 | WHERE org_id = $1 AND deactivated_at IS NOT NULL |
| 103 | ORDER BY deactivated_at DESC", |
| 104 | ) |
| 105 | .bind(org_id) |
| 106 | .fetch_all(pool) |
| 107 | .await?) |
| 108 | } |