Model workspace invitation policies and document ownership

This commit is contained in:
2026-12-02 15:39:26 +08:00
parent 83a30fad4a
commit 0a74ef159b
4 changed files with 377 additions and 4 deletions
+4 -1
View File
@@ -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,
};
+15
View File
@@ -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)
+184 -3
View File
@@ -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 {