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
| Comment | File | Size | Author |
|---|---|---|---|
| #74 | 2881776-74.patch.txt | 74.03 KB | josephr5000 |
| #68 | 2881776-68.patch | 72.54 KB | gcb |
Issue fork field_permissions-2881776
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
aschilling commentedI took a shot at it. The current version doesn't make it optional. The configuration is always per-bundle. This might overload the Field Permissions overview page, but it should keep the Drupal permissions page relatively clean, since only the field instances with "custom" field permissions are listed.
Comment #3
aschilling commentedThe old version had the problem, that it saved the FieldConfig too early and this lead to an error when using certain contrib modules (e.g. geocoder), which needed the third party settings for communication. Now the FieldConfig is saved on form submit.
Comment #4
sgurlt commentedTested and works very well in my current project :)
Comment #5
pifagor commentedComment #6
robertom commentedAttached a rerolled patch for 8.x-1.0-rc2
Comment #7
attisanwould an update path be possible for - e.g. set all permissions to bundles according to the pre-patched permissions?
Comment #8
anruetherThe patch works well, but an upgrade path would be great. Atm all existing field permissions do not have an effect any more.
Comment #9
jhedstromWe either need an update hook to address #8, or even better, make this opt-in as noted in the IS:
That way, no update hook is needed, existing sites will continue to function, and only fields that need per-bundle permissions will get them (thus preventing an explosion of permissions if all fields had per-bundler perms.)
Comment #10
codywyatt commentedWas in need of this patch with updated permissions so I created an update to address #8. The update goes through field storages transferring settings to field configs and updating roles.
Comment #11
dipakmdhrm commentedThis change results in the following error on some JsonAPI calls:
There are still some calls present to this function that pass field storage definition instead of field instance definition.
One such example in FieldPermissionsService class:
Comment #12
dipakmdhrm commentedComment #13
paulocsPatch #12 needs reroll. So I attached it.
Comment #14
paulocsThis is NOT the correct patch.
Comment #15
paulocsActually patch #13 is the right one.
I did a little mess with the files, so I'm attaching it again.
Sorry...
Comment #16
jsutta commented#12 installed and worked for me on D8.9.13. I couldn't get #13 or #14 to install.
Comment #17
jsutta commentedI noticed I was getting a similar error to the one reported in #11. Mine was the following:
Argument 3 passed to Drupal\field_permissions\Plugin\FieldPermissionType\Manager::createInstance() must implement interface Drupal\field\FieldConfigInterface or be null, instance of Drupal\field\Entity\FieldStorageConfig given, called in /mnt/www/html/vcsweb/docroot/modules/contrib/field_permissions/src/FieldPermissionsService.php on line 232 in Drupal\field_permissions\Plugin\FieldPermissionType\Manager->createInstance() (line 51 of /mnt/www/html/vcsweb/docroot/modules/contrib/field_permissions/src/Plugin/FieldPermissionType/Manager.php) #0 /mnt/www/html/vcsweb/docroot/modules/contrib/field_permissions/src/FieldPermissionsService.php(232): Drupal\field_permissions\Plugin\FieldPermissionType\Manager->createInstance('custom', Array, Object(Drupal\field\Entity\FieldStorageConfig))
I was also getting it when I would do a JSON API call (via Entity Share).
I've rerolled the patch and corrected the issue reported in #11. I also checked and permissions set at a field level before installing the patch appear to be preserved.
My site uses D8.9.13, and I created the patch using the 8.x-1.x-dev branch (hopefully I got this terminology right, I'm still learning!).
Comment #18
jsutta commentedI ran into a few more errors where the issue mentioned in #11 wasn't fixed, so I've updated my version of the patch to correct those issues. I've double-checked to make sure that that all issues that existed in my use case have been fixed.
Comment #19
gcbI'm strongly in favor of adding this to the module: at the very least, the entity-type-level distinction seems like a huge bug. Is there consideration of merging this in? Maybe it needs to be 2.x to reflect the significant change in data structure?
Comment #20
mariacha1 commentedThis is the current blocker for merging: https://www.drupal.org/project/field_permissions/issues/2881776#comment-... -- the goal is to make the per-bundle option a configuration, not something you'd have to do on all entity types for all field permissions. The current patch does not allow this, and forces all entities to use per-bundle field permissions, and based on the number of bundles per entity, that might make this page:
/admin/people/permissionsunloadable.I don't think adding the make-this-per-bundle configuration option would be too hard -- just none of the patches so far have done it!
I do also really like the idea of putting this into a 2.x branch as well though.
Comment #21
ethomas08 commentedRe-rolled patch from comment 18 for the 8.x-1.1.0 release.
Comment #22
mmbkHi, I've installed and tested #18, (not using #21, since it does not patch the actual dev-HEAD.)
So far it is working as expected, after adjusting our implemented custom plugins that extend `Drupal\field_permissions\Plugin\FieldPermissionType\Base`
Because of the interface changes, I strongly vote for leaving 1.x untouched and consider this as first new feature for the 2.x-branch
Comment #23
geek-merlinComment #24
osopolarPatch from #18 works for me too. I have many instances of a field in different bundles (and in different entity types).
In first place I could have named the fields differently for each entity type, which would have solved my issue. But I was not aware of the issue and now it would mean additional work, as it would imply creating migrations from old to new field. Luckily it got already fixed.
Would be nice if the patch could applied to the module ASAP, as there is an update hook involved. If there is a future update adding the same function (field_permissions_update_8001()) updating the patched version may get complicated.
For me it would be fine to pump the mayor version in case there are still concerns about compatibility or "performance" of the permission form (#3284722: Permissions not being saved).
Comment #25
osopolarComment #26
lendudeReroll should apply to version 1.2
Comment #28
lendudeInstall file went *poof* during reroll, let's put that back
Comment #29
osopolarComment #30
osopolarTrying to create an interdiff I got the error:
Diff between #18 and #28 https://github.com/osopolar/patchdiff/commit/26e4d7b6c6ce0506fcbb8691b95...
Comment #31
osopolarComment #32
osopolarI fixed the whitespace issue in 2881776-18.patch, see 2881776-18.whitespaces-fixed.patch by adding a whitespace on each empty line and created interdiff between #18 and #28.
Comment #33
osopolarI modified the patch from #28 to work with version 8.x-1.2. Besides the patch I added the interdiff between #28 and #32. Interdiff shows no differences between #18 and #32.
Patch does not apply to current dev version, therefore I leave this issue on needs work.
Comment #35
mariacha1 commentedI'm applying the patch from https://www.drupal.org/project/field_permissions/issues/2881776#comment-... 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)Tests are failing, so will keep reviewing.
Comment #36
mariacha1 commentedOk, 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.
Comment #37
jhedstromI think this should go into a 2.x branch as it is a substantial change.
Comment #38
lind101 commentedCan 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.
Comment #39
mariacha1 commentedI'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). This should fix https://www.drupal.org/project/field_permissions/issues/3373500 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.
Comment #40
gcbPatch reroll against 1.3.
Comment #41
gcbAnd how about a reroll that doesn't cause whitescreens!
Comment #42
gcbAnd another reroll that lets the field configuration page load.
Comment #43
gcbReroll diff.
Comment #44
mariacha1 commentedI'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.
Comment #45
mariacha1 commentedComment #46
gcbAttaching Snapshot PR of the latest version of the Merge Request by mariacha. works with the latest release and tested on D10.
Comment #47
gcbHuh. 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.
Comment #48
mariacha1 commentedI'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-....
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.
Comment #49
mariacha1 commentedComment #50
mariacha1 commentedHere's the interdiff.
Comment #51
colanHere'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)
Comment #52
sassafrass commentedI 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:
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.
Comment #53
colanComment #54
osopolar@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() can return \Drupal\Core\Field\FieldConfigInterface.
I did a diff between Merge request !10 and patch from #47, which also indicates that \Drupal\field\FieldStorageConfigInterface is the class to use (xdebug also suggests that):
- 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\field\FieldStorageConfigInterface') || !$field_definition->getTargetBundle()) {
++ if (!$field_definition->isDisplayConfigurable($context) || empty($items) || !is_a($field_definition->getFieldStorageDefinition(), '\Drupal\Core\Field\FieldConfigInterface') || !$field_definition->getTargetBundle()) {
Besides that difference the info file related stuff and the migration tests, which where removed in MR!10, I did not see other differences.
Comment #55
osopolarThis patch is the fixed version of patch from #47 (with
\Drupal\field\FieldStorageConfigInterfaceinstead of\Drupal\Core\Field\FieldConfigInterface)Comment #56
camilo.escobar commentedSimilar to @sassafrass in comment #52, 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:
I also tried with the the 8.x-2.x-dev module version and the patch didn't apply.
Comment #57
japerryMoving back to 8.x-1.x for now, in hopes we can redo the 2.x branch correctly (semantic versioning)
Comment #59
gcbAlright, 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.
Comment #60
osopolarThe reason why #55 worked for me and some others might be, that we ran the update hook under Drupal 9. In Drupal 10 and above 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.
Comment #61
osopolarCopy of patch from https://git.drupalcode.org/project/field_permissions/-/merge_requests/34... (Merge request !34) attached, to be used safely with composer".
Comment #63
idebr commentedThe new MR34 did not merge all 8.x-1.x changes correctly. I updated the original MR10. The automated tests are green again.
Comment #66
tgaugesComment #67
tgaugesComment #68
gcbhere's the latest state of the MR as a patch for stable builds:
Comment #69
andycarlbergandycarlberg made their first commit to this issue’s fork.I removed these commits because I don't think I got it right. There's more going on than simple class changes. After brief initial testing, it seems like the patch from #68 works for us for now.
Comment #70
tgaugesI merged the current 8.x-1.x version (1.5) into 2881776-consider-implementing-per-bundle: https://git.drupalcode.org/project/field_permissions/-/merge_requests/10/diffs?commit_id=e3c30a47aa8d4cf663d1db2e78be54bc037e7368
The conflicts were pretty straightforward. Tests are still good.
If you relied on a dynamic URL to patch your installation and don't want to update yet, you can download the previous diff here: https://git.drupalcode.org/project/field_permissions/-/merge_requests/10/diffs.diff?diff_id=1866781
Comment #71
megakeegman commentedI was testing patch #68 and ran into a funny issue. Notably I am only experiencing this issue with my admin permissions config, and it does not appear to have any functional consequences. It is just an issue with inconsistency in my config files.
I am only noticing this right now with 2 fields. One is called field_office_hours. The other is called field_order.
After I installed an configured field permissions with this issue patch (initially #68, but problem persists with latest on MR 10), I exported config and these are the changes I see in my admin config:
Strange because usually admin permission changes do not get managed in code like this. I did not think anything of it at the time though.
Later, I made more updates to permissions and now see this diff:
I found that if I go directly to the field_order field edit page and click save, this diff will revert and my git working tree will be clean. But if I do the same thing on the field_office_hours edit page, or click save on the overall permissions page, then this diff comes back. Like I said, I don't think it has any affect on anything functionally, but I do find this behavior a little bit concerning. Has anyone else seen something like this?
These are 2 of 9 fields on this content type. field_office_hours has no custom permissions set, where field_order is restricted to certain roles. There are multiple of both, but I have only seen these 2 permissions sneak their way into the admin permissions config file.
Comment #72
megakeegman commentedI see this in submitAdminForm (CustomAccess.php):
Which makes it look like the issue I am reporting should not be able to occur
Comment #73
megakeegman commentedFWIW, I restored my admin permissions config file to the state it was before configuring field_permissions (no permissions or dependencies listed). After doing that, I have not been able to reproduce the issue I describe in #71.
Comment #74
josephr5000 commentedHere's a patch for the changes in
#69#70 aboveCORRECTED following helpful comment in #75 below.
Comment #75
osopolarThanks for all your work. I was using the patch from #61 (based on MR !34) until 8.x-1.5 broke it, so I decided to switch to MR !10 and reviewed the changes.
The update hook was improved meanwhile, but as we already ran it we do not need to run it again. The rest of the changes are compatible with #61, so switching to MR !10 should be safe.
The patch in #74 seems to refer to #70 (not #69), as it matches diff under
https://git.drupalcode.org/project/field_permissions/-/merge_requests/10/diffs.diff?diff_id=1939210(also safe to be used with composer patch) — which I didn't know before — you get this URL from the MR overview page (https://git.drupalcode.org/project/field_permissions/-/merge_requests/10) by taking the latest "Compare with previous version" link and adding .diff before ?diff_id=1939210 and removing the start_sha parameter.