Check account membership on DB level.

This commit is contained in:
2026-10-11 14:03:39 +02:00
parent 3faea141dd
commit 793f71e32e
12 changed files with 261 additions and 44 deletions
+17 -15
View File
@@ -44,8 +44,8 @@ async fn list_account_members(
};
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
ensure_account_ownership(&mut tx, auth.user_id(), account_id).await?;
let members = repository::account_members::list(&mut *tx, query)
ensure_account_access(&mut tx, auth.user_id(), account_id).await?;
let members = repository::account_members::list(&mut *tx, auth.user_id(), query)
.await?
.into_iter()
.map(AccountMemberResponse::from)
@@ -62,12 +62,10 @@ async fn get_account_member(
State(pool): State<PgPool>,
Path((account_id, member_id)): Path<(Uuid, Uuid)>,
) -> Result<Json<AccountMemberResponse>, AppError> {
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
ensure_account_ownership(&mut tx, auth.user_id(), account_id).await?;
let member = repository::account_members::get_by_id(&mut *tx, account_id, member_id)
.await?
.ok_or(AppError::NotFound)?;
tx.commit().await.map_err(RepositoryError::from)?;
let member =
repository::account_members::get_by_id(&pool, auth.user_id(), account_id, member_id)
.await?
.ok_or(AppError::NotFound)?;
Ok(Json(member.into()))
}
@@ -82,10 +80,9 @@ async fn upsert_account_member(
) -> Result<(StatusCode, Json<AccountMemberResponse>), AppError> {
let member = member.into_domain(member_id, account_id)?;
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
ensure_account_ownership(&mut tx, auth.user_id(), account_id).await?;
let upserted = repository::account_members::upsert(&mut *tx, member).await?;
tx.commit().await.map_err(RepositoryError::from)?;
let upserted = repository::account_members::upsert(&pool, auth.user_id(), member)
.await?
.ok_or(AppError::NotFound)?;
let status = if upserted.previous.is_some() {
StatusCode::OK
@@ -103,10 +100,15 @@ async fn delete_account_member(
State(pool): State<PgPool>,
Path((account_id, member_id)): Path<(Uuid, Uuid)>,
) -> Result<StatusCode, AppError> {
let user_id = auth.user_id();
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
ensure_account_ownership(&mut tx, auth.user_id(), account_id).await?;
repository::accounts::get_by_id_locking(&mut *tx, user_id, account_id)
.await?
.ok_or(AppError::NotFound)?;
let members = repository::account_members::list(
&mut *tx,
user_id,
repository::account_members::Query {
account_id,
limit: Some(2),
@@ -120,7 +122,7 @@ async fn delete_account_member(
return Err(AppError::LastAccountMemberRemoval);
}
let deleted =
repository::account_members::delete_by_id(&mut *tx, account_id, member_id).await?;
repository::account_members::delete_by_id(&mut *tx, user_id, account_id, member_id).await?;
tx.commit().await.map_err(RepositoryError::from)?;
deleted.ok_or(AppError::NotFound)?;
@@ -128,7 +130,7 @@ async fn delete_account_member(
Ok(StatusCode::NO_CONTENT)
}
async fn ensure_account_ownership(
async fn ensure_account_access(
connection: &mut sqlx::PgConnection,
user_id: Uuid,
account_id: Uuid,