Define an Enum for entity operations

Created on 31 August 2023, over 1 year ago
Updated 5 August 2024, 5 months ago

Problem/Motivation

In πŸ› document the reason for 'edit' vs. 'update' operations in field and entity access operation name Fixed :

This is confusing and leads to issues like #3260920: Contact's MessageEntityTest wrongly uses 'edit' access operation on entities instead of 'update' β†’ and are difficult/impossible to statically analyze and find.

Which has me thinking -- could we declare somewhere the possible values of $operation, so that the access() method can check it's getting a sane value?

PHP Enums would seem an obvious solution, but one of the features of $operation is that's it's extensible -- contrib or custom code can invent new operations like 'duplicate' ( ✨ Introduce a "duplicate" entity operation Needs work ) or 'discombobulate' or whatever, and using custom access handlers or an access hook, control the access for that new operation. So that won't work.

Another idea would be to define the possible values as a class constant on the entity access handler. Custom entities can use a custom handler. Existing entities can have their handler replaced. However, that won't allow a case like 'duplicate', where the defining module wants to add this operation across all entity types.

So I wonder whether we simply add it to the entity annotation as an array of operation machine names.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

✨ Feature request
Status

Active

Version

11.0 πŸ”₯

Component
EntityΒ  β†’

Last updated about 15 hours ago

Created by

πŸ‡¬πŸ‡§United Kingdom joachim

Live updates comments and jobs are added and updated live.
  • Needs subsystem maintainer review

    It is used to alert the maintainer(s) of a particular core subsystem that an issue significantly impacts their subsystem, and their signoff is needed (see the governance policy draft for more information). Also, if you use this tag, make sure the issue component is set to the correct subsystem. If an issue significantly impacts more than one subsystem, use needs framework manager review instead.

Sign in to follow issues

Comments & Activities

  • Issue created by @joachim
  • πŸ‡ΊπŸ‡ΈUnited States bradjones1 Digital Nomad Life

    Hmmmm... I love the idea of an Enum, but there's def. a question of extensibility. Enums can implement interfaces, so perhaps there's some way to typehint that, and some sort of trait or static method implemented by the Enum can validate the operation? Is that crazy or too clever?

  • πŸ‡¦πŸ‡ΊAustralia acbramley

    Been thinking of something similar recently so glad to find an existing issue :)

    Definitely a +1 for Enums, we can use interfaces too as @bradjones1 says to typehint access() calls, etc.

    I can't tell you the number of times I've written edit instead of update in an entity access control handler or test and lost $time to these magical strings.

    I'm going to slightly retitle this as I think Enums are the obvious pick here, we already have enums replacing constants in core with FileExists.

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

    So if I understand this correctly, we'd define two interfaces, EntityAccessOperationInterface and FieldAccessOperationInterface.

    We'd then have in core an enum implementing each one, EntityAccess and FieldAccess, so we'd have EntityAccess::Edit, EntityAccess::Create, etc.

    A contrib module could then define something like:

    enum MyModuleEntityAccess implements EntityAccessOperationInterface {
    
      case Clone;
    }
    

    And since it implements the interface, code can pass MyModuleEntityAccess::Clone to the entity access check API.

    And for BC, we change the parameter types to EntityAccessOperationInterface|string.

  • πŸ‡ΊπŸ‡ΈUnited States bradjones1 Digital Nomad Life

    Yes.

  • πŸ‡¦πŸ‡ΊAustralia acbramley

    #4 - 100%

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

    Oh one other thing - the enums should be backed, as the access system still needs to pass string operations when invoking the various access hooks.

    (Converting those to take enums will be a whole other issue to deal with the BC.)

    Tagging as it would be good to get maintainer approval on the approach before starting work.

Production build 0.71.5 2024