Pages can now be shared with people and groups beyond their space, and restricted so that only those grants count. A grant covers the page and everything under it. Space admins always keep access, and an editor who restricts a page keeps editor through a grant of their own. Sharing a page with someone from the organization who is outside the workspace adds them as a guest. Anyone else gets a guest invite that carries the page grant, which is made when they accept. Guests see a "limited" space holding only the pages shared with them. The page tree, breadcrumbs, search, favorites, recents, trash and import all follow the page role. Public links skip restricted subtrees. Grants are cleaned up when a member leaves or a group is deleted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
108 lines
3.6 KiB
Go
108 lines
3.6 KiB
Go
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package access
|
|
|
|
import "docwell/server/internal/apperr"
|
|
|
|
// ---- Page permissions ------------------------------------------------------
|
|
//
|
|
// Pages inherit their space's rights, and two things refine them per page:
|
|
//
|
|
// grant a user or group gets editor, commenter or viewer on a page and
|
|
// everything under it, on top of their space role. This is how a
|
|
// page is shared with a guest who cannot open the space.
|
|
// restricted a restricted page and its subtree ignore the space role and
|
|
// the grants above it: only grants on the restricted page or
|
|
// below it count. Space admins always keep admin.
|
|
//
|
|
// Page roles use the space role names; grants never give admin.
|
|
|
|
// PageNode is one step from a page up to its space root, as seen by one
|
|
// caller: whether the page is restricted and the caller's best grant on it
|
|
// ("" when none).
|
|
type PageNode struct {
|
|
Restricted bool
|
|
Grant string
|
|
}
|
|
|
|
// ValidPageGrant reports whether role can be granted on a page.
|
|
func ValidPageGrant(role string) bool { return ValidDefaultRole(role) }
|
|
|
|
// PageRole is the effective role on a page for someone with space role
|
|
// spaceRole ("" when they cannot open the space), given the chain from the
|
|
// page itself up to its top-level ancestor. "" = no access.
|
|
func PageRole(spaceRole string, chain []PageNode) string {
|
|
if SpaceAtLeast(spaceRole, SpaceAdmin) {
|
|
return SpaceAdmin
|
|
}
|
|
best := ""
|
|
for _, n := range chain {
|
|
if ValidPageGrant(n.Grant) {
|
|
best = HigherSpaceRole(best, n.Grant)
|
|
}
|
|
if n.Restricted {
|
|
return best
|
|
}
|
|
}
|
|
return HigherSpaceRole(spaceRole, best)
|
|
}
|
|
|
|
// ManagePageAccess (grants and the restricted switch) needs editor on the page.
|
|
func ManagePageAccess(pageRole string) error {
|
|
if err := ViewSpace(pageRole); err != nil {
|
|
return apperr.NotFound("page not found")
|
|
}
|
|
if !SpaceAtLeast(pageRole, SpaceEditor) {
|
|
return apperr.Forbidden("editor role required")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SetPageGrant checks granting role on a page to a user whose workspace role
|
|
// is targetWsRole ("" for a group). Workspace admins already manage every
|
|
// page, so granting them anything is pointless.
|
|
func SetPageGrant(actorPageRole, targetWsRole string, isGroup bool, role string) error {
|
|
if !ValidPageGrant(role) {
|
|
return apperr.Invalid("role must be editor, commenter or viewer")
|
|
}
|
|
if err := ManagePageAccess(actorPageRole); err != nil {
|
|
return err
|
|
}
|
|
if isGroup {
|
|
return nil
|
|
}
|
|
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 page")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// InvitePageGuest checks sharing a page with someone outside the workspace,
|
|
// who joins it as a guest. Workspace members and above may; guests may not
|
|
// bring in more people.
|
|
func InvitePageGuest(actorWsRole, actorPageRole string) error {
|
|
if err := ManagePageAccess(actorPageRole); err != nil {
|
|
return err
|
|
}
|
|
if !AtLeast(actorWsRole, WorkspaceMember) {
|
|
return apperr.Forbidden("guests cannot invite people")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// CreatePageUnder checks creating a page: under a parent it needs editor on
|
|
// the parent; at the top level, editor on the space.
|
|
func CreatePageUnder(spaceRole, parentRole string, topLevel bool) error {
|
|
if topLevel {
|
|
if spaceRole == "" {
|
|
// Someone who sees only shared pages still sees the space.
|
|
return apperr.Forbidden("only people with access to the whole space can add top-level pages")
|
|
}
|
|
return EditPages(spaceRole)
|
|
}
|
|
return EditPages(parentRole)
|
|
}
|