// SPDX-License-Identifier: AGPL-3.0-or-later // Package access holds Docwell's authorization rules as pure functions, so // every decision is testable without a database. // // Rights cascade down a hierarchy (same model as DriverVault): // // superadmin every organization, every workspace // └ admin their own organization and all of its workspaces // └ user only the workspaces they were added to, in their organization // └ workspace role (owner > admin > member > guest) // // Nobody can grant a role above their own, act on someone who outranks them, // or change their own role (no self-lockout, no self-promotion). package access import ( "strings" "docwell/server/internal/apperr" ) // Instance roles stored in users.role. Empty means RoleUser. const ( RoleUser = "user" RoleAdmin = "admin" RoleSuperadmin = "superadmin" ) // Caller is the signed-in user a decision is made for. type Caller struct { ID string Role string OrgID string // "" when the user belongs to no organization } func (c Caller) IsSuperadmin() bool { return c.Role == RoleSuperadmin } // IsManager reports admin or superadmin: the roles that manage users and orgs. func (c Caller) IsManager() bool { return c.Role == RoleAdmin || c.Role == RoleSuperadmin } // NormalizeRole validates an instance role; "" becomes RoleUser. func NormalizeRole(role string) (string, bool) { switch strings.ToLower(strings.TrimSpace(role)) { case "", RoleUser: return RoleUser, true case RoleAdmin: return RoleAdmin, true case RoleSuperadmin: return RoleSuperadmin, true } return "", false } // ---- Instance -------------------------------------------------------------- // ManageInstance checks changing instance-wide settings (signup rules, // branding, mail, storage, backups): superadmins only. func ManageInstance(c Caller) error { if !c.IsSuperadmin() { return apperr.Forbidden("superadmin role required") } return nil } // ---- Users ----------------------------------------------------------------- // UserScope says which users a manager may list: all of them, or one org. // A plain user, or an admin without an organization, sees nobody. func UserScope(c Caller) (all bool, orgID string, err error) { switch { case c.IsSuperadmin(): return true, "", nil case c.Role == RoleAdmin && c.OrgID != "": return false, c.OrgID, nil case c.Role == RoleAdmin: return false, "", nil } return false, "", apperr.Forbidden("admin role required") } // CreateUser checks a new account's role and organization and returns the // organization it must be created in. Admins create only inside their own // organization and never superadmins; superadmins choose freely (or none). func CreateUser(c Caller, role, org string) (string, error) { if !c.IsManager() { return "", apperr.Forbidden("admin role required") } if c.IsSuperadmin() { return org, nil } if role == RoleSuperadmin { return "", apperr.Forbidden("only a superadmin can create superadmins") } if c.OrgID == "" { return "", apperr.Forbidden("your account is not attached to an organization") } return c.OrgID, nil } // Target is the user a management action is aimed at. type Target struct { ID string Role string OrgID string } // ManageUser checks that c may edit or delete target at all. func ManageUser(c Caller, t Target) error { if !c.IsManager() { return apperr.Forbidden("admin role required") } if c.IsSuperadmin() { return nil } if c.OrgID == "" || t.OrgID != c.OrgID { return apperr.Forbidden("user is outside your organization") } if t.Role == RoleSuperadmin { return apperr.Forbidden("you cannot manage a superadmin") } return nil } // ChangeRole checks setting target's role to role (already normalized). func ChangeRole(c Caller, t Target, role string) error { if err := ManageUser(c, t); err != nil { return err } if c.ID == t.ID && role != c.Role { return apperr.Invalid("you cannot change your own role") } if role == RoleSuperadmin && !c.IsSuperadmin() { return apperr.Forbidden("only a superadmin can grant the superadmin role") } return nil } // MoveUser checks moving target to organization org ("" = no organization). // Only superadmins move people between organizations. func MoveUser(c Caller, t Target, org string) error { if err := ManageUser(c, t); err != nil { return err } if org != t.OrgID && !c.IsSuperadmin() { return apperr.Forbidden("you cannot move users to another organization") } return nil } // DeleteUser checks deleting target. Nobody deletes their own account here. func DeleteUser(c Caller, t Target) error { if c.ID == t.ID { return apperr.Invalid("you cannot delete your own account") } return ManageUser(c, t) } // ---- Organizations --------------------------------------------------------- // OrgScope says which organizations c may list. func OrgScope(c Caller) (all bool, orgID string, err error) { return UserScope(c) } // CreateOrg checks creating an organization. A superadmin creates any number // and stays unattached; anyone else may create one only while they belong to // none, and becomes its admin (joinAsAdmin). func CreateOrg(c Caller) (joinAsAdmin bool, err error) { if c.IsSuperadmin() { return false, nil } if c.OrgID != "" { return false, apperr.Forbidden("you already belong to an organization") } return true, nil } // RenameOrg checks renaming organization org. func RenameOrg(c Caller, org string) error { if c.IsSuperadmin() { return nil } if c.Role != RoleAdmin || c.OrgID == "" || c.OrgID != org { return apperr.Forbidden("you can only rename your own organization") } return nil } // DeleteOrg checks deleting organization org. A superadmin deletes any empty // organization; an admin deletes only their own, and only as its last member // (selfDetach: they are detached and demoted to user first). func DeleteOrg(c Caller, org string) (selfDetach bool, err error) { if c.IsSuperadmin() { return false, nil } if c.Role != RoleAdmin || c.OrgID == "" || c.OrgID != org { return false, apperr.Forbidden("you can only delete your own organization") } return true, nil } // ---- Workspaces ------------------------------------------------------------ // Workspace roles stored in workspace_members.role. const ( WorkspaceOwner = "owner" WorkspaceAdmin = "admin" WorkspaceMember = "member" WorkspaceGuest = "guest" ) var workspaceRank = map[string]int{WorkspaceGuest: 1, WorkspaceMember: 2, WorkspaceAdmin: 3, WorkspaceOwner: 4} // ValidWorkspaceRole reports whether role is a workspace role. func ValidWorkspaceRole(role string) bool { return workspaceRank[role] > 0 } // AtLeast reports whether workspace role has is at or above need. func AtLeast(has, need string) bool { return workspaceRank[has] >= workspaceRank[need] && has != "" } // WorkspaceRole is c's effective role in a workspace of organization wsOrg, // given their explicit membership role ("" when not a member). "" = no access. // // The cascade: a superadmin owns every workspace; an org admin owns every // workspace of their organization; anyone else gets their membership role, // and only while they still belong to the workspace's organization. func WorkspaceRole(c Caller, wsOrg, memberRole string) string { if c.IsSuperadmin() { return WorkspaceOwner } if c.OrgID == "" || c.OrgID != wsOrg { return "" } if c.Role == RoleAdmin { return WorkspaceOwner } if ValidWorkspaceRole(memberRole) { return memberRole } return "" } // CreateWorkspace checks creating a workspace and returns its organization. // Org admins create in their own organization; superadmins must pick one. func CreateWorkspace(c Caller, org string) (string, error) { switch { case c.IsSuperadmin(): if org == "" { return "", apperr.Invalid("organization is required") } return org, nil case c.Role == RoleAdmin && c.OrgID != "": if org != "" && org != c.OrgID { return "", apperr.Forbidden("you can only create workspaces in your own organization") } return c.OrgID, nil } return "", apperr.Forbidden("only organization admins can create workspaces") } // RenameWorkspace needs workspace admin; DeleteWorkspace needs owner. func RenameWorkspace(effective string) error { if !AtLeast(effective, WorkspaceAdmin) { return apperr.Forbidden("workspace admin role required") } return nil } func DeleteWorkspace(effective string) error { if !AtLeast(effective, WorkspaceOwner) { return apperr.Forbidden("workspace owner role required") } return nil } // ViewMembers needs any access to the workspace. func ViewMembers(effective string) error { if effective == "" { return apperr.NotFound("workspace not found") } return nil } // SetMember checks granting or changing a membership: the actor (effective // role actorRole) sets target (current membership role current, "" if none) // to role. Workspace admins manage members up to their own rank and never // touch someone ranked above them; nobody changes their own role. func SetMember(actorID, actorRole, targetID, current, 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 actorID == targetID && role != current { return apperr.Invalid("you cannot change your own role") } if workspaceRank[role] > workspaceRank[actorRole] { return apperr.Forbidden("you cannot grant a role above your own") } if workspaceRank[current] > workspaceRank[actorRole] { return apperr.Forbidden("you cannot change a member ranked above you") } return nil } // RemoveMember checks removing target (membership role current). Anyone may // leave a workspace; removing others follows SetMember's ranking. func RemoveMember(actorID, actorRole, targetID, current string) error { if actorID == targetID { return nil } if !AtLeast(actorRole, WorkspaceAdmin) { return apperr.Forbidden("workspace admin role required") } if workspaceRank[current] > workspaceRank[actorRole] { return apperr.Forbidden("you cannot remove a member ranked above you") } return nil }