Review main Merged

76122[TASK] Make FormProtectionFactory injectable - part 1

Benni Mack CR +2 V +2 Hard +187 −167 updated 10/20/22, 14:08

Commit message

FormProtectionFactory is hard to mock, and relies
a lot on global state and global variables. The functionality
was built in times for 6.2, so neither PSR-7 (v7+) nor
DI (v10+) was planned.

This is a first patch of a series to change the
behaviour so FormProtectionFactory is fully injectable
via DI.

For this, FormProtectionFactory can now be instantiated,
and can determine the actual protection type by the request
object via the ->createFromRequest() method.

The existing functionality is kept (FormProtectionFactory::get()),
until all places are migrated and the static functionality
can then be deprecated.

This first patch uses DI and the new method "createFromRequest()"
in the installer.

The next patch will then migrate all other places as much as possible
to use DI with a possible - possibly with a "createForType()" method -
where the third patch will deprecate the static methods.

Review

  • Code review CR +2 Done
    • Benni Mack +2
    • Stefan Bürk +1
    • Andreas Kienast +1
  • Verified V +2 Done
    • Benni Mack +2
    • Stefan Bürk +1
    • Andreas Kienast +1
    • core-ci +1 CI

Issues 1

Files 11

Review log 2330

Show 35 earlier messages
  1. Benni Mack uploaded patch set 1 10/14/22, 16:01

  2. core-ci voted on patch set 1 V -1 10/14/22, 16:13 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/32345

  3. Benni Mack uploaded patch set 2 10/14/22, 18:51

  4. core-ci voted on patch set 2 V +1 10/14/22, 18:59 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/32347

  5. Christian Kuhn commented on patch set 2 10/14/22, 22:00

    FormProtectionFactory.php:65

    can we make $request mandatory and skip $enforcedType? there shouldn't be usage case 'enforce' is really needed and there shouldn't be use case without request, either. if there are any, those are issues in consumers, i'd say?

    FormProtectionFactory.php:212

    making this protected should be added to our list of todos ... "only public to be used in tests" is not acceptable ... and we can change it without being breaking.
  1. Benni Mack commented on patch set 2 10/16/22, 17:57

    FormProtectionFactory.php:65 Reply

    you wouldn't be able to create a formProtection in FE mode now for a logged-in backend user. This is actually quite useful to build this for Admin Panel or Workspaces in the future, I think

    FormProtectionFactory.php:212 Reply

    @internal does not harm currently yet, but hopefully FormProtectionFactory will be in really good shape in v13.0 :)
  2. Benni Mack updated the commit message in patch set 3 10/16/22, 17:58

  3. core-ci voted on patch set 3 V +1 10/16/22, 18:06 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/32363

  4. Stefan Bürk commented on patch set 3 10/17/22, 02:19

    FormProtectionFactory.php:65 Reply

    Giving this method the "right" name is hard. `createFromRequest()` is not really covering all "cases". "createFromEnforcedTypeOrRequest()" is "more" correct, also not really clear ... and must be "properly" cleared through a docblock comment ?. Not sure, if should be providing two dedicated simple factory methods and leaveing the decision to calling code - what do you think ? Little sidenote, the method docblock description is incomplete --> "Should be used instead of ...???..."
  5. Oliver Hader commented on patch set 3 10/17/22, 07:18

    FormProtectionFactory.php:65 Reply

    I also have the feeling it should be a bit more explicit, the corresponding controller should know the requirements - in terms of "when using the edit/update action one needs to be logged in with a frontend user". The guess-work is only required for type `default`, which currently uses the "best matching" strategy. Currently there are some calls to `FormProtectionFactory::get()` (implicitly using `default`) which could be more specific, e.g. using `backend` in the cases I know. However, there are still thins like form-protections being using in a Fluid template, where the actual target scope is not known (because it has not been told explicitly by the controller). For instance the HTML templates for `ext:adminpanel` do not know their backend context.

    FormProtectionFactory.php:171

    What about adjusting and using `\TYPO3\CMS\Core\Http\ApplicationType` here?
  6. Benni Mack commented on patch set 3 10/17/22, 08:30

    FormProtectionFactory.php:65 Reply

    How about we'll go with "createFromRequest" and once we touch the Admin Panel area, we'll add a "createForType" as a second method within the factory?
  7. Benni Mack uploaded patch set 4 10/17/22, 11:27

  8. core-ci voted on patch set 4 V +1 10/17/22, 11:35 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/32386

  9. Andreas Kienast commented on patch set 4 CR -1 10/17/22, 11:47

    FormProtectionFactory.php:64

    The method is non-static, so please use $this->

    FormProtectionFactory.php:75

    Could am enum be interesting here?
  10. Benni Mack commented on patch set 4 10/17/22, 13:55

    FormProtectionFactory.php:64 Reply

    Done

    FormProtectionFactory.php:75 Reply

    I use constants for the time being.
  11. Benni Mack commented on patch set 4 10/17/22, 14:02

    FormProtectionFactory.php:75 Reply

    I think we should do this at a later point.
  12. Benni Mack commented on patch set 5 10/17/22, 14:02

    Published edit on patch set 4.
  13. Andreas Kienast commented on patch set 5 10/17/22, 14:12

    FormProtectionFactory.php:75 Reply

    Ack
  14. core-ci voted on patch set 5 V +1 10/17/22, 14:19 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/32393

  15. Benni Mack commented on patch set 5 10/17/22, 14:42

    FormProtectionFactory.php:65 Reply

    solved it as mentioned

    FormProtectionFactory.php:171 Reply

    not possible right now. ApplicationType itself only exposes isBackend() or isFrontend()
  16. Benni Mack updated the commit message in patch set 6 10/17/22, 15:25

  17. core-ci voted on patch set 6 V +1 10/17/22, 15:33 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/32398

  18. Benni Mack rebased onto patch set 7 10/17/22, 15:49

  19. core-ci voted on patch set 7 V +1 10/17/22, 16:00 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/32400

  20. Andreas Kienast voted on patch set 7 CR +1 10/18/22, 07:41

  21. Benni Mack commented on patch set 7 10/18/22, 10:57

    FormProtectionFactory.php:212 Reply

    Done
  22. Stefan Bürk voted on patch set 7 V +1 CR +1 10/18/22, 10:59

  23. Andreas Kienast voted on patch set 7 V +1 10/20/22, 14:06

  24. Benni Mack voted on patch set 7 V +2 CR +2 10/20/22, 14:08

  25. Benni Mack merged patch set 8 10/20/22, 14:08