Browse Source

Add tests later

Maurits van der Schee 6 years ago
parent
commit
f94f825aaa

+ 16
- 0
src/Tqdev/PhpCrudApi/Database/GenericDB.php View File

@@ -2,6 +2,8 @@
2 2
 namespace Tqdev\PhpCrudApi\Database;
3 3
 
4 4
 use Tqdev\PhpCrudApi\Column\Reflection\ReflectedTable;
5
+use Tqdev\PhpCrudApi\Middleware\Communication\VariableStore;
6
+use Tqdev\PhpCrudApi\Record\Condition\AndCondition;
5 7
 use Tqdev\PhpCrudApi\Record\Condition\ColumnCondition;
6 8
 use Tqdev\PhpCrudApi\Record\Condition\Condition;
7 9
 
@@ -95,6 +97,12 @@ class GenericDB
95 97
         return $this->definition;
96 98
     }
97 99
 
100
+    private function addAuthorizationCondition(Condition $condition2): Condition
101
+    {
102
+        $condition1 = VariableStore::get('authorization.condition');
103
+        return $condition1 ? AndCondition::fromArray([$condition1, $condition2]) : $condition2;
104
+    }
105
+
98 106
     public function createSingle(ReflectedTable $table, array $columnValues) /*: ?String*/
99 107
     {
100 108
         $this->converter->convertColumnValues($table, $columnValues);
@@ -122,6 +130,7 @@ class GenericDB
122 130
         $selectColumns = $this->columns->getSelect($table, $columnNames);
123 131
         $tableName = $table->getName();
124 132
         $condition = new ColumnCondition($table->getPk(), 'eq', $id);
133
+        $condition = $this->addAuthorizationCondition($condition);
125 134
         $parameters = array();
126 135
         $whereClause = $this->conditions->getWhereClause($condition, $parameters);
127 136
         $sql = 'SELECT ' . $selectColumns . ' FROM "' . $tableName . '" ' . $whereClause;
@@ -143,6 +152,7 @@ class GenericDB
143 152
         $selectColumns = $this->columns->getSelect($table, $columnNames);
144 153
         $tableName = $table->getName();
145 154
         $condition = new ColumnCondition($table->getPk(), 'in', implode(',', $ids));
155
+        $condition = $this->addAuthorizationCondition($condition);
146 156
         $parameters = array();
147 157
         $whereClause = $this->conditions->getWhereClause($condition, $parameters);
148 158
         $sql = 'SELECT ' . $selectColumns . ' FROM "' . $tableName . '" ' . $whereClause;
@@ -155,6 +165,7 @@ class GenericDB
155 165
     public function selectCount(ReflectedTable $table, Condition $condition): int
156 166
     {
157 167
         $tableName = $table->getName();
168
+        $condition = $this->addAuthorizationCondition($condition);
158 169
         $parameters = array();
159 170
         $whereClause = $this->conditions->getWhereClause($condition, $parameters);
160 171
         $sql = 'SELECT COUNT(*) FROM "' . $tableName . '"' . $whereClause;
@@ -166,6 +177,7 @@ class GenericDB
166 177
     {
167 178
         $selectColumns = $this->columns->getSelect($table, $columnNames);
168 179
         $tableName = $table->getName();
180
+        $condition = $this->addAuthorizationCondition($condition);
169 181
         $parameters = array();
170 182
         $whereClause = $this->conditions->getWhereClause($condition, $parameters);
171 183
         $sql = 'SELECT ' . $selectColumns . ' FROM "' . $tableName . '"' . $whereClause;
@@ -182,6 +194,7 @@ class GenericDB
182 194
         }
183 195
         $selectColumns = $this->columns->getSelect($table, $columnNames);
184 196
         $tableName = $table->getName();
197
+        $condition = $this->addAuthorizationCondition($condition);
185 198
         $parameters = array();
186 199
         $whereClause = $this->conditions->getWhereClause($condition, $parameters);
187 200
         $orderBy = $this->columns->getOrderBy($table, $columnOrdering);
@@ -202,6 +215,7 @@ class GenericDB
202 215
         $updateColumns = $this->columns->getUpdate($table, $columnValues);
203 216
         $tableName = $table->getName();
204 217
         $condition = new ColumnCondition($table->getPk(), 'eq', $id);
218
+        $condition = $this->addAuthorizationCondition($condition);
205 219
         $parameters = array_values($columnValues);
206 220
         $whereClause = $this->conditions->getWhereClause($condition, $parameters);
207 221
         $sql = 'UPDATE "' . $tableName . '" SET ' . $updateColumns . $whereClause;
@@ -213,6 +227,7 @@ class GenericDB
213 227
     {
214 228
         $tableName = $table->getName();
215 229
         $condition = new ColumnCondition($table->getPk(), 'eq', $id);
230
+        $condition = $this->addAuthorizationCondition($condition);
216 231
         $parameters = array();
217 232
         $whereClause = $this->conditions->getWhereClause($condition, $parameters);
218 233
         $sql = 'DELETE FROM "' . $tableName . '" ' . $whereClause;
@@ -229,6 +244,7 @@ class GenericDB
229 244
         $updateColumns = $this->columns->getIncrement($table, $columnValues);
230 245
         $tableName = $table->getName();
231 246
         $condition = new ColumnCondition($table->getPk(), 'eq', $id);
247
+        $condition = $this->addAuthorizationCondition($condition);
232 248
         $parameters = array_values($columnValues);
233 249
         $whereClause = $this->conditions->getWhereClause($condition, $parameters);
234 250
         $sql = 'UPDATE "' . $tableName . '" SET ' . $updateColumns . $whereClause;

+ 20
- 0
src/Tqdev/PhpCrudApi/Middleware/AuthorizationMiddleware.php View File

@@ -4,7 +4,9 @@ namespace Tqdev\PhpCrudApi\Middleware;
4 4
 use Tqdev\PhpCrudApi\Column\ReflectionService;
5 5
 use Tqdev\PhpCrudApi\Controller\Responder;
6 6
 use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
7
+use Tqdev\PhpCrudApi\Middleware\Communication\VariableStore;
7 8
 use Tqdev\PhpCrudApi\Middleware\Router\Router;
9
+use Tqdev\PhpCrudApi\Record\FilterInfo;
8 10
 use Tqdev\PhpCrudApi\Request;
9 11
 use Tqdev\PhpCrudApi\Response;
10 12
 
@@ -70,6 +72,23 @@ class AuthorizationMiddleware extends Middleware
70 72
         }
71 73
     }
72 74
 
75
+    private function handleRecords(String $method, String $path, String $databaseName, String $tableName) /*: void*/
76
+    {
77
+        if (!$this->reflection->hasTable($tableName)) {
78
+            return;
79
+        }
80
+        $recordHandler = $this->getProperty('recordHandler', '');
81
+        if ($recordHandler) {
82
+            $query = call_user_func($recordHandler, $method, $path, $databaseName, $tableName);
83
+            $filters = new FilterInfo();
84
+            $table = $this->reflection->getTable($tableName);
85
+            $query = str_replace('][]=', ']=', str_replace('=', '[]=', $query));
86
+            parse_str($query, $params);
87
+            $condition = $filters->getCombinedConditions($table, $params);
88
+            VariableStore::set('authorization.condition', $condition);
89
+        }
90
+    }
91
+
73 92
     public function handle(Request $request): Response
74 93
     {
75 94
         $method = $request->getMethod();
@@ -82,6 +101,7 @@ class AuthorizationMiddleware extends Middleware
82 101
             if (isset($params['join'])) {
83 102
                 $this->handleJoinTables($method, $path, $databaseName, $params['join']);
84 103
             }
104
+            $this->handleRecords($method, $path, $databaseName, $tableName);
85 105
         } elseif ($path == 'columns') {
86 106
             $tableName = $request->getPathSegment(2);
87 107
             if ($tableName) {

+ 20
- 0
src/Tqdev/PhpCrudApi/Middleware/Communication/VariableStore.php View File

@@ -0,0 +1,20 @@
1
+<?php
2
+namespace Tqdev\PhpCrudApi\Middleware\Communication;
3
+
4
+class VariableStore
5
+{
6
+    static $values = array();
7
+
8
+    public static function get(String $key)
9
+    {
10
+        if (isset(self::$values[$key])) {
11
+            return self::$values[$key];
12
+        }
13
+        return null;
14
+    }
15
+
16
+    public static function set(String $key, /* object */ $value)
17
+    {
18
+        self::$values[$key] = $value;
19
+    }
20
+}

+ 3
- 0
tests/config/base.php View File

@@ -10,6 +10,9 @@ $settings = [
10 10
     'authorization.columnHandler' => function ($method, $path, $databaseName, $tableName, $columnName) {
11 11
         return !($columnName == 'invisible');
12 12
     },
13
+    'authorization.recordHandler' => function ($method, $path, $databaseName, $tableName) {
14
+        return ($tableName == 'comments') ? 'filter=id,neq,3' : '';
15
+    },
13 16
     'sanitation.handler' => function ($method, $tableName, $column, $value) {
14 17
         return is_string($value) ? strip_tags($value) : $value;
15 18
     },

Loading…
Cancel
Save