@@ -917,10 +917,6 @@ public function removeTable(String $tableName): bool
917917 return $ this ->database ->removeTable ($ tableName );
918918 }
919919
920- public function removeColumn (String $ tableName , String $ columnName ): bool
921- {
922- return $ this ->getTable ($ tableName )->removeColumn ($ columnName );
923- }
924920}
925921
926922// file: src/Tqdev/PhpCrudApi/Controller/CacheController.php
@@ -1824,10 +1820,17 @@ public function definition(): GenericDefinition
18241820 return $ this ->definition ;
18251821 }
18261822
1827- private function addAuthorizationCondition (String $ tableName , Condition $ condition2 ): Condition
1823+ private function addMiddlewareConditions (String $ tableName , Condition $ condition ): Condition
18281824 {
18291825 $ 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 ;
18311834 }
18321835
18331836 public function createSingle (ReflectedTable $ table , array $ columnValues ) /*: ?String*/
@@ -1855,7 +1858,7 @@ public function selectSingle(ReflectedTable $table, array $columnNames, String $
18551858 $ selectColumns = $ this ->columns ->getSelect ($ table , $ columnNames );
18561859 $ tableName = $ table ->getName ();
18571860 $ condition = new ColumnCondition ($ table ->getPk (), 'eq ' , $ id );
1858- $ condition = $ this ->addAuthorizationCondition ($ tableName , $ condition );
1861+ $ condition = $ this ->addMiddlewareConditions ($ tableName , $ condition );
18591862 $ parameters = array ();
18601863 $ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
18611864 $ sql = 'SELECT ' . $ selectColumns . ' FROM " ' . $ tableName . '" ' . $ whereClause ;
@@ -1877,7 +1880,7 @@ public function selectMultiple(ReflectedTable $table, array $columnNames, array
18771880 $ selectColumns = $ this ->columns ->getSelect ($ table , $ columnNames );
18781881 $ tableName = $ table ->getName ();
18791882 $ condition = new ColumnCondition ($ table ->getPk (), 'in ' , implode (', ' , $ ids ));
1880- $ condition = $ this ->addAuthorizationCondition ($ tableName , $ condition );
1883+ $ condition = $ this ->addMiddlewareConditions ($ tableName , $ condition );
18811884 $ parameters = array ();
18821885 $ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
18831886 $ sql = 'SELECT ' . $ selectColumns . ' FROM " ' . $ tableName . '" ' . $ whereClause ;
@@ -1890,7 +1893,7 @@ public function selectMultiple(ReflectedTable $table, array $columnNames, array
18901893 public function selectCount (ReflectedTable $ table , Condition $ condition ): int
18911894 {
18921895 $ tableName = $ table ->getName ();
1893- $ condition = $ this ->addAuthorizationCondition ($ tableName , $ condition );
1896+ $ condition = $ this ->addMiddlewareConditions ($ tableName , $ condition );
18941897 $ parameters = array ();
18951898 $ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
18961899 $ sql = 'SELECT COUNT(*) FROM " ' . $ tableName . '" ' . $ whereClause ;
@@ -1902,7 +1905,7 @@ public function selectAllUnordered(ReflectedTable $table, array $columnNames, Co
19021905 {
19031906 $ selectColumns = $ this ->columns ->getSelect ($ table , $ columnNames );
19041907 $ tableName = $ table ->getName ();
1905- $ condition = $ this ->addAuthorizationCondition ($ tableName , $ condition );
1908+ $ condition = $ this ->addMiddlewareConditions ($ tableName , $ condition );
19061909 $ parameters = array ();
19071910 $ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
19081911 $ sql = 'SELECT ' . $ selectColumns . ' FROM " ' . $ tableName . '" ' . $ whereClause ;
@@ -1919,7 +1922,7 @@ public function selectAll(ReflectedTable $table, array $columnNames, Condition $
19191922 }
19201923 $ selectColumns = $ this ->columns ->getSelect ($ table , $ columnNames );
19211924 $ tableName = $ table ->getName ();
1922- $ condition = $ this ->addAuthorizationCondition ($ tableName , $ condition );
1925+ $ condition = $ this ->addMiddlewareConditions ($ tableName , $ condition );
19231926 $ parameters = array ();
19241927 $ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
19251928 $ orderBy = $ this ->columns ->getOrderBy ($ table , $ columnOrdering );
@@ -1940,7 +1943,7 @@ public function updateSingle(ReflectedTable $table, array $columnValues, String
19401943 $ updateColumns = $ this ->columns ->getUpdate ($ table , $ columnValues );
19411944 $ tableName = $ table ->getName ();
19421945 $ condition = new ColumnCondition ($ table ->getPk (), 'eq ' , $ id );
1943- $ condition = $ this ->addAuthorizationCondition ($ tableName , $ condition );
1946+ $ condition = $ this ->addMiddlewareConditions ($ tableName , $ condition );
19441947 $ parameters = array_values ($ columnValues );
19451948 $ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
19461949 $ sql = 'UPDATE " ' . $ tableName . '" SET ' . $ updateColumns . $ whereClause ;
@@ -1952,7 +1955,7 @@ public function deleteSingle(ReflectedTable $table, String $id)
19521955 {
19531956 $ tableName = $ table ->getName ();
19541957 $ condition = new ColumnCondition ($ table ->getPk (), 'eq ' , $ id );
1955- $ condition = $ this ->addAuthorizationCondition ($ tableName , $ condition );
1958+ $ condition = $ this ->addMiddlewareConditions ($ tableName , $ condition );
19561959 $ parameters = array ();
19571960 $ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
19581961 $ sql = 'DELETE FROM " ' . $ tableName . '" ' . $ whereClause ;
@@ -1969,7 +1972,7 @@ public function incrementSingle(ReflectedTable $table, array $columnValues, Stri
19691972 $ updateColumns = $ this ->columns ->getIncrement ($ table , $ columnValues );
19701973 $ tableName = $ table ->getName ();
19711974 $ condition = new ColumnCondition ($ table ->getPk (), 'eq ' , $ id );
1972- $ condition = $ this ->addAuthorizationCondition ($ tableName , $ condition );
1975+ $ condition = $ this ->addMiddlewareConditions ($ tableName , $ condition );
19731976 $ parameters = array_values ($ columnValues );
19741977 $ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
19751978 $ sql = 'UPDATE " ' . $ tableName . '" SET ' . $ updateColumns . $ whereClause ;
@@ -2842,7 +2845,7 @@ private function handleColumns(String $operation, String $tableName) /*: void*/
28422845 foreach ($ table ->columnNames () as $ columnName ) {
28432846 $ allowed = call_user_func ($ columnHandler , $ operation , $ tableName , $ columnName );
28442847 if (!$ allowed ) {
2845- $ this -> reflection -> removeColumn ($ tableName , $ columnName );
2848+ $ table -> removeColumn ($ columnName );
28462849 }
28472850 }
28482851 }
@@ -3172,6 +3175,91 @@ public function handle(Request $request): Response
31723175 }
31733176}
31743177
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+
31753263// file: src/Tqdev/PhpCrudApi/Middleware/SanitationMiddleware.php
31763264
31773265class SanitationMiddleware extends Middleware
@@ -4702,6 +4790,9 @@ public function __construct(Config $config)
47024790 case 'sanitation ' :
47034791 new SanitationMiddleware ($ router , $ responder , $ properties , $ reflection );
47044792 break ;
4793+ case 'multiTenancy ' :
4794+ new MultiTenancyMiddleware ($ router , $ responder , $ properties , $ reflection );
4795+ break ;
47054796 case 'authorization ' :
47064797 new AuthorizationMiddleware ($ router , $ responder , $ properties , $ reflection );
47074798 break ;
0 commit comments