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>
447 lines
14 KiB
Go
447 lines
14 KiB
Go
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package directory
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
|
|
"docwell/server/internal/access"
|
|
"docwell/server/internal/apperr"
|
|
"docwell/server/internal/store/pocketbase"
|
|
)
|
|
|
|
// ---- Groups ----------------------------------------------------------------
|
|
//
|
|
// Groups belong to a workspace and hold some of its members. Spaces grant
|
|
// roles to groups like to people (space_members.principal_type = "group").
|
|
|
|
type Group struct {
|
|
ID string `json:"id"`
|
|
Workspace string `json:"workspace"`
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
Members int `json:"members"`
|
|
Created string `json:"created"`
|
|
}
|
|
|
|
type groupRecord struct {
|
|
ID string `json:"id"`
|
|
Workspace string `json:"workspace"`
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
Created string `json:"created"`
|
|
}
|
|
|
|
type groupMemberRecord struct {
|
|
ID string `json:"id"`
|
|
Group string `json:"group"`
|
|
User string `json:"user"`
|
|
Expand struct {
|
|
User *struct {
|
|
ID string `json:"id"`
|
|
Email string `json:"email"`
|
|
Name string `json:"name"`
|
|
} `json:"user"`
|
|
} `json:"expand"`
|
|
}
|
|
|
|
// SpaceGroup is a group's role in a space.
|
|
type SpaceGroup struct {
|
|
Group GroupRef `json:"group"`
|
|
Role string `json:"role"`
|
|
}
|
|
|
|
type GroupRef struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
// userGroups lists the ids of userID's groups in workspace wsID.
|
|
func (s *Service) userGroups(ctx context.Context, userID, wsID string) ([]string, error) {
|
|
recs, err := listAll[groupMemberRecord](ctx, s, "group_members", pocketbase.ListOptions{
|
|
Filter: fmt.Sprintf("user = '%s' && group.workspace = '%s'", userID, wsID),
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ids := make([]string, 0, len(recs))
|
|
for _, r := range recs {
|
|
ids = append(ids, r.Group)
|
|
}
|
|
return ids, nil
|
|
}
|
|
|
|
// groupGrants lists the space grants of groups; spaceID "" means every space.
|
|
func (s *Service) groupGrants(ctx context.Context, groups []string, spaceID string) ([]spaceMemberRecord, error) {
|
|
if len(groups) == 0 {
|
|
return nil, nil
|
|
}
|
|
ors := make([]string, len(groups))
|
|
for i, g := range groups {
|
|
ors[i] = fmt.Sprintf("principal_id = '%s'", g)
|
|
}
|
|
filter := "principal_type = 'group' && (" + strings.Join(ors, " || ") + ")"
|
|
if spaceID != "" {
|
|
filter += fmt.Sprintf(" && space = '%s'", spaceID)
|
|
}
|
|
return listAll[spaceMemberRecord](ctx, s, "space_members", pocketbase.ListOptions{Filter: filter})
|
|
}
|
|
|
|
// explicitSpaceRole is the highest of userID's own and their groups' roles
|
|
// in space sp ("" when neither has one).
|
|
func (s *Service) explicitSpaceRole(ctx context.Context, sp spaceRecord, userID string) (string, error) {
|
|
member, err := s.spaceMembership(ctx, sp.ID, userID)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
role := member.Role
|
|
groups, err := s.userGroups(ctx, userID, sp.Workspace)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
grants, err := s.groupGrants(ctx, groups, sp.ID)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
for _, g := range grants {
|
|
role = access.HigherSpaceRole(role, g.Role)
|
|
}
|
|
return role, nil
|
|
}
|
|
|
|
func (s *Service) groupFor(ctx context.Context, c access.Caller, id string) (groupRecord, string, error) {
|
|
if !validID(id) {
|
|
return groupRecord{}, "", apperr.NotFound("group not found")
|
|
}
|
|
g, err := pocketbase.Get[groupRecord](ctx, s.pb, "groups", id)
|
|
if err != nil {
|
|
return g, "", mapPB(err, "group not found")
|
|
}
|
|
_, wsRole, err := s.workspaceFor(ctx, c, g.Workspace)
|
|
if err != nil {
|
|
return g, "", apperr.NotFound("group not found")
|
|
}
|
|
if err := access.ViewGroups(wsRole); err != nil {
|
|
return g, "", apperr.NotFound("group not found")
|
|
}
|
|
return g, wsRole, nil
|
|
}
|
|
|
|
func (s *Service) groupView(ctx context.Context, g groupRecord) (Group, error) {
|
|
n, err := s.count(ctx, "group_members", fmt.Sprintf("group = '%s'", g.ID))
|
|
return Group{ID: g.ID, Workspace: g.Workspace, Name: g.Name, Description: g.Description, Members: n, Created: g.Created}, err
|
|
}
|
|
|
|
func groupNameError(err error) error {
|
|
var pbErr *pocketbase.Error
|
|
if errors.As(err, &pbErr) && pbErr.Status == 400 {
|
|
// The unique (workspace, name) index fails as a generic constraint error.
|
|
for _, f := range pbErr.Data {
|
|
if f.Code == "validation_not_unique" {
|
|
return apperr.Conflict("a group with this name already exists")
|
|
}
|
|
}
|
|
}
|
|
return mapPB(err, "group not found")
|
|
}
|
|
|
|
// ListGroups lists a workspace's groups (members and above).
|
|
func (s *Service) ListGroups(ctx context.Context, c access.Caller, wsID string) ([]Group, error) {
|
|
ws, wsRole, err := s.workspaceFor(ctx, c, wsID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := access.ViewGroups(wsRole); err != nil {
|
|
return nil, err
|
|
}
|
|
recs, err := listAll[groupRecord](ctx, s, "groups", pocketbase.ListOptions{
|
|
Filter: fmt.Sprintf("workspace = '%s'", ws.ID), Sort: "name",
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]Group, 0, len(recs))
|
|
for _, g := range recs {
|
|
v, err := s.groupView(ctx, g)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, v)
|
|
}
|
|
sort.SliceStable(out, func(i, j int) bool { return strings.ToLower(out[i].Name) < strings.ToLower(out[j].Name) })
|
|
return out, nil
|
|
}
|
|
|
|
type GroupInput struct {
|
|
Name *string `json:"name"`
|
|
Description *string `json:"description"`
|
|
}
|
|
|
|
func (in GroupInput) patch() (map[string]any, error) {
|
|
patch := map[string]any{}
|
|
if in.Name != nil {
|
|
name, err := cleanName(*in.Name, "Group")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
patch["name"] = name
|
|
}
|
|
if in.Description != nil {
|
|
d := strings.TrimSpace(*in.Description)
|
|
if len(d) > 500 {
|
|
return nil, apperr.InvalidFields(map[string]string{"description": "Description is too long (max 500)."})
|
|
}
|
|
patch["description"] = d
|
|
}
|
|
return patch, nil
|
|
}
|
|
|
|
// CreateGroup adds a group to a workspace (workspace admin).
|
|
func (s *Service) CreateGroup(ctx context.Context, c access.Caller, wsID string, in GroupInput) (Group, error) {
|
|
if in.Name == nil {
|
|
in.Name = new(string)
|
|
}
|
|
patch, err := in.patch()
|
|
if err != nil {
|
|
return Group{}, err
|
|
}
|
|
ws, wsRole, err := s.workspaceFor(ctx, c, wsID)
|
|
if err != nil {
|
|
return Group{}, err
|
|
}
|
|
if err := access.ManageGroups(wsRole); err != nil {
|
|
return Group{}, err
|
|
}
|
|
patch["workspace"] = ws.ID
|
|
g, err := pocketbase.Create[groupRecord](ctx, s.pb, "groups", patch)
|
|
if err != nil {
|
|
return Group{}, groupNameError(err)
|
|
}
|
|
return s.groupView(ctx, g)
|
|
}
|
|
|
|
// UpdateGroup renames a group or changes its description (workspace admin).
|
|
func (s *Service) UpdateGroup(ctx context.Context, c access.Caller, id string, in GroupInput) (Group, error) {
|
|
g, wsRole, err := s.groupFor(ctx, c, id)
|
|
if err != nil {
|
|
return Group{}, err
|
|
}
|
|
if err := access.ManageGroups(wsRole); err != nil {
|
|
return Group{}, err
|
|
}
|
|
patch, err := in.patch()
|
|
if err != nil {
|
|
return Group{}, err
|
|
}
|
|
if len(patch) > 0 {
|
|
if g, err = pocketbase.Update[groupRecord](ctx, s.pb, "groups", g.ID, patch); err != nil {
|
|
return Group{}, groupNameError(err)
|
|
}
|
|
}
|
|
return s.groupView(ctx, g)
|
|
}
|
|
|
|
// DeleteGroup deletes a group and its space grants (workspace admin).
|
|
func (s *Service) DeleteGroup(ctx context.Context, c access.Caller, id string) error {
|
|
g, wsRole, err := s.groupFor(ctx, c, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := access.ManageGroups(wsRole); err != nil {
|
|
return err
|
|
}
|
|
// space_members.principal_id is plain text, so PocketBase cannot cascade.
|
|
grants, err := s.groupGrants(ctx, []string{g.ID}, "")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, m := range grants {
|
|
if err := s.pb.Delete(ctx, "space_members", m.ID); err != nil && !pocketbase.IsNotFound(err) {
|
|
return err
|
|
}
|
|
}
|
|
s.dropPageGrants(ctx, "group", g.ID, "")
|
|
return mapPB(s.pb.Delete(ctx, "groups", g.ID), "group not found")
|
|
}
|
|
|
|
// ListGroupMembers lists a group's members (members and above).
|
|
func (s *Service) ListGroupMembers(ctx context.Context, c access.Caller, id string) ([]MemberUser, error) {
|
|
g, _, err := s.groupFor(ctx, c, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
recs, err := listAll[groupMemberRecord](ctx, s, "group_members", pocketbase.ListOptions{
|
|
Filter: fmt.Sprintf("group = '%s'", g.ID), Expand: "user",
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]MemberUser, 0, len(recs))
|
|
for _, r := range recs {
|
|
if u := r.Expand.User; u != nil {
|
|
out = append(out, MemberUser{ID: u.ID, Email: u.Email, Name: u.Name})
|
|
}
|
|
}
|
|
sort.Slice(out, func(i, j int) bool { return out[i].Email < out[j].Email })
|
|
return out, nil
|
|
}
|
|
|
|
// AddGroupMember adds a workspace member (by email) to a group (workspace admin).
|
|
func (s *Service) AddGroupMember(ctx context.Context, c access.Caller, id, email string) (MemberUser, error) {
|
|
g, wsRole, err := s.groupFor(ctx, c, id)
|
|
if err != nil {
|
|
return MemberUser{}, err
|
|
}
|
|
if err := access.ManageGroups(wsRole); err != nil {
|
|
return MemberUser{}, err
|
|
}
|
|
u, err := s.findUserByEmail(ctx, email)
|
|
if err != nil {
|
|
return MemberUser{}, err
|
|
}
|
|
ws, err := s.getWorkspace(ctx, g.Workspace)
|
|
if err != nil {
|
|
return MemberUser{}, err
|
|
}
|
|
if role, err := s.effective(ctx, u.Caller(), ws); err != nil {
|
|
return MemberUser{}, err
|
|
} else if role == "" {
|
|
return MemberUser{}, apperr.Invalid("that user is not a member of this workspace")
|
|
}
|
|
existing, err := pocketbase.List[groupMemberRecord](ctx, s.pb, "group_members", pocketbase.ListOptions{
|
|
PerPage: 1, Filter: fmt.Sprintf("group = '%s' && user = '%s'", g.ID, u.ID),
|
|
})
|
|
if err != nil {
|
|
return MemberUser{}, err
|
|
}
|
|
if len(existing.Items) == 0 {
|
|
if _, err := pocketbase.Create[groupMemberRecord](ctx, s.pb, "group_members", map[string]any{"group": g.ID, "user": u.ID}); err != nil {
|
|
return MemberUser{}, mapPB(err, "group not found")
|
|
}
|
|
}
|
|
return MemberUser{ID: u.ID, Email: u.Email, Name: u.Name}, nil
|
|
}
|
|
|
|
// RemoveGroupMember removes someone from a group (workspace admin). Idempotent.
|
|
func (s *Service) RemoveGroupMember(ctx context.Context, c access.Caller, id, userID string) error {
|
|
g, wsRole, err := s.groupFor(ctx, c, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := access.ManageGroups(wsRole); err != nil {
|
|
return err
|
|
}
|
|
if !validID(userID) {
|
|
return apperr.NotFound("member not found")
|
|
}
|
|
existing, err := pocketbase.List[groupMemberRecord](ctx, s.pb, "group_members", pocketbase.ListOptions{
|
|
PerPage: 1, Filter: fmt.Sprintf("group = '%s' && user = '%s'", g.ID, userID),
|
|
})
|
|
if err != nil || len(existing.Items) == 0 {
|
|
return err
|
|
}
|
|
return mapPB(s.pb.Delete(ctx, "group_members", existing.Items[0].ID), "member not found")
|
|
}
|
|
|
|
// dropGroupMemberships removes userID from every group of workspace wsID ("" = all; best effort).
|
|
func (s *Service) dropGroupMemberships(ctx context.Context, userID, wsID string) {
|
|
filter := fmt.Sprintf("user = '%s'", userID)
|
|
if wsID != "" {
|
|
filter += fmt.Sprintf(" && group.workspace = '%s'", wsID)
|
|
}
|
|
recs, err := listAll[groupMemberRecord](ctx, s, "group_members", pocketbase.ListOptions{Filter: filter})
|
|
if err != nil {
|
|
return
|
|
}
|
|
for _, r := range recs {
|
|
_ = s.pb.Delete(ctx, "group_members", r.ID)
|
|
}
|
|
}
|
|
|
|
// ListSpaceGroups lists the groups with a role in a space (any space role may look).
|
|
func (s *Service) ListSpaceGroups(ctx context.Context, c access.Caller, spaceID string) ([]SpaceGroup, error) {
|
|
a, err := s.spaceFor(ctx, c, spaceID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
grants, err := listAll[spaceMemberRecord](ctx, s, "space_members", pocketbase.ListOptions{
|
|
Filter: fmt.Sprintf("space = '%s' && principal_type = 'group'", a.space.ID),
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]SpaceGroup, 0, len(grants))
|
|
for _, m := range grants {
|
|
if !validID(m.PrincipalID) {
|
|
continue
|
|
}
|
|
g, err := pocketbase.Get[groupRecord](ctx, s.pb, "groups", m.PrincipalID)
|
|
if err != nil {
|
|
continue // a deleted group's leftover row
|
|
}
|
|
out = append(out, SpaceGroup{Group: GroupRef{ID: g.ID, Name: g.Name}, Role: m.Role})
|
|
}
|
|
sort.Slice(out, func(i, j int) bool { return strings.ToLower(out[i].Group.Name) < strings.ToLower(out[j].Group.Name) })
|
|
return out, nil
|
|
}
|
|
|
|
// SetSpaceGroup grants a group of the space's workspace a role in the space (space admin).
|
|
func (s *Service) SetSpaceGroup(ctx context.Context, c access.Caller, spaceID, groupID, role string) (SpaceGroup, error) {
|
|
a, err := s.spaceFor(ctx, c, spaceID)
|
|
if err != nil {
|
|
return SpaceGroup{}, err
|
|
}
|
|
if err := access.SetSpaceGroup(a.role, role); err != nil {
|
|
return SpaceGroup{}, err
|
|
}
|
|
if !validID(groupID) {
|
|
return SpaceGroup{}, apperr.NotFound("group not found")
|
|
}
|
|
g, err := pocketbase.Get[groupRecord](ctx, s.pb, "groups", groupID)
|
|
if err != nil || g.Workspace != a.space.Workspace {
|
|
return SpaceGroup{}, apperr.NotFound("group not found")
|
|
}
|
|
existing, err := pocketbase.List[spaceMemberRecord](ctx, s.pb, "space_members", pocketbase.ListOptions{
|
|
PerPage: 1, Filter: fmt.Sprintf("space = '%s' && principal_type = 'group' && principal_id = '%s'", a.space.ID, g.ID),
|
|
})
|
|
if err != nil {
|
|
return SpaceGroup{}, err
|
|
}
|
|
if len(existing.Items) > 0 {
|
|
_, err = pocketbase.Update[spaceMemberRecord](ctx, s.pb, "space_members", existing.Items[0].ID, map[string]any{"role": role})
|
|
} else {
|
|
_, err = pocketbase.Create[spaceMemberRecord](ctx, s.pb, "space_members", map[string]any{
|
|
"space": a.space.ID, "principal_type": "group", "principal_id": g.ID, "role": role,
|
|
})
|
|
}
|
|
if err != nil {
|
|
return SpaceGroup{}, mapPB(err, "space not found")
|
|
}
|
|
return SpaceGroup{Group: GroupRef{ID: g.ID, Name: g.Name}, Role: role}, nil
|
|
}
|
|
|
|
// RemoveSpaceGroup takes a group's role in a space away (space admin). Idempotent.
|
|
func (s *Service) RemoveSpaceGroup(ctx context.Context, c access.Caller, spaceID, groupID string) error {
|
|
a, err := s.spaceFor(ctx, c, spaceID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := access.ManageSpace(a.role); err != nil {
|
|
return err
|
|
}
|
|
if !validID(groupID) {
|
|
return apperr.NotFound("group not found")
|
|
}
|
|
existing, err := pocketbase.List[spaceMemberRecord](ctx, s.pb, "space_members", pocketbase.ListOptions{
|
|
PerPage: 1, Filter: fmt.Sprintf("space = '%s' && principal_type = 'group' && principal_id = '%s'", a.space.ID, groupID),
|
|
})
|
|
if err != nil || len(existing.Items) == 0 {
|
|
return err
|
|
}
|
|
return mapPB(s.pb.Delete(ctx, "space_members", existing.Items[0].ID), "group not found")
|
|
}
|