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>
214 lines
6.8 KiB
Go
214 lines
6.8 KiB
Go
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package directory
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"docwell/server/internal/access"
|
|
"docwell/server/internal/apperr"
|
|
"docwell/server/internal/store/pocketbase"
|
|
)
|
|
|
|
// ---- Public share links ----------------------------------------------------
|
|
//
|
|
// A share link lets anyone read a page without an account; with subpages, the
|
|
// pages under it too. Editors turn it on and off. Reads through a link never
|
|
// see trashed pages, and attachments are reachable only for shared pages.
|
|
|
|
// ShareLink is a page's public link, as editors see it.
|
|
type ShareLink struct {
|
|
Token string `json:"token"`
|
|
URL string `json:"url"`
|
|
IncludeSubpages bool `json:"includeSubpages"`
|
|
Created string `json:"created"`
|
|
}
|
|
|
|
type shareRecord struct {
|
|
ID string `json:"id"`
|
|
Page string `json:"page"`
|
|
Workspace string `json:"workspace"`
|
|
Token string `json:"token"`
|
|
IncludeSubpages bool `json:"include_subpages"`
|
|
Created string `json:"created"`
|
|
}
|
|
|
|
func (s *Service) shareView(r shareRecord) ShareLink {
|
|
return ShareLink{
|
|
Token: r.Token, URL: strings.TrimRight(s.mailer.WebURL, "/") + "/share/" + r.Token,
|
|
IncludeSubpages: r.IncludeSubpages, Created: r.Created,
|
|
}
|
|
}
|
|
|
|
func (s *Service) shareOf(ctx context.Context, pageID string) (shareRecord, error) {
|
|
res, err := pocketbase.List[shareRecord](ctx, s.pb, "share_links", pocketbase.ListOptions{
|
|
PerPage: 1, Filter: fmt.Sprintf("page = '%s'", pageID),
|
|
})
|
|
if err != nil || len(res.Items) == 0 {
|
|
return shareRecord{}, err
|
|
}
|
|
return res.Items[0], nil
|
|
}
|
|
|
|
// GetShareLink returns a page's link; ok is false when sharing is off (viewer).
|
|
func (s *Service) GetShareLink(ctx context.Context, c access.Caller, pageID string) (ShareLink, bool, error) {
|
|
p, _, err := s.pageFor(ctx, c, pageID)
|
|
if err != nil {
|
|
return ShareLink{}, false, err
|
|
}
|
|
rec, err := s.shareOf(ctx, p.ID)
|
|
if err != nil || rec.ID == "" {
|
|
return ShareLink{}, false, err
|
|
}
|
|
return s.shareView(rec), true, nil
|
|
}
|
|
|
|
// SetShareLink turns a page's link on (keeping its token if it exists) and
|
|
// sets whether it covers subpages (editor).
|
|
func (s *Service) SetShareLink(ctx context.Context, c access.Caller, pageID string, includeSubpages bool) (ShareLink, error) {
|
|
p, err := s.editablePage(ctx, c, pageID)
|
|
if err != nil {
|
|
return ShareLink{}, err
|
|
}
|
|
rec, err := s.shareOf(ctx, p.ID)
|
|
if err != nil {
|
|
return ShareLink{}, err
|
|
}
|
|
if rec.ID != "" {
|
|
rec, err = pocketbase.Update[shareRecord](ctx, s.pb, "share_links", rec.ID, map[string]any{"include_subpages": includeSubpages})
|
|
return s.shareView(rec), mapPB(err, "page not found")
|
|
}
|
|
b := make([]byte, 24)
|
|
if _, err := rand.Read(b); err != nil {
|
|
return ShareLink{}, err
|
|
}
|
|
rec, err = pocketbase.Create[shareRecord](ctx, s.pb, "share_links", map[string]any{
|
|
"page": p.ID, "workspace": p.Workspace, "token": base64.RawURLEncoding.EncodeToString(b),
|
|
"include_subpages": includeSubpages, "created_by": c.ID,
|
|
})
|
|
if err != nil {
|
|
return ShareLink{}, mapPB(err, "page not found")
|
|
}
|
|
return s.shareView(rec), nil
|
|
}
|
|
|
|
// DeleteShareLink turns a page's link off; the old URL stops working (editor).
|
|
func (s *Service) DeleteShareLink(ctx context.Context, c access.Caller, pageID string) error {
|
|
// Unlike other edits this works in the trash too: hiding a page there
|
|
// should not need a restore first.
|
|
p, a, err := s.pageFor(ctx, c, pageID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := access.EditPages(a.role); err != nil {
|
|
return err
|
|
}
|
|
rec, err := s.shareOf(ctx, p.ID)
|
|
if err != nil || rec.ID == "" {
|
|
return err
|
|
}
|
|
return mapPB(s.pb.Delete(ctx, "share_links", rec.ID), "page not found")
|
|
}
|
|
|
|
// SharedPage is what a link's visitors see.
|
|
type SharedPage struct {
|
|
ID string `json:"id"`
|
|
Title string `json:"title"`
|
|
Icon string `json:"icon"`
|
|
Updated string `json:"updated"`
|
|
Content json.RawMessage `json:"content"`
|
|
// Root is the shared page; Pages lists the pages under it when the link
|
|
// includes subpages (tree order, empty otherwise).
|
|
Root PageRef `json:"root"`
|
|
Pages []PageNode `json:"pages"`
|
|
}
|
|
|
|
// sharedScope resolves a token and checks that pageID ("" = the shared page)
|
|
// is readable through it. It returns the link and the page.
|
|
func (s *Service) sharedScope(ctx context.Context, token, pageID string) (shareRecord, pageRecord, tree, error) {
|
|
notFound := apperr.NotFound("this link does not exist or was turned off")
|
|
token = strings.TrimSpace(token)
|
|
if len(token) < 24 || len(token) > 64 || !validID(token) {
|
|
return shareRecord{}, pageRecord{}, tree{}, notFound
|
|
}
|
|
res, err := pocketbase.List[shareRecord](ctx, s.pb, "share_links", pocketbase.ListOptions{
|
|
PerPage: 1, Filter: fmt.Sprintf("token = '%s'", token),
|
|
})
|
|
if err != nil {
|
|
return shareRecord{}, pageRecord{}, tree{}, err
|
|
}
|
|
if len(res.Items) == 0 {
|
|
return shareRecord{}, pageRecord{}, tree{}, notFound
|
|
}
|
|
link := res.Items[0]
|
|
root, err := s.getPage(ctx, link.Page)
|
|
if err != nil {
|
|
return link, pageRecord{}, tree{}, notFound
|
|
}
|
|
t, err := s.loadTree(ctx, root.Space)
|
|
if err != nil {
|
|
return link, pageRecord{}, tree{}, err
|
|
}
|
|
if !t.live(root.ID) {
|
|
return link, pageRecord{}, tree{}, notFound
|
|
}
|
|
if pageID == "" || pageID == root.ID {
|
|
return link, root, t, nil
|
|
}
|
|
p, ok := t.byID[pageID]
|
|
if !ok || !link.IncludeSubpages || !t.live(p.ID) || !t.within(p.ID, root.ID) || t.restrictedBelow(p.ID, root.ID) {
|
|
return link, pageRecord{}, tree{}, apperr.NotFound("page not found")
|
|
}
|
|
return link, p, t, nil
|
|
}
|
|
|
|
// GetSharedPage returns a page through a link: the shared page, or with
|
|
// pageID one of the pages under it.
|
|
func (s *Service) GetSharedPage(ctx context.Context, token, pageID string) (SharedPage, error) {
|
|
link, p, t, err := s.sharedScope(ctx, token, pageID)
|
|
if err != nil {
|
|
return SharedPage{}, err
|
|
}
|
|
doc, err := s.docs.Content(ctx, p.ID)
|
|
if err != nil {
|
|
return SharedPage{}, err
|
|
}
|
|
root := t.byID[link.Page]
|
|
out := SharedPage{
|
|
ID: p.ID, Title: p.Title, Icon: p.Icon, Updated: p.Updated, Content: doc,
|
|
Root: PageRef{ID: root.ID, Title: root.Title, Icon: root.Icon}, Pages: []PageNode{},
|
|
}
|
|
if link.IncludeSubpages {
|
|
var walk func(parent string)
|
|
walk = func(parent string) {
|
|
for _, c := range t.children[parent] {
|
|
if c.Restricted {
|
|
continue
|
|
}
|
|
out.Pages = append(out.Pages, c.node())
|
|
walk(c.ID)
|
|
}
|
|
}
|
|
walk(root.ID)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// OpenSharedFile opens an attachment of a page readable through a link.
|
|
func (s *Service) OpenSharedFile(ctx context.Context, token, fileID string, header http.Header) (FileDownload, error) {
|
|
rec, err := s.getAttachment(ctx, fileID)
|
|
if err != nil {
|
|
return FileDownload{}, err
|
|
}
|
|
if _, _, _, err := s.sharedScope(ctx, token, rec.Page); err != nil {
|
|
return FileDownload{}, apperr.NotFound("file not found")
|
|
}
|
|
return s.openFile(ctx, rec, header)
|
|
}
|