More scoping by user ID at DB level.
This commit is contained in:
@@ -10,7 +10,6 @@ use crate::{
|
||||
};
|
||||
|
||||
pub struct Query {
|
||||
pub account_id: Uuid,
|
||||
pub limit: Option<u64>,
|
||||
pub offset: Option<u64>,
|
||||
}
|
||||
@@ -18,13 +17,13 @@ pub struct Query {
|
||||
pub async fn list<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
account_id: Uuid,
|
||||
query: Query,
|
||||
) -> Result<Vec<AccountMember>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let query = db::Query {
|
||||
account_id: query.account_id,
|
||||
limit: query
|
||||
.limit
|
||||
.map(|limit| i64::try_from(limit).map_err(|_| QueryConversionError::Limit(limit)))
|
||||
@@ -35,7 +34,7 @@ where
|
||||
.transpose()?,
|
||||
};
|
||||
|
||||
let members = db::list(executor, user_id, query)
|
||||
let members = db::list(executor, user_id, account_id, query)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(decode)
|
||||
@@ -79,7 +78,9 @@ where
|
||||
Ok(member)
|
||||
}
|
||||
|
||||
/// Adds the first member of a newly created account, without checking account access.
|
||||
/// Inserts a member without checking account access. Only meant for adding the first member of a
|
||||
/// newly created account, where no member exists yet to grant access. Use [upsert] for regular
|
||||
/// member insertion or updating existing members.
|
||||
pub async fn insert_initial<'e, E>(
|
||||
executor: E,
|
||||
member: AccountMember,
|
||||
|
||||
@@ -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()?;
|
||||
|
||||
@@ -8,17 +8,19 @@ use crate::{
|
||||
};
|
||||
|
||||
pub struct Query {
|
||||
pub user_id: Uuid,
|
||||
pub limit: Option<u64>,
|
||||
pub offset: Option<u64>,
|
||||
}
|
||||
|
||||
pub async fn list<'e, E>(executor: E, query: Query) -> Result<Vec<UserApp>, RepositoryError>
|
||||
pub async fn list<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
query: Query,
|
||||
) -> Result<Vec<UserApp>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let query = db::Query {
|
||||
user_id: query.user_id,
|
||||
limit: query
|
||||
.limit
|
||||
.map(|limit| i64::try_from(limit).map_err(|_| QueryConversionError::Limit(limit)))
|
||||
@@ -29,7 +31,7 @@ where
|
||||
.transpose()?,
|
||||
};
|
||||
|
||||
let apps = db::list(executor, query)
|
||||
let apps = db::list(executor, user_id, query)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(decode)
|
||||
@@ -38,36 +40,50 @@ where
|
||||
Ok(apps)
|
||||
}
|
||||
|
||||
pub async fn get_by_id<'e, E>(executor: E, id: Uuid) -> Result<Option<UserApp>, RepositoryError>
|
||||
pub async fn get_by_id<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
id: Uuid,
|
||||
) -> Result<Option<UserApp>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let app = db::get_by_id(executor, id).await?.map(decode).transpose()?;
|
||||
let app = db::get_by_id(executor, user_id, id)
|
||||
.await?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
|
||||
Ok(app)
|
||||
}
|
||||
|
||||
pub async fn find_owner_by_id<'e, E>(
|
||||
pub async fn get_by_id_unscoped<'e, E>(
|
||||
executor: E,
|
||||
app_id: Uuid,
|
||||
) -> Result<Option<Uuid>, RepositoryError>
|
||||
id: Uuid,
|
||||
) -> Result<Option<UserApp>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let owner = db::find_owner_by_id(executor, app_id).await?;
|
||||
let app = db::get_by_id_unscoped(executor, id)
|
||||
.await?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
|
||||
Ok(owner)
|
||||
Ok(app)
|
||||
}
|
||||
|
||||
pub async fn upsert<'e, E>(executor: E, app: UserApp) -> Result<Upsert<UserApp>, RepositoryError>
|
||||
pub async fn upsert<'e, E>(
|
||||
executor: E,
|
||||
app: UserApp,
|
||||
) -> Result<Option<Upsert<UserApp>>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let app = db::upsert(executor, encode(app))
|
||||
.await
|
||||
.map_err(classify_upsert_error)?
|
||||
.try_map(decode)?
|
||||
.into();
|
||||
.map(|upserted| upserted.try_map(decode))
|
||||
.transpose()?
|
||||
.map(Into::into);
|
||||
|
||||
Ok(app)
|
||||
}
|
||||
@@ -86,11 +102,15 @@ fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Option<UserApp>, RepositoryError>
|
||||
pub async fn delete_by_id<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
id: Uuid,
|
||||
) -> Result<Option<UserApp>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let app = db::delete_by_id(executor, id)
|
||||
let app = db::delete_by_id(executor, user_id, id)
|
||||
.await?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
|
||||
Reference in New Issue
Block a user