More scoping by user ID at DB level.
This commit is contained in:
@@ -37,20 +37,22 @@ async fn list_account_members(
|
||||
Path(account_id): Path<Uuid>,
|
||||
Query(query): Query<QueryParams>,
|
||||
) -> Result<Json<Vec<AccountMemberResponse>>, AppError> {
|
||||
let user_id = auth.user_id();
|
||||
let query = repository::account_members::Query {
|
||||
account_id,
|
||||
limit: Some(query.limit.unwrap_or(20)),
|
||||
offset: query.offset,
|
||||
};
|
||||
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
ensure_account_access(&mut tx, auth.user_id(), account_id).await?;
|
||||
let members = repository::account_members::list(&mut *tx, auth.user_id(), query)
|
||||
repository::accounts::get_by_id(&pool, user_id, account_id)
|
||||
.await?
|
||||
.map(|_| ())
|
||||
.ok_or(AppError::NotFound)?;
|
||||
|
||||
let members = repository::account_members::list(&pool, user_id, account_id, query)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(AccountMemberResponse::from)
|
||||
.collect();
|
||||
tx.commit().await.map_err(RepositoryError::from)?;
|
||||
|
||||
Ok(Json(members))
|
||||
}
|
||||
@@ -103,14 +105,16 @@ async fn delete_account_member(
|
||||
let user_id = auth.user_id();
|
||||
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
|
||||
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,
|
||||
account_id,
|
||||
repository::account_members::Query {
|
||||
account_id,
|
||||
limit: Some(2),
|
||||
offset: None,
|
||||
},
|
||||
@@ -123,20 +127,10 @@ async fn delete_account_member(
|
||||
}
|
||||
let deleted =
|
||||
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)?;
|
||||
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
async fn ensure_account_access(
|
||||
connection: &mut sqlx::PgConnection,
|
||||
user_id: Uuid,
|
||||
account_id: Uuid,
|
||||
) -> Result<(), AppError> {
|
||||
repository::accounts::get_by_id(&mut *connection, user_id, account_id)
|
||||
.await?
|
||||
.map(|_| ())
|
||||
.ok_or(AppError::NotFound)
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ async fn issue_token(
|
||||
return Err(AppError::InvalidClientCredentials);
|
||||
}
|
||||
|
||||
let user_app = repository::user_apps::get_by_id(&mut *tx, client_id)
|
||||
let user_app = repository::user_apps::get_by_id_unscoped(&mut *tx, client_id)
|
||||
.await?
|
||||
.ok_or(AppError::InvalidClientCredentials)?;
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ use super::{
|
||||
use crate::{
|
||||
crypto::{generate_secret, hash_secret},
|
||||
error::AppError,
|
||||
repository::{self, RepositoryError},
|
||||
repository,
|
||||
};
|
||||
|
||||
pub fn create_router() -> axum::Router<PgPool> {
|
||||
@@ -44,20 +44,20 @@ async fn list_user_app_secrets(
|
||||
return Err(AppError::NotFound);
|
||||
}
|
||||
|
||||
repository::user_apps::get_by_id(&pool, user_id, app_id)
|
||||
.await?
|
||||
.ok_or(AppError::NotFound)?;
|
||||
|
||||
let query = repository::user_app_secrets::Query {
|
||||
app_id,
|
||||
limit: Some(query.limit.unwrap_or(20)),
|
||||
offset: query.offset,
|
||||
};
|
||||
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
ensure_app_ownership(&mut tx, user_id, app_id).await?;
|
||||
let secrets = repository::user_app_secrets::list(&mut *tx, query)
|
||||
let secrets = repository::user_app_secrets::list(&pool, user_id, app_id, query)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(UserAppSecretResponse::from)
|
||||
.collect();
|
||||
tx.commit().await.map_err(RepositoryError::from)?;
|
||||
|
||||
Ok(Json(secrets))
|
||||
}
|
||||
@@ -73,12 +73,9 @@ async fn get_user_app_secret(
|
||||
return Err(AppError::NotFound);
|
||||
}
|
||||
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
ensure_app_ownership(&mut tx, user_id, app_id).await?;
|
||||
let secret = repository::user_app_secrets::get_by_id(&mut *tx, app_id, secret_id)
|
||||
let secret = repository::user_app_secrets::get_by_id(&pool, user_id, app_id, secret_id)
|
||||
.await?
|
||||
.ok_or(AppError::NotFound)?;
|
||||
tx.commit().await.map_err(RepositoryError::from)?;
|
||||
|
||||
Ok(Json(secret.into()))
|
||||
}
|
||||
@@ -100,22 +97,20 @@ async fn upsert_user_app_secret(
|
||||
let hashed_secret = hash_secret(&generated_secret);
|
||||
let secret = request.into_domain(secret_id, app_id, hashed_secret, now)?;
|
||||
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
ensure_app_ownership(&mut tx, user_id, app_id).await?;
|
||||
let upserted = repository::user_app_secrets::upsert(&mut *tx, secret).await?;
|
||||
tx.commit().await.map_err(RepositoryError::from)?;
|
||||
let upserted = repository::user_app_secrets::upsert(&pool, user_id, secret)
|
||||
.await?
|
||||
.ok_or(AppError::NotFound)?;
|
||||
|
||||
let status = if upserted.previous.is_some() {
|
||||
StatusCode::OK
|
||||
} else {
|
||||
StatusCode::CREATED
|
||||
};
|
||||
let secret = upserted.current;
|
||||
|
||||
Ok((
|
||||
status,
|
||||
Json(CreatedUserAppSecretResponse {
|
||||
credential: secret.into(),
|
||||
credential: upserted.current.into(),
|
||||
secret: generated_secret,
|
||||
}),
|
||||
))
|
||||
@@ -132,23 +127,9 @@ async fn delete_user_app_secret(
|
||||
return Err(AppError::NotFound);
|
||||
}
|
||||
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
ensure_app_ownership(&mut tx, user_id, app_id).await?;
|
||||
let deleted = repository::user_app_secrets::delete_by_id(&mut *tx, app_id, secret_id).await?;
|
||||
tx.commit().await.map_err(RepositoryError::from)?;
|
||||
|
||||
deleted.ok_or(AppError::NotFound)?;
|
||||
repository::user_app_secrets::delete_by_id(&pool, user_id, app_id, secret_id)
|
||||
.await?
|
||||
.ok_or(AppError::NotFound)?;
|
||||
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
async fn ensure_app_ownership(
|
||||
connection: &mut sqlx::PgConnection,
|
||||
user_id: Uuid,
|
||||
app_id: Uuid,
|
||||
) -> Result<(), AppError> {
|
||||
match repository::user_apps::find_owner_by_id(&mut *connection, app_id).await? {
|
||||
Some(owner) if owner == user_id => Ok(()),
|
||||
Some(_) | None => Err(AppError::NotFound),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,7 @@ use super::{
|
||||
dtos::user_app::{UserAppRequest, UserAppResponse},
|
||||
extractors::{Auth, Json, Path, Query},
|
||||
};
|
||||
use crate::{
|
||||
error::AppError,
|
||||
repository::{self, RepositoryError},
|
||||
};
|
||||
use crate::{error::AppError, repository};
|
||||
|
||||
pub fn create_router() -> axum::Router<PgPool> {
|
||||
axum::Router::new()
|
||||
@@ -42,12 +39,11 @@ async fn list_user_apps(
|
||||
}
|
||||
|
||||
let query = repository::user_apps::Query {
|
||||
user_id,
|
||||
limit: Some(query.limit.unwrap_or(20)),
|
||||
offset: query.offset,
|
||||
};
|
||||
|
||||
let apps = repository::user_apps::list(&pool, query)
|
||||
let apps = repository::user_apps::list(&pool, user_id, query)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(UserAppResponse::from)
|
||||
@@ -67,9 +63,8 @@ async fn get_user_app(
|
||||
return Err(AppError::NotFound);
|
||||
}
|
||||
|
||||
let app = repository::user_apps::get_by_id(&pool, app_id)
|
||||
let app = repository::user_apps::get_by_id(&pool, user_id, app_id)
|
||||
.await?
|
||||
.filter(|app| app.user_id == user_id)
|
||||
.ok_or(AppError::NotFound)?;
|
||||
|
||||
Ok(Json(app.into()))
|
||||
@@ -88,17 +83,9 @@ async fn upsert_user_app(
|
||||
}
|
||||
|
||||
let app = request.into_domain(user_id, app_id);
|
||||
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
let upserted = repository::user_apps::upsert(&mut *tx, app).await?;
|
||||
if upserted
|
||||
.previous
|
||||
.as_ref()
|
||||
.is_some_and(|previous| previous.user_id != user_id)
|
||||
{
|
||||
return Err(AppError::AppIdAlreadyExists);
|
||||
}
|
||||
tx.commit().await.map_err(RepositoryError::from)?;
|
||||
let upserted = repository::user_apps::upsert(&pool, app)
|
||||
.await?
|
||||
.ok_or(AppError::AppIdAlreadyExists)?;
|
||||
|
||||
let status = if upserted.previous.is_some() {
|
||||
StatusCode::OK
|
||||
@@ -120,14 +107,9 @@ async fn delete_user_app(
|
||||
return Err(AppError::NotFound);
|
||||
}
|
||||
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
if repository::user_apps::delete_by_id(&mut *tx, app_id)
|
||||
repository::user_apps::delete_by_id(&pool, user_id, app_id)
|
||||
.await?
|
||||
.is_none_or(|user_app| user_app.user_id != user_id)
|
||||
{
|
||||
return Err(AppError::NotFound);
|
||||
}
|
||||
tx.commit().await.map_err(RepositoryError::from)?;
|
||||
.ok_or(AppError::NotFound)?;
|
||||
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user