Skip to content

OpenConceptLab/ocl_online#741 | Collection versions holding a resource, and contains, name only versions viewable with their HEAD - #1035

Merged
snyaggarwal merged 1 commit into
masterfrom
ocl_online-741-collection-versions-viewable
Nov 23, 2026
Merged

snyaggarwal merged 1 commit into
masterfrom
ocl_online-741-collection-versions-viewable

Conversation

@snyaggarwal

Copy link
Copy Markdown
Contributor

For OpenConceptLab/ocl_online#741. Once a collection's HEAD is made private, its released versions are private as well: has_view_access needs the HEAD to be viewable too. But the lists that name the collection versions holding a concept or mapping checked only each version's own public_access. They didn't look at its HEAD, or at whether it was active. So a signed-out user still saw those versions' URLs, which name the private collection and say which of its versions hold the resource.

What changes

  • filter_by_viewable_collection_version (in core/common/mixins.py) now applies Collection.get_viewable_versions_criteria(user) from OpenConceptLab/ocl_online#718 | A collection named in the query string counts only through versions the requester can view #1027, the query form of has_view_access:

    • the version and its HEAD must both be viewable, and the version must be active;
    • staff see everything, as before;
    • owners and org members still see their own.

    It runs as one correlated EXISTS per expansion row, looked up by the collection version's primary key. That one function covers every path the ticket lists:

    • collection_versions on concept and mapping version details, and on their versions lists with includeCollectionVersions=true;
    • /concepts/X/collection-versions/ and the mapping route like it;
    • $cascade bundles, which use the same serializers.
  • /collections/?contains=<expression> (Collection.get_base_queryset) took any version with public access. It now takes only versions that are publicly viewable: public, active, and under a public HEAD. That's the same criteria for a signed-out user. As before, it doesn't depend on who's asking.

Tests

core/collections/tests/tests_viewable_collection_versions.py sets up a public release under a public HEAD, then makes the HEAD private. It checks concepts and mappings on:

  • version detail;
  • the versions list;
  • the collection-versions/ route;
  • contains.

Signed-out users and outsiders no longer see the release. An org member and staff still do. Four of the five tests fail against master's code; the fifth checks that a public HEAD still shows its release.

Locally, with Redis unreachable as in CI, 800 tests in core.concepts, core.mappings, core.collections and tests_access_control pass, except four. The same four also fail on master in that setup: they're source-version search tests, one answering 400 on a ?q= search. The new tests also pass, and pylint is clean on the changed files.

Detail-page timing

The ticket asks for this to be checked, because the filter runs on every concept and mapping detail. To check it, a read-only EXPLAIN (ANALYZE, BUFFERS) can be run on production, using a concept that many collection versions hold. The SQL is in the PR comment.

🤖 Generated with Claude Code

…ing a resource, and contains, name only versions viewable with their HEAD
@snyaggarwal

Copy link
Copy Markdown
Contributor Author

Read-only timing check for the detail page (ocl_online#741's second criterion). This is the query a signed-out concept detail now runs for collection_versions. Put in the id of a concept row that many collection versions hold, for example a CIEL concept's latest version:

EXPLAIN (ANALYZE, BUFFERS)
SELECT c.uri
FROM collection_expansions_concepts ec
JOIN collection_expansions e ON ec.expansion_id = e.id
JOIN collections c ON e.collection_version_id = c.id
WHERE ec.concept_id = <concept row id>
  AND NOT (c.version = 'HEAD')
  AND EXISTS (
    SELECT 1 FROM collections v
    WHERE v.id = e.collection_version_id
      AND v.is_active AND v.public_access IN ('View', 'Edit')
      AND NOT EXISTS (
        SELECT 1 FROM collections h
        WHERE (h.organization_id = v.organization_id OR h.user_id = v.user_id)
          AND h.is_active AND h.mnemonic = v.mnemonic AND h.version = 'HEAD'
          AND NOT (h.public_access IN ('View', 'Edit'))
        LIMIT 1)
    LIMIT 1);

What to expect: one primary-key lookup on collections per expansion row, and one index lookup for the HEAD by owner and mnemonic. Nothing should scan collections as a whole.

@snyaggarwal snyaggarwal left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: ready to merge once CI passes. No findings on 5c11eda.

What I checked:

  • The shared check: filter_by_viewable_collection_version now applies get_viewable_versions_criteria from #1027, the query form of has_view_access, as a correlated EXISTS on the collection version's primary key:
    • staff get no filter, as before;
    • owners and org members match through own, as they did through organization__members;
    • versions under a private HEAD, and inactive versions, now drop out for everyone else.
  • What it covers: every caller goes through this function, so one change covers version detail, the versions lists (load_collection_versions, a page at a time), both collection-versions/ routes, and $cascade, which uses the same serializers.
  • contains: it now uses the signed-out criteria, so a version counts only if it's public, active and under a public HEAD. As before, the answer doesn't depend on who asks. It also now counts versions with the legacy Edit access, because PUBLIC_ACCESS_VALUES treats that as public, which is what has_view_access does.
  • The tests: four of the five new tests fail on master's code. The four access-control failures in the wider local run fail the same way on master, in source-version search, so they're unrelated.

Detail-page timing: the read-only EXPLAIN in the comment above is the last check the ticket asks for. It's a production run, for whoever has access.

@snyaggarwal
snyaggarwal merged commit b65a3ca into master Nov 23, 2026
1 of 2 checks passed
@snyaggarwal
snyaggarwal deleted the ocl_online-741-collection-versions-viewable branch November 23, 2026 12:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant