// SPDX-License-Identifier: AGPL-3.0-or-later package httpapi import ( "context" "net/http" "docwell/server/internal/access" "docwell/server/internal/directory" ) // PermissionService is the part of directory.Service for page-level access. type PermissionService interface { GetPageAccess(ctx context.Context, c access.Caller, id string) (directory.PageAccess, error) SetPageRestricted(ctx context.Context, c access.Caller, id string, on bool) (directory.PageAccess, error) SharePageWithUser(ctx context.Context, c access.Caller, id, email, role string) (directory.SharePageResult, error) SharePageWithGroup(ctx context.Context, c access.Caller, id, groupID, role string) (directory.PageAccess, error) UnsharePage(ctx context.Context, c access.Caller, id, principalType, principalID string) (directory.PageAccess, error) } func (a *api) permissionRoutes(mux *routeMux) { mux.HandleFunc("GET /api/v1/pages/{id}/access", a.requireUser(func(w http.ResponseWriter, r *http.Request) { pa, err := a.dir.GetPageAccess(r.Context(), caller(r), r.PathValue("id")) a.respond(w, pa, err) })) mux.HandleFunc("PATCH /api/v1/pages/{id}/access", a.requireUser(func(w http.ResponseWriter, r *http.Request) { var in struct{ Restricted bool } if !a.decode(w, r, &in) { return } pa, err := a.dir.SetPageRestricted(r.Context(), caller(r), r.PathValue("id"), in.Restricted) a.respond(w, pa, err) })) mux.HandleFunc("PUT /api/v1/pages/{id}/access/users", a.requireUser(func(w http.ResponseWriter, r *http.Request) { var in struct{ Email, Role string } if !a.decode(w, r, &in) { return } res, err := a.dir.SharePageWithUser(r.Context(), caller(r), r.PathValue("id"), in.Email, in.Role) a.respond(w, res, err) })) mux.HandleFunc("DELETE /api/v1/pages/{id}/access/users/{userId}", a.requireUser(func(w http.ResponseWriter, r *http.Request) { pa, err := a.dir.UnsharePage(r.Context(), caller(r), r.PathValue("id"), "user", r.PathValue("userId")) a.respond(w, pa, err) })) mux.HandleFunc("PUT /api/v1/pages/{id}/access/groups/{groupId}", a.requireUser(func(w http.ResponseWriter, r *http.Request) { var in struct{ Role string } if !a.decode(w, r, &in) { return } pa, err := a.dir.SharePageWithGroup(r.Context(), caller(r), r.PathValue("id"), r.PathValue("groupId"), in.Role) a.respond(w, pa, err) })) mux.HandleFunc("DELETE /api/v1/pages/{id}/access/groups/{groupId}", a.requireUser(func(w http.ResponseWriter, r *http.Request) { pa, err := a.dir.UnsharePage(r.Context(), caller(r), r.PathValue("id"), "group", r.PathValue("groupId")) a.respond(w, pa, err) })) }