MCPcopy Create free account
hub / github.com/PerpetualSoftware/pad / filterUserGrantsForCaller

Method filterUserGrantsForCaller

internal/server/server.go:2293–2343  ·  view source on GitHub ↗

filterUserGrantsForCaller narrows collGrants/itemGrants — the TARGET user's grants, already loaded by the caller — down to what the CALLER can see. Only meaningful when caller != target; handleListUserGrants skips calling this for self-queries (a user can always see their own grants). BUG-1928: han

(r *http.Request, workspaceID string, collGrants []models.CollectionGrant, itemGrants []models.ItemGrant)

Source from the content-addressed store, hash-verified

2291// call (state-agnostic; no deleted_at filter) rather than N per-grant
2292// lookups.
2293func (s *Server) filterUserGrantsForCaller(r *http.Request, workspaceID string, collGrants []models.CollectionGrant, itemGrants []models.ItemGrant) ([]models.CollectionGrant, []models.ItemGrant, error) {
2294 fullCollIDs, grantedItemIDs, err := s.guestResourceFilter(r, workspaceID)
2295 if err != nil {
2296 return nil, nil, err
2297 }
2298 if fullCollIDs == nil && grantedItemIDs == nil {
2299 // Unrestricted caller (admin/cookie session, or a member with
2300 // full collection access) — no filtering, and no further store
2301 // calls needed.
2302 return collGrants, itemGrants, nil
2303 }
2304
2305 filteredColl := make([]models.CollectionGrant, 0, len(collGrants))
2306 for _, g := range collGrants {
2307 if isCollectionVisible(g.CollectionID, fullCollIDs) {
2308 filteredColl = append(filteredColl, g)
2309 }
2310 }
2311
2312 filteredItem := make([]models.ItemGrant, 0, len(itemGrants))
2313 if len(itemGrants) > 0 {
2314 itemIDs := make([]string, len(itemGrants))
2315 for i, g := range itemGrants {
2316 itemIDs[i] = g.ItemID
2317 }
2318 refs, err := s.store.GetItemCollectionRefs(workspaceID, itemIDs)
2319 if err != nil {
2320 return nil, nil, err
2321 }
2322 collByItem := make(map[string]string, len(refs))
2323 for _, ref := range refs {
2324 collByItem[ref.ID] = ref.CollectionID
2325 }
2326 for _, g := range itemGrants {
2327 collID, ok := collByItem[g.ItemID]
2328 if !ok {
2329 // item_grants.item_id is ON DELETE CASCADE, so a grant
2330 // row can't outlive its item — this should be
2331 // unreachable. Exclude defensively rather than show a
2332 // grant with no resolvable parent.
2333 continue
2334 }
2335 item := &models.Item{ID: g.ItemID, CollectionID: collID}
2336 if s.isItemVisibleToGuest(r, workspaceID, item, fullCollIDs, grantedItemIDs) {
2337 filteredItem = append(filteredItem, g)
2338 }
2339 }
2340 }
2341
2342 return filteredColl, filteredItem, nil
2343}
2344
2345// requireEditPermission checks if the user has edit access to the given item.
2346// For regular members (editor/owner), this uses the standard role check.

Callers 1

handleListUserGrantsMethod · 0.95

Calls 4

guestResourceFilterMethod · 0.95
isItemVisibleToGuestMethod · 0.95
isCollectionVisibleFunction · 0.85
GetItemCollectionRefsMethod · 0.80

Tested by

no test coverage detected