Repository basics: - CONTRIBUTING.md and CODE_OF_CONDUCT.md (Contributor Covenant 2.1) - SPDX AGPL-3.0-or-later headers on all hand-written sources, checked in CI - golangci-lint (shared .golangci.yml), ESLint and Prettier, all run in CI API codegen from api/openapi.yaml (npm run generate, drift fails CI): - Go types via oapi-codegen in Server/internal/api - a contract test that the router and the spec list the same routes - Dart models via Phone-App/tool/generate_api.dart; the app uses them Spike: shared Tiptap editor in a Flutter WebView (ADR-4) - @docwell/editor-bridge builds the editor into one HTML page with a versioned JSON protocol; Phone-App hosts it with webview_flutter and a native toolbar. Integration test passes on an Android API 35 emulator. - flutter_inappwebview dropped: its Android plugin fails on AGP 9. Spike: Wails v3 desktop (ADR-11) - system tray, docwell:// deep links (page and space only) with single-instance hand-off, and the built-in updater with a required ed25519 signing key. Verified on Windows. - regenerated build assets register the scheme and drop template placeholders. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
125 lines
3.8 KiB
Go
125 lines
3.8 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 }
|
|
|
|
// 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
|
|
}
|