More scoping by user ID at DB level.

This commit is contained in:
2026-10-12 11:20:41 +02:00
parent 793f71e32e
commit b40d686ef9
18 changed files with 235 additions and 188 deletions
+17 -10
View File
@@ -10,17 +10,20 @@ use crate::{
};
pub struct Query {
pub app_id: Uuid,
pub limit: Option<u64>,
pub offset: Option<u64>,
}
pub async fn list<'e, E>(executor: E, query: Query) -> Result<Vec<UserAppSecret>, RepositoryError>
pub async fn list<'e, E>(
executor: E,
user_id: Uuid,
app_id: Uuid,
query: Query,
) -> Result<Vec<UserAppSecret>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let query = db::Query {
app_id: query.app_id,
limit: query
.limit
.map(|limit| i64::try_from(limit).map_err(|_| QueryConversionError::Limit(limit)))
@@ -31,7 +34,7 @@ where
.transpose()?,
};
let secrets = db::list(executor, query)
let secrets = db::list(executor, user_id, app_id, query)
.await?
.into_iter()
.map(decode)
@@ -42,13 +45,14 @@ where
pub async fn get_by_id<'e, E>(
executor: E,
user_id: Uuid,
app_id: Uuid,
id: Uuid,
) -> Result<Option<UserAppSecret>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let secret = db::get_by_id(executor, app_id, id)
let secret = db::get_by_id(executor, user_id, app_id, id)
.await?
.map(decode)
.transpose()?;
@@ -58,16 +62,18 @@ where
pub async fn upsert<'e, E>(
executor: E,
user_id: Uuid,
secret: UserAppSecret,
) -> Result<Upsert<UserAppSecret>, RepositoryError>
) -> Result<Option<Upsert<UserAppSecret>>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let secret = db::upsert(executor, encode(secret))
let secret = db::upsert(executor, user_id, encode(secret))
.await
.map_err(classify_upsert_error)?
.try_map(decode)?
.into();
.map(|upserted| upserted.try_map(decode))
.transpose()?
.map(Into::into);
Ok(secret)
}
@@ -101,13 +107,14 @@ where
pub async fn delete_by_id<'e, E>(
executor: E,
user_id: Uuid,
app_id: Uuid,
id: Uuid,
) -> Result<Option<UserAppSecret>, RepositoryError>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let secret = db::delete_by_id(executor, app_id, id)
let secret = db::delete_by_id(executor, user_id, app_id, id)
.await?
.map(decode)
.transpose()?;