Files
Docwell/Server/internal/access/spaces.go
T
tajniak81andClaude Opus 5.5 92cd81fd96 Add the slash menu, bubble menu, mentions and inline comments
Editor:
- "/" opens a filterable block menu (text, headings, lists, quote, code,
  divider, and image or file upload when the host can store files).
- Selecting text shows a formatting toolbar (bold, italic, strike, code,
  link) and, for anyone who may comment, a Comment button.
- "@" finds people in the page's workspace; the mention node is always in
  the schema so documents with mentions open in every client.
- The popups are plain DOM in the design system's slash-menu style, so
  the framework-free mobile bridge keeps building and can use them.
- Menu words come from the host; the web app passes English or Polish.

Inline comments (basic): commenters cannot edit the document, so a
comment's anchor is a pair of Yjs relative positions stored with the
comment rather than a mark in the text. Highlights follow local edits by
mapping and are re-resolved when comments change or remote edits arrive.
Pages get a comments panel (side column, or a bottom sheet on narrow
screens). Authors and space admins can delete comments.

Server: comments collection and endpoints, and a people lookup any
workspace member may use for mentions.

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

184 lines
5.6 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
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 }
// HigherSpaceRole returns the higher of two space roles ("" ranks lowest).
func HigherSpaceRole(a, b string) string {
if spaceRank[b] > spaceRank[a] {
return b
}
return a
}
// 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; the highest of the person's own
// and their groups' grants, see HigherSpaceRole). "" = 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
}
// CommentPages (writing comments) needs commenter.
func CommentPages(spaceRole string) error {
if err := ViewSpace(spaceRole); err != nil {
return err
}
if !SpaceAtLeast(spaceRole, SpaceCommenter) {
return apperr.Forbidden("commenter role required")
}
return nil
}
// DeleteComment: authors delete their own comments, space admins any.
func DeleteComment(actorID, authorID, spaceRole string) error {
if actorID != "" && actorID == authorID {
return nil
}
if !SpaceAtLeast(spaceRole, SpaceAdmin) {
return apperr.Forbidden("only the author or a space admin can delete this comment")
}
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
}
// SetSpaceGroup checks granting role in a space to a group of the space's
// workspace.
func SetSpaceGroup(actorRole, role string) error {
if !ValidSpaceRole(role) {
return apperr.Invalid("role must be admin, editor, commenter or viewer")
}
return ManageSpace(actorRole)
}
// ---- Groups ----------------------------------------------------------------
// ManageGroups (create, rename, delete, change members) needs workspace admin.
func ManageGroups(wsRole string) error {
if !AtLeast(wsRole, WorkspaceAdmin) {
return apperr.Forbidden("workspace admin role required")
}
return nil
}
// ViewGroups needs workspace membership above guest: guests see only what
// was shared with them, not the workspace's structure.
func ViewGroups(wsRole string) error {
if !AtLeast(wsRole, WorkspaceMember) {
return apperr.Forbidden("workspace member role required")
}
return nil
}