Browse Source

Add IpAddress middleware for #519

Maurits van der Schee 6 years ago
parent
commit
ab4773c3d4

+ 2
- 1
README.md View File

@@ -195,7 +195,8 @@ You can tune the middleware behavior using middleware specific configuration par
195 195
 - "authorization.columnHandler": Handler to implement column authorization rules ("")
196 196
 - "authorization.recordHandler": Handler to implement record authorization filter rules ("")
197 197
 - "validation.handler": Handler to implement validation rules for input values ("")
198
-- "ipAddress.column": Column to protect and override with the IP address on create ("")
198
+- "ipAddress.tables": Tables to search for columns to override with IP address ("")
199
+- "ipAddress.columns": Columns to protect and override with the IP address on create ("")
199 200
 - "sanitation.handler": Handler to implement sanitation rules for input values ("")
200 201
 - "multiTenancy.handler": Handler to implement simple multi-tenancy rules ("")
201 202
 - "pageLimits.pages": The maximum page number that a list operation allows ("100")

+ 30
- 16
api.php View File

@@ -3264,12 +3264,16 @@ class IpAddressMiddleware extends Middleware
3264 3264
     private function callHandler($record, String $operation, ReflectedTable $table) /*: object */
3265 3265
     {
3266 3266
         $context = (array) $record;
3267
-        $columnName = $this->getProperty('column', '');
3268
-        if ($table->hasColumn($columnName)) {
3269
-            if ($operation == 'create') {
3270
-                $context[$columnName] = $_SERVER['REMOTE_ADDR'];
3271
-            } else {
3272
-                unset($context[$columnName]);
3267
+        $columnNames = $this->getProperty('columns', '');
3268
+        if ($columnNames) {
3269
+            foreach (explode(',', $columnNames) as $columnName) {
3270
+                if ($table->hasColumn($columnName)) {
3271
+                    if ($operation == 'create') {
3272
+                        $context[$columnName] = $_SERVER['REMOTE_ADDR'];
3273
+                    } else {
3274
+                        unset($context[$columnName]);
3275
+                    }
3276
+                }
3273 3277
             }
3274 3278
         }
3275 3279
         return (object) $context;
@@ -3279,19 +3283,22 @@ class IpAddressMiddleware extends Middleware
3279 3283
     {
3280 3284
         $operation = $this->utils->getOperation($request);
3281 3285
         if (in_array($operation, ['create', 'update', 'increment'])) {
3286
+            $tableNames = $this->getProperty('tables', '');
3282 3287
             $tableName = $request->getPathSegment(2);
3283
-            if ($this->reflection->hasTable($tableName)) {
3284
-                $record = $request->getBody();
3285
-                if ($record !== null) {
3286
-                    $table = $this->reflection->getTable($tableName);
3287
-                    if (is_array($record)) {
3288
-                        foreach ($record as &$r) {
3289
-                            $r = $this->callHandler($r, $operation, $table);
3288
+            if (!$tableNames || in_array($tableName, explode(',', $tableNames))) {
3289
+                if ($this->reflection->hasTable($tableName)) {
3290
+                    $record = $request->getBody();
3291
+                    if ($record !== null) {
3292
+                        $table = $this->reflection->getTable($tableName);
3293
+                        if (is_array($record)) {
3294
+                            foreach ($record as &$r) {
3295
+                                $r = $this->callHandler($r, $operation, $table);
3296
+                            }
3297
+                        } else {
3298
+                            $record = $this->callHandler($record, $operation, $table);
3290 3299
                         }
3291
-                    } else {
3292
-                        $record = $this->callHandler($record, $operation, $table);
3300
+                        $request->setBody($record);
3293 3301
                     }
3294
-                    $request->setBody($record);
3295 3302
                 }
3296 3303
             }
3297 3304
         }
@@ -3958,6 +3965,13 @@ class OpenApiBuilder
3958 3965
                 foreach ($properties as $key => $value) {
3959 3966
                     $this->openapi->set("$prefix|properties|$columnName|$key", $value);
3960 3967
                 }
3968
+                if ($column->getPk()) {
3969
+                    $this->openapi->set("$prefix|properties|$columnName|x-primary-key", true);
3970
+                }
3971
+                $fk = $column->getFk();
3972
+                if ($fk) {
3973
+                    $this->openapi->set("$prefix|properties|$columnName|x-references", $fk);
3974
+                }
3961 3975
             }
3962 3976
         }
3963 3977
     }

+ 23
- 16
src/Tqdev/PhpCrudApi/Middleware/IpAddressMiddleware.php View File

@@ -24,12 +24,16 @@ class IpAddressMiddleware extends Middleware
24 24
     private function callHandler($record, String $operation, ReflectedTable $table) /*: object */
25 25
     {
26 26
         $context = (array) $record;
27
-        $columnName = $this->getProperty('column', '');
28
-        if ($table->hasColumn($columnName)) {
29
-            if ($operation == 'create') {
30
-                $context[$columnName] = $_SERVER['REMOTE_ADDR'];
31
-            } else {
32
-                unset($context[$columnName]);
27
+        $columnNames = $this->getProperty('columns', '');
28
+        if ($columnNames) {
29
+            foreach (explode(',', $columnNames) as $columnName) {
30
+                if ($table->hasColumn($columnName)) {
31
+                    if ($operation == 'create') {
32
+                        $context[$columnName] = $_SERVER['REMOTE_ADDR'];
33
+                    } else {
34
+                        unset($context[$columnName]);
35
+                    }
36
+                }
33 37
             }
34 38
         }
35 39
         return (object) $context;
@@ -39,19 +43,22 @@ class IpAddressMiddleware extends Middleware
39 43
     {
40 44
         $operation = $this->utils->getOperation($request);
41 45
         if (in_array($operation, ['create', 'update', 'increment'])) {
46
+            $tableNames = $this->getProperty('tables', '');
42 47
             $tableName = $request->getPathSegment(2);
43
-            if ($this->reflection->hasTable($tableName)) {
44
-                $record = $request->getBody();
45
-                if ($record !== null) {
46
-                    $table = $this->reflection->getTable($tableName);
47
-                    if (is_array($record)) {
48
-                        foreach ($record as &$r) {
49
-                            $r = $this->callHandler($r, $operation, $table);
48
+            if (!$tableNames || in_array($tableName, explode(',', $tableNames))) {
49
+                if ($this->reflection->hasTable($tableName)) {
50
+                    $record = $request->getBody();
51
+                    if ($record !== null) {
52
+                        $table = $this->reflection->getTable($tableName);
53
+                        if (is_array($record)) {
54
+                            foreach ($record as &$r) {
55
+                                $r = $this->callHandler($r, $operation, $table);
56
+                            }
57
+                        } else {
58
+                            $record = $this->callHandler($record, $operation, $table);
50 59
                         }
51
-                    } else {
52
-                        $record = $this->callHandler($record, $operation, $table);
60
+                        $request->setBody($record);
53 61
                     }
54
-                    $request->setBody($record);
55 62
                 }
56 63
             }
57 64
         }

+ 7
- 0
src/Tqdev/PhpCrudApi/OpenApi/OpenApiBuilder.php View File

@@ -198,6 +198,13 @@ class OpenApiBuilder
198 198
                 foreach ($properties as $key => $value) {
199 199
                     $this->openapi->set("$prefix|properties|$columnName|$key", $value);
200 200
                 }
201
+                if ($column->getPk()) {
202
+                    $this->openapi->set("$prefix|properties|$columnName|x-primary-key", true);
203
+                }
204
+                $fk = $column->getFk();
205
+                if ($fk) {
206
+                    $this->openapi->set("$prefix|properties|$columnName|x-references", $fk);
207
+                }
201 208
             }
202 209
         }
203 210
     }

Loading…
Cancel
Save