Skip to content

Commit a137513

Browse files
committed
build for #434
1 parent 3ec487b commit a137513

File tree

1 file changed

+56
-4
lines changed

1 file changed

+56
-4
lines changed

api.php

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,10 @@ public function __construct(String $name, String $type, int $length, int $precis
348348
public static function fromReflection(GenericReflection $reflection, array $columnResult): ReflectedColumn
349349
{
350350
$name = $columnResult['COLUMN_NAME'];
351-
$length = $columnResult['CHARACTER_MAXIMUM_LENGTH'] + 0;
351+
$length = (int) $columnResult['CHARACTER_MAXIMUM_LENGTH'];
352352
$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'];
355355
$nullable = in_array(strtoupper($columnResult['IS_NULLABLE']), ['TRUE', 'YES', 'T', 'Y', '1']);
356356
$pk = false;
357357
$fk = '';
@@ -1824,6 +1824,12 @@ public function definition(): GenericDefinition
18241824
return $this->definition;
18251825
}
18261826

1827+
private function addAuthorizationCondition(Condition $condition2): Condition
1828+
{
1829+
$condition1 = VariableStore::get('authorization.condition');
1830+
return $condition1 ? AndCondition::fromArray([$condition1, $condition2]) : $condition2;
1831+
}
1832+
18271833
public function createSingle(ReflectedTable $table, array $columnValues) /*: ?String*/
18281834
{
18291835
$this->converter->convertColumnValues($table, $columnValues);
@@ -1849,6 +1855,7 @@ public function selectSingle(ReflectedTable $table, array $columnNames, String $
18491855
$selectColumns = $this->columns->getSelect($table, $columnNames);
18501856
$tableName = $table->getName();
18511857
$condition = new ColumnCondition($table->getPk(), 'eq', $id);
1858+
$condition = $this->addAuthorizationCondition($condition);
18521859
$parameters = array();
18531860
$whereClause = $this->conditions->getWhereClause($condition, $parameters);
18541861
$sql = 'SELECT ' . $selectColumns . ' FROM "' . $tableName . '" ' . $whereClause;
@@ -1870,6 +1877,7 @@ public function selectMultiple(ReflectedTable $table, array $columnNames, array
18701877
$selectColumns = $this->columns->getSelect($table, $columnNames);
18711878
$tableName = $table->getName();
18721879
$condition = new ColumnCondition($table->getPk(), 'in', implode(',', $ids));
1880+
$condition = $this->addAuthorizationCondition($condition);
18731881
$parameters = array();
18741882
$whereClause = $this->conditions->getWhereClause($condition, $parameters);
18751883
$sql = 'SELECT ' . $selectColumns . ' FROM "' . $tableName . '" ' . $whereClause;
@@ -1882,6 +1890,7 @@ public function selectMultiple(ReflectedTable $table, array $columnNames, array
18821890
public function selectCount(ReflectedTable $table, Condition $condition): int
18831891
{
18841892
$tableName = $table->getName();
1893+
$condition = $this->addAuthorizationCondition($condition);
18851894
$parameters = array();
18861895
$whereClause = $this->conditions->getWhereClause($condition, $parameters);
18871896
$sql = 'SELECT COUNT(*) FROM "' . $tableName . '"' . $whereClause;
@@ -1893,6 +1902,7 @@ public function selectAllUnordered(ReflectedTable $table, array $columnNames, Co
18931902
{
18941903
$selectColumns = $this->columns->getSelect($table, $columnNames);
18951904
$tableName = $table->getName();
1905+
$condition = $this->addAuthorizationCondition($condition);
18961906
$parameters = array();
18971907
$whereClause = $this->conditions->getWhereClause($condition, $parameters);
18981908
$sql = 'SELECT ' . $selectColumns . ' FROM "' . $tableName . '"' . $whereClause;
@@ -1909,6 +1919,7 @@ public function selectAll(ReflectedTable $table, array $columnNames, Condition $
19091919
}
19101920
$selectColumns = $this->columns->getSelect($table, $columnNames);
19111921
$tableName = $table->getName();
1922+
$condition = $this->addAuthorizationCondition($condition);
19121923
$parameters = array();
19131924
$whereClause = $this->conditions->getWhereClause($condition, $parameters);
19141925
$orderBy = $this->columns->getOrderBy($table, $columnOrdering);
@@ -1929,6 +1940,7 @@ public function updateSingle(ReflectedTable $table, array $columnValues, String
19291940
$updateColumns = $this->columns->getUpdate($table, $columnValues);
19301941
$tableName = $table->getName();
19311942
$condition = new ColumnCondition($table->getPk(), 'eq', $id);
1943+
$condition = $this->addAuthorizationCondition($condition);
19321944
$parameters = array_values($columnValues);
19331945
$whereClause = $this->conditions->getWhereClause($condition, $parameters);
19341946
$sql = 'UPDATE "' . $tableName . '" SET ' . $updateColumns . $whereClause;
@@ -1940,6 +1952,7 @@ public function deleteSingle(ReflectedTable $table, String $id)
19401952
{
19411953
$tableName = $table->getName();
19421954
$condition = new ColumnCondition($table->getPk(), 'eq', $id);
1955+
$condition = $this->addAuthorizationCondition($condition);
19431956
$parameters = array();
19441957
$whereClause = $this->conditions->getWhereClause($condition, $parameters);
19451958
$sql = 'DELETE FROM "' . $tableName . '" ' . $whereClause;
@@ -1956,6 +1969,7 @@ public function incrementSingle(ReflectedTable $table, array $columnValues, Stri
19561969
$updateColumns = $this->columns->getIncrement($table, $columnValues);
19571970
$tableName = $table->getName();
19581971
$condition = new ColumnCondition($table->getPk(), 'eq', $id);
1972+
$condition = $this->addAuthorizationCondition($condition);
19591973
$parameters = array_values($columnValues);
19601974
$whereClause = $this->conditions->getWhereClause($condition, $parameters);
19611975
$sql = 'UPDATE "' . $tableName . '" SET ' . $updateColumns . $whereClause;
@@ -2685,6 +2699,26 @@ protected function getProperty(String $key, $default)
26852699
}
26862700
}
26872701

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+
26882722
// file: src/Tqdev/PhpCrudApi/Middleware/Router/Router.php
26892723

26902724
interface Router extends Handler
@@ -2848,6 +2882,23 @@ private function handleAllTables(String $method, String $path, String $databaseN
28482882
}
28492883
}
28502884

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+
28512902
public function handle(Request $request): Response
28522903
{
28532904
$method = $request->getMethod();
@@ -2860,6 +2911,7 @@ public function handle(Request $request): Response
28602911
if (isset($params['join'])) {
28612912
$this->handleJoinTables($method, $path, $databaseName, $params['join']);
28622913
}
2914+
$this->handleRecords($method, $path, $databaseName, $tableName);
28632915
} elseif ($path == 'columns') {
28642916
$tableName = $request->getPathSegment(2);
28652917
if ($tableName) {
@@ -3402,7 +3454,7 @@ public function _or(Condition $condition): Condition
34023454
return $condition;
34033455
}
34043456

3405-
public function not(): Condition
3457+
public function _not(): Condition
34063458
{
34073459
return $this;
34083460
}

0 commit comments

Comments
 (0)