218 lines
6.1 KiB
Rust
218 lines
6.1 KiB
Rust
use thiserror::Error;
|
|
|
|
use super::contextualize::Contextualize;
|
|
use super::{
|
|
Document,
|
|
id::{IdError, validate_id},
|
|
organization::{OrganizationId, OrganizationMembership},
|
|
tree::{DocumentTree, TreeError},
|
|
user::UserId,
|
|
};
|
|
|
|
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
|
pub struct WorkspaceId(String);
|
|
|
|
impl WorkspaceId {
|
|
pub const MAX_BYTES: usize = 96;
|
|
|
|
pub fn new(value: impl Into<String>) -> Result<Self, IdError> {
|
|
let value = value.into();
|
|
validate_id(&value, Self::MAX_BYTES, IdError::Workspace)?;
|
|
Ok(Self(value))
|
|
}
|
|
|
|
pub fn as_str(&self) -> &str {
|
|
&self.0
|
|
}
|
|
}
|
|
|
|
#[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 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 {
|
|
fn render_context(&self) -> String {
|
|
format!("Workspace: {}", self.name)
|
|
}
|
|
}
|