// SPDX-License-Identifier: AGPL-3.0-or-later package access import "docwell/server/internal/apperr" // ---- Invites --------------------------------------------------------------- // // An invite is a promise to create an account (or attach an existing one // without an organization) later. Inviting follows the same rules as doing the // thing directly: creating a user for organization invites, adding a member // for workspace invites. // InviteToOrg checks inviting someone with instance role into organization org // and returns the organization the invite is for (see CreateUser). func InviteToOrg(c Caller, role, org string) (string, error) { return CreateUser(c, role, org) } // InviteToWorkspace checks a workspace invite: the inviter (effective role // actorRole) may grant role up to their own rank. func InviteToWorkspace(actorRole, role string) error { if !ValidWorkspaceRole(role) { return apperr.Invalid("role must be owner, admin, member or guest") } if !AtLeast(actorRole, WorkspaceAdmin) { return apperr.Forbidden("workspace admin role required") } if workspaceRank[role] > workspaceRank[actorRole] { return apperr.Forbidden("you cannot grant a role above your own") } return nil } // ManageInvite checks revoking or resending an invite for organization org. // The inviter may always; otherwise a manager of that organization, or // (wsRole, for workspace invites) a workspace admin. func ManageInvite(c Caller, invitedBy, org, wsRole string) error { switch { case c.ID == invitedBy, c.IsSuperadmin(): return nil case c.Role == RoleAdmin && c.OrgID != "" && c.OrgID == org: return nil case AtLeast(wsRole, WorkspaceAdmin): return nil } return apperr.Forbidden("you cannot manage this invite") } // AcceptInvite checks attaching an existing account (instance role role, // organization userOrg) to the invite's organization org. func AcceptInvite(role, userOrg, org string) error { if userOrg == org { return nil } if userOrg != "" { return apperr.Conflict("your account already belongs to another organization") } if role == RoleSuperadmin { // Superadmins span every organization; attaching one would demote them. return apperr.Conflict("a superadmin account cannot join an organization") } return nil }