@@ -917,10 +917,6 @@ public function removeTable(String $tableName): bool
917
917
return $ this ->database ->removeTable ($ tableName );
918
918
}
919
919
920
- public function removeColumn (String $ tableName , String $ columnName ): bool
921
- {
922
- return $ this ->getTable ($ tableName )->removeColumn ($ columnName );
923
- }
924
920
}
925
921
926
922
// file: src/Tqdev/PhpCrudApi/Controller/CacheController.php
@@ -1824,10 +1820,17 @@ public function definition(): GenericDefinition
1824
1820
return $ this ->definition ;
1825
1821
}
1826
1822
1827
- private function addAuthorizationCondition (String $ tableName , Condition $ condition2 ): Condition
1823
+ private function addMiddlewareConditions (String $ tableName , Condition $ condition ): Condition
1828
1824
{
1829
1825
$ condition1 = VariableStore::get ("authorization.conditions. $ tableName " );
1830
- return $ condition1 ? AndCondition::fromArray ([$ condition1 , $ condition2 ]) : $ condition2 ;
1826
+ if ($ condition1 ) {
1827
+ $ condition = $ condition ->_and ($ condition1 );
1828
+ }
1829
+ $ condition2 = VariableStore::get ("multiTenancy.conditions. $ tableName " );
1830
+ if ($ condition2 ) {
1831
+ $ condition = $ condition ->_and ($ condition2 );
1832
+ }
1833
+ return $ condition ;
1831
1834
}
1832
1835
1833
1836
public function createSingle (ReflectedTable $ table , array $ columnValues ) /*: ?String*/
@@ -1855,7 +1858,7 @@ public function selectSingle(ReflectedTable $table, array $columnNames, String $
1855
1858
$ selectColumns = $ this ->columns ->getSelect ($ table , $ columnNames );
1856
1859
$ tableName = $ table ->getName ();
1857
1860
$ condition = new ColumnCondition ($ table ->getPk (), 'eq ' , $ id );
1858
- $ condition = $ this ->addAuthorizationCondition ($ tableName , $ condition );
1861
+ $ condition = $ this ->addMiddlewareConditions ($ tableName , $ condition );
1859
1862
$ parameters = array ();
1860
1863
$ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
1861
1864
$ sql = 'SELECT ' . $ selectColumns . ' FROM " ' . $ tableName . '" ' . $ whereClause ;
@@ -1877,7 +1880,7 @@ public function selectMultiple(ReflectedTable $table, array $columnNames, array
1877
1880
$ selectColumns = $ this ->columns ->getSelect ($ table , $ columnNames );
1878
1881
$ tableName = $ table ->getName ();
1879
1882
$ condition = new ColumnCondition ($ table ->getPk (), 'in ' , implode (', ' , $ ids ));
1880
- $ condition = $ this ->addAuthorizationCondition ($ tableName , $ condition );
1883
+ $ condition = $ this ->addMiddlewareConditions ($ tableName , $ condition );
1881
1884
$ parameters = array ();
1882
1885
$ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
1883
1886
$ sql = 'SELECT ' . $ selectColumns . ' FROM " ' . $ tableName . '" ' . $ whereClause ;
@@ -1890,7 +1893,7 @@ public function selectMultiple(ReflectedTable $table, array $columnNames, array
1890
1893
public function selectCount (ReflectedTable $ table , Condition $ condition ): int
1891
1894
{
1892
1895
$ tableName = $ table ->getName ();
1893
- $ condition = $ this ->addAuthorizationCondition ($ tableName , $ condition );
1896
+ $ condition = $ this ->addMiddlewareConditions ($ tableName , $ condition );
1894
1897
$ parameters = array ();
1895
1898
$ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
1896
1899
$ sql = 'SELECT COUNT(*) FROM " ' . $ tableName . '" ' . $ whereClause ;
@@ -1902,7 +1905,7 @@ public function selectAllUnordered(ReflectedTable $table, array $columnNames, Co
1902
1905
{
1903
1906
$ selectColumns = $ this ->columns ->getSelect ($ table , $ columnNames );
1904
1907
$ tableName = $ table ->getName ();
1905
- $ condition = $ this ->addAuthorizationCondition ($ tableName , $ condition );
1908
+ $ condition = $ this ->addMiddlewareConditions ($ tableName , $ condition );
1906
1909
$ parameters = array ();
1907
1910
$ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
1908
1911
$ sql = 'SELECT ' . $ selectColumns . ' FROM " ' . $ tableName . '" ' . $ whereClause ;
@@ -1919,7 +1922,7 @@ public function selectAll(ReflectedTable $table, array $columnNames, Condition $
1919
1922
}
1920
1923
$ selectColumns = $ this ->columns ->getSelect ($ table , $ columnNames );
1921
1924
$ tableName = $ table ->getName ();
1922
- $ condition = $ this ->addAuthorizationCondition ($ tableName , $ condition );
1925
+ $ condition = $ this ->addMiddlewareConditions ($ tableName , $ condition );
1923
1926
$ parameters = array ();
1924
1927
$ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
1925
1928
$ orderBy = $ this ->columns ->getOrderBy ($ table , $ columnOrdering );
@@ -1940,7 +1943,7 @@ public function updateSingle(ReflectedTable $table, array $columnValues, String
1940
1943
$ updateColumns = $ this ->columns ->getUpdate ($ table , $ columnValues );
1941
1944
$ tableName = $ table ->getName ();
1942
1945
$ condition = new ColumnCondition ($ table ->getPk (), 'eq ' , $ id );
1943
- $ condition = $ this ->addAuthorizationCondition ($ tableName , $ condition );
1946
+ $ condition = $ this ->addMiddlewareConditions ($ tableName , $ condition );
1944
1947
$ parameters = array_values ($ columnValues );
1945
1948
$ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
1946
1949
$ sql = 'UPDATE " ' . $ tableName . '" SET ' . $ updateColumns . $ whereClause ;
@@ -1952,7 +1955,7 @@ public function deleteSingle(ReflectedTable $table, String $id)
1952
1955
{
1953
1956
$ tableName = $ table ->getName ();
1954
1957
$ condition = new ColumnCondition ($ table ->getPk (), 'eq ' , $ id );
1955
- $ condition = $ this ->addAuthorizationCondition ($ tableName , $ condition );
1958
+ $ condition = $ this ->addMiddlewareConditions ($ tableName , $ condition );
1956
1959
$ parameters = array ();
1957
1960
$ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
1958
1961
$ sql = 'DELETE FROM " ' . $ tableName . '" ' . $ whereClause ;
@@ -1969,7 +1972,7 @@ public function incrementSingle(ReflectedTable $table, array $columnValues, Stri
1969
1972
$ updateColumns = $ this ->columns ->getIncrement ($ table , $ columnValues );
1970
1973
$ tableName = $ table ->getName ();
1971
1974
$ condition = new ColumnCondition ($ table ->getPk (), 'eq ' , $ id );
1972
- $ condition = $ this ->addAuthorizationCondition ($ tableName , $ condition );
1975
+ $ condition = $ this ->addMiddlewareConditions ($ tableName , $ condition );
1973
1976
$ parameters = array_values ($ columnValues );
1974
1977
$ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
1975
1978
$ sql = 'UPDATE " ' . $ tableName . '" SET ' . $ updateColumns . $ whereClause ;
@@ -2842,7 +2845,7 @@ private function handleColumns(String $operation, String $tableName) /*: void*/
2842
2845
foreach ($ table ->columnNames () as $ columnName ) {
2843
2846
$ allowed = call_user_func ($ columnHandler , $ operation , $ tableName , $ columnName );
2844
2847
if (!$ allowed ) {
2845
- $ this -> reflection -> removeColumn ($ tableName , $ columnName );
2848
+ $ table -> removeColumn ($ columnName );
2846
2849
}
2847
2850
}
2848
2851
}
@@ -3172,6 +3175,91 @@ public function handle(Request $request): Response
3172
3175
}
3173
3176
}
3174
3177
3178
+ // file: src/Tqdev/PhpCrudApi/Middleware/MultiTenancyMiddleware.php
3179
+
3180
+ class MultiTenancyMiddleware extends Middleware
3181
+ {
3182
+ private $ reflection ;
3183
+
3184
+ public function __construct (Router $ router , Responder $ responder , array $ properties , ReflectionService $ reflection )
3185
+ {
3186
+ parent ::__construct ($ router , $ responder , $ properties );
3187
+ $ this ->reflection = $ reflection ;
3188
+ $ this ->utils = new RequestUtils ($ reflection );
3189
+ }
3190
+
3191
+ private function getCondition (String $ tableName , array $ pairs ): Condition
3192
+ {
3193
+ $ condition = new NoCondition ();
3194
+ $ table = $ this ->reflection ->getTable ($ tableName );
3195
+ foreach ($ pairs as $ k => $ v ) {
3196
+ $ condition = $ condition ->_and (new ColumnCondition ($ table ->get ($ k ), 'eq ' , $ v ));
3197
+ }
3198
+ return $ condition ;
3199
+ }
3200
+
3201
+ private function getPairs ($ handler , String $ operation , String $ tableName ): array
3202
+ {
3203
+ $ result = array ();
3204
+ $ pairs = call_user_func ($ handler , $ operation , $ tableName );
3205
+ $ table = $ this ->reflection ->getTable ($ tableName );
3206
+ foreach ($ pairs as $ k => $ v ) {
3207
+ if ($ table ->exists ($ k )) {
3208
+ $ result [$ k ] = $ v ;
3209
+ }
3210
+ }
3211
+ return $ result ;
3212
+ }
3213
+
3214
+ private function handleRecord (Request $ request , String $ operation , array $ pairs ) /*: void*/
3215
+ {
3216
+ $ record = $ request ->getBody ();
3217
+ if ($ record === null ) {
3218
+ return ;
3219
+ }
3220
+ $ multi = is_array ($ record );
3221
+ $ records = $ multi ? $ record : [$ record ];
3222
+ foreach ($ records as &$ record ) {
3223
+ foreach ($ pairs as $ column => $ value ) {
3224
+ if ($ operation == 'create ' ) {
3225
+ $ record ->$ column = $ value ;
3226
+ } else {
3227
+ if (isset ($ record ->$ column )) {
3228
+ unset($ record ->$ column );
3229
+ }
3230
+ }
3231
+ }
3232
+ }
3233
+ $ request ->setBody ($ multi ? $ records : $ records [0 ]);
3234
+ }
3235
+
3236
+ public function handle (Request $ request ): Response
3237
+ {
3238
+ $ handler = $ this ->getProperty ('handler ' , '' );
3239
+ if ($ handler !== '' ) {
3240
+ $ path = $ request ->getPathSegment (1 );
3241
+ if ($ path == 'records ' ) {
3242
+ $ operation = $ this ->utils ->getOperation ($ request );
3243
+ $ tableNames = $ this ->utils ->getTableNames ($ request );
3244
+ foreach ($ tableNames as $ i => $ tableName ) {
3245
+ if (!$ this ->reflection ->hasTable ($ tableName )) {
3246
+ continue ;
3247
+ }
3248
+ $ pairs = $ this ->getPairs ($ handler , $ operation , $ tableName );
3249
+ if ($ i == 0 ) {
3250
+ if (in_array ($ operation , ['create ' , 'update ' , 'increment ' ])) {
3251
+ $ this ->handleRecord ($ request , $ operation , $ pairs );
3252
+ }
3253
+ }
3254
+ $ condition = $ this ->getCondition ($ tableName , $ pairs );
3255
+ VariableStore::set ("multiTenancy.conditions. $ tableName " , $ condition );
3256
+ }
3257
+ }
3258
+ }
3259
+ return $ this ->next ->handle ($ request );
3260
+ }
3261
+ }
3262
+
3175
3263
// file: src/Tqdev/PhpCrudApi/Middleware/SanitationMiddleware.php
3176
3264
3177
3265
class SanitationMiddleware extends Middleware
@@ -4702,6 +4790,9 @@ public function __construct(Config $config)
4702
4790
case 'sanitation ' :
4703
4791
new SanitationMiddleware ($ router , $ responder , $ properties , $ reflection );
4704
4792
break ;
4793
+ case 'multiTenancy ' :
4794
+ new MultiTenancyMiddleware ($ router , $ responder , $ properties , $ reflection );
4795
+ break ;
4705
4796
case 'authorization ' :
4706
4797
new AuthorizationMiddleware ($ router , $ responder , $ properties , $ reflection );
4707
4798
break ;
0 commit comments