2025-09-14 16:19:44 -04:00
|
|
|
use derive_builder::Builder;
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
2025-12-10 22:01:47 +00:00
|
|
|
use sqlx::{query, query_as};
|
2025-09-14 16:19:44 -04:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
2025-12-10 22:01:47 +00:00
|
|
|
use crate::{
|
|
|
|
|
client::AppDbClient,
|
|
|
|
|
errors::{QueryError, QueryResult},
|
|
|
|
|
};
|
2025-09-14 16:19:44 -04:00
|
|
|
|
|
|
|
|
/// 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 }
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-10 22:01:47 +00:00
|
|
|
/// Build an upsert statement to create a new object. If the new object is a
|
|
|
|
|
/// duplicate, no update is performed.
|
|
|
|
|
pub fn upsert() -> UpsertBuilder {
|
|
|
|
|
UpsertBuilder::default()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Build a delete statement to remove the matching record if it exists.
|
|
|
|
|
pub fn delete() -> DeleteBuilder {
|
|
|
|
|
DeleteBuilder::default()
|
2025-09-14 16:19:44 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[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)]
|
2025-12-10 22:01:47 +00:00
|
|
|
#[builder(build_fn(error = "QueryError"), vis = "pub", pattern = "owned")]
|
|
|
|
|
struct Upsert {
|
2025-09-14 16:19:44 -04:00
|
|
|
workspace_id: Uuid,
|
|
|
|
|
user_id: Uuid,
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-10 22:01:47 +00:00
|
|
|
impl UpsertBuilder {
|
|
|
|
|
pub async fn execute(self, app_db: &mut AppDbClient) -> QueryResult<()> {
|
|
|
|
|
// To circumvent performance and complexity concerns, this method does not
|
|
|
|
|
// return the upserted record. Refer to:
|
|
|
|
|
// https://stackoverflow.com/a/42217872
|
|
|
|
|
|
|
|
|
|
let spec = self.build()?;
|
|
|
|
|
query!(
|
2025-09-14 16:19:44 -04:00
|
|
|
r#"
|
2025-12-10 22:01:47 +00:00
|
|
|
insert into workspace_memberships (workspace_id, user_id) values ($1, $2)
|
|
|
|
|
on conflict (workspace_id, user_id) do nothing
|
2025-09-14 16:19:44 -04:00
|
|
|
"#,
|
2025-12-10 22:01:47 +00:00
|
|
|
spec.workspace_id,
|
|
|
|
|
spec.user_id,
|
2025-09-14 16:19:44 -04:00
|
|
|
)
|
2025-12-10 22:01:47 +00:00
|
|
|
.execute(app_db.get_conn())
|
|
|
|
|
.await?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Builder, Clone, Debug)]
|
|
|
|
|
#[builder(build_fn(error = "QueryError"), vis = "pub", pattern = "owned")]
|
|
|
|
|
pub struct Delete {
|
|
|
|
|
workspace_id: Uuid,
|
|
|
|
|
user_id: Uuid,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl DeleteBuilder {
|
|
|
|
|
pub async fn execute(self, app_db: &mut AppDbClient) -> QueryResult<()> {
|
|
|
|
|
let spec = self.build()?;
|
|
|
|
|
query!(
|
|
|
|
|
r#"
|
|
|
|
|
delete from workspace_memberships
|
|
|
|
|
where workspace_id = $1 and user_id = $2
|
|
|
|
|
"#,
|
|
|
|
|
spec.workspace_id,
|
|
|
|
|
spec.user_id,
|
|
|
|
|
)
|
|
|
|
|
.execute(app_db.get_conn())
|
|
|
|
|
.await?;
|
|
|
|
|
Ok(())
|
2025-09-14 16:19:44 -04:00
|
|
|
}
|
|
|
|
|
}
|