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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

namespace SP\Infrastructure\Adapter\In\Web\Controllers\ConfigAccount;

use SP\Infrastructure\Adapter\In\Web\Controllers\Helpers\SimpleControllerHelper;
use SP\Application\Application;
use SP\Application\Config\Ports\ConfigBackupService;
use SP\Domain\Core\Events\Event;
use SP\Domain\Core\Events\EventMessage;
use SP\Domain\Common\Attributes\Action;
Expand All @@ -44,8 +47,17 @@
*/
final class SaveController extends SimpleControllerBase
{

use ConfigTrait;

public function __construct(
Application $application,
SimpleControllerHelper $simpleControllerHelper,
private readonly ConfigBackupService $configBackup
) {
parent::__construct($application, $simpleControllerHelper);
}

private const MAX_FILES_SIZE = 16384;

private const SECONDS_PER_DAY = 24 * 3600;
Expand Down Expand Up @@ -76,6 +88,7 @@ public function saveAction(): ActionResponse
return $this->saveConfig(
$configData,
$this->config,
$this->configBackup,
fn() => $this->eventDispatcher->notify(new Event('save.config.account', $this, $eventMessage))
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

namespace SP\Infrastructure\Adapter\In\Web\Controllers\ConfigAuth;

use SP\Infrastructure\Adapter\In\Web\Controllers\Helpers\SimpleControllerHelper;
use SP\Application\Application;
use SP\Application\Config\Ports\ConfigBackupService;
use SP\Domain\Core\Events\Event;
use SP\Domain\Core\Events\EventMessage;
use SP\Domain\Common\Attributes\Action;
Expand All @@ -18,8 +21,17 @@

final class SaveController extends SimpleControllerBase
{

use ConfigTrait;

public function __construct(
Application $application,
SimpleControllerHelper $simpleControllerHelper,
private readonly ConfigBackupService $configBackup
) {
parent::__construct($application, $simpleControllerHelper);
}

#[Action(ResponseType::JSON)]
public function saveAction(): ActionResponse
{
Expand All @@ -31,6 +43,7 @@ public function saveAction(): ActionResponse
return $this->saveConfig(
$configData,
$this->config,
$this->configBackup,
fn() => $this->eventDispatcher->notify(new Event('save.config.auth', $this, $eventMessage))
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

namespace SP\Infrastructure\Adapter\In\Web\Controllers\ConfigDokuWiki;

use SP\Infrastructure\Adapter\In\Web\Controllers\Helpers\SimpleControllerHelper;
use SP\Application\Application;
use SP\Application\Config\Ports\ConfigBackupService;
use SP\Domain\Core\Events\Event;
use SP\Domain\Core\Events\EventMessage;
use SP\Domain\Common\Attributes\Action;
Expand All @@ -38,8 +41,17 @@

final class SaveController extends SimpleControllerBase
{

use ConfigTrait;

public function __construct(
Application $application,
SimpleControllerHelper $simpleControllerHelper,
private readonly ConfigBackupService $configBackup
) {
parent::__construct($application, $simpleControllerHelper);
}

#[Action(ResponseType::JSON)]
public function saveAction(): ActionResponse
{
Expand Down Expand Up @@ -79,6 +91,7 @@ public function saveAction(): ActionResponse
return $this->saveConfig(
$configData,
$this->config,
$this->configBackup,
function () use ($eventMessage) {
$this->eventDispatcher->notify(new Event('save.config.dokuwiki', $this, $eventMessage));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

namespace SP\Infrastructure\Adapter\In\Web\Controllers\ConfigEvents;

use SP\Infrastructure\Adapter\In\Web\Controllers\Helpers\SimpleControllerHelper;
use SP\Application\Application;
use SP\Application\Config\Ports\ConfigBackupService;
use SP\Domain\Core\Events\Event;
use SP\Domain\Core\Events\EventMessage;
use SP\Domain\Common\Attributes\Action;
Expand All @@ -20,8 +23,17 @@

final class SaveController extends SimpleControllerBase
{

use ConfigTrait;

public function __construct(
Application $application,
SimpleControllerHelper $simpleControllerHelper,
private readonly ConfigBackupService $configBackup
) {
parent::__construct($application, $simpleControllerHelper);
}

#[Action(ResponseType::JSON)]
public function saveAction(): ActionResponse
{
Expand All @@ -33,6 +45,7 @@ public function saveAction(): ActionResponse
return $this->saveConfig(
$configData,
$this->config,
$this->configBackup,
fn() => $this->eventDispatcher->notify(new Event('save.config.events', $this, $eventMessage))
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

namespace SP\Infrastructure\Adapter\In\Web\Controllers\ConfigGeneral;

use SP\Application\Config\Ports\ConfigBackupService;
use SP\Application\Application;
use SP\Domain\Core\Events\Event;
use SP\Domain\Common\Attributes\Action;
Expand Down Expand Up @@ -53,7 +54,8 @@ final class SaveController extends SimpleControllerBase
public function __construct(
Application $application,
SimpleControllerHelper $simpleControllerHelper,
private readonly AppLockHandler $appLock
private readonly AppLockHandler $appLock,
private readonly ConfigBackupService $configBackup
) {
parent::__construct($application, $simpleControllerHelper);
}
Expand All @@ -74,6 +76,7 @@ public function saveAction(): ActionResponse
return $this->saveConfig(
$configData,
$this->config,
$this->configBackup,
function () use ($configData) {
if ($configData->isMaintenance()) {
$this->appLock->lock($this->session->getUserData()->id, 'config');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

namespace SP\Infrastructure\Adapter\In\Web\Controllers\ConfigLdap;

use SP\Infrastructure\Adapter\In\Web\Controllers\Helpers\SimpleControllerHelper;
use SP\Application\Application;
use SP\Application\Config\Ports\ConfigBackupService;
use SP\Domain\Core\Events\Event;
use SP\Domain\Core\Events\EventMessage;
use SP\Domain\Auth\Providers\Ldap\LdapParams;
Expand All @@ -47,8 +50,17 @@
*/
final class SaveController extends SimpleControllerBase
{

use ConfigTrait;

public function __construct(
Application $application,
SimpleControllerHelper $simpleControllerHelper,
private readonly ConfigBackupService $configBackup
) {
parent::__construct($application, $simpleControllerHelper);
}

/**
* @throws ValidationException
* @throws SPException
Expand Down Expand Up @@ -116,6 +128,7 @@ public function saveAction(): ActionResponse
return $this->saveConfig(
$configData,
$this->config,
$this->configBackup,
function () use ($eventMessage) {
$this->eventDispatcher->notify(new Event('save.config.ldap', $this, $eventMessage));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

namespace SP\Infrastructure\Adapter\In\Web\Controllers\ConfigMail;

use SP\Infrastructure\Adapter\In\Web\Controllers\Helpers\SimpleControllerHelper;
use SP\Application\Application;
use SP\Application\Config\Ports\ConfigBackupService;
use SP\Domain\Core\Events\Event;
use SP\Domain\Core\Events\EventMessage;
use SP\Domain\Common\Attributes\Action;
Expand All @@ -44,8 +47,17 @@
*/
final class SaveController extends SimpleControllerBase
{

use ConfigTrait;

public function __construct(
Application $application,
SimpleControllerHelper $simpleControllerHelper,
private readonly ConfigBackupService $configBackup
) {
parent::__construct($application, $simpleControllerHelper);
}

/**
* @throws SPException
*/
Expand Down Expand Up @@ -108,6 +120,7 @@ public function saveAction(): ActionResponse
return $this->saveConfig(
$configData,
$this->config,
$this->configBackup,
function () use ($eventMessage) {
$this->eventDispatcher->notify(new Event('save.config.mail', $this, $eventMessage));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

namespace SP\Infrastructure\Adapter\In\Web\Controllers\ConfigSecurity;

use SP\Application\Config\Ports\ConfigBackupService;
use SP\Application\Application;
use SP\Domain\Core\Events\Event;
use SP\Domain\Common\Attributes\Action;
Expand All @@ -43,6 +44,7 @@ final class SaveController extends SimpleControllerBase
public function __construct(
Application $application,
SimpleControllerHelper $simpleControllerHelper,
private readonly ConfigBackupService $configBackup
) {
parent::__construct($application, $simpleControllerHelper);
}
Expand All @@ -61,6 +63,7 @@ public function saveAction(): ActionResponse
return $this->saveConfig(
$configData,
$this->config,
$this->configBackup,
function () {
$this->eventDispatcher->notify(new Event('save.config.security', $this));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

namespace SP\Infrastructure\Adapter\In\Web\Controllers\ConfigWiki;

use SP\Infrastructure\Adapter\In\Web\Controllers\Helpers\SimpleControllerHelper;
use SP\Application\Application;
use SP\Application\Config\Ports\ConfigBackupService;
use JsonException;
use SP\Domain\Core\Events\Event;
use SP\Domain\Core\Events\EventMessage;
Expand All @@ -43,8 +46,17 @@
*/
final class SaveController extends SimpleControllerBase
{

use ConfigTrait;

public function __construct(
Application $application,
SimpleControllerHelper $simpleControllerHelper,
private readonly ConfigBackupService $configBackup
) {
parent::__construct($application, $simpleControllerHelper);
}


/**
* @return ActionResponse
Expand Down Expand Up @@ -85,6 +97,7 @@ public function saveAction(): ActionResponse
return $this->saveConfig(
$configData,
$this->config,
$this->configBackup,
function () use ($eventMessage) {
$this->eventDispatcher->notify(new Event('save.config.wiki', $this, $eventMessage));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Exception;
use SP\Domain\Common\Dtos\ActionResponse;
use SP\Domain\Config\Ports\ConfigDataInterface;
use SP\Application\Config\Ports\ConfigBackupService;
use SP\Application\Config\Ports\ConfigFileService;
use SP\Domain\Core\Exceptions\SPException;

Expand All @@ -47,14 +48,34 @@ trait ConfigTrait
*/
protected function saveConfig(
ConfigDataInterface $configData,
ConfigFileService $config,
?callable $onSuccess = null
ConfigFileService $config,
ConfigBackupService $configBackup,
?callable $onSuccess = null
): ActionResponse {
try {
if ($configData->isDemoEnabled()) {
return ActionResponse::warning(__u('Ey, this is a DEMO!!'));
}

// Keep the configuration being replaced, so there is something to go back to.
//
// `ConfigBackupService::backup()` has existed since this rewrite was imported and was
// called from nowhere, so `config_backup` was never written — and the "Download config
// backup" link the Information page renders answered "Unable to retrieve the
// configuration" every time, for every installation.
//
// Here rather than inside `ConfigFile::save()`, which is where it belongs on paper:
// that would put `ConfigBackupService` in the constructor of a service the container
// builds while booting, and it needs `ConfigService`, which needs `Application`, which
// needs `ConfigFileService` — a cycle that only a lazy proxy breaks, on the one object
// every request depends on. This is every door an administrator changes configuration
// through, and it costs the boot path nothing.
//
// `getConfigData()` answers a clone, and `save()` has not run yet, so what it hands
// over here is still the previous configuration. `backup()` logs and swallows its own
// failures, so a database that cannot take it does not stop the save.
$configBackup->backup($config->getConfigData());

$config->save($configData);

if ($onSuccess !== null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

namespace SP\Tests\Unit\Infrastructure\Adapter\In\Web\Controllers\ConfigAccount;

use SP\Application\Config\Ports\ConfigBackupService;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\Exception;
Expand Down Expand Up @@ -93,7 +94,8 @@ public function savingIsRefusedWhenTheAclDenies(): void

new SaveController(
$application,
$this->simpleControllerHelper($acl, 'configAccount', 'save')
$this->simpleControllerHelper($acl, 'configAccount', 'save'),
self::createStub(ConfigBackupService::class)
);
}

Expand All @@ -116,7 +118,8 @@ public function savingReportsAFailureBehindItRatherThanEscaping(): void

$response = (new SaveController(
$application,
$this->simpleControllerHelper($acl, 'configAccount', 'save')
$this->simpleControllerHelper($acl, 'configAccount', 'save'),
self::createStub(ConfigBackupService::class)
))->saveAction();

self::assertSame(ResponseStatus::ERROR, $response->status);
Expand Down
Loading