2025-09-14 16:19:44 -04:00
|
|
|
use derive_builder::Builder;
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
2025-10-22 00:43:53 -07:00
|
|
|
use sqlx::query_as;
|
2025-09-14 16:19:44 -04:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
|
|
use crate::client::AppDbClient;
|
|
|
|
|
|
|
|
|
|
/// Assigns an access control permission on a workspace to a user. These are
|
|
|
|
|
/// derived from the permission grants of the workspace's backing database.
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
2025-10-22 00:43:53 -07:00
|
|
|
pub struct WorkspaceMembership {
|
2025-09-14 16:19:44 -04:00
|
|
|
/// Primary key (defaults to UUIDv7).
|
|
|
|
|
pub id: Uuid,
|
|
|
|
|
|
|
|
|
|
/// Workspace to which the permission belongs.
|
|
|
|
|
pub workspace_id: Uuid,
|
|
|
|
|
|
|
|
|
|
/// **Synthesized field** generated by joining to the `workspaces` table.
|
2025-11-01 00:17:07 +00:00
|
|
|
pub workspace_display_name: String,
|
2025-09-14 16:19:44 -04:00
|
|
|
|
|
|
|
|
/// User to which the permission belongs.
|
|
|
|
|
pub user_id: Uuid,
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-22 00:43:53 -07:00
|
|
|
impl WorkspaceMembership {
|
2025-09-14 16:19:44 -04:00
|
|
|
/// Construct a single-field query to fetch workspace permissions assigned
|
|
|
|
|
/// to a user.
|
|
|
|
|
pub fn belonging_to_user(id: Uuid) -> BelongingToUserQuery {
|
|
|
|
|
BelongingToUserQuery { id }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Build an insert statement to create a new object.
|
|
|
|
|
pub fn insert() -> InsertBuilder {
|
|
|
|
|
InsertBuilder::default()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
pub struct BelongingToUserQuery {
|
|
|
|
|
id: Uuid,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl BelongingToUserQuery {
|
|
|
|
|
pub async fn fetch_all(
|
|
|
|
|
self,
|
|
|
|
|
app_db: &mut AppDbClient,
|
2025-10-22 00:43:53 -07:00
|
|
|
) -> Result<Vec<WorkspaceMembership>, sqlx::Error> {
|
2025-09-14 16:19:44 -04:00
|
|
|
query_as!(
|
2025-10-22 00:43:53 -07:00
|
|
|
WorkspaceMembership,
|
2025-09-14 16:19:44 -04:00
|
|
|
r#"
|
|
|
|
|
select
|
|
|
|
|
p.id as id,
|
|
|
|
|
p.workspace_id as workspace_id,
|
|
|
|
|
p.user_id as user_id,
|
2025-11-01 00:17:07 +00:00
|
|
|
w.display_name as workspace_display_name
|
2025-10-22 00:43:53 -07:00
|
|
|
from workspace_memberships as p
|
2025-09-14 16:19:44 -04:00
|
|
|
inner join workspaces as w
|
|
|
|
|
on w.id = p.workspace_id
|
|
|
|
|
where p.user_id = $1
|
|
|
|
|
"#,
|
|
|
|
|
self.id,
|
|
|
|
|
)
|
|
|
|
|
.fetch_all(app_db.get_conn())
|
|
|
|
|
.await
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Builder, Clone, Debug)]
|
|
|
|
|
pub struct Insert {
|
|
|
|
|
workspace_id: Uuid,
|
|
|
|
|
user_id: Uuid,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Insert {
|
2025-10-22 00:43:53 -07:00
|
|
|
pub async fn execute(
|
|
|
|
|
self,
|
|
|
|
|
app_db: &mut AppDbClient,
|
|
|
|
|
) -> Result<WorkspaceMembership, sqlx::Error> {
|
2025-09-14 16:19:44 -04:00
|
|
|
query_as!(
|
2025-10-22 00:43:53 -07:00
|
|
|
WorkspaceMembership,
|
2025-09-14 16:19:44 -04:00
|
|
|
r#"
|
|
|
|
|
with p as (
|
2025-10-22 00:43:53 -07:00
|
|
|
insert into workspace_memberships (workspace_id, user_id) values ($1, $2)
|
2025-09-14 16:19:44 -04:00
|
|
|
returning
|
|
|
|
|
id,
|
|
|
|
|
workspace_id,
|
2025-10-22 00:43:53 -07:00
|
|
|
user_id
|
2025-09-14 16:19:44 -04:00
|
|
|
)
|
|
|
|
|
select
|
|
|
|
|
p.id as id,
|
|
|
|
|
p.workspace_id as workspace_id,
|
|
|
|
|
p.user_id as user_id,
|
2025-11-01 00:17:07 +00:00
|
|
|
w.display_name as workspace_display_name
|
|
|
|
|
from p inner join workspaces as w
|
|
|
|
|
on w.id = p.workspace_id
|
2025-09-14 16:19:44 -04:00
|
|
|
"#,
|
|
|
|
|
self.workspace_id,
|
|
|
|
|
self.user_id,
|
|
|
|
|
)
|
|
|
|
|
.fetch_one(app_db.get_conn())
|
|
|
|
|
.await
|
|
|
|
|
}
|
|
|
|
|
}
|