Merge pull request '实现 Workspace 邀请策略与 Document 归属' (#31) from codex/document-model into main
Deploy Rust Bedrock / deploy (push) Successful in 20m57s
Deploy Rust Bedrock / deploy (push) Successful in 20m57s
Reviewed-on: #31
This commit was merged in pull request #31.
This commit is contained in:
@@ -16,4 +16,7 @@ pub use organization::{Organization, OrganizationId, OrganizationMembership, Org
|
||||
pub use rich_text::{Inline, RichText};
|
||||
pub use tree::{DocumentTree, TreeError};
|
||||
pub use user::{User, UserId};
|
||||
pub use workspace::{Workspace, WorkspaceId};
|
||||
pub use workspace::{
|
||||
Workspace, WorkspaceAccess, WorkspaceAccessError, WorkspaceId, WorkspaceInvitation,
|
||||
WorkspaceMembership, WorkspaceRole,
|
||||
};
|
||||
|
||||
@@ -2,6 +2,7 @@ use super::contextualize::Contextualize;
|
||||
use super::{
|
||||
id::{IdError, validate_id},
|
||||
user::UserId,
|
||||
workspace::{Workspace, WorkspaceAccess, WorkspaceAccessError, WorkspaceId},
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
||||
@@ -42,6 +43,20 @@ pub struct OrganizationMembership {
|
||||
pub role: OrganizationRole,
|
||||
}
|
||||
|
||||
impl Organization {
|
||||
pub fn create_workspace(&self, id: WorkspaceId, name: impl Into<String>) -> Workspace {
|
||||
Workspace::owned_by(id, name, self.id.clone())
|
||||
}
|
||||
|
||||
pub fn set_workspace_access(
|
||||
&self,
|
||||
workspace: &mut Workspace,
|
||||
access: WorkspaceAccess,
|
||||
) -> Result<(), WorkspaceAccessError> {
|
||||
workspace.set_organization_access(&self.id, access)
|
||||
}
|
||||
}
|
||||
|
||||
impl Contextualize for Organization {
|
||||
fn render_context(&self) -> String {
|
||||
format!("Organization: {}", self.name)
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
use thiserror::Error;
|
||||
|
||||
use super::contextualize::Contextualize;
|
||||
use super::{
|
||||
Document,
|
||||
id::{IdError, validate_id},
|
||||
organization::OrganizationId,
|
||||
organization::{OrganizationId, OrganizationMembership},
|
||||
tree::{DocumentTree, TreeError},
|
||||
user::UserId,
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
||||
@@ -21,12 +26,188 @@ impl WorkspaceId {
|
||||
}
|
||||
}
|
||||
|
||||
/// A workspace belongs to one organization and contains documents.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum WorkspaceRole {
|
||||
Owner,
|
||||
Admin,
|
||||
Editor,
|
||||
Viewer,
|
||||
}
|
||||
|
||||
/// Controls which roles can be granted to people outside the owning organization.
|
||||
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
|
||||
pub enum WorkspaceAccess {
|
||||
#[default]
|
||||
MembersOnly,
|
||||
ExternalViewers,
|
||||
ExternalEditors,
|
||||
}
|
||||
|
||||
impl WorkspaceAccess {
|
||||
fn allows_external(self, role: WorkspaceRole) -> bool {
|
||||
matches!(
|
||||
(self, role),
|
||||
(
|
||||
Self::ExternalViewers | Self::ExternalEditors,
|
||||
WorkspaceRole::Viewer
|
||||
) | (Self::ExternalEditors, WorkspaceRole::Editor)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
struct OrganizationWorkspace {
|
||||
id: OrganizationId,
|
||||
access: WorkspaceAccess,
|
||||
}
|
||||
|
||||
/// A workspace contains documents and may be owned by an organization.
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct Workspace {
|
||||
pub id: WorkspaceId,
|
||||
pub organization_id: OrganizationId,
|
||||
pub name: String,
|
||||
organization: Option<OrganizationWorkspace>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct WorkspaceInvitation {
|
||||
workspace_id: WorkspaceId,
|
||||
invitee: UserId,
|
||||
role: WorkspaceRole,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct WorkspaceMembership {
|
||||
pub workspace_id: WorkspaceId,
|
||||
pub user_id: UserId,
|
||||
pub role: WorkspaceRole,
|
||||
}
|
||||
|
||||
#[derive(Debug, Error, Eq, PartialEq)]
|
||||
pub enum WorkspaceAccessError {
|
||||
#[error("the workspace does not belong to this organization")]
|
||||
OrganizationMismatch,
|
||||
#[error("the workspace access policy does not allow this role for the invitee")]
|
||||
RoleNotAllowed,
|
||||
#[error("the invitation is for a different workspace or user")]
|
||||
InvitationMismatch,
|
||||
}
|
||||
|
||||
impl Workspace {
|
||||
pub fn independent(id: WorkspaceId, name: impl Into<String>) -> Self {
|
||||
Self {
|
||||
id,
|
||||
name: name.into(),
|
||||
organization: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn owned_by(
|
||||
id: WorkspaceId,
|
||||
name: impl Into<String>,
|
||||
organization_id: OrganizationId,
|
||||
) -> Self {
|
||||
Self {
|
||||
id,
|
||||
name: name.into(),
|
||||
organization: Some(OrganizationWorkspace {
|
||||
id: organization_id,
|
||||
access: WorkspaceAccess::default(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn organization_id(&self) -> Option<&OrganizationId> {
|
||||
self.organization
|
||||
.as_ref()
|
||||
.map(|organization| &organization.id)
|
||||
}
|
||||
|
||||
pub fn organization_access(&self) -> Option<WorkspaceAccess> {
|
||||
self.organization
|
||||
.as_ref()
|
||||
.map(|organization| organization.access)
|
||||
}
|
||||
|
||||
pub(super) fn set_organization_access(
|
||||
&mut self,
|
||||
organization_id: &OrganizationId,
|
||||
access: WorkspaceAccess,
|
||||
) -> Result<(), WorkspaceAccessError> {
|
||||
let organization = self
|
||||
.organization
|
||||
.as_mut()
|
||||
.filter(|organization| &organization.id == organization_id)
|
||||
.ok_or(WorkspaceAccessError::OrganizationMismatch)?;
|
||||
organization.access = access;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Evaluates the invitee against the current policy. The caller supplies the
|
||||
/// invitee's current membership in the owning organization, if any.
|
||||
pub fn invite(
|
||||
&self,
|
||||
invitee: UserId,
|
||||
role: WorkspaceRole,
|
||||
organization_membership: Option<&OrganizationMembership>,
|
||||
) -> Result<WorkspaceInvitation, WorkspaceAccessError> {
|
||||
self.check_access(&invitee, role, organization_membership)?;
|
||||
Ok(WorkspaceInvitation {
|
||||
workspace_id: self.id.clone(),
|
||||
invitee,
|
||||
role,
|
||||
})
|
||||
}
|
||||
|
||||
/// Rechecks policy and membership so a pending invitation cannot preserve
|
||||
/// privileges after either has changed.
|
||||
pub fn accept_invitation(
|
||||
&self,
|
||||
user: &UserId,
|
||||
invitation: &WorkspaceInvitation,
|
||||
organization_membership: Option<&OrganizationMembership>,
|
||||
) -> Result<WorkspaceMembership, WorkspaceAccessError> {
|
||||
if invitation.workspace_id != self.id || &invitation.invitee != user {
|
||||
return Err(WorkspaceAccessError::InvitationMismatch);
|
||||
}
|
||||
self.check_access(user, invitation.role, organization_membership)?;
|
||||
Ok(WorkspaceMembership {
|
||||
workspace_id: self.id.clone(),
|
||||
user_id: user.clone(),
|
||||
role: invitation.role,
|
||||
})
|
||||
}
|
||||
|
||||
/// Assembles an ordered tree of document nodes in this workspace.
|
||||
pub fn document_tree(
|
||||
&self,
|
||||
root: Document,
|
||||
descendants: impl IntoIterator<Item = Document>,
|
||||
) -> Result<DocumentTree, TreeError> {
|
||||
if root.key.workspace != self.id {
|
||||
return Err(TreeError::WorkspaceMismatch);
|
||||
}
|
||||
DocumentTree::new(root, descendants)
|
||||
}
|
||||
|
||||
fn check_access(
|
||||
&self,
|
||||
user: &UserId,
|
||||
role: WorkspaceRole,
|
||||
membership: Option<&OrganizationMembership>,
|
||||
) -> Result<(), WorkspaceAccessError> {
|
||||
let Some(organization) = &self.organization else {
|
||||
return Ok(());
|
||||
};
|
||||
let internal = membership.is_some_and(|membership| {
|
||||
membership.organization_id == organization.id && &membership.user_id == user
|
||||
});
|
||||
if internal || organization.access.allows_external(role) {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(WorkspaceAccessError::RoleNotAllowed)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Contextualize for Workspace {
|
||||
|
||||
@@ -0,0 +1,174 @@
|
||||
use documents::{
|
||||
Document, DocumentContent, DocumentId, DocumentKey, Organization, OrganizationId,
|
||||
OrganizationMembership, OrganizationRole, TreeError, UserId, Workspace, WorkspaceAccess,
|
||||
WorkspaceAccessError, WorkspaceId, WorkspaceRole,
|
||||
};
|
||||
|
||||
fn organization(id: &str) -> Organization {
|
||||
Organization {
|
||||
id: OrganizationId::new(id).unwrap(),
|
||||
name: id.into(),
|
||||
}
|
||||
}
|
||||
|
||||
fn member(organization: &Organization, user: &UserId) -> OrganizationMembership {
|
||||
OrganizationMembership {
|
||||
organization_id: organization.id.clone(),
|
||||
user_id: user.clone(),
|
||||
role: OrganizationRole::Member,
|
||||
}
|
||||
}
|
||||
|
||||
fn workspace_id(value: &str) -> WorkspaceId {
|
||||
WorkspaceId::new(value).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn organization_controls_the_roles_available_to_external_users() {
|
||||
use WorkspaceAccess::{ExternalEditors, ExternalViewers, MembersOnly};
|
||||
use WorkspaceRole::{Admin, Editor, Owner, Viewer};
|
||||
|
||||
let organization = organization("org");
|
||||
let mut workspace = organization.create_workspace(workspace_id("workspace"), "Workspace");
|
||||
let outside = UserId::new("outside").unwrap();
|
||||
let inside = UserId::new("inside").unwrap();
|
||||
let membership = member(&organization, &inside);
|
||||
assert_eq!(workspace.organization_id(), Some(&organization.id));
|
||||
assert_eq!(workspace.organization_access(), Some(MembersOnly));
|
||||
|
||||
for (policy, allowed) in [
|
||||
(MembersOnly, &[][..]),
|
||||
(ExternalViewers, &[Viewer][..]),
|
||||
(ExternalEditors, &[Viewer, Editor][..]),
|
||||
] {
|
||||
organization
|
||||
.set_workspace_access(&mut workspace, policy)
|
||||
.unwrap();
|
||||
for role in [Owner, Admin, Editor, Viewer] {
|
||||
let invitation = workspace.invite(outside.clone(), role, None);
|
||||
if allowed.contains(&role) {
|
||||
let membership = workspace
|
||||
.accept_invitation(&outside, &invitation.unwrap(), None)
|
||||
.unwrap();
|
||||
assert_eq!(membership.user_id, outside);
|
||||
assert_eq!(membership.workspace_id, workspace.id);
|
||||
assert_eq!(membership.role, role);
|
||||
} else {
|
||||
assert_eq!(invitation, Err(WorkspaceAccessError::RoleNotAllowed));
|
||||
}
|
||||
assert!(
|
||||
workspace
|
||||
.invite(inside.clone(), role, Some(&membership))
|
||||
.is_ok()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn acceptance_checks_the_invite_target_and_current_policy() {
|
||||
let owner = organization("owner");
|
||||
let other = organization("other");
|
||||
let mut workspace = owner.create_workspace(workspace_id("workspace"), "Workspace");
|
||||
let independent = Workspace::independent(workspace_id("personal"), "Personal");
|
||||
let outsider = UserId::new("outside").unwrap();
|
||||
let insider = UserId::new("inside").unwrap();
|
||||
let inside_membership = member(&owner, &insider);
|
||||
let other_membership = member(&other, &outsider);
|
||||
|
||||
for membership in [&inside_membership, &other_membership] {
|
||||
assert_eq!(
|
||||
workspace.invite(outsider.clone(), WorkspaceRole::Admin, Some(membership)),
|
||||
Err(WorkspaceAccessError::RoleNotAllowed)
|
||||
);
|
||||
}
|
||||
let invitation = workspace
|
||||
.invite(
|
||||
insider.clone(),
|
||||
WorkspaceRole::Admin,
|
||||
Some(&inside_membership),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
workspace.accept_invitation(&insider, &invitation, None),
|
||||
Err(WorkspaceAccessError::RoleNotAllowed)
|
||||
);
|
||||
assert_eq!(
|
||||
independent.accept_invitation(&insider, &invitation, Some(&inside_membership)),
|
||||
Err(WorkspaceAccessError::InvitationMismatch)
|
||||
);
|
||||
assert_eq!(
|
||||
workspace.accept_invitation(&outsider, &invitation, None),
|
||||
Err(WorkspaceAccessError::InvitationMismatch)
|
||||
);
|
||||
assert_eq!(independent.organization_id(), None);
|
||||
assert!(
|
||||
independent
|
||||
.invite(outsider.clone(), WorkspaceRole::Editor, None)
|
||||
.is_ok()
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
other.set_workspace_access(&mut workspace, WorkspaceAccess::ExternalEditors),
|
||||
Err(WorkspaceAccessError::OrganizationMismatch)
|
||||
);
|
||||
owner
|
||||
.set_workspace_access(&mut workspace, WorkspaceAccess::ExternalEditors)
|
||||
.unwrap();
|
||||
let external = workspace
|
||||
.invite(outsider.clone(), WorkspaceRole::Editor, None)
|
||||
.unwrap();
|
||||
owner
|
||||
.set_workspace_access(&mut workspace, WorkspaceAccess::ExternalViewers)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
workspace.accept_invitation(&outsider, &external, None),
|
||||
Err(WorkspaceAccessError::RoleNotAllowed)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn workspace_contains_nested_documents_of_different_kinds() {
|
||||
let workspace = Workspace::independent(workspace_id("workspace"), "Workspace");
|
||||
let key = |id: &str| DocumentKey {
|
||||
workspace: workspace.id.clone(),
|
||||
document: DocumentId::new(id).unwrap(),
|
||||
};
|
||||
let page = Document::new(
|
||||
key("page"),
|
||||
DocumentContent::Page {
|
||||
title: "Page".into(),
|
||||
},
|
||||
);
|
||||
let mut child_page = Document::new(
|
||||
key("subpage"),
|
||||
DocumentContent::Page {
|
||||
title: "Nested".into(),
|
||||
},
|
||||
);
|
||||
child_page.parent_id = Some(page.key.document.clone());
|
||||
let mut code = Document::new(
|
||||
key("code"),
|
||||
DocumentContent::Code {
|
||||
language: "rust".into(),
|
||||
source: "fn main() {}".into(),
|
||||
},
|
||||
);
|
||||
code.parent_id = Some(child_page.key.document.clone());
|
||||
|
||||
let tree = workspace
|
||||
.document_tree(page.clone(), [code, child_page])
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
tree.nodes()
|
||||
.map(|(depth, document)| (depth, document.key.document.as_str()))
|
||||
.collect::<Vec<_>>(),
|
||||
[(0, "page"), (1, "subpage"), (2, "code")]
|
||||
);
|
||||
|
||||
let other = Workspace::independent(workspace_id("other"), "Other");
|
||||
assert!(matches!(
|
||||
other.document_tree(page, []),
|
||||
Err(TreeError::WorkspaceMismatch)
|
||||
));
|
||||
}
|
||||
Reference in New Issue
Block a user