Problem/Motivation

Follow up to #2862422: Add per-media type creation permissions for media and #2889855: Unpublished media entity can not be accessed by owner and update any media/delete any media access possibly cached by user, https://www.drupal.org/project/drupal/issues/2862422#comment-12253537 for some context.

Proposed resolution

Add a view any unpublished media permission OR Add per bundle view any unpublished media permissions, probably the former though!

Add per bundle permissions for viewing any unpublished media.

Remaining tasks

  1. Sub-system maintainer review
  2. Commit!

Comments

acbramley created an issue. See original summary.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

nitebreed’s picture

Assigned: Unassigned » nitebreed
nitebreed’s picture

StatusFileSize
new1.41 KB

See attached patch

nitebreed’s picture

Status: Active » Needs review
nitebreed’s picture

StatusFileSize
new3.02 KB
new2.08 KB

Added test and removed the setReason() since we now don't have a single reason why the access can return FALSE. Also, I checked other modules in core and setReason() isn't used often.

mycw1991’s picture

Issue summary: View changes

Status: Needs review » Needs work

The last submitted patch, 6: view-any-unpublished-media-2936652-6.patch, failed testing. View results

nitebreed’s picture

Status: Needs work » Needs review
StatusFileSize
new3.76 KB
new562 bytes

Test failed due to the MediaResourceTestBase class that was validating the getExpectedUnauthorizedAccessMessage(), but the message was removed. I updated this class accordingly.

rogierbom’s picture

+++ b/core/modules/media/src/MediaAccessControlHandler.php
@@ -26,10 +26,8 @@ protected function checkAccess(EntityInterface $entity, $operation, AccountInter
+          ->orIf(AccessResult::allowedIf($account->hasPermission('view any unpublished media') && !$entity->isPublished()));

Nitpick, but for readability I would prefer to have the allowedIf() and orIf() together.

nitebreed’s picture

StatusFileSize
new3.74 KB
new849 bytes

Updated the patch with the review comment in #10

The last submitted patch, 9: view-any-unpublished-media-2936652-9.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 11: view-any-unpublished-media-2936652-11.patch, failed testing. View results

nitebreed’s picture

Status: Needs work » Needs review
StatusFileSize
new4.08 KB
new2.46 KB

After some digging we found out that the message couldn't be deleted because of the different REST tests that validate CRUD access to entities. But we made the message more general since it doesn't only check on the 'view media' permission.

rogierbom’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me! If the testbot agrees I would say this is RTBC!

xjm’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: +Needs subsystem maintainer review

Hm, not sure about this, for a few reasons:

  1. We intend to deprecate the "any" media permissions (create any, edit any, etc.) in favor of per-bundle permissions. I'm not sure why a blanket permission for this would be different from those?
  2. Even nodes do not provide a blanket permission like this separate from administrator-level permissions, so there is not a core precedent for even having this permission.
  3. I'm not sure if it belongs in core. Contrib could easily provide such a feature for modules that require more advanced access control.

Tagging for subsystem maintainer review and setting NR for further discussion.

xjm’s picture

Title: Unpublished Media can only be viewed by user with administer media or the owner of the media » Add a "view any unpublished media" permission
Category: Bug report » Feature request

Also, this is a feature request, so retitling and changing the category accordingly. Thanks!

acbramley’s picture

@xjm the reason behind creating this is currently viewing an unpublished media item is specifically tied to the "administer media" permission (or being the owner) as previously stated by the issue title. It pretty much means #2862422: Add per-media type creation permissions for media is moot for anyone wanting to have a role that can author media site wide for specific bundles.

For example, say I have an author role that I only want to be able to manage Image and Document bundles. I want them to be able to manage ALL Images and Documents on the site. I also have some other bundles like Video and Audio that I don't want them to be able to author. Currently that's not possible to do (from what I can tell anyway, please correct me if I'm wrong).

If we are intending to deprecate the any permissions then we could go with my second option in the IS which is add view unpublished media per bundle.

Linking my original comment again for context https://www.drupal.org/project/drupal/issues/2862422#comment-12253537

Apologies for creating as a Bug report, that was a mistake.

amateescu’s picture

Re #16:

Even nodes do not provide a blanket permission like this separate from administrator-level permissions, so there is not a core precedent for even having this permission.

Note that there is a very old feature request for adding a "view any unpublished" permission for nodes at #273595: Move permission "view any unpublished content" from Content Moderation to Node, and many people have expressed the need for having it in core in that issue.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

bucefal91’s picture

Hello guys!

The patch from #14 no longer applied to latest Drupal core. I do understand the concerns/inputs that were given since the comment #14. I think the idea of entity permissions provider is really nice, but for now we just needed a permission to view unpublished media :)

The only difference I introduced compared to #14 is to make it per bundle (especially in the light that it is the desired direction for all the access system). I haven't worked on tests since first I just wanted to get a confirmation whether this set up of permissions is acceptable.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Ruchi Joshi’s picture

StatusFileSize
new38.48 KB
new30.2 KB
new11.89 KB
new27.04 KB
new27.06 KB
new33.77 KB
new123.38 KB
new77.27 KB

Applied patch#25 on Drupal 8.9 and Drupal 9.1 and its working for both. +1 for RTBC

Steps:
1. Visit /admin/modules
2. Install Media and Media Library on D8.9 and D9.1
3. Now Visit /admin/people/permissions
4. Check whether "View any unpublished media" permission is visible
5. Then Create an authenticated user with permissions "Image: View any unpublished media".
6. Keep this permission unchecked for anonymous user.
7. As an admin, create a node with 2 image files using entity reference field.
8. Now unpublish one of the media file.
9. Open the created node as an anonymous, only 1 image will be visible.
10. Now open the same node as authenticated user, both the image can be seen.

Screenshots are attached for the tested scenarios.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

abhijith s’s picture

StatusFileSize
new57.54 KB
new58.31 KB

Applied patch #25 . The new permissions for items in media bundle are available after the patch
Screenshots:
before:
before

after:
after

RTBC

abhijith s’s picture

Status: Needs review » Reviewed & tested by the community
phenaproxima’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Thank you for your contributions and the thorough manual testing!

This is not quite ready to go, though, because it needs automated test coverage. If anyone would like to write it but doesn't know how, the documentation at https://www.drupal.org/docs/automated-testing/phpunit-in-drupal is a good place to start. At a minimum, we need a test that proves the functionality works as intended -- that is, roles with the new permission(s) can see unpublished media of various types. It would also be useful to test that the permissions work correctly when Content Moderation is enabled, and media entities have moderation applied.

In light of this, I'm going to mark this issue as needing tests, and kick it back to "Needs work" for that. :)

acbramley’s picture

Assigned: nitebreed » acbramley
Issue tags: +DrupalGov 2020
+++ b/core/modules/media/src/MediaAccessControlHandler.php
@@ -32,6 +32,9 @@ protected function checkAccess(EntityInterface $entity, $operation, AccountInter
+          $access_result = AccessResult::allowed()->cachePerPermissions();

This needs the entity's cacheable metadata as well.

Working on this and adding tests as part of DrupalGov sprint day

acbramley’s picture

Assigned: acbramley » Unassigned
Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new3.72 KB
new2.78 KB
acbramley’s picture

Issue summary: View changes

Updated IS

acbramley’s picture

StatusFileSize
new4.78 KB
new933 bytes

Added to Functional tests as well which seem to have a bit of overlap with the Kernel tests.

The last submitted patch, 33: 2936652-33.patch, failed testing. View results

amneh.shawish’s picture

Patch #25 is working fine. The new permissions for items in media are appearing after apply the patch.

dxvargas’s picture

I've tested #35 with dev versions of 8.9 and 9.2.
I've done the same test as in #27 and checked the list of permissions as in #29.
Everything worked as expected.

mattdanger’s picture

I've also tested #35 with 8.9.13 and found no problems.

dxvargas’s picture

Status: Needs review » Reviewed & tested by the community

Changing to RTBC after feedbacks.

catch’s picture

@xjm's comment on whether this is suitable for core is from 2018, but I'm inclined to ask the same question - per-bundle permissions means a very big permissions form usually. Tagging for product manager review as well (either subsystem or product feedback would be fine here, but think we need one).

phenaproxima’s picture

EDIT: Never mind; need to ponder this a bit more.

phenaproxima’s picture

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

catch’s picture

Title: Add a "view any unpublished media" permission » Add "view unpublished $bundle media" permissions for each media bundle

Re-titling.

gábor hojtsy’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: -Needs product manager review

I am a core product manager.

I understand @acbramley's use case, however that the use case is not possible to implement with core only permissions does not mean core should provide the feature. Content (nodes) have "view own unpublished content" but it does not even have permissions for not-own unpublished items. Only if #273595: Move permission "view any unpublished content" from Content Moderation to Node lands would it have more of a blanket permission but even then not per bundle.

Why are media items special that *core* should provide this feature? Is it not possible to provide this feature as a contributed/custom project?

The node permission's pair "view own unpublished media" already exists in core.

BTW the "any" media permissions deprecation issue is #2925459: Deprecate generic media permissions.

vikashsoni’s picture

StatusFileSize
new22.66 KB
new37.58 KB

Applied patch #25 . successfully applied
After patch new permissions for items in media bundle are added
Thanks for the patch

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

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

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Postponed

Until the subsystem maintainer can review.

anybody’s picture

Sorry for the propably dumb question, but after searching for a core issue or an answer to this requirement (I only found good solutions for nodes), I'm asking myself, why only "view unpublished $bundle media"?

If you have "private" media types (with private download fields) that should only be accessible for certain roles, there doesn't seem to be a way, as there's only "View media" and "View own media".

So shouldn't this issue also solve that part and add "View" and "View own" for media?

What am I missing?

Edit:
For that reason I now created #3353503: Add "view $bundle media" permission
As written in #22 I'd vote to instead of creating another custom solution we should all focus on #2809177: Introduce entity permission providers and postpone this issue on that one... shouldn't we?

anybody’s picture

anybody’s picture

dww’s picture

@Anybody re: #52: See #3213506-12: Add a way to limit the available media items when embedding into other entities and other comments in there for why a "View own media items" permission isn't the right approach (IMHO).

anybody’s picture

@dww: Thanks! You can see our use-case in #3353503: Add "view $bundle media" permission. "View own" isn't really relevant for us, but we need to limit (only certain) media types to elevated roles. I think that's not an untypical use-case. I got your point in #52. In our case, it's expected behavior, that lower roles can neither see the media nor download the file. But let's better proceed with the discussion in #3353503: Add "view $bundle media" permission? If you'd like to have a look.

--

If anyone needs this (view own) in the meantime, the functionality could perhaps be added into the https://www.drupal.org/project/media_private_access module. Just create an issue and a MR there, so there's a solution until this might be (or not) fixed in core?

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.

van.dordafog’s picture

Version: 11.x-dev » 10.3.x-dev
StatusFileSize
new1.55 KB

rerolled for 10.3.10, didn't include the tests, since expecting this to get a more general solution

van.dordafog’s picture

StatusFileSize
new1.52 KB

#58 doesn't apply because of windows lineend char

quietone’s picture

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

Changes are made on on 11.x (our main development branch) first, and are then back ported as needed according to our policies.

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.