// Package org owns the tenant boundary: orgs, the workspaces that divide them, // and the memberships that give an identity a role in either package org import ( "errors" "fmt" "strings" "github.com/samcharles93/archie-core/internal/domain/identity" ) // OrgID and WorkspaceID identify an org and a workspace. type ( OrgID string WorkspaceID string ) // The org and workspace every record of a single-operator install, and every // record written before orgs existed, belongs to. const ( DefaultOrgID OrgID = "org-sys" DefaultWorkspaceID WorkspaceID = "default" ) var ( ErrInvalidRole = errors.New("org: invalid role") ErrInvalidOrg = errors.New("org: invalid org") ErrInvalidMembership = errors.New("org: invalid membership") ErrInvalidWorkspace = errors.New("org: invalid workspace") ErrInvalidAssignment = errors.New("org: invalid agent assignment") ErrOrgNotFound = errors.New("org: org not found") ErrOrgExists = errors.New("org: org already exists") ErrWorkspaceExists = errors.New("org: workspace already exists") ErrMembershipExists = errors.New("org: membership already exists") ErrLastOwner = errors.New("org: this is the organisation's last active owner; make another member owner first") ) // Role is a member's role in an org or one of its workspaces. type Role string const ( RoleOwner Role = "owner" RoleAdmin Role = "admin" RoleDeveloper Role = "developer" RoleViewer Role = "viewer" ) // Validate reports whether r is one of the shipped roles. func (r Role) Validate() error { switch r { case RoleOwner, RoleAdmin, RoleDeveloper, RoleViewer: return nil } return fmt.Errorf("%w: %q", ErrInvalidRole, r) } // Org is a tenant. type Org struct { ID OrgID `json:"id"` Name string `json:"name"` } // Validate requires an org to have an ID and a name. A slash is refused // because audit record keys encode the org as a path segment. func (o Org) Validate() error { if o.ID == "" || strings.TrimSpace(o.Name) == "" { return fmt.Errorf("%w: ID and name are required", ErrInvalidOrg) } if strings.Contains(string(o.ID), "/") { return fmt.Errorf("%w: ID must not contain a slash", ErrInvalidOrg) } return nil } // Workspace divides an org. Environment is a free-form attribute policies can // test; it is not required. type Workspace struct { ID WorkspaceID `json:"id"` OrgID OrgID `json:"org_id"` Name string `json:"name"` Environment string `json:"environment,omitempty"` } // Validate requires a workspace to belong to an org and to have a name. A // slash is refused for the same reason an org refuses one: audit record keys // encode scope as path segments. func (w Workspace) Validate() error { if w.OrgID == "" { return fmt.Errorf("%w: no org", ErrInvalidWorkspace) } if strings.TrimSpace(w.Name) == "" { return fmt.Errorf("%w: no name", ErrInvalidWorkspace) } if strings.Contains(string(w.ID), "/") || strings.Contains(string(w.OrgID), "/") { return fmt.Errorf("%w: IDs must not contain a slash", ErrInvalidWorkspace) } return nil } // Membership gives an identity a role in an org, or in one workspace of it // when WorkspaceID is set. type Membership struct { IdentityID identity.IdentityID `json:"identity_id"` OrgID OrgID `json:"org_id"` WorkspaceID WorkspaceID `json:"workspace_id,omitempty"` Role Role `json:"role"` } // Member is one identity's membership in an org or workspace, with the // identity's display name and kind for the dashboard. type Member struct { IdentityID identity.IdentityID `json:"identity_id"` Kind identity.Kind `json:"kind"` DisplayName string `json:"display_name"` WorkspaceID WorkspaceID `json:"workspace_id,omitempty"` Role Role `json:"role"` } // Validate requires an identity, an org and a shipped role. func (m Membership) Validate() error { if m.IdentityID == "" { return fmt.Errorf("%w: no identity", ErrInvalidMembership) } if m.OrgID == "" { return fmt.Errorf("%w: no org", ErrInvalidMembership) } return m.Role.Validate() } // AgentAssignment records the one org an agent or service identity serves. // People join orgs through memberships; agents are assigned to exactly one. type AgentAssignment struct { IdentityID identity.IdentityID `json:"identity_id"` OrgID OrgID `json:"org_id"` } // Validate requires an identity and an org. func (a AgentAssignment) Validate() error { if a.IdentityID == "" || a.OrgID == "" { return fmt.Errorf("%w: identity and org are required", ErrInvalidAssignment) } return nil }