Made accounts and their transaction accessible to account members only.
This commit is contained in:
@@ -44,7 +44,7 @@ async fn list_account_members(
|
||||
};
|
||||
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
ensure_account_exists(&mut tx, account_id).await?;
|
||||
ensure_account_ownership(&mut tx, auth.user_id(), account_id).await?;
|
||||
let members = repository::account_members::list(&mut *tx, query)
|
||||
.await?
|
||||
.into_iter()
|
||||
@@ -63,7 +63,7 @@ async fn get_account_member(
|
||||
Path((account_id, member_id)): Path<(Uuid, Uuid)>,
|
||||
) -> Result<Json<AccountMemberResponse>, AppError> {
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
ensure_account_exists(&mut tx, account_id).await?;
|
||||
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)?;
|
||||
@@ -83,7 +83,7 @@ async fn upsert_account_member(
|
||||
let member = member.into_domain(member_id, account_id)?;
|
||||
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
ensure_account_exists(&mut tx, account_id).await?;
|
||||
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)?;
|
||||
|
||||
@@ -104,7 +104,21 @@ async fn delete_account_member(
|
||||
Path((account_id, member_id)): Path<(Uuid, Uuid)>,
|
||||
) -> Result<StatusCode, AppError> {
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
ensure_account_exists(&mut tx, account_id).await?;
|
||||
ensure_account_ownership(&mut tx, auth.user_id(), account_id).await?;
|
||||
let members = repository::account_members::list(
|
||||
&mut *tx,
|
||||
repository::account_members::Query {
|
||||
account_id,
|
||||
limit: Some(2),
|
||||
offset: None,
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
if let [only] = members.as_slice()
|
||||
&& only.id == member_id
|
||||
{
|
||||
return Err(AppError::LastAccountMemberRemoval);
|
||||
}
|
||||
let deleted =
|
||||
repository::account_members::delete_by_id(&mut *tx, account_id, member_id).await?;
|
||||
tx.commit().await.map_err(RepositoryError::from)?;
|
||||
@@ -114,11 +128,12 @@ async fn delete_account_member(
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
async fn ensure_account_exists(
|
||||
async fn ensure_account_ownership(
|
||||
connection: &mut sqlx::PgConnection,
|
||||
user_id: Uuid,
|
||||
account_id: Uuid,
|
||||
) -> Result<(), AppError> {
|
||||
repository::accounts::get_by_id(&mut *connection, account_id)
|
||||
repository::accounts::get_by_id(&mut *connection, user_id, account_id)
|
||||
.await?
|
||||
.map(|_| ())
|
||||
.ok_or(AppError::NotFound)
|
||||
|
||||
Reference in New Issue
Block a user