Edit in GitHubLog an issue

Modify media library folder permissions

The media gallery gives admins the ability to upload image files in specific folders. The Storage class for images in the CMS module manages image file uploads, file retrievals, and directory creation.

Due to code changes, how you extend your media gallery depends on your version of Adobe Commerce or Magento Open Source:

Once you've listed both product names, you can call the product Commerce.

  • Commerce versions 2.4.3-p1 and later
  • Commerce versions 2.4.0-2.4.3

Commerce versions 2.4.3-p1 and higher#

In versions 2.4.3-p1 and higher, you must modify media library folder permissions in a 'config.xml' file.

For security purposes, Commerce provides media gallery access to contents in specific folders. The configuration path system/media_storage_configuration/allowed_resources/media_gallery_image_folders in config.xml defines the "Media Gallery Allowed" folders.

By default, Commerce allows the media gallery access to the following two directories under /pub/media:

  • catalog/category
  • wysiwyg

In this tutorial, you will learn how to extend "Media Gallery Allowed" folders using the config.xml file.

  1. Create a config.xml file.

    If your module does not have one, create a config.xml file under the etc directory.

  2. Add a new "Media Gallery Allowed" folder:

    Copied to your clipboard
    1<system>
    2 <media_storage_configuration>
    3 <allowed_resources>
    4 <media_gallery_image_folders>
    5 <!-- new "Media Gallery Allowed" folders -->
    6 <my_image_folder>custom_folder_name</my_image_folder>
    7 <my_catalog_image_folder>catalog/custom_folder_name</my_catalog_image_folder>
    8 </media_gallery_image_folders>
    9 </allowed_resources>
    10 </media_storage_configuration>
    11</system>

Commerce versions 2.4.0 - 2.4.3#

In versions 2.4.0 - 2.4.3, you must modify media library folder permissions in a di.xml file. If you subsequently upgrade to version 2.4.3-p1 or higher, you must implement the config.xml method.

For security purposes, Commerce does not provide Media Library access to contents in specific folders. This configuration is set in the CMS module's di.xml file and injected into the Storage class constructor.

By default, Commerce allows Media Library access to all directories under /pub/media except the following:

  • captcha
  • catalog/product
  • customer
  • downloadable
  • import
  • theme
  • theme_customization
  • tmp

In this tutorial, you will learn how to specify Media Library view permissions for folders using the di.xml file.

Step 1: Create a di.xml file#

If your module does not have one, create a di.xml file under the etc directory.

Step 2: Specify the class type configuration#

To work with the constructor arguments for the Storage class, create a new type element with the name property set to Magento\Cms\Model\Wysiwyg\Images\Storage in the di.xml file.

Under the type element, create an arguments element.

Copied to your clipboard
1<type name="Magento\Cms\Model\Wysiwyg\Images\Storage">
2 <arguments>
3 </arguments>
4</type>

Step 3: Specify an argument name#

To change the content of the dirs argument provided to the constructor, create a new argument array element with the name dirs under arguments.

Copied to your clipboard
1<type name="Magento\Cms\Model\Wysiwyg\Images\Storage">
2 <arguments>
3 <argument name="dirs" xsi:type="array">
4 </argument>
5 </arguments>
6</type>

Step 4: Specify the directory to exclude or include#

Add entries to the exclude array to extend the list of view-restricted directories.

The following configuration restricts Media Library access to content under pub/media/private-directory/:

Copied to your clipboard
1<type name="Magento\Cms\Model\Wysiwyg\Images\Storage">
2 <arguments>
3 <argument name="dirs" xsi:type="array">
4 <item name="exclude" xsi:type="array">
5 <item name="private-directory" xsi:type="array">
6 <item name="regexp" xsi:type="boolean">true</item>
7 <item name="name" xsi:type="string">pub[/\\]+media[/\\]+private-directory[/\\]*$</item>
8 </item>
9 </item>
10 </argument>
11 </arguments>
12</type>

Add entries to the include array to override directory restrictions.

The following configuration overrides the default directory restriction for the pub/media/downloadable/ directory:

Copied to your clipboard
1<type name="Magento\Cms\Model\Wysiwyg\Images\Storage">
2 <arguments>
3 <argument name="dirs" xsi:type="array">
4 <item name="include" xsi:type="array">
5 <item name="downloadable" xsi:type="array">
6 <item name="regexp" xsi:type="boolean">true</item>
7 <item name="name" xsi:type="string">pub[/\\]+media[/\\]+downloadable[/\\]*$</item>
8 </item>
9 </item>
10 </argument>
11 </arguments>
12</type>
Was this helpful?
  • Privacy
  • Terms of Use
  • Do not sell my personal information
  • AdChoices
Copyright © 2022 Adobe. All rights reserved.