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
- Sub-system maintainer review
- Commit!
| Comment | File | Size | Author |
|---|---|---|---|
| #59 | 2936652-58-fixed-endline.patch | 1.52 KB | van.dordafog |
| #58 | 2936652-58.patch | 1.55 KB | van.dordafog |
| #47 | after--patch--pic.png | 37.58 KB | vikashsoni |
| #47 | Before--patch--pic.png | 22.66 KB | vikashsoni |
| #35 | interdiff-2936652-33-35.txt | 933 bytes | acbramley |
Comments
Comment #3
nitebreedComment #4
nitebreedSee attached patch
Comment #5
nitebreedComment #6
nitebreedAdded 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.
Comment #7
mycw1991 commentedComment #9
nitebreedTest failed due to the MediaResourceTestBase class that was validating the getExpectedUnauthorizedAccessMessage(), but the message was removed. I updated this class accordingly.
Comment #10
rogierbom commentedNitpick, but for readability I would prefer to have the allowedIf() and orIf() together.
Comment #11
nitebreedUpdated the patch with the review comment in #10
Comment #14
nitebreedAfter 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.
Comment #15
rogierbom commentedLooks good to me! If the testbot agrees I would say this is RTBC!
Comment #16
xjmHm, not sure about this, for a few reasons:
Tagging for subsystem maintainer review and setting NR for further discussion.
Comment #17
xjmAlso, this is a feature request, so retitling and changing the category accordingly. Thanks!
Comment #18
acbramley commented@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.
Comment #19
amateescu commentedRe #16:
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.
Comment #20
amateescu commentedComment #22
manuel garcia commentedShould we focus instead on #2809177: Introduce entity permission providers ?
Comment #25
bucefal91 commentedHello 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.
Comment #27
Ruchi Joshi commentedApplied 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.
Comment #29
abhijith s commentedApplied patch #25 . The new permissions for items in media bundle are available after the patch

Screenshots:
before:
after:

RTBC
Comment #30
abhijith s commentedComment #31
phenaproximaThank 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. :)
Comment #32
acbramley commentedThis needs the entity's cacheable metadata as well.
Working on this and adding tests as part of DrupalGov sprint day
Comment #33
acbramley commentedComment #34
acbramley commentedUpdated IS
Comment #35
acbramley commentedAdded to Functional tests as well which seem to have a bit of overlap with the Kernel tests.
Comment #37
amneh.shawish commentedPatch #25 is working fine. The new permissions for items in media are appearing after apply the patch.
Comment #38
dxvargas commentedI'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.
Comment #39
mattdanger commentedI've also tested #35 with 8.9.13 and found no problems.
Comment #40
dxvargas commentedChanging to RTBC after feedbacks.
Comment #41
catch@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).
Comment #42
phenaproximaEDIT: Never mind; need to ponder this a bit more.
Comment #43
phenaproximaComment #45
catchRe-titling.
Comment #46
gábor hojtsyI 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.
Comment #47
vikashsoni commentedApplied patch #25 . successfully applied
After patch new permissions for items in media bundle are added
Thanks for the patch
Comment #51
smustgrave commentedUntil the subsystem maintainer can review.
Comment #52
anybodySorry 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?
Comment #53
anybodyComment #54
anybodyComment #55
dww@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).
Comment #56
anybody@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?
Comment #58
van.dordafog commentedrerolled for 10.3.10, didn't include the tests, since expecting this to get a more general solution
Comment #59
van.dordafog commented#58 doesn't apply because of windows lineend char
Comment #60
quietone commentedChanges are made on on 11.x (our main development branch) first, and are then back ported as needed according to our policies.