Skip to content
Merged
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
39 changes: 28 additions & 11 deletions lib/Service/ModuleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,27 @@ public function toggle(int $spaceId, string $id, bool $enabled): array {
if (!isset($this->discover()[$id])) {
throw new DomainException('module_not_found');
}
$q = $this->db->getQueryBuilder();
$q->select('id')->from('lf_module_flags')
->where($q->expr()->eq('space_id', $q->createNamedParameter($spaceId, IQueryBuilder::PARAM_INT)))
->andWhere($q->expr()->eq('module_id', $q->createNamedParameter($id)));
$existing = $q->executeQuery()->fetchOne();
if ($existing === false) {
$this->write->insert('lf_module_flags', [
'space_id' => $spaceId, 'module_id' => $id, 'enabled' => $enabled ? 1 : 0,
]);
$key = ['space_id' => $spaceId, 'module_id' => $id];
$values = ['enabled' => $enabled ? 1 : 0];
if ($this->flagExists($spaceId, $id)) {
$this->write->update('lf_module_flags', $values, $key);
} else {
$this->write->update('lf_module_flags',
['enabled' => $enabled ? 1 : 0], ['space_id' => $spaceId, 'module_id' => $id]);
try {
$this->write->insert('lf_module_flags', $key + $values);
} catch (\OCP\DB\Exception $e) {
// Another request may have created the same unique flag after
// our read. Only recover when the precise scoped row exists;
// preserve unrelated database errors and recheck authority.
if ($e->getReason() !== \OCP\DB\Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION
|| !$this->flagExists($spaceId, $id)) {
throw $e;
}
$this->access->requireFinancialCapability($spaceId, 'modules.configure');
if (!isset($this->discover()[$id])) {
throw new DomainException('module_not_found');
}
$this->write->update('lf_module_flags', $values, $key);
}
}
// Return the effective state, not a misleading 'enabled' response
// when an ancestor policy still prohibits the module.
Expand All @@ -149,6 +158,14 @@ public function toggle(int $spaceId, string $id, bool $enabled): array {
'blockedByPolicy' => $blockedByPolicy];
}

private function flagExists(int $spaceId, string $id): bool {
$q = $this->db->getQueryBuilder();
$q->select('id')->from('lf_module_flags')
->where($q->expr()->eq('space_id', $q->createNamedParameter($spaceId, IQueryBuilder::PARAM_INT)))
->andWhere($q->expr()->eq('module_id', $q->createNamedParameter($id)));
return $q->executeQuery()->fetchOne() !== false;
}

/** Does not grant access or reveal a parent's confidential settings. */
private function blockedByPolicy(int $spaceId, string $id): bool {
$policy = match ($id) {
Expand Down
2 changes: 1 addition & 1 deletion tests/ci/smoke.sh
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ assert_integration_complete() {

if [ "$(printenv LF_BROWSER_DEBUG_ONLY || echo 0)" != 1 ]; then
# Run native PHP integration suites inside the same Nextcloud PHP runtime.
for script in basic personal-finance agenda-search-sort spaces advanced governance catalog categories companies custom-fields field-studio-v3 ux-enhancements company-logo bank-identity payment-methods currency-rate-history; do
for script in basic personal-finance agenda-search-sort spaces advanced governance catalog categories companies custom-fields field-studio-v3 ux-enhancements company-logo bank-identity payment-methods currency-rate-history module-toggle-race; do
assert_integration_complete "/var/www/html/custom_apps/librefinance/tests/integration/$script.php"
done
echo "PASS: Native PHP authorization, ledger, group access and concurrency tests on Nextcloud Server master."
Expand Down
50 changes: 50 additions & 0 deletions tests/integration/module-toggle-race.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* SPDX-FileCopyrightText: 2026 LibreCode Coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
declare(strict_types=1);

require_once __DIR__ . '/TestHarness.php';

use LibreFinance\Tests\Integration\TestHarness;

$t = new TestHarness();
$a = 'lfmaintestA';
$b = 'lfmaintestB';
$report = 'librefinance_reports';
$create = static fn(string $name): array => $t->check('create isolated module configuration space', $a,
'POST', '/spaces', ['name' => $name, 'kind' => 'collective', 'currency' => 'BRL'], 201);
$control = $create('Synthetic module isolation control');
$controlPath = '/spaces/' . (int)$control['id'];

$hasModule = static fn(bool $enabled): callable => static fn(array $r): bool =>
count(array_filter($r['modules'] ?? [], static fn(array $m): bool =>
$m['id'] === $report && $m['enabled'] === $enabled)) === 1;

for ($i = 1; $i <= 12; ++$i) {
$space = $create('Synthetic module first-write race ' . $i);
$path = '/spaces/' . (int)$space['id'];
$t->check('grant independent manager', $a, 'POST', $path . '/members',
['uid' => $b, 'role' => 'manager'], 200);
$t->check('module starts enabled without a configuration row', $a, 'GET',
$path . '/modules', null, 200, $hasModule(true));
$route = $path . '/modules/' . $report;
$request = ['method' => 'PUT', 'path' => $route, 'data' => ['enabled' => false]];
[$first, $second] = $t->parallel(['user' => $a, ...$request], ['user' => $b, ...$request]);
$t->record('concurrent first disables both succeed #' . $i,
$first['code'] === 200 && $second['code'] === 200
&& ($first['body']['enabled'] ?? null) === false
&& ($second['body']['enabled'] ?? null) === false,
'HTTP ' . $first['code'] . '/' . $second['code'], [$first, $second]);
$t->check('both authorized readers observe the disabled module', $b, 'GET',
$path . '/modules', null, 200, $hasModule(false));
$t->check('report access denied after concurrent disable', $a, 'GET',
$path . '/trial-balance?through=2026-12-31', null, 403,
static fn(array $r): bool => ($r['error'] ?? null) === 'module_disabled');
$t->check('owner can re-enable the existing flag', $a, 'PUT', $route,
['enabled' => true], 200, static fn(array $r): bool => ($r['enabled'] ?? null) === true);
$t->check('other workspace remains unaffected', $a, 'GET',
$controlPath . '/modules', null, 200, $hasModule(true));
}
$t->finish();