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

  1. Add field permissions (via module or custom code) that restrict access to the directory field.
  2. Open the browser and select a sub-folder.
  3. 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

Comments

devkinetic created an issue. See original summary.

devkinetic’s picture

Issue summary: View changes
devkinetic’s picture

Issue summary: View changes
ytsurk’s picture

Your 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.

devkinetic’s picture

When I understand you right, these non-admin users still shall be able to view the folder, to select medias, right?

Yes, 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.

devkinetic’s picture

After 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:

  1. Field access check on the directory field (I'm using hook_entity_field_access).
  2. The patch from #3180139: Expose setting to enable drag and drop functionality to disable the drag and drop functionality.
  3. The patch I'm including now which adds a new permission that gets checked when pressing the "Add Media" button.

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.

devkinetic’s picture

StatusFileSize
new3.46 KB

I 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.

ytsurk’s picture

What 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?

ytsurk’s picture

Status: Active » Needs work
rang501’s picture

Status: Needs work » Closed (outdated)

Not sure, if needed anymore, reopen a new issue targeting 3.x if still needed. Marking as outdated.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.