Maurits van der Schee 6 years ago
parent
commit
d26628fe79

+ 97
- 0
src/Tqdev/PhpCrudApi/Middleware/MultiTenancyMiddleware.php View File

@@ -0,0 +1,97 @@
1
+<?php
2
+namespace Tqdev\PhpCrudApi\Middleware;
3
+
4
+use Tqdev\PhpCrudApi\Column\ReflectionService;
5
+use Tqdev\PhpCrudApi\Controller\Responder;
6
+use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
7
+use Tqdev\PhpCrudApi\Middleware\Communication\VariableStore;
8
+use Tqdev\PhpCrudApi\Middleware\Router\Router;
9
+use Tqdev\PhpCrudApi\Record\Condition\ColumnCondition;
10
+use Tqdev\PhpCrudApi\Record\Condition\Condition;
11
+use Tqdev\PhpCrudApi\Record\Condition\NoCondition;
12
+use Tqdev\PhpCrudApi\Record\RequestUtils;
13
+use Tqdev\PhpCrudApi\Request;
14
+use Tqdev\PhpCrudApi\Response;
15
+
16
+class MultiTenancyMiddleware extends Middleware
17
+{
18
+    private $reflection;
19
+
20
+    public function __construct(Router $router, Responder $responder, array $properties, ReflectionService $reflection)
21
+    {
22
+        parent::__construct($router, $responder, $properties);
23
+        $this->reflection = $reflection;
24
+        $this->utils = new RequestUtils($reflection);
25
+    }
26
+
27
+    private function getCondition(String $tableName, array $pairs): Condition
28
+    {
29
+        $condition = new NoCondition();
30
+        $table = $this->reflection->getTable($tableName);
31
+        foreach ($pairs as $k => $v) {
32
+            $condition = $condition->_and(new ColumnCondition($table->get($k), 'eq', $v));
33
+        }
34
+        return $condition;
35
+    }
36
+
37
+    private function getPairs($handler, String $operation, String $tableName): array
38
+    {
39
+        $result = array();
40
+        $pairs = call_user_func($handler, $operation, $tableName);
41
+        $table = $this->reflection->getTable($tableName);
42
+        foreach ($pairs as $k => $v) {
43
+            if ($table->exists($k)) {
44
+                $result[$k] = $v;
45
+            }
46
+        }
47
+        return $result;
48
+    }
49
+
50
+    private function handleRecord(Request $request, String $operation, array $pairs) /*: void*/
51
+    {
52
+        $record = $request->getBody();
53
+        if ($record === null) {
54
+            return;
55
+        }
56
+        $multi = is_array($record);
57
+        $records = $multi ? $record : [$record];
58
+        foreach ($records as &$record) {
59
+            foreach ($pairs as $column => $value) {
60
+                if ($operation == 'create') {
61
+                    $record->$column = $value;
62
+                } else {
63
+                    if (isset($record->$column)) {
64
+                        unset($record->$column);
65
+                    }
66
+                }
67
+            }
68
+        }
69
+        $request->setBody($multi ? $records : $records[0]);
70
+    }
71
+
72
+    public function handle(Request $request): Response
73
+    {
74
+        $handler = $this->getProperty('handler', '');
75
+        if ($handler !== '') {
76
+            $path = $request->getPathSegment(1);
77
+            if ($path == 'records') {
78
+                $operation = $this->utils->getOperation($request);
79
+                $tableNames = $this->utils->getTableNames($request);
80
+                foreach ($tableNames as $i => $tableName) {
81
+                    if (!$this->reflection->hasTable($tableName)) {
82
+                        continue;
83
+                    }
84
+                    $pairs = $this->getPairs($handler, $operation, $tableName);
85
+                    if ($i == 0) {
86
+                        if (in_array($operation, ['create', 'update', 'increment'])) {
87
+                            $this->handleRecord($request, $operation, $pairs);
88
+                        }
89
+                    }
90
+                    $condition = $this->getCondition($tableName, $pairs);
91
+                    VariableStore::set("multiTenancy.conditions.$tableName", $condition);
92
+                }
93
+            }
94
+        }
95
+        return $this->next->handle($request);
96
+    }
97
+}

+ 51
- 0
tests/functional/001_records/073_multi_tenancy_kunsthåndværk.log View File

@@ -0,0 +1,51 @@
1
+POST /records/kunsthåndværk
2
+
3
+{"id":"b55decba-8eb5-436b-af3e-148f7b4eacda","Umlauts ä_ö_ü-COUNT":4,"user_id":2}
4
+===
5
+200
6
+Content-Type: application/json
7
+Content-Length: 38
8
+
9
+"b55decba-8eb5-436b-af3e-148f7b4eacda"
10
+===
11
+GET /records/kunsthåndværk/b55decba-8eb5-436b-af3e-148f7b4eacda
12
+===
13
+200
14
+Content-Type: application/json
15
+Content-Length: 84
16
+
17
+{"id":"b55decba-8eb5-436b-af3e-148f7b4eacda","Umlauts ä_ö_ü-COUNT":4,"user_id":1}
18
+===
19
+PUT /records/kunsthåndværk/b55decba-8eb5-436b-af3e-148f7b4eacda
20
+
21
+{"id":"b55decba-8eb5-436b-af3e-148f7b4eacda","Umlauts ä_ö_ü-COUNT":4,"user_id":2}
22
+===
23
+200
24
+Content-Type: application/json
25
+Content-Length: 1
26
+
27
+1
28
+===
29
+GET /records/kunsthåndværk/b55decba-8eb5-436b-af3e-148f7b4eacda
30
+===
31
+200
32
+Content-Type: application/json
33
+Content-Length: 84
34
+
35
+{"id":"b55decba-8eb5-436b-af3e-148f7b4eacda","Umlauts ä_ö_ü-COUNT":4,"user_id":1}
36
+===
37
+DELETE /records/kunsthåndværk/e31ecfe6-591f-4660-9fbd-1a232083037f
38
+===
39
+200
40
+Content-Type: application/json
41
+Content-Length: 1
42
+
43
+0
44
+===
45
+DELETE /records/kunsthåndværk/b55decba-8eb5-436b-af3e-148f7b4eacda
46
+===
47
+200
48
+Content-Type: application/json
49
+Content-Length: 1
50
+
51
+1

Loading…
Cancel
Save