|
| 1 | +//! Original Author Validation Rule |
| 2 | +
|
| 3 | +#[cfg(test)] |
| 4 | +mod tests; |
| 5 | + |
| 6 | +use std::collections::HashSet; |
| 7 | + |
| 8 | +use catalyst_types::catalyst_id::CatalystId; |
| 9 | + |
| 10 | +use crate::{providers::CatalystSignedDocumentProvider, CatalystSignedDocument}; |
| 11 | + |
| 12 | +/// Returns `true` if the document has a single author. |
| 13 | +/// |
| 14 | +/// If not, it adds to the document's problem report. |
| 15 | +fn single_author_check(doc: &CatalystSignedDocument) -> bool { |
| 16 | + let is_valid = doc.authors().len() == 1; |
| 17 | + if !is_valid { |
| 18 | + doc.report().functional_validation( |
| 19 | + "New document must only be signed by a single author", |
| 20 | + "Valid documents must only be signed by the original author", |
| 21 | + ); |
| 22 | + } |
| 23 | + is_valid |
| 24 | +} |
| 25 | + |
| 26 | +/// Document Ownership Validation Rule |
| 27 | +#[derive(Debug)] |
| 28 | +pub(crate) struct DocumentOwnershipRule { |
| 29 | + /// Collaborators are allowed. |
| 30 | + pub(crate) allow_collaborators: bool, |
| 31 | +} |
| 32 | + |
| 33 | +impl DocumentOwnershipRule { |
| 34 | + /// Check document ownership rule |
| 35 | + pub(crate) async fn check<Provider>( |
| 36 | + &self, |
| 37 | + doc: &CatalystSignedDocument, |
| 38 | + provider: &Provider, |
| 39 | + ) -> anyhow::Result<bool> |
| 40 | + where |
| 41 | + Provider: CatalystSignedDocumentProvider, |
| 42 | + { |
| 43 | + let doc_id = doc.doc_id()?; |
| 44 | + let first_doc_opt = provider.try_get_first_doc(doc_id).await?; |
| 45 | + |
| 46 | + if self.allow_collaborators { |
| 47 | + if let Some(first_doc) = first_doc_opt { |
| 48 | + // This a new version of an existing `doc_id` |
| 49 | + let Some(last_doc) = provider.try_get_last_doc(doc_id).await? else { |
| 50 | + anyhow::bail!( |
| 51 | + "A latest version of the document must exist if a first version exists" |
| 52 | + ); |
| 53 | + }; |
| 54 | + // Allowed authors for this document are the original author, and collaborators |
| 55 | + // defined in the last published version of the Document ID. |
| 56 | + let mut allowed_authors = first_doc |
| 57 | + .authors() |
| 58 | + .into_iter() |
| 59 | + .collect::<HashSet<CatalystId>>(); |
| 60 | + allowed_authors.extend(last_doc.doc_meta().collaborators().iter().cloned()); |
| 61 | + |
| 62 | + let doc_authors = doc.authors().into_iter().collect::<HashSet<CatalystId>>(); |
| 63 | + |
| 64 | + let is_valid = allowed_authors.intersection(&doc_authors).count() > 0; |
| 65 | + |
| 66 | + if !is_valid { |
| 67 | + doc.report().functional_validation( |
| 68 | + "New document must only be signed by a single author or collaborators defined in the previous version", |
| 69 | + "Valid documents must only be signed by the original author or known collaborators", |
| 70 | + ); |
| 71 | + } |
| 72 | + return Ok(is_valid); |
| 73 | + } |
| 74 | + |
| 75 | + // This is a first version of the doc |
| 76 | + return Ok(single_author_check(doc)); |
| 77 | + } |
| 78 | + |
| 79 | + // No collaborators are allowed |
| 80 | + if let Some(first_doc) = first_doc_opt { |
| 81 | + // This a new version of an existing `doc_id` |
| 82 | + let is_valid = first_doc.authors() == doc.authors(); |
| 83 | + if !is_valid { |
| 84 | + doc.report().functional_validation( |
| 85 | + &format!("New document authors must match the authors from the first version for Document ID {doc_id}"), |
| 86 | + "Valid documents must only be signed by the original author", |
| 87 | + ); |
| 88 | + } |
| 89 | + return Ok(is_valid); |
| 90 | + } |
| 91 | + |
| 92 | + // This is a first version of the doc |
| 93 | + Ok(single_author_check(doc)) |
| 94 | + } |
| 95 | +} |
0 commit comments