Implement field permissions per-bundle (field instance)

Created on 26 May 2017, over 7 years ago
Updated 16 September 2024, 2 months ago

Problem/Motivation

This is split off from #2825905: Field permissions are being shared across entity types β†’ as a new feature request. The downside to doing this is potentially 5x as many permissions for each instance a field has:

If we were to change this to per-instance settings, for a single field, say used on 5 bundles, this change would suddenly result in 25 individual permissions, which makes management 5 times as much work as it is now (and 5 times as error prone in making a mistake in permissions). Multiply this for a site with 50 or 100 fields...

Proposed resolution

Potentially make this feature configurable so only fields that need per-bundle permissions would generate them.

Remaining tasks

* Make this feature configurable so only fields that need per-bundle permissions would generate them. Right now all permissions are stored at the bundle level.

User interface changes

API changes

Data model changes

✨ Feature request
Status

Needs work

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States jhedstrom Portland, OR

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States mariacha1

    I'm applying the patch from https://www.drupal.org/project/field_permissions/issues/2881776#comment-... ✨ Implement field permissions per-bundle (field instance) Needs work in a new merge request open here:

    https://git.drupalcode.org/project/field_permissions/-/merge_requests/10...

    It does two things.

    1. Applies to the latest version of the 8.x-1.x branch.
    2. Updates the referenced class to be Drupal\Core\Field\FieldConfigInterface instead of Drupal\field\FieldConfigInterface. The first locks us from altering permissions on fields defined using \Drupal\Core\Field\Entity\BaseFieldOverride (like the promoted field) which was technically possible with the entity-wide version of the module (although to get it to work with the UI, you needed something like https://www.drupal.org/project/base_field_override_ui β†’ )

    Let's see how tests go!

  • πŸ‡ΊπŸ‡ΈUnited States mariacha1

    Ok, at this point the only test failing is related to the d7 -> d8+ field migration, which checks to make sure that the third-party settings on the FieldStorage match between the d7 and d8+ config. Since this patch changes where the third-party settings for this module live, pushing them onto the field config (field.field) level, it's a valid failure.

    That migration process would need to be rewritten to move the settings onto the bundle level, and then this test would need to be updated.

    Alternatively, if this is going onto an 8.x-2.x branch, I suppose we could say that branch doesn't support migration from d7, and drop both the migration and drop the test. If we find ourselves beyond end of life for D7 and this hasn't been merged into a 2.x branch, that is the easy solution.

  • πŸ‡ΊπŸ‡ΈUnited States jhedstrom Portland, OR

    Alternatively, if this is going onto an 8.x-2.x branch

    I think this should go into a 2.x branch as it is a substantial change.

  • πŸ‡¬πŸ‡§United Kingdom lind101

    Can I suggest that if we're talking about a new 2.x branch that it might be worth rolling this and the following issue in together:

    https://www.drupal.org/project/field_permissions/issues/3110867 β†’

    The patch on that issue already uses patch #10 from this issue as a base.

  • Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update over 1 year ago
    Not currently mergeable.
  • πŸ‡ΊπŸ‡ΈUnited States mariacha1

    I'm attaching a patch that is compatible with the latest dev release and contains a fix to this patch that fails if you're looking at the field in a view (which does not provide a bundle, core issue: https://www.drupal.org/project/drupal/issues/2898635 πŸ› [PP-1] bundleFieldDefinitions() are not added in EntityViewsData Needs work ). This should fix https://www.drupal.org/project/field_permissions/issues/3373500 πŸ› TypeError: Drupal\field_permissions\FieldPermissionsService::fieldGetPermissionType(): Argument #1 ($field) must be of type Drupal\field\FieldStorageConfigInterface Fixed if people are seeing it.

    I'll try to get this merged into the new 2.x branch this week or next so we can stop with all the patching.

  • πŸ‡ΊπŸ‡ΈUnited States gcb

    Patch reroll against 1.3.

  • πŸ‡ΊπŸ‡ΈUnited States gcb

    And how about a reroll that doesn't cause whitescreens!

  • πŸ‡ΊπŸ‡ΈUnited States gcb

    And another reroll that lets the field configuration page load.

  • πŸ‡ΊπŸ‡ΈUnited States gcb

    Reroll diff.

  • πŸ‡ΊπŸ‡ΈUnited States mariacha1

    I'm taking steps to move this issue onto the 8.x-2.x branch, which currently matches the 8.x-1.x branch. This per-bundle permissions feature will be the major difference between branches.

  • πŸ‡ΊπŸ‡ΈUnited States mariacha1
  • πŸ‡ΊπŸ‡ΈUnited States gcb

    Attaching Snapshot PR of the latest version of the Merge Request by mariacha. works with the latest release and tested on D10.

  • πŸ‡ΊπŸ‡ΈUnited States gcb

    Huh. The last patch doesn't apply in builds because it patches the info file and the info file has extra junk in it from automated D.O. packaging. Here's one usable for those cases.

  • πŸ‡ΊπŸ‡ΈUnited States mariacha1

    I'm attaching the interdiff between 42 and 45 (ignoring the .info stuff). It's worth noting that the merge request is slightly different based on the need in https://www.drupal.org/project/field_permissions/issues/2881776#comment-... ✨ Implement field permissions per-bundle (field instance) Needs work .

    It would be good to know if we are still trying to make the per-bundle option something a user can choose to configure. In the current patches all field permissions are per-bundle and none are per-entity.

  • Status changed to Needs review 9 months ago
  • πŸ‡ΊπŸ‡ΈUnited States mariacha1
  • πŸ‡ΊπŸ‡ΈUnited States mariacha1

    Here's the interdiff.

  • πŸ‡¨πŸ‡¦Canada colan Toronto πŸ‡¨πŸ‡¦

    Here's a link to this issue's previous comment with helpful d.o markup (so we know the comment #): #2881776-35: Implement field permissions per-bundle (field instance) β†’

  • πŸ‡ΊπŸ‡ΈUnited States sassafrass

    I first tried the patch: 2881776-packaged-45.patch to the latest stable version Field Permissions: 8.x-1.3 and it applied cleanly. However, I get the following error when trying to save a new field permission:

    The website encountered an unexpected error. Try again later.
    
    RuntimeException: Adding non-existent permissions to a role is not allowed. The incorrect permissions are "create field_sidebar_sections", "edit own field_sidebar_sections", "view own field_sidebar_sections". in Drupal\user\Entity\Role->calculateDependencies() (line 207 of core/modules/user/src/Entity/Role.php).
    Drupal\Core\Config\Entity\ConfigEntityBase->preSave(Object) (Line: 179)
    Drupal\user\Entity\Role->preSave(Object) (Line: 528)
    Drupal\Core\Entity\EntityStorageBase->doPreSave(Object) (Line: 483)
    Drupal\Core\Entity\EntityStorageBase->save(Object) (Line: 257)
    Drupal\Core\Config\Entity\ConfigEntityStorage->save(Object) (Line: 352)
    Drupal\Core\Entity\EntityBase->save() (Line: 609)
    Drupal\Core\Config\Entity\ConfigEntityBase->save() (Line: 96)
    Drupal\field_permissions\Plugin\FieldPermissionType\CustomAccess->submitAdminForm(Array, Object, Object) (Line: 154)
    field_permission_field_config_edit_form_submit(Array, Object)
    call_user_func_array('field_permission_field_config_edit_form_submit', Array) (Line: 129)
    Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 67)
    Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 597)
    Drupal\Core\Form\FormBuilder->processForm('field_config_edit_form', Array, Object) (Line: 325)
    Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 73)
    Drupal\Core\Controller\FormController->getContentResult(Object, Object)
    call_user_func_array(Array, Array) (Line: 123)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 627)
    Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 181)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 76)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 28)
    Drupal\Core\StackMiddleware\ContentLength->handle(Object, 1, 1) (Line: 32)
    Drupal\big_pipe\StackMiddleware\ContentLength->handle(Object, 1, 1) (Line: 106)
    Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
    Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 36)
    Drupal\Core\StackMiddleware\AjaxPageState->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 704)
    Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

    I then installed the 8.x-2.x-dev module version and tried to apply the patch: 2881776-packaged-45.patch. It couldn't apply.

  • Status changed to Needs work 8 months ago
  • πŸ‡¨πŸ‡¦Canada colan Toronto πŸ‡¨πŸ‡¦
  • πŸ‡©πŸ‡ͺGermany osopolar πŸ‡©πŸ‡ͺ GER 🌐

    @gcb is patch in #47 actually working for you? Looking at field_permissions_entity_field_access():

    -  if (!$field_definition->isDisplayConfigurable($context) || empty($items) || !is_a($field_definition->getFieldStorageDefinition(), '\Drupal\field\FieldStorageConfigInterface')) {
    +  if (!$field_definition->isDisplayConfigurable($context) || empty($items) || !is_a($field_definition->getFieldStorageDefinition(), '\Drupal\Core\Field\FieldConfigInterface') || !$field_definition->getTargetBundle()) {
    

    I don't understand how $field_definition->getFieldStorageDefinition() will return \Drupal\Core\Field\FieldConfigInterface.

  • πŸ‡©πŸ‡ͺGermany osopolar πŸ‡©πŸ‡ͺ GER 🌐

    This patch is the fixed version of patch from #47 (with \Drupal\field\FieldStorageConfigInterface instead of \Drupal\Core\Field\FieldConfigInterface)

  • πŸ‡¨πŸ‡΄Colombia camilo.escobar

    Similar to @sassafrass in comment #52 ✨ Implement field permissions per-bundle (field instance) Needs work , I tried the patch 2881776-55--packaged-45-fixed.patch in the latest 1.3 version. It applied, but then when running the database updates, I'm getting:

      Module              Update ID   Type            Description                                                     
     ------------------- ----------- --------------- ---------------------------------------------------------------- 
      field_permissions   8001        hook_update_n   8001 - Migrates existing third party settings and permissions.  
     ------------------- ----------- --------------- ---------------------------------------------------------------- 
     // Do you wish to run the specified pending updates?: yes.                                                             
    
    >  [notice] Update started: field_permissions_update_8001
    >  [error]  Adding non-existent permissions to a role is not allowed. The incorrect permissions are "create field_1", "edit field_2", "edit field_3", "view field_4", ( ...)
    >  [error]  Update failed: field_permissions_update_8001 
     [error]  Update aborted by: field_permissions_update_8001 
     [error]  Finished performing updates. 
    

    I also tried with the the 8.x-2.x-dev module version and the patch didn't apply.

  • πŸ‡ΊπŸ‡ΈUnited States japerry KVUO

    Moving back to 8.x-1.x for now, in hopes we can redo the 2.x branch correctly (semantic versioning)

  • Pipeline finished with Failed
    4 months ago
    Total: 178s
    #238806
  • Pipeline finished with Failed
    4 months ago
    Total: 254s
    #238849
  • Pipeline finished with Failed
    4 months ago
    Total: 168s
    #238855
  • Pipeline finished with Failed
    4 months ago
    Total: 203s
    #238863
  • Pipeline finished with Failed
    4 months ago
    Total: 245s
    #238874
  • Pipeline finished with Running
    4 months ago
    #238877
  • πŸ‡ΊπŸ‡ΈUnited States gcb

    Alright, I've attempted to merge 8.x-1.4 into the work on this branch. There were a LOT of conflicts. I believe my branch is functioning as desired, but I have failed to get the automated tests fixed. I'm not terribly familiar with how these tests work, so I'd love some help getting those running on my branch.

  • πŸ‡©πŸ‡ͺGermany osopolar πŸ‡©πŸ‡ͺ GER 🌐

    The reason why #55 worked for me and some others seems to be that we still didn't upgrade to Drupal 10 πŸ™ˆ. Upgrading to Drupal 10 I found the note in upgrade status that all permissions in a user role must be defined in a module.permissions.yml file or a permissions callback, linking to the CR Permissions must exist β†’ .

  • πŸ‡©πŸ‡ͺGermany osopolar πŸ‡©πŸ‡ͺ GER 🌐
  • First commit to issue fork.
  • Pipeline finished with Failed
    16 days ago
    Total: 264s
    #325762
  • Pipeline finished with Failed
    16 days ago
    Total: 270s
    #325784
  • Pipeline finished with Success
    16 days ago
    Total: 315s
    #325825
  • Pipeline finished with Canceled
    16 days ago
    Total: 155s
    #325860
  • Pipeline finished with Failed
    16 days ago
    Total: 209s
    #325861
  • Pipeline finished with Success
    16 days ago
    Total: 181s
    #325867
  • Pipeline finished with Success
    16 days ago
    Total: 175s
    #325868
  • Pipeline finished with Success
    16 days ago
    Total: 196s
    #325880
  • πŸ‡³πŸ‡±Netherlands idebr

    The new MR34 did not merge all 8.x-1.x changes correctly. I updated the original MR10. The automated tests are green again.

  • πŸ‡³πŸ‡±Netherlands idebr

    idebr β†’ changed the visibility of the branch 2881776-consider-implementing-per-bundle_8x-14 to hidden.

Production build 0.71.5 2024