moacode / craft-fab-permissions

Restrict access to fields and tabs for particular user groups across your Craft elements.
Other
16 stars 9 forks source link

Permissions overwritten if the same field is re-used in different layouts #20

Open goraxan opened 4 years ago

goraxan commented 4 years ago

Due to how the Craft fields service handles layout updates (they delete the layout and re-create it every time https://github.com/craftcms/cms/blob/master/src/services/Fields.php#L1248) the permissions attached to one field get deleted every time a layout is saved because the ON CASCADE FK on fieldId references the fieldlayoutfields table. When the Fab service saves the permissions (in the saveFieldLayoutPermissions method) it only saves the permissions of the layout being modified at that moment, the rest remain deleted.

Is there any way to re-use a field across different layouts?

goraxan commented 4 years ago

One way of solving it would be to remove the FKs to fieldlayoutfields and fieldlayouttabs, thus avoid the implicit deletion of the current permissions every time a layout is saved, and rely on EVENT_AFTER_DELETE_FIELD_LAYOUT and EVENT_AFTER_DELETE_FIELD events to explicitly delete the related permissions upon deletion.

moacode commented 4 years ago

Thanks @goraxan, I hadn't encountered this scenario—It is a bit of a pain how Craft re-creates the layout each time. What you've suggested sounds like a good solution though so I'll have a crack and keep you updated.