80 lines
2.0 KiB
Rust
80 lines
2.0 KiB
Rust
use super::contextualize::Contextualize;
|
|
use super::{
|
|
id::{IdError, validate_id},
|
|
user::UserId,
|
|
workspace::{Workspace, WorkspaceAccess, WorkspaceAccessError, WorkspaceId},
|
|
};
|
|
|
|
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
|
pub struct OrganizationId(String);
|
|
|
|
impl OrganizationId {
|
|
pub const MAX_BYTES: usize = 128;
|
|
|
|
pub fn new(value: impl Into<String>) -> Result<Self, IdError> {
|
|
let value = value.into();
|
|
validate_id(&value, Self::MAX_BYTES, IdError::Organization)?;
|
|
Ok(Self(value))
|
|
}
|
|
|
|
pub fn as_str(&self) -> &str {
|
|
&self.0
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct Organization {
|
|
pub id: OrganizationId,
|
|
pub name: String,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
pub enum OrganizationRole {
|
|
Owner,
|
|
Admin,
|
|
Member,
|
|
}
|
|
|
|
/// Links a user to an organization with a role in that organization.
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct OrganizationMembership {
|
|
pub organization_id: OrganizationId,
|
|
pub user_id: UserId,
|
|
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)
|
|
}
|
|
}
|
|
|
|
impl Contextualize for OrganizationMembership {
|
|
fn render_context(&self) -> String {
|
|
let role = match self.role {
|
|
OrganizationRole::Owner => "owner",
|
|
OrganizationRole::Admin => "admin",
|
|
OrganizationRole::Member => "member",
|
|
};
|
|
format!(
|
|
"User {} is a {role} of organization {}",
|
|
self.user_id.as_str(),
|
|
self.organization_id.as_str()
|
|
)
|
|
}
|
|
}
|