Problem/Motivation

Creating a media item with restricted (limited to certain roles) view / download access is not possible.

An example for this would be a media type "private documents" which should only be available for elevated user roles and not to anyone having "view media" permission.
Media is what the site builder expects to use for this (as it's private documents), but due to the missing permissions, you currently can't do it.

A very important point here is, how private files work in Drupal to determine their download access permission: They check the "view" permission of their parent entity. And that's what's missing here. For details see this blog post.

In #2862422: Add per-media type creation permissions for media most permissions were split into bundle permissions, but "view media" wasn't. For view unpublished $bundle media there's already an issue: #2936652: Add "view unpublished $bundle media" permissions for each media bundle but that doesn't solve the problem for published media with restricted view access.

#2904842: Make private file access handling respect the full entity reference chain goes even further by determining the media entity access by the parent entities, but that seems to be far future. For example, it's blocked by heavy tasks like #2835840: Track media usage and present it to the site builder (in the media library, media view, on media deletion confirmation, etc.)
In https://drupal.org/project/media_private_access some of the ideas were already implemented, also the view $bundle media permission requested here was implemented experimentally:
https://git.drupalcode.org/project/media_private_access/-/blob/8.x-1.x/s...

Steps to reproduce

Add a media type, which files should only be accessible for certain roles, like "customer", "team member", "forum member" or whoever.
See that it's not possible, as there's only the global "view media" permission and no view media permission per bundle.

There's no way to restrict media bundles access to certain roles without custom code, while granular permissions exist for CRUD!

Proposed resolution

  1. Introduce view $bundle media permission, if possible, utilizing #2809177: Introduce entity permission providers
  2. Grant view $bundle media for all bundles for anyone, who had view media permission before, like it was already done for create / edit / delete in #2936652: Add "view unpublished $bundle media" permissions for each media bundle and rename "View media" to "View any media"
  3. Deprecate general "view media" permission with #2925459: Deprecate generic media permissions

In the meantime & proof of concept:
We might discuss, if it makes sense to create a media_access module like other examples: https://www.drupal.org/project/block_access
OR
Revive https://www.drupal.org/project/media_private_access with Drupal 10 compatibility and make it less experimental. I contacted @marcoscano for that reason to get his feedback.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Comments

Anybody created an issue. See original summary.

anybody’s picture

Issue summary: View changes
anybody’s picture

Issue summary: View changes
anybody’s picture

Issue summary: View changes
anybody’s picture

Issue summary: View changes

#2809177: Introduce entity permission providers says:

Note that view permissions are never per-bundle cause we have no way to enforce it, we'd need query access for that (ala node access).

Is that still true after #2977231: EntityPermissionProvider should provide per-bundle view permissions has been fixed? Could someone clarify that perhaps?

Edit: I asked for clarification now in https://www.drupal.org/project/drupal/issues/2809177#comment-15006019

anybody’s picture

Mhm perhaps it makes sense to postpone this on #2809177: Introduce entity permission providers and focus on that?

It would be super nice to have some maintainer feedback here - also on that point.

anybody’s picture

anybody’s picture

Issue summary: View changes
anybody’s picture

Issue summary: View changes
anybody’s picture

Issue summary: View changes

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.