mirror of
https://github.com/modrinth/code.git
synced 2026-08-27 18:14:49 +00:00
feat(labrinth): granular disclosure locking (#7108)
* feat(labrinth): granular disclosure locking * chore(labrinth): update query cache
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
use std::collections::HashSet;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::str::FromStr;
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
database::models::{DBProjectId, DBUserId, DatabaseError},
|
||||
models::v3::disclosures::{ProjectDisclosure, ProjectDisclosureType},
|
||||
models::v3::disclosures::{
|
||||
DisclosureLockStatus, ProjectDisclosure, ProjectDisclosureType,
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
@@ -16,6 +19,7 @@ pub struct DBProjectDisclosure {
|
||||
pub updated_by: DBUserId,
|
||||
pub set_by_moderator: bool,
|
||||
pub deleted_at: Option<DateTime<Utc>>,
|
||||
pub lock_status: DisclosureLockStatus,
|
||||
}
|
||||
|
||||
impl DBProjectDisclosure {
|
||||
@@ -32,20 +36,22 @@ impl DBProjectDisclosure {
|
||||
|
||||
sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO project_disclosures (project_id, type, metadata, updated_by, set_by_moderator)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
INSERT INTO project_disclosures (project_id, type, metadata, updated_by, set_by_moderator, lock_status)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
ON CONFLICT (project_id, type) DO UPDATE SET
|
||||
metadata = $3,
|
||||
updated_at = now(),
|
||||
updated_by = $4,
|
||||
set_by_moderator = $5,
|
||||
deleted_at = NULL
|
||||
deleted_at = NULL,
|
||||
lock_status = $6
|
||||
"#,
|
||||
self.project_id as DBProjectId,
|
||||
disclosure_type,
|
||||
metadata,
|
||||
self.updated_by as DBUserId,
|
||||
self.set_by_moderator,
|
||||
<&'static str>::from(self.lock_status),
|
||||
)
|
||||
.execute(exec)
|
||||
.await?;
|
||||
@@ -60,7 +66,7 @@ impl DBProjectDisclosure {
|
||||
) -> Result<Vec<DBProjectDisclosure>, DatabaseError> {
|
||||
let rows = sqlx::query!(
|
||||
r#"
|
||||
SELECT project_id, type AS "disclosure_type!", metadata, updated_at, updated_by, set_by_moderator, deleted_at
|
||||
SELECT project_id, type AS "disclosure_type!", metadata, updated_at, updated_by, set_by_moderator, deleted_at, lock_status
|
||||
FROM project_disclosures
|
||||
WHERE project_id = $1 AND ($2 OR deleted_at IS NULL)
|
||||
ORDER BY updated_at DESC
|
||||
@@ -88,6 +94,14 @@ impl DBProjectDisclosure {
|
||||
updated_by: DBUserId(row.updated_by),
|
||||
set_by_moderator: row.set_by_moderator,
|
||||
deleted_at: row.deleted_at,
|
||||
lock_status: DisclosureLockStatus::from_str(
|
||||
&row.lock_status,
|
||||
)
|
||||
.map_err(|e| {
|
||||
DatabaseError::Internal(eyre::Report::new(e).wrap_err(
|
||||
"failed to parse project disclosure lock status",
|
||||
))
|
||||
})?,
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
@@ -115,24 +129,37 @@ impl DBProjectDisclosure {
|
||||
Ok(rows.into_iter().map(DBProjectId).collect())
|
||||
}
|
||||
|
||||
pub async fn any_set_by_moderator(
|
||||
pub async fn get_lock_statuses_for_project(
|
||||
project_id: DBProjectId,
|
||||
types: &[String],
|
||||
exec: impl crate::database::Executor<'_, Database = sqlx::Postgres>,
|
||||
) -> Result<bool, DatabaseError> {
|
||||
let existing = sqlx::query_scalar!(
|
||||
) -> Result<HashMap<String, DisclosureLockStatus>, DatabaseError> {
|
||||
let rows = sqlx::query!(
|
||||
r#"
|
||||
SELECT 1 FROM project_disclosures
|
||||
WHERE project_id = $1 AND type = ANY($2) AND set_by_moderator AND deleted_at IS NULL
|
||||
LIMIT 1
|
||||
SELECT type AS "disclosure_type!", lock_status
|
||||
FROM project_disclosures
|
||||
WHERE project_id = $1 AND type = ANY($2)
|
||||
"#,
|
||||
project_id as DBProjectId,
|
||||
types,
|
||||
)
|
||||
.fetch_optional(exec)
|
||||
.fetch_all(exec)
|
||||
.await?;
|
||||
|
||||
Ok(existing.is_some())
|
||||
rows.into_iter()
|
||||
.map(|row| {
|
||||
let lock_status = DisclosureLockStatus::from_str(
|
||||
&row.lock_status,
|
||||
)
|
||||
.map_err(|e| {
|
||||
DatabaseError::Internal(eyre::Report::new(e).wrap_err(
|
||||
"failed to parse project disclosure lock status",
|
||||
))
|
||||
})?;
|
||||
|
||||
Ok((row.disclosure_type, lock_status))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub async fn remove(
|
||||
|
||||
@@ -77,11 +77,42 @@ impl ProjectDisclosure {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(
|
||||
Debug,
|
||||
Clone,
|
||||
Copy,
|
||||
Default,
|
||||
Serialize,
|
||||
Deserialize,
|
||||
ToSchema,
|
||||
IntoStaticStr,
|
||||
EnumString,
|
||||
)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
#[strum(serialize_all = "snake_case")]
|
||||
pub enum DisclosureLockStatus {
|
||||
#[default]
|
||||
Unlocked,
|
||||
CannotDisable,
|
||||
FullyLocked,
|
||||
}
|
||||
|
||||
impl DisclosureLockStatus {
|
||||
pub fn allows_edit(self) -> bool {
|
||||
!matches!(self, Self::FullyLocked)
|
||||
}
|
||||
|
||||
pub fn allows_removal(self) -> bool {
|
||||
matches!(self, Self::Unlocked)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
|
||||
pub struct ProjectDisclosureData {
|
||||
#[serde(flatten)]
|
||||
pub disclosure: ProjectDisclosure,
|
||||
pub set_by_moderator: bool,
|
||||
pub lock_status: DisclosureLockStatus,
|
||||
pub updated_at: DateTime<Utc>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub updated_by: Option<UserId>,
|
||||
@@ -100,6 +131,7 @@ impl ProjectDisclosureData {
|
||||
Self {
|
||||
disclosure: value.disclosure,
|
||||
set_by_moderator: value.set_by_moderator,
|
||||
lock_status: value.lock_status,
|
||||
updated_at: value.updated_at,
|
||||
updated_by,
|
||||
deleted_at: value.deleted_at,
|
||||
|
||||
@@ -10,7 +10,8 @@ use crate::auth::get_user_from_headers;
|
||||
use crate::database::{DBProject, models as db_models};
|
||||
use crate::database::{PgPool, ReadOnlyPgPool};
|
||||
use crate::models::disclosures::{
|
||||
ProjectDisclosure, ProjectDisclosureData, ProjectDisclosureType,
|
||||
DisclosureLockStatus, ProjectDisclosure, ProjectDisclosureData,
|
||||
ProjectDisclosureType,
|
||||
};
|
||||
use crate::models::pats::Scopes;
|
||||
use crate::models::teams::ProjectPermissions;
|
||||
@@ -99,6 +100,8 @@ pub async fn get_project_disclosures(
|
||||
pub struct ModifyProjectDisclosures {
|
||||
pub set: Vec<ProjectDisclosure>,
|
||||
pub remove: Vec<String>,
|
||||
#[serde(default)]
|
||||
pub lock_status: Option<DisclosureLockStatus>,
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
@@ -164,28 +167,57 @@ pub async fn modify_project_disclosures(
|
||||
)));
|
||||
}
|
||||
|
||||
if !user.role.is_mod() {
|
||||
let modified_types = body
|
||||
.set
|
||||
.iter()
|
||||
.map(|disclosure| {
|
||||
<&'static str>::from(ProjectDisclosureType::from(disclosure))
|
||||
.to_owned()
|
||||
})
|
||||
.chain(body.remove.iter().cloned())
|
||||
.collect::<Vec<_>>();
|
||||
let is_moderator = user.role.is_mod();
|
||||
|
||||
if db_models::DBProjectDisclosure::any_set_by_moderator(
|
||||
if body.lock_status.is_some() && !is_moderator {
|
||||
return Err(ApiError::Auth(eyre!(
|
||||
"only moderators can set the lock status of a disclosure"
|
||||
)));
|
||||
}
|
||||
|
||||
let modified_types = body
|
||||
.set
|
||||
.iter()
|
||||
.map(|disclosure| {
|
||||
<&'static str>::from(ProjectDisclosureType::from(disclosure))
|
||||
.to_owned()
|
||||
})
|
||||
.chain(body.remove.iter().cloned())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let lock_statuses =
|
||||
db_models::DBProjectDisclosure::get_lock_statuses_for_project(
|
||||
project.inner.id,
|
||||
&modified_types,
|
||||
&**pool,
|
||||
)
|
||||
.await
|
||||
.wrap_internal_err("failed to check moderator disclosures")?
|
||||
{
|
||||
return Err(ApiError::Auth(eyre!(
|
||||
"you cannot modify a disclosure set by a moderator"
|
||||
)));
|
||||
.wrap_internal_err("failed to fetch disclosure lock statuses")?;
|
||||
|
||||
let lock_status_of = |disclosure_type: &str| {
|
||||
lock_statuses
|
||||
.get(disclosure_type)
|
||||
.copied()
|
||||
.unwrap_or_default()
|
||||
};
|
||||
|
||||
if !is_moderator {
|
||||
for disclosure in &body.set {
|
||||
let disclosure_type =
|
||||
<&'static str>::from(ProjectDisclosureType::from(disclosure));
|
||||
if !lock_status_of(disclosure_type).allows_edit() {
|
||||
return Err(ApiError::Auth(eyre!(
|
||||
"you cannot modify the `{disclosure_type}` disclosure, it is locked by a moderator"
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
for disclosure_type in &body.remove {
|
||||
if !lock_status_of(disclosure_type).allows_removal() {
|
||||
return Err(ApiError::Auth(eyre!(
|
||||
"you cannot remove the `{disclosure_type}` disclosure, it is locked by a moderator"
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -195,13 +227,20 @@ pub async fn modify_project_disclosures(
|
||||
.wrap_internal_err("starting database transaction")?;
|
||||
|
||||
for disclosure in body.set {
|
||||
let disclosure_type =
|
||||
<&'static str>::from(ProjectDisclosureType::from(&disclosure));
|
||||
|
||||
let lock_status =
|
||||
body.lock_status.unwrap_or(lock_status_of(disclosure_type));
|
||||
|
||||
db_models::DBProjectDisclosure {
|
||||
project_id: project.inner.id,
|
||||
disclosure,
|
||||
updated_at: Utc::now(),
|
||||
updated_by: user.id.into(),
|
||||
set_by_moderator: user.role.is_mod(),
|
||||
set_by_moderator: is_moderator,
|
||||
deleted_at: None,
|
||||
lock_status,
|
||||
}
|
||||
.upsert(&mut transaction)
|
||||
.await
|
||||
|
||||
@@ -15,7 +15,9 @@ use crate::database::{self, models as db_models};
|
||||
use crate::database::{PgPool, PgTransaction, ReadOnlyPgPool};
|
||||
use crate::env::ENV;
|
||||
use crate::file_hosting::{FileHost, FileHostPublicity};
|
||||
use crate::models::disclosures::{ProjectDisclosure, ProjectDisclosureType};
|
||||
use crate::models::disclosures::{
|
||||
DisclosureLockStatus, ProjectDisclosure, ProjectDisclosureType,
|
||||
};
|
||||
use crate::models::ids::{ProjectId, VersionId};
|
||||
use crate::models::images::ImageContext;
|
||||
use crate::models::notifications::NotificationBody;
|
||||
@@ -517,6 +519,7 @@ pub async fn project_edit_internal(
|
||||
updated_by: user.id.into(),
|
||||
set_by_moderator: user.role.is_mod(),
|
||||
deleted_at: None,
|
||||
lock_status: DisclosureLockStatus::Unlocked,
|
||||
}
|
||||
.upsert(&mut transaction)
|
||||
.await
|
||||
@@ -726,7 +729,8 @@ pub async fn project_edit_internal(
|
||||
|
||||
if sync_archival_disclosure
|
||||
&& archival_disclosure.is_some_and(|disclosure| {
|
||||
user.role.is_mod() || !disclosure.set_by_moderator
|
||||
user.role.is_mod()
|
||||
|| disclosure.lock_status.allows_removal()
|
||||
})
|
||||
{
|
||||
db_models::DBProjectDisclosure::remove(
|
||||
|
||||
Reference in New Issue
Block a user