Problem/Motivation
I have a project in which group members have permissions to create media but no permission to create a node (page). They do however have permission to edit a page and thus to add new media on that page. But when those group members tried to edit a page and opened the media library modal to upload a new image and create a new media item the popup had a ajax error after uploading the new image file.
I also noticed a difference between a media field directly added to the content type (like a teaser for example) and a media field added to a paragraph type which is referenced by a field on the content type. The problem does not happen on the teaser field but does happen on the paragraph.
After searching for a few days I discovered that after selecting a file to upload it got an access result for the entity type node:page instead of the entity type media:image. Which was of course denied since the permission to create nodes was turned off. After searching and testing some more I found out this module was using $group_state->getGroupRelationPluginId() in the openerAccess method instead of $group_state->getGroupMediaRelationPluginId() like it does in the addFormAcceess method just above it.
So I created a patch and tested the change which now works as expected.
The only problem is I'm just not sure if I'm causing unwanted side effects by making this change. So a code review is more than welcome.
Steps to reproduce
Create content type with media teaser field and paragraphs with paragraph type that references a media type.
Create a group type with permissions to edit a content type (but no permission to add new nodes).
Add permissions to add/edit media items.
As an admin: create a node and add it to the group.
Log in as a user who's member of that group and try to edit the node. See the difference when adding a new media item when editing the teaser field or when adding a new paragraph.
| Comment | File | Size | Author |
|---|---|---|---|
| #10 | 3454626-10-opener-access-host-entity.patch | 6.05 KB | harishst |
Issue fork group_media_library-3454626
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
aimevpI'm trying to making a merge request but can't figure out why I keep getting access denied in git to push my changes in the MR branch. So for now here's a patch file...
Comment #3
msnassar commentedSorry for late answer...
This is not a typo... It is intended to check the access to create/edit the root host entity (in your case the page content type). I recommend to look into this code snippet. Usually, if you have the access to edit the root host entity, you should be able to add/upload new media to it. Please, let me know your finding...
I am hiding your patch as it has a security vulnerability...
Comment #4
aimevpI'm a bit confused what to do now because clearly the code wasn't working as intended in my case. People who had node edit permission (without create permission) within a group were unable to create new media. Has this been tested in that manner? Or can you think of a reason (bad configuration for example) why this isn't working in my project?
Comment #5
msnassar commentedI did quick debug.... I confirm the issue... It seems this is due to the fact that we check for entity create access but not update. See here
I believe, in case the entity is exist, we instead have to check the access to update. You can get the entity and its type from MediaLibraryState e.g.
MediaLibraryState::fromRequest($this->requestStack->getCurrentRequest())Then to check for the access to update, we use
entityAccessfromDrupal\group\Plugin\Group\RelationHandler.As soon as I have the time, I will look deeply into it. However, patch is very welcome :)
Comment #6
socialnicheguru commentedComment #7
vistree commentedHi, I have the same setup as @aimevp. For me the patch from #2 seems to work. Can we use this patch or is it better to wait for a patch related to #5?
Comment #8
msnassar commentedHello
It is not recommended to use the patch in #2
Comment #9
naidim commentedUpdated patch. It works for me, but a second pair of eyes would be helpful.
Comment #10
harishstSetup: Group members can edit the host node and manage media, but cannot create the host entity.
#9 works when opening the media library from an existing paragraph. It still fails with 403 when adding a new nested paragraph: the entity_id is NULL, and we fall through to host create access.
The attached patch follows the approach from #5, but for the nested unsaved case:
In the field widget, store the saved form entity (host_entity_type_id / host_entity_id) in the hashed group_parameters.
In openerAccess(), if those are present, check group update on that host; otherwise keep the existing create check.
That covers both existing and newly added paragraphs on an already-saved host.
Tests are welcome!
Comment #11
harishst