Problem/Motivation
I would like to restrict the ability for users to add Media to a directory. For example, on a current project, only admins should be able to organize the media into directories.
As-is I can disallow users from creating new folders with taxonomy permissions. With #3180139: Expose setting to enable drag and drop functionality I can disable the ability to drag and drop media in the UI, but the "Add Media" form passes active_directory into the form state, which sets a hidden value:
$form['active_directory'] = [
'#type' => 'hidden',
'#value' => $this->getDirectory($form_state),
];This bypasses field permissions on the "directory" field I have configured. So even if the user does not have access to that field, the media is still added to the directory that the user was in when the button was pressed.
Steps to reproduce
- Add field permissions (via module or custom code) that restrict access to the directory field.
- Open the browser and select a sub-folder.
- Press the "Add Media" button. The field is not visible, but the Media entity is still added to the selected folder.
Proposed resolution
Implement permission checks that user has access to the directory field on create/update operations.
Alternatively, instead of using a hidden value on the form, adjust the code to explicitly set the directory field value, if it exists.
Remaining tasks
Decide on an approach and write code to implement.
User interface changes
Add a status message that lets the user know that the media they added could not be added to the folder due to permissions.
API changes
n/a
Data model changes
n/a
| Comment | File | Size | Author |
|---|---|---|---|
| #7 | media_directories-restrict_directories-3279129-7.patch | 3.46 KB | devkinetic |
Comments
Comment #2
devkinetic commentedComment #3
devkinetic commentedComment #4
ytsurkYour requirements seem more like a usecase for content moderation (core) or workflow (contrib module).
But I must agree that we have a flaw here. It should not be possible to add media to a folder (taxonomy term) where the user has no permission.
When I understand you right, these non-admin users still shall be able to view the folder, to select medias, right?
Somehow this needs fine grained permissions, where we should rely on the view, edit and delete permissions of the vocabulary, and even allow term based permissions via the permission by terms module or else. See also #3113914: Dealing with permissions of folder terms.
In the scope of this module, this is kind of high level architecture, and must expand the scope of your issue.
Still - you can alter our add media form, and unset the folder field, if the user is non-admin.
Comment #5
devkinetic commentedYes, we don't want users adding media to folders willy-nilly. Only users with the proper roles should be able to do that. We do want any user to be able to select media from within a folder for use.
I was trying to decide if this should be a presave hook that checks access to the directory field eg,
$entity->get('directory')->access('update');or if this should be a form alter.In either situation, we now run into a UX issue where if a user has a folder selected, and they try to add a new media item, normally it will upload it to that folder and select it for placement. This is especially true when using the WYSIWYG dialog. If the file is added to the root, they will have to change folders to find it. With that in mind... it seems like the better solution would be to set a form access callback so they can only press upload when the root folder is selected, and a status message gets shown so there is indication of what just happened.
Comment #6
devkinetic commentedAfter further experimentation, I determined that to accomplish this within the UI, I had to add the check to the mediaAdd() method, which is what gets called when you press the "Add Media" button. This way the message gets added to the ajax response and shows up above the browser. I've added a patch here that demonstrates that.
To summarize, currently the full solution involves:
I'll be the first to admit this is a bit...clumsy but it gets the job done :) A more complete solution would cover access to the directory field, ui drag/drop, and the add media button. I'd imagine that it could utilize the permission I wrote which amounts to "can the user upload to a directory?". That would be sufficient for the time being.
Comment #7
devkinetic commentedI combined 1, 2 and 3 from my previous comment into a new patch that all uses the same permission. This way you don't have to completely disable drag & drop with that other patch if you don't want to.
Comment #8
ytsurkWhat I don't like here is, that the permission needs to be granted. We 4K module installs, they all need to do then something.
Also - during install another permission has to be granted, there are quite a lot already ..
I would prefer a module setting, where we specify roles that are only allowed to add to root?
Drag & drop needs then to be disabled for these roles too. Maybe the setting could be named, disable DnD and only allow adding new media to root. But then it's a very project specific setting for you, not?
Comment #9
ytsurkComment #10
rang501 commentedNot sure, if needed anymore, reopen a new issue targeting 3.x if still needed. Marking as outdated.