'How to force scope/filter on a related/serialized entity

I have an entity exposed thanks to api-plateform.

The simple example here is a list of item, that should display only action of the item that has been done by the logged user.

When I try to search online for something, with my key words, I am often lead to people who add an extension (or voter): I do not want to list the item that have only actions made by me, I want to see all item, but allowed to see only the action of me. When you are in an extension, you can filter the requested object (item), but i couldnt find to filter its children (action).

I believe it is a simple use case, but I fail to find the right documentation for this, any help welcome.

In my_serialization.yaml:

App\Entity\Item:
    attributes:
        id:
            groups: ['item:read', 'item:read:collection']
        label:
            groups: ['item:read', 'item:read:collection']

App\Entity\Action:
    attributes:
        id:
            groups: ['item:read', 'item:read:collection']
        label:
            groups: ['item:read', 'item:read:collection']
        createdAt:
            groups: ['item:read', 'item:read:collection']
        createdBy:
            groups: ['item:read', 'item:read:collection']

Current result:

[
  {
    "id": 0,
    "label": "string",
    "actions": [
      {
        "id": 0,
        "label": "string",
        "creadtedAt": "2022-05-17T09:24:28.965Z",
        "creadtedBy": someone,
      },
      {
        "id": 0,
        "label": "string",
        "creadtedAt": "2022-05-17T09:24:28.965Z",
        "creadtedBy": someoneElse,
      },
      {
        "id": 0,
        "label": "string",
        "creadtedAt": "2022-05-17T09:24:28.965Z",
        "creadtedBy": me,
      }
    ],
  }
]

Expected result:

[
  {
    "id": 0,
    "label": "string",
    "actions": [
      {
        "id": 0,
        "label": "string",
        "creadtedAt": "2022-05-17T09:24:28.965Z",
        "creadtedBy": me,
      }
    ],
  }
]

As you can see on the expected result, I want the list of related action only made by me. It is not a filter, it is a forced scope.

version: "symfony/framework-bundle": 5.4.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source