More scoping by user ID at DB level.
This commit is contained in:
@@ -6,7 +6,6 @@ use super::{
|
||||
};
|
||||
|
||||
pub struct Query {
|
||||
pub account_id: Uuid,
|
||||
pub limit: Option<i64>,
|
||||
pub offset: Option<i64>,
|
||||
}
|
||||
@@ -14,6 +13,7 @@ pub struct Query {
|
||||
pub async fn list<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
account_id: Uuid,
|
||||
query: Query,
|
||||
) -> Result<Vec<AccountMemberRow>, DatabaseError>
|
||||
where
|
||||
@@ -31,7 +31,7 @@ where
|
||||
OFFSET $4
|
||||
"#,
|
||||
user_id,
|
||||
query.account_id,
|
||||
account_id,
|
||||
query.limit,
|
||||
query.offset,
|
||||
)
|
||||
|
||||
@@ -6,12 +6,16 @@ use super::{
|
||||
};
|
||||
|
||||
pub struct Query {
|
||||
pub app_id: Uuid,
|
||||
pub limit: Option<i64>,
|
||||
pub offset: Option<i64>,
|
||||
}
|
||||
|
||||
pub async fn list<'e, E>(executor: E, query: Query) -> Result<Vec<UserAppSecretRow>, DatabaseError>
|
||||
pub async fn list<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
app_id: Uuid,
|
||||
query: Query,
|
||||
) -> Result<Vec<UserAppSecretRow>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
@@ -20,12 +24,14 @@ where
|
||||
r#"
|
||||
SELECT id, app_id, secret_hash, expires_at, created_at
|
||||
FROM user_app_secrets
|
||||
WHERE app_id = $1
|
||||
WHERE app_id IN (SELECT id FROM user_apps WHERE user_id = $1)
|
||||
AND app_id = $2
|
||||
ORDER BY created_at DESC, id DESC
|
||||
LIMIT $2
|
||||
OFFSET $3
|
||||
LIMIT $3
|
||||
OFFSET $4
|
||||
"#,
|
||||
query.app_id,
|
||||
user_id,
|
||||
app_id,
|
||||
query.limit,
|
||||
query.offset,
|
||||
)
|
||||
@@ -36,6 +42,7 @@ where
|
||||
|
||||
pub async fn get_by_id<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
app_id: Uuid,
|
||||
id: Uuid,
|
||||
) -> Result<Option<UserAppSecretRow>, DatabaseError>
|
||||
@@ -47,8 +54,11 @@ where
|
||||
r#"
|
||||
SELECT id, app_id, secret_hash, expires_at, created_at
|
||||
FROM user_app_secrets
|
||||
WHERE app_id = $1 AND id = $2
|
||||
WHERE app_id IN (SELECT id FROM user_apps WHERE user_id = $1)
|
||||
AND app_id = $2
|
||||
AND id = $3
|
||||
"#,
|
||||
user_id,
|
||||
app_id,
|
||||
id,
|
||||
)
|
||||
@@ -59,15 +69,19 @@ where
|
||||
|
||||
pub async fn upsert<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
secret: UserAppSecretRow,
|
||||
) -> Result<Upsert<UserAppSecretRow>, DatabaseError>
|
||||
) -> Result<Option<Upsert<UserAppSecretRow>>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let row = sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO user_app_secrets (id, app_id, secret_hash, expires_at)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
SELECT $2, $3, $4, $5
|
||||
FROM user_apps
|
||||
WHERE user_id = $1 AND id = $3
|
||||
FOR KEY SHARE
|
||||
ON CONFLICT (id, app_id) DO UPDATE SET
|
||||
secret_hash = EXCLUDED.secret_hash,
|
||||
expires_at = EXCLUDED.expires_at
|
||||
@@ -83,15 +97,21 @@ where
|
||||
new_row.expires_at AS "expires_at!",
|
||||
new_row.created_at AS "created_at!"
|
||||
"#,
|
||||
user_id,
|
||||
secret.id,
|
||||
secret.app_id,
|
||||
secret.secret_hash,
|
||||
secret.expires_at,
|
||||
)
|
||||
.fetch_one(executor)
|
||||
.fetch_optional(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
// Nothing is written when the app does not exist or is not owned by the user.
|
||||
let Some(row) = row else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
// `old_id` indicates whether there was a previous row before the upsert, other old fields are
|
||||
// coalesced with the new row's values in case there was no previous row. This enables static
|
||||
// typing on the previous row instead of having to check nullability for each old field in code.
|
||||
@@ -103,7 +123,7 @@ where
|
||||
created_at: row.old_created_at,
|
||||
});
|
||||
|
||||
Ok(Upsert {
|
||||
Ok(Some(Upsert {
|
||||
previous,
|
||||
current: UserAppSecretRow {
|
||||
id: row.id,
|
||||
@@ -112,7 +132,7 @@ where
|
||||
expires_at: row.expires_at,
|
||||
created_at: row.created_at,
|
||||
},
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
pub async fn verify<'e, E>(
|
||||
@@ -141,6 +161,7 @@ where
|
||||
|
||||
pub async fn delete_by_id<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
app_id: Uuid,
|
||||
id: Uuid,
|
||||
) -> Result<Option<UserAppSecretRow>, DatabaseError>
|
||||
@@ -151,9 +172,12 @@ where
|
||||
UserAppSecretRow,
|
||||
r#"
|
||||
DELETE FROM user_app_secrets
|
||||
WHERE app_id = $1 AND id = $2
|
||||
WHERE app_id IN (SELECT id FROM user_apps WHERE user_id = $1)
|
||||
AND app_id = $2
|
||||
AND id = $3
|
||||
RETURNING id, app_id, secret_hash, expires_at, created_at
|
||||
"#,
|
||||
user_id,
|
||||
app_id,
|
||||
id,
|
||||
)
|
||||
|
||||
@@ -6,12 +6,15 @@ use super::{
|
||||
};
|
||||
|
||||
pub struct Query {
|
||||
pub user_id: Uuid,
|
||||
pub limit: Option<i64>,
|
||||
pub offset: Option<i64>,
|
||||
}
|
||||
|
||||
pub async fn list<'e, E>(executor: E, query: Query) -> Result<Vec<UserAppRow>, DatabaseError>
|
||||
pub async fn list<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
query: Query,
|
||||
) -> Result<Vec<UserAppRow>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
@@ -25,7 +28,7 @@ where
|
||||
LIMIT $2
|
||||
OFFSET $3
|
||||
"#,
|
||||
query.user_id,
|
||||
user_id,
|
||||
query.limit,
|
||||
query.offset,
|
||||
)
|
||||
@@ -34,7 +37,29 @@ where
|
||||
.map_err(DatabaseError::from)
|
||||
}
|
||||
|
||||
pub async fn get_by_id<'e, E>(executor: E, id: Uuid) -> Result<Option<UserAppRow>, DatabaseError>
|
||||
pub async fn get_by_id<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
id: Uuid,
|
||||
) -> Result<Option<UserAppRow>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
sqlx::query_as!(
|
||||
UserAppRow,
|
||||
"SELECT id, user_id FROM user_apps WHERE user_id = $1 AND id = $2",
|
||||
user_id,
|
||||
id,
|
||||
)
|
||||
.fetch_optional(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)
|
||||
}
|
||||
|
||||
pub async fn get_by_id_unscoped<'e, E>(
|
||||
executor: E,
|
||||
id: Uuid,
|
||||
) -> Result<Option<UserAppRow>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
@@ -48,26 +73,10 @@ where
|
||||
.map_err(DatabaseError::from)
|
||||
}
|
||||
|
||||
pub async fn find_owner_by_id<'e, E>(
|
||||
executor: E,
|
||||
app_id: Uuid,
|
||||
) -> Result<Option<Uuid>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
sqlx::query_scalar!(
|
||||
"SELECT user_id FROM user_apps WHERE id = $1 FOR KEY SHARE",
|
||||
app_id,
|
||||
)
|
||||
.fetch_optional(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)
|
||||
}
|
||||
|
||||
pub async fn upsert<'e, E>(
|
||||
executor: E,
|
||||
app: UserAppRow,
|
||||
) -> Result<Upsert<UserAppRow>, DatabaseError>
|
||||
) -> Result<Option<Upsert<UserAppRow>>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
@@ -77,6 +86,7 @@ where
|
||||
VALUES ($1, $2)
|
||||
ON CONFLICT (id) DO UPDATE SET
|
||||
user_id = user_apps.user_id
|
||||
WHERE user_apps.user_id = EXCLUDED.user_id
|
||||
RETURNING WITH (OLD AS old_row, NEW AS new_row)
|
||||
old_row.id AS "old_id?",
|
||||
COALESCE(old_row.user_id, new_row.user_id) AS "old_user_id!",
|
||||
@@ -86,10 +96,15 @@ where
|
||||
app.id,
|
||||
app.user_id,
|
||||
)
|
||||
.fetch_one(executor)
|
||||
.fetch_optional(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
// Nothing is written when the app ID already exists for another user.
|
||||
let Some(row) = row else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
// `old_id` indicates whether there was a previous row before the upsert, other old fields are
|
||||
// coalesced with the new row's values in case there was no previous row. This enables static
|
||||
// typing on the previous row instead of having to check nullability for each old field in code.
|
||||
@@ -98,22 +113,27 @@ where
|
||||
user_id: row.old_user_id,
|
||||
});
|
||||
|
||||
Ok(Upsert {
|
||||
Ok(Some(Upsert {
|
||||
previous,
|
||||
current: UserAppRow {
|
||||
id: row.id,
|
||||
user_id: row.user_id,
|
||||
},
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Option<UserAppRow>, DatabaseError>
|
||||
pub async fn delete_by_id<'e, E>(
|
||||
executor: E,
|
||||
user_id: Uuid,
|
||||
id: Uuid,
|
||||
) -> Result<Option<UserAppRow>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
sqlx::query_as!(
|
||||
UserAppRow,
|
||||
"DELETE FROM user_apps WHERE id = $1 RETURNING id, user_id",
|
||||
"DELETE FROM user_apps WHERE user_id = $1 AND id = $2 RETURNING id, user_id",
|
||||
user_id,
|
||||
id,
|
||||
)
|
||||
.fetch_optional(executor)
|
||||
|
||||
Reference in New Issue
Block a user