Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/Service/RowService.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use OCA\Tables\Model\RowDataInput;
use OCA\Tables\ResponseDefinitions;
use OCA\Tables\Service\ColumnTypes\IColumnTypeBusiness;
use OCA\Tables\Service\ValueObject\ViewColumnInformation;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\DB\Exception;
Expand Down Expand Up @@ -274,10 +275,27 @@ private function enhanceWithViewDefaults(?View $view, RowDataInput $data): RowDa
return $data;
}

/**
* @return array<int, true>
*/
private function extractReadOnlyColumns (View $view): array {
$columnSettings = $view->getColumnsSettingsArray();
return array_reduce($columnSettings, static function (array $carry, ViewColumnInformation $column) {
if ($column[ViewColumnInformation::KEY_READONLY]) {
$carry[$column->getId()] = true;
}
return $carry;
}, []);
}

/**
* @throws InternalError
*/
private function cleanupData(RowDataInput $data, array $columns, ?int $tableId, ?int $viewId): RowDataInput {
$readOnlyColumns = $viewId
? $this->extractReadOnlyColumns($this->viewMapper->find($viewId))
: null;

$out = new RowDataInput();
foreach ($data as $entry) {
$column = $this->getColumnFromColumnsArray((int)$entry['columnId'], $columns);
Expand All @@ -295,6 +313,10 @@ private function cleanupData(RowDataInput $data, array $columns, ?int $tableId,
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': ' . $e->getMessage());
}

if ($readOnlyColumns && isset($readOnlyColumns[$entry['columnId']])) {
continue;
}

// parse given value to respect the column type value format
$out->add((int)$entry['columnId'], $this->parseValueByColumnType($column, $entry['value']));
}
Expand Down
21 changes: 20 additions & 1 deletion lib/Service/ValueObject/ViewColumnInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
class ViewColumnInformation implements ArrayAccess, JsonSerializable {
public const KEY_ID = 'columnId';
public const KEY_ORDER = 'order';
public const KEY_READONLY = 'readonly';

/** @var array{columndId?: int, order?: int} */
/** @var array{columndId?: int, order?: int, readonly?: bool} */
protected array $data = [];
protected const KEYS = [
self::KEY_ID,
self::KEY_ORDER,
self::KEY_READONLY,
];

public function __construct(
Expand All @@ -33,6 +35,10 @@
$this->offsetSet(self::KEY_ORDER, $order);
}

public function getId(): int {
return $this->offsetGet(self::KEY_ID);
}

public function getOrder(): int {
return $this->offsetGet(self::KEY_ORDER);
}
Expand All @@ -57,7 +63,8 @@
if (!$this->offsetExists($offset)) {
return;
}
$this->ensureType($offset, $value);
$this->data[(string)$offset] = $value;

Check failure on line 67 in lib/Service/ValueObject/ViewColumnInformation.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable30

RedundantCast

lib/Service/ValueObject/ViewColumnInformation.php:67:15: RedundantCast: Redundant cast to string (see https://psalm.dev/262)

Check failure on line 67 in lib/Service/ValueObject/ViewColumnInformation.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

RedundantCast

lib/Service/ValueObject/ViewColumnInformation.php:67:15: RedundantCast: Redundant cast to string (see https://psalm.dev/262)
}

public function offsetUnset(mixed $offset): void {
Expand All @@ -70,4 +77,16 @@
public function jsonSerialize(): array {
return $this->data;
}

protected function ensureType(string $offset, mixed &$value): void {
switch ($offset) {
case self::KEY_ID:
case self::KEY_ORDER:
$value = (int)$value;
break;
case self::KEY_READONLY:
$value = (bool)$value;
break;
}
}
}
Loading