@@ -348,10 +348,10 @@ public function __construct(String $name, String $type, int $length, int $precis
348
348
public static function fromReflection (GenericReflection $ reflection , array $ columnResult ): ReflectedColumn
349
349
{
350
350
$ name = $ columnResult ['COLUMN_NAME ' ];
351
- $ length = $ columnResult ['CHARACTER_MAXIMUM_LENGTH ' ] + 0 ;
351
+ $ length = ( int ) $ columnResult ['CHARACTER_MAXIMUM_LENGTH ' ];
352
352
$ type = $ reflection ->toJdbcType ($ columnResult ['DATA_TYPE ' ], $ length );
353
- $ precision = $ columnResult ['NUMERIC_PRECISION ' ] + 0 ;
354
- $ scale = $ columnResult ['NUMERIC_SCALE ' ] + 0 ;
353
+ $ precision = ( int ) $ columnResult ['NUMERIC_PRECISION ' ];
354
+ $ scale = ( int ) $ columnResult ['NUMERIC_SCALE ' ];
355
355
$ nullable = in_array (strtoupper ($ columnResult ['IS_NULLABLE ' ]), ['TRUE ' , 'YES ' , 'T ' , 'Y ' , '1 ' ]);
356
356
$ pk = false ;
357
357
$ fk = '' ;
@@ -1824,6 +1824,12 @@ public function definition(): GenericDefinition
1824
1824
return $ this ->definition ;
1825
1825
}
1826
1826
1827
+ private function addAuthorizationCondition (Condition $ condition2 ): Condition
1828
+ {
1829
+ $ condition1 = VariableStore::get ('authorization.condition ' );
1830
+ return $ condition1 ? AndCondition::fromArray ([$ condition1 , $ condition2 ]) : $ condition2 ;
1831
+ }
1832
+
1827
1833
public function createSingle (ReflectedTable $ table , array $ columnValues ) /*: ?String*/
1828
1834
{
1829
1835
$ this ->converter ->convertColumnValues ($ table , $ columnValues );
@@ -1849,6 +1855,7 @@ public function selectSingle(ReflectedTable $table, array $columnNames, String $
1849
1855
$ selectColumns = $ this ->columns ->getSelect ($ table , $ columnNames );
1850
1856
$ tableName = $ table ->getName ();
1851
1857
$ condition = new ColumnCondition ($ table ->getPk (), 'eq ' , $ id );
1858
+ $ condition = $ this ->addAuthorizationCondition ($ condition );
1852
1859
$ parameters = array ();
1853
1860
$ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
1854
1861
$ sql = 'SELECT ' . $ selectColumns . ' FROM " ' . $ tableName . '" ' . $ whereClause ;
@@ -1870,6 +1877,7 @@ public function selectMultiple(ReflectedTable $table, array $columnNames, array
1870
1877
$ selectColumns = $ this ->columns ->getSelect ($ table , $ columnNames );
1871
1878
$ tableName = $ table ->getName ();
1872
1879
$ condition = new ColumnCondition ($ table ->getPk (), 'in ' , implode (', ' , $ ids ));
1880
+ $ condition = $ this ->addAuthorizationCondition ($ condition );
1873
1881
$ parameters = array ();
1874
1882
$ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
1875
1883
$ sql = 'SELECT ' . $ selectColumns . ' FROM " ' . $ tableName . '" ' . $ whereClause ;
@@ -1882,6 +1890,7 @@ public function selectMultiple(ReflectedTable $table, array $columnNames, array
1882
1890
public function selectCount (ReflectedTable $ table , Condition $ condition ): int
1883
1891
{
1884
1892
$ tableName = $ table ->getName ();
1893
+ $ condition = $ this ->addAuthorizationCondition ($ condition );
1885
1894
$ parameters = array ();
1886
1895
$ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
1887
1896
$ sql = 'SELECT COUNT(*) FROM " ' . $ tableName . '" ' . $ whereClause ;
@@ -1893,6 +1902,7 @@ public function selectAllUnordered(ReflectedTable $table, array $columnNames, Co
1893
1902
{
1894
1903
$ selectColumns = $ this ->columns ->getSelect ($ table , $ columnNames );
1895
1904
$ tableName = $ table ->getName ();
1905
+ $ condition = $ this ->addAuthorizationCondition ($ condition );
1896
1906
$ parameters = array ();
1897
1907
$ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
1898
1908
$ sql = 'SELECT ' . $ selectColumns . ' FROM " ' . $ tableName . '" ' . $ whereClause ;
@@ -1909,6 +1919,7 @@ public function selectAll(ReflectedTable $table, array $columnNames, Condition $
1909
1919
}
1910
1920
$ selectColumns = $ this ->columns ->getSelect ($ table , $ columnNames );
1911
1921
$ tableName = $ table ->getName ();
1922
+ $ condition = $ this ->addAuthorizationCondition ($ condition );
1912
1923
$ parameters = array ();
1913
1924
$ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
1914
1925
$ orderBy = $ this ->columns ->getOrderBy ($ table , $ columnOrdering );
@@ -1929,6 +1940,7 @@ public function updateSingle(ReflectedTable $table, array $columnValues, String
1929
1940
$ updateColumns = $ this ->columns ->getUpdate ($ table , $ columnValues );
1930
1941
$ tableName = $ table ->getName ();
1931
1942
$ condition = new ColumnCondition ($ table ->getPk (), 'eq ' , $ id );
1943
+ $ condition = $ this ->addAuthorizationCondition ($ condition );
1932
1944
$ parameters = array_values ($ columnValues );
1933
1945
$ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
1934
1946
$ sql = 'UPDATE " ' . $ tableName . '" SET ' . $ updateColumns . $ whereClause ;
@@ -1940,6 +1952,7 @@ public function deleteSingle(ReflectedTable $table, String $id)
1940
1952
{
1941
1953
$ tableName = $ table ->getName ();
1942
1954
$ condition = new ColumnCondition ($ table ->getPk (), 'eq ' , $ id );
1955
+ $ condition = $ this ->addAuthorizationCondition ($ condition );
1943
1956
$ parameters = array ();
1944
1957
$ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
1945
1958
$ sql = 'DELETE FROM " ' . $ tableName . '" ' . $ whereClause ;
@@ -1956,6 +1969,7 @@ public function incrementSingle(ReflectedTable $table, array $columnValues, Stri
1956
1969
$ updateColumns = $ this ->columns ->getIncrement ($ table , $ columnValues );
1957
1970
$ tableName = $ table ->getName ();
1958
1971
$ condition = new ColumnCondition ($ table ->getPk (), 'eq ' , $ id );
1972
+ $ condition = $ this ->addAuthorizationCondition ($ condition );
1959
1973
$ parameters = array_values ($ columnValues );
1960
1974
$ whereClause = $ this ->conditions ->getWhereClause ($ condition , $ parameters );
1961
1975
$ sql = 'UPDATE " ' . $ tableName . '" SET ' . $ updateColumns . $ whereClause ;
@@ -2685,6 +2699,26 @@ protected function getProperty(String $key, $default)
2685
2699
}
2686
2700
}
2687
2701
2702
+ // file: src/Tqdev/PhpCrudApi/Middleware/Communication/VariableStore.php
2703
+
2704
+ class VariableStore
2705
+ {
2706
+ static $ values = array ();
2707
+
2708
+ public static function get (String $ key )
2709
+ {
2710
+ if (isset (self ::$ values [$ key ])) {
2711
+ return self ::$ values [$ key ];
2712
+ }
2713
+ return null ;
2714
+ }
2715
+
2716
+ public static function set (String $ key , /* object */ $ value )
2717
+ {
2718
+ self ::$ values [$ key ] = $ value ;
2719
+ }
2720
+ }
2721
+
2688
2722
// file: src/Tqdev/PhpCrudApi/Middleware/Router/Router.php
2689
2723
2690
2724
interface Router extends Handler
@@ -2848,6 +2882,23 @@ private function handleAllTables(String $method, String $path, String $databaseN
2848
2882
}
2849
2883
}
2850
2884
2885
+ private function handleRecords (String $ method , String $ path , String $ databaseName , String $ tableName ) /*: void*/
2886
+ {
2887
+ if (!$ this ->reflection ->hasTable ($ tableName )) {
2888
+ return ;
2889
+ }
2890
+ $ recordHandler = $ this ->getProperty ('recordHandler ' , '' );
2891
+ if ($ recordHandler ) {
2892
+ $ query = call_user_func ($ recordHandler , $ method , $ path , $ databaseName , $ tableName );
2893
+ $ filters = new FilterInfo ();
2894
+ $ table = $ this ->reflection ->getTable ($ tableName );
2895
+ $ query = str_replace ('][]= ' , ']= ' , str_replace ('= ' , '[]= ' , $ query ));
2896
+ parse_str ($ query , $ params );
2897
+ $ condition = $ filters ->getCombinedConditions ($ table , $ params );
2898
+ VariableStore::set ('authorization.condition ' , $ condition );
2899
+ }
2900
+ }
2901
+
2851
2902
public function handle (Request $ request ): Response
2852
2903
{
2853
2904
$ method = $ request ->getMethod ();
@@ -2860,6 +2911,7 @@ public function handle(Request $request): Response
2860
2911
if (isset ($ params ['join ' ])) {
2861
2912
$ this ->handleJoinTables ($ method , $ path , $ databaseName , $ params ['join ' ]);
2862
2913
}
2914
+ $ this ->handleRecords ($ method , $ path , $ databaseName , $ tableName );
2863
2915
} elseif ($ path == 'columns ' ) {
2864
2916
$ tableName = $ request ->getPathSegment (2 );
2865
2917
if ($ tableName ) {
@@ -3402,7 +3454,7 @@ public function _or(Condition $condition): Condition
3402
3454
return $ condition ;
3403
3455
}
3404
3456
3405
- public function not (): Condition
3457
+ public function _not (): Condition
3406
3458
{
3407
3459
return $ this ;
3408
3460
}
0 commit comments