Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WJ-1281] Require passing in last revision ID for file operations #2129

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions deepwell/src/services/file/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ impl FileService {
page_id,
file_id,
user_id,
last_revision_id,
revision_comments,
bypass_filter,
body,
Expand All @@ -121,6 +122,8 @@ impl FileService {
let last_revision =
FileRevisionService::get_latest(ctx, site_id, page_id, file_id).await?;

check_last_revision(&last_revision, last_revision_id)?;

let EditFileBody {
name,
licensing,
Expand Down Expand Up @@ -203,6 +206,7 @@ impl FileService {
destination_page_id,
file_id,
user_id,
last_revision_id,
revision_comments,
}: MoveFile,
) -> Result<Option<MoveFileOutput>> {
Expand All @@ -211,6 +215,8 @@ impl FileService {
FileRevisionService::get_latest(ctx, site_id, current_page_id, file_id)
.await?;

check_last_revision(&last_revision, last_revision_id)?;

// Get destination filename
let name = name.unwrap_or_else(|| last_revision.name.clone());

Expand Down Expand Up @@ -261,6 +267,7 @@ impl FileService {
pub async fn delete(
ctx: &ServiceContext<'_>,
DeleteFile {
last_revision_id,
revision_comments,
site_id,
page_id,
Expand All @@ -284,6 +291,8 @@ impl FileService {
let last_revision =
FileRevisionService::get_latest(ctx, site_id, page_id, file_id).await?;

check_last_revision(&last_revision, last_revision_id)?;

// Create tombstone revision
// This outdates the page, etc
let output = FileRevisionService::create_tombstone(
Expand Down Expand Up @@ -567,3 +576,22 @@ impl FileService {
Ok(())
}
}

/// Verifies that the `last_revision_id` argument is the most recent.
///
/// See the helper function with the same name in `services/page/service.rs`.
fn check_last_revision(
last_revision_model: &FileRevisionModel,
arg_last_revision_id: i64,
) -> Result<()> {
if last_revision_model.revision_id != arg_last_revision_id {
error!(
"Latest revision ID in file table is {}, but user argument has ID {}",
last_revision_model.revision_id, arg_last_revision_id,
);

return Err(Error::NotLatestRevisionId);
}

Ok(())
}
3 changes: 3 additions & 0 deletions deepwell/src/services/file/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ pub struct EditFile {
pub page_id: i64,
pub file_id: i64,
pub user_id: i64,
pub last_revision_id: i64,
pub revision_comments: String,

#[serde(flatten)]
Expand All @@ -110,6 +111,7 @@ pub struct MoveFile {
pub site_id: i64,
pub file_id: i64,
pub user_id: i64,
pub last_revision_id: i64,
pub name: Option<String>,
pub current_page_id: i64,
pub destination_page_id: i64,
Expand All @@ -119,6 +121,7 @@ pub type MoveFileOutput = CreateFileRevisionOutput;

#[derive(Deserialize, Debug, Clone)]
pub struct DeleteFile<'a> {
pub last_revision_id: i64,
pub revision_comments: String,
pub site_id: i64,
pub page_id: i64,
Expand Down
Loading