Commit 6253c9a
refactor: complete middleware consolidation (phase 3) — remaining entity middleware and inline loads (#8176)
Completes the middleware consolidation started in #8166/#8169 by
refactoring all five remaining middleware classes to extend
`AbstractEntityMiddleware` and eliminating all identified inline entity
loads from route handlers.
## Middleware refactors
- **`FamilyMiddleware`** / **`PersonMiddleware`** — straightforward
extends; removes manual `MiddlewareInterface` boilerplate and legacy
`withStatus(412, ...)` error responses
- **`PersonMiddleware`** gains an optional `$routeParamName` constructor
param (default `'personId'`) to support routes whose path param is named
differently (e.g. `{userID}`)
- **`EventsMiddleware`** — standardises missing-param response to
**412** via `renderErrorJSON` (was a bespoke 400 via `renderJSON`)
- **`PropertyMiddleware`** — extends `AbstractEntityMiddleware`; the
constructor-injected `$type` filter is applied inside `loadEntity()`: if
the loaded property's class doesn't match, `null` is returned and the
base class returns a standard 404. No `process()` override needed.
- **`UserMiddleware`** — extends `AbstractEntityMiddleware` with a
`process()` override that preserves the auth logic: current-user
identity shortcut sets the attribute directly from the in-memory user;
non-admin requesting another user's data gets a 401; admin path
delegates to `parent::process()` for the DB lookup + 404 guard.
```php
// UserMiddleware — auth logic preserved, entity load delegated to parent
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$loggedInUser = AuthenticationManager::getCurrentUser();
if ($loggedInUser->getId() == $userId) {
return $handler->handle($request->withAttribute('user', $loggedInUser)); // shortcut
}
if (!$loggedInUser->isAdmin()) {
return $response->withStatus(401);
}
return parent::process($request, $handler); // loads from DB + 404 guard
}
```
## Inline entity load fixes
| Route | Was | Now |
|---|---|---|
| `GET /groups/{groupID}/roles` |
`GroupQuery::create()->findOneById($groupID)` |
`$request->getAttribute('group')` + `GroupMiddleware` |
| `DELETE /groups/{groupID}/removeperson/{userID}` |
`PersonQuery::create()->findPk($args['userID'])` |
`$request->getAttribute('person')` + `new PersonMiddleware('userID')` |
| `POST /groups/{groupID}/addperson/{userID}` | same as above | same fix
|
| `GET /api/user/{userId}/permissions` |
`UserQuery::create()->findPk($userId)` (redundant — `UserMiddleware`
already on the group) | `$request->getAttribute('user')` |
Removes now-unused `PersonQuery` import from `people-groups.php` and
`UserQuery` import from `user-admin.php`.
<!-- START COPILOT ORIGINAL PROMPT -->
<details>
<summary>Original prompt</summary>
>
> ----
>
> *This section details on the original issue you should resolve*
>
> <issue_title>refactor: complete middleware consolidation (phase 3) —
remaining entity middleware and inline loads</issue_title>
> <issue_description>## Context
>
> PR #8166 introduced `AbstractEntityMiddleware`,
`InputSanitizationMiddleware`, and
`RequestParameterValidationMiddleware`.
> PR #8169 refactored `GroupMiddleware`,
`DepositMiddleware`, `CalendarMiddleware`, and `KioskDeviceMiddleware`
to extend `AbstractEntityMiddleware`, and wired entity middleware to 17
routes.
>
> This issue tracks the **remaining work** to complete the
consolidation.
>
> ---
>
> ## 1 — Middleware files not yet extending `AbstractEntityMiddleware`
>
> | File | Route param | Attribute | Notes |
> |------|-------------|-----------|-------|
> | `Slim/Middleware/Api/FamilyMiddleware.php` | `familyId` | `family` |
Straightforward refactor |
> | `Slim/Middleware/Api/PersonMiddleware.php` | `personId` | `person` |
Straightforward refactor |
> | `Slim/Middleware/EventsMiddleware.php` | `id` | `event` | Returns
**400** (not 412) for missing param — needs `getNotFoundMessage()` +
consider moving to `Api/` |
> | `Slim/Middleware/Api/UserMiddleware.php` | `userId` | `user` | Has
additional auth checks alongside entity loading; evaluate separation |
> | `Slim/Middleware/Api/PropertyMiddleware.php` | `propertyId` |
`property` | Has constructor-injected type parameter; needs factory
pattern or override |
>
> > **Skip**: `PublicCalendarMiddleware` — loads by access token (not
route param), sets multiple attributes; not a simple PK load.
>
> ---
>
> ## 2 — Inline entity loads still in route handlers
>
> | Route | File | Line | Entity loaded | Middleware exists? |
> |-------|------|------|---------------|--------------------|
> | `GET /{groupID}/roles` | `api/routes/people/people-groups.php` |
~197 | `Group` via `GroupQuery::create()->findOneById($groupID)` | Yes —
`GroupMiddleware` |
> | `DELETE /{groupID}/removeperson/{userID}` |
`api/routes/people/people-groups.php` | ~298 | `Person` via
`PersonQuery::create()->findPk($userID)` | Yes — `PersonMiddleware` |
> | `POST /{groupID}/addperson/{userID}` |
`api/routes/people/people-groups.php` | ~337 | `Person` via
`PersonQuery::create()->findPk($userID)` | Yes — `PersonMiddleware` |
> | `GET /api/user/{userId}/permissions` |
`admin/routes/api/user-admin.php` | ~135 | `User` via
`UserQuery::create()->findPk($userId)` | Yes — `UserMiddleware` |
>
> ---
>
> ## Suggested order
>
> 1. `FamilyMiddleware` and `PersonMiddleware` — simple extends, unblock
the inline-load fixes
> 2. Fix inline loads in `people-groups.php` (after `PersonMiddleware`
is refactored)
> 3. Fix inline load in `user-admin.php` (after `UserMiddleware` is
evaluated)
> 4. `EventsMiddleware` — confirm 400 vs 412 is intentional, then
refactor + consider namespace move
> 5. `UserMiddleware` and `PropertyMiddleware` — more complex; separate
issues if needed</issue_description>
>
> ## Comments on the Issue (you are @copilot in this section)
>
> <comments>
> </comments>
>
</details>
<!-- START COPILOT CODING AGENT SUFFIX -->
- Fixes #8170
<!-- START COPILOT CODING AGENT TIPS -->
---
💬 We'd love your input! Share your thoughts on Copilot coding agent in
our [2 minute survey](https://gh.io/copilot-coding-agent-survey).
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: DawoudIO <554959+DawoudIO@users.noreply.github.com>
Co-authored-by: George Dawoud <george@dawouds.com>1 parent bf25256 commit 6253c9a
File tree
15 files changed
+226
-220
lines changed- cypress/e2e/api/private/standard
- src
- ChurchCRM/Slim/Middleware
- Api
- admin/routes/api
- api/routes/people
- plugins/core/google-analytics/templates
15 files changed
+226
-220
lines changedLines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
91 | | - | |
92 | | - | |
| 91 | + | |
| 92 | + | |
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
| |||
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
26 | | - | |
| 25 | + | |
| 26 | + | |
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | | - | |
5 | 3 | | |
6 | 4 | | |
7 | 5 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | | - | |
5 | 3 | | |
6 | 4 | | |
7 | 5 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | 6 | | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
| 7 | + | |
15 | 8 | | |
16 | | - | |
| 9 | + | |
17 | 10 | | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
| 11 | + | |
| 12 | + | |
23 | 13 | | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
28 | 18 | | |
29 | | - | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
30 | 23 | | |
31 | | - | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
32 | 27 | | |
33 | 28 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | | - | |
5 | 3 | | |
6 | 4 | | |
7 | 5 | | |
| |||
Lines changed: 0 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | | - | |
5 | 3 | | |
6 | 4 | | |
7 | 5 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | 6 | | |
13 | | - | |
| 7 | + | |
14 | 8 | | |
15 | | - | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
16 | 12 | | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
| 13 | + | |
| 14 | + | |
22 | 15 | | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
27 | 20 | | |
28 | | - | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
29 | 25 | | |
30 | | - | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
31 | 29 | | |
32 | 30 | | |
Lines changed: 19 additions & 28 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
| 4 | + | |
3 | 5 | | |
4 | 6 | | |
5 | 7 | | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | 8 | | |
15 | | - | |
| 9 | + | |
16 | 10 | | |
17 | | - | |
| 11 | + | |
18 | 12 | | |
19 | | - | |
| 13 | + | |
20 | 14 | | |
21 | | - | |
| 15 | + | |
22 | 16 | | |
23 | 17 | | |
24 | | - | |
| 18 | + | |
25 | 19 | | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
| 20 | + | |
| 21 | + | |
36 | 22 | | |
37 | | - | |
38 | | - | |
39 | | - | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
40 | 28 | | |
41 | 29 | | |
42 | | - | |
| 30 | + | |
| 31 | + | |
43 | 32 | | |
44 | | - | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
45 | 36 | | |
46 | 37 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
9 | 8 | | |
| 9 | + | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
13 | | - | |
14 | 12 | | |
15 | | - | |
| 13 | + | |
16 | 14 | | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
17 | 35 | | |
18 | 36 | | |
19 | 37 | | |
20 | | - | |
| 38 | + | |
| 39 | + | |
21 | 40 | | |
22 | | - | |
| 41 | + | |
23 | 42 | | |
24 | 43 | | |
25 | 44 | | |
26 | 45 | | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
| 46 | + | |
35 | 47 | | |
36 | 48 | | |
37 | | - | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
38 | 52 | | |
39 | | - | |
| 53 | + | |
40 | 54 | | |
41 | 55 | | |
0 commit comments