Skip to content

Commit 168f8a2

Browse files
committed
Add IpAddress middleware for #519
1 parent 4f0fd33 commit 168f8a2

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ You can enable the following middleware using the "middlewares" config parameter
156156
- "basicAuth": Support for "Basic Authentication"
157157
- "authorization": Restrict access to certain tables or columns
158158
- "validation": Return input validation errors for custom rules
159+
- "ipAddress": Fill a protected field with the IP address on create
159160
- "sanitation": Apply input sanitation on create and update
160161
- "multiTenancy": Restricts tenants access in a multi-tenant scenario
161162
- "pageLimits": Restricts list operations to prevent database scraping

src/Tqdev/PhpCrudApi/Middleware/IpAddressMiddleware.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function __construct(Router $router, Responder $responder, array $propert
2121
$this->utils = new RequestUtils($reflection);
2222
}
2323

24-
private function callHandler($handler, $record, String $operation, ReflectedTable $table) /*: object */
24+
private function callHandler($record, String $operation, ReflectedTable $table) /*: object */
2525
{
2626
$context = (array) $record;
2727
$columnName = $this->getProperty('column', '');
@@ -43,18 +43,15 @@ public function handle(Request $request): Response
4343
if ($this->reflection->hasTable($tableName)) {
4444
$record = $request->getBody();
4545
if ($record !== null) {
46-
$handler = $this->getProperty('handler', '');
47-
if ($handler !== '') {
48-
$table = $this->reflection->getTable($tableName);
49-
if (is_array($record)) {
50-
foreach ($record as &$r) {
51-
$r = $this->callHandler($handler, $r, $operation, $table);
52-
}
53-
} else {
54-
$record = $this->callHandler($handler, $record, $operation, $table);
46+
$table = $this->reflection->getTable($tableName);
47+
if (is_array($record)) {
48+
foreach ($record as &$r) {
49+
$r = $this->callHandler($r, $operation, $table);
5550
}
56-
$request->setBody($record);
51+
} else {
52+
$record = $this->callHandler($record, $operation, $table);
5753
}
54+
$request->setBody($record);
5855
}
5956
}
6057
}

0 commit comments

Comments
 (0)