Files
Docwell/Server/internal/access/spaces.go
T
tajniak81andClaude Opus 5.5 a96c6ad3ce Add spaces, the page tree and the Docwell design system
Spaces and pages:
- Spaces inside workspaces, open or private, with admin/editor/commenter/viewer
  roles that cascade from the workspace role (rules in internal/access)
- Page tree per space with fractional position keys, move with cycle checks,
  trash that hides whole subtrees, restore and permanent delete
- OpenAPI, TypeScript client, and web app sidebar tree with drag-and-drop,
  page, space, trash and space settings views (EN + PL)

Design system:
- Tokens, type scale, radii and shadows from the design kit in
  @docwell/ui/theme.css; Inter, Source Serif 4 and JetBrains Mono bundled
- Vue ports of Button, TextField, Badge, Callout, Icon, Breadcrumbs,
  SlashMenu and PageTreeItem, plus Logo; favicons and web manifest
- Web app and admin panel restyled on the new tokens, in light and dark

Also: fix the Vite dev server crashing on /config.json, and stop tracking
the local Design/ and docs/ folders.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 11:14:31 +01:00

123 lines
3.8 KiB
Go

package access
import "docwell/server/internal/apperr"
// ---- Spaces and pages ------------------------------------------------------
//
// Space rights cascade from the workspace role:
//
// workspace owner/admin admin of every space in the workspace
// member explicit space role, else the space's default role in open spaces
// guest explicit space role only (guests never see open spaces by default)
//
// Space roles: admin > editor > commenter > viewer. Pages inherit their space's rights.
// Space roles stored in space_members.role.
const (
SpaceAdmin = "admin"
SpaceEditor = "editor"
SpaceCommenter = "commenter"
SpaceViewer = "viewer"
)
// Space visibilities stored in spaces.visibility.
const (
SpaceOpen = "open"
SpacePrivate = "private"
)
var spaceRank = map[string]int{SpaceViewer: 1, SpaceCommenter: 2, SpaceEditor: 3, SpaceAdmin: 4}
// ValidSpaceRole reports whether role is a space role.
func ValidSpaceRole(role string) bool { return spaceRank[role] > 0 }
// ValidDefaultRole reports whether role can be an open space's default role.
func ValidDefaultRole(role string) bool { return ValidSpaceRole(role) && role != SpaceAdmin }
// SpaceAtLeast reports whether space role has is at or above need.
func SpaceAtLeast(has, need string) bool { return has != "" && spaceRank[has] >= spaceRank[need] }
// SpaceRole is the effective role in a space for someone with workspace role
// wsRole (already resolved by WorkspaceRole) and explicit space role
// memberRole ("" when not a space member). "" = no access.
func SpaceRole(wsRole, visibility, defaultRole, memberRole string) string {
switch {
case wsRole == "":
return ""
case AtLeast(wsRole, WorkspaceAdmin):
return SpaceAdmin
case ValidSpaceRole(memberRole):
return memberRole
case visibility == SpaceOpen && wsRole != WorkspaceGuest:
if ValidDefaultRole(defaultRole) {
return defaultRole
}
return SpaceEditor
}
return ""
}
// CreateSpace and DeleteSpace need workspace admin: spaces are the workspace's structure.
func CreateSpace(wsRole string) error {
if !AtLeast(wsRole, WorkspaceAdmin) {
return apperr.Forbidden("workspace admin role required")
}
return nil
}
func DeleteSpace(wsRole string) error { return CreateSpace(wsRole) }
// ManageSpace (settings and members) needs space admin.
func ManageSpace(spaceRole string) error {
if !SpaceAtLeast(spaceRole, SpaceAdmin) {
return apperr.Forbidden("space admin role required")
}
return nil
}
// ViewSpace needs any access; a space someone cannot see does not exist for them.
func ViewSpace(spaceRole string) error {
if spaceRole == "" {
return apperr.NotFound("space not found")
}
return nil
}
// EditPages (create, rename, move, trash, restore) needs editor.
func EditPages(spaceRole string) error {
if err := ViewSpace(spaceRole); err != nil {
return err
}
if !SpaceAtLeast(spaceRole, SpaceEditor) {
return apperr.Forbidden("editor role required")
}
return nil
}
// PurgePage (permanent delete from the trash) needs space admin.
func PurgePage(spaceRole string) error {
if err := ViewSpace(spaceRole); err != nil {
return err
}
return ManageSpace(spaceRole)
}
// SetSpaceMember checks granting role to someone whose workspace role is
// targetWsRole. The target must be able to open the workspace; workspace
// admins are already space admins everywhere, so they are not listed.
func SetSpaceMember(actorRole, targetWsRole, role string) error {
if !ValidSpaceRole(role) {
return apperr.Invalid("role must be admin, editor, commenter or viewer")
}
if err := ManageSpace(actorRole); err != nil {
return err
}
if targetWsRole == "" {
return apperr.Invalid("that user is not a member of this workspace")
}
if AtLeast(targetWsRole, WorkspaceAdmin) {
return apperr.Invalid("workspace admins already manage every space")
}
return nil
}