Browse Source

Add IpAddress middleware for #519

Maurits van der Schee 6 years ago
parent
commit
168f8a28b7
2 changed files with 9 additions and 11 deletions
  1. 1
    0
      README.md
  2. 8
    11
      src/Tqdev/PhpCrudApi/Middleware/IpAddressMiddleware.php

+ 1
- 0
README.md View File

@@ -156,6 +156,7 @@ You can enable the following middleware using the "middlewares" config parameter
156 156
 - "basicAuth": Support for "Basic Authentication"
157 157
 - "authorization": Restrict access to certain tables or columns
158 158
 - "validation": Return input validation errors for custom rules
159
+- "ipAddress": Fill a protected field with the IP address on create
159 160
 - "sanitation": Apply input sanitation on create and update
160 161
 - "multiTenancy": Restricts tenants access in a multi-tenant scenario
161 162
 - "pageLimits": Restricts list operations to prevent database scraping

+ 8
- 11
src/Tqdev/PhpCrudApi/Middleware/IpAddressMiddleware.php View File

@@ -21,7 +21,7 @@ class IpAddressMiddleware extends Middleware
21 21
         $this->utils = new RequestUtils($reflection);
22 22
     }
23 23
 
24
-    private function callHandler($handler, $record, String $operation, ReflectedTable $table) /*: object */
24
+    private function callHandler($record, String $operation, ReflectedTable $table) /*: object */
25 25
     {
26 26
         $context = (array) $record;
27 27
         $columnName = $this->getProperty('column', '');
@@ -43,18 +43,15 @@ class IpAddressMiddleware extends Middleware
43 43
             if ($this->reflection->hasTable($tableName)) {
44 44
                 $record = $request->getBody();
45 45
                 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);
55 50
                         }
56
-                        $request->setBody($record);
51
+                    } else {
52
+                        $record = $this->callHandler($record, $operation, $table);
57 53
                     }
54
+                    $request->setBody($record);
58 55
                 }
59 56
             }
60 57
         }

Loading…
Cancel
Save