diff --git a/app/modules/api/Controllers/Config/BackupController.php b/app/modules/api/Controllers/Config/BackupController.php new file mode 100644 index 00000000..3f2971d3 --- /dev/null +++ b/app/modules/api/Controllers/Config/BackupController.php @@ -0,0 +1,121 @@ +. + */ + +namespace SP\Modules\Api\Controllers\Config; + +use Exception; +use Klein\Klein; +use SP\Core\Acl\Acl; +use SP\Core\Acl\ActionsInterface; +use SP\Core\Application; +use SP\Core\Events\Event; +use SP\Core\Events\EventMessage; +use SP\Domain\Api\ApiServiceInterface; +use SP\Domain\Api\Services\ApiResponse; +use SP\Domain\Export\FileBackupServiceInterface; +use SP\Domain\Export\Services\BackupFiles; +use SP\Modules\Api\Controllers\ControllerBase; +use SP\Modules\Api\Controllers\Help\ConfigHelp; + +/** + * Class BackupController + * + * @package SP\Modules\Api\Controllers + */ +final class BackupController extends ControllerBase +{ + private FileBackupServiceInterface $fileBackupService; + + /** + * @throws \SP\Core\Exceptions\InvalidClassException + */ + public function __construct( + Application $application, + Klein $router, + ApiServiceInterface $apiService, + Acl $acl, + FileBackupServiceInterface $fileBackupService + ) { + parent::__construct($application, $router, $apiService, $acl); + + $this->fileBackupService = $fileBackupService; + + $this->apiService->setHelpClass(ConfigHelp::class); + } + + /** + * backupAction + */ + public function backupAction(): void + { + try { + $this->setupApi(ActionsInterface::CONFIG_BACKUP_RUN); + + $path = $this->apiService->getParamString('path', false, BACKUP_PATH); + + $this->fileBackupService->doBackup($path); + + $this->eventDispatcher->notifyEvent( + 'run.backup.end', + new Event( + $this, + EventMessage::factory() + ->addDescription(__u('Application and database backup completed successfully')) + ->addDetail(__u('Path'), $path) + ) + ); + + $this->returnResponse( + ApiResponse::makeSuccess($this->buildBackupFiles($path), null, __('Backup process finished')) + ); + } catch (Exception $e) { + processException($e); + + $this->returnResponseException($e); + } + } + + /** + * @param string|null $path + * + * @return array[] + */ + private function buildBackupFiles(?string $path): array + { + return [ + 'files' => [ + 'app' => BackupFiles::getAppBackupFilename( + $path, + $this->fileBackupService->getHash(), + true + ), + 'db' => BackupFiles::getDbBackupFilename( + $path, + $this->fileBackupService->getHash(), + true + ), + ], + ]; + } +} \ No newline at end of file diff --git a/app/modules/api/Controllers/Config/ExportController.php b/app/modules/api/Controllers/Config/ExportController.php new file mode 100644 index 00000000..fea0aefb --- /dev/null +++ b/app/modules/api/Controllers/Config/ExportController.php @@ -0,0 +1,103 @@ +. + */ + +namespace SP\Modules\Api\Controllers\Config; + + +use Exception; +use Klein\Klein; +use SP\Core\Acl\Acl; +use SP\Core\Acl\ActionsInterface; +use SP\Core\Application; +use SP\Core\Events\Event; +use SP\Core\Events\EventMessage; +use SP\Domain\Api\ApiServiceInterface; +use SP\Domain\Api\Services\ApiResponse; +use SP\Domain\Export\XmlExportServiceInterface; +use SP\Modules\Api\Controllers\ControllerBase; +use SP\Modules\Api\Controllers\Help\ConfigHelp; + +/** + * Class ExportController + */ +final class ExportController extends ControllerBase +{ + private XmlExportServiceInterface $xmlExportService; + + /** + * @throws \SP\Core\Exceptions\InvalidClassException + */ + public function __construct( + Application $application, + Klein $router, + ApiServiceInterface $apiService, + Acl $acl, + XmlExportServiceInterface $xmlExportService + ) { + parent::__construct($application, $router, $apiService, $acl); + + $this->xmlExportService = $xmlExportService; + + $this->apiService->setHelpClass(ConfigHelp::class); + } + + /** + * exportAction + */ + public function exportAction(): void + { + try { + $this->setupApi(ActionsInterface::CONFIG_EXPORT_RUN); + + $password = $this->apiService->getParamString('password'); + $path = $this->apiService->getParamString('path', false, BACKUP_PATH); + + $this->eventDispatcher->notifyEvent( + 'run.export.start', + new Event( + $this, + EventMessage::factory() + ->addDescription(__u('sysPass XML export')) + ->addDetail(__u('Path'), $path) + ) + ); + + $this->xmlExportService->doExport($path, $password); + + + $this->eventDispatcher->notifyEvent( + 'run.export.end', + new Event($this, EventMessage::factory()->addDescription(__u('Export process finished'))) + ); + + $exportFiles = ['files' => ['xml' => $this->xmlExportService->getExportFile()]]; + + $this->returnResponse(ApiResponse::makeSuccess($exportFiles, null, __('Export process finished'))); + } catch (Exception $e) { + processException($e); + + $this->returnResponseException($e); + } + } +} \ No newline at end of file diff --git a/app/modules/api/Controllers/ConfigController.php b/app/modules/api/Controllers/ConfigController.php deleted file mode 100644 index 40b93b2c..00000000 --- a/app/modules/api/Controllers/ConfigController.php +++ /dev/null @@ -1,158 +0,0 @@ -. - */ - -namespace SP\Modules\Api\Controllers; - -use Exception; -use SP\Core\Acl\ActionsInterface; -use SP\Core\Events\Event; -use SP\Core\Events\EventMessage; -use SP\Core\Exceptions\InvalidClassException; -use SP\Domain\Api\Services\ApiResponse; -use SP\Domain\Export\Services\BackupFiles; -use SP\Domain\Export\Services\FileBackupService; -use SP\Domain\Export\Services\XmlExportService; -use SP\Modules\Api\Controllers\Help\ConfigHelp; - -/** - * Class ConfigController - * - * @package SP\Modules\Api\Controllers - */ -final class ConfigController extends ControllerBase -{ - /** - * backupAction - */ - public function backupAction(): void - { - try { - $this->setupApi(ActionsInterface::CONFIG_BACKUP_RUN); - - $path = $this->apiService->getParamString('path', false, BACKUP_PATH); - - $backupService = $this->dic->get(FileBackupService::class); - $backupService->doBackup($path); - - $this->eventDispatcher->notifyEvent( - 'run.backup.end', - new Event( - $this, - EventMessage::factory() - ->addDescription(__u('Application and database backup completed successfully')) - ->addDetail(__u('Path'), $path) - ) - ); - - $backupFiles = [ - 'files' => [ - 'app' => BackupFiles::getAppBackupFilename( - $path, - $backupService->getHash(), - true - ), - 'db' => BackupFiles::getDbBackupFilename( - $path, - $backupService->getHash(), - true - ) - ] - ]; - - $this->returnResponse( - ApiResponse::makeSuccess( - $backupFiles, - null, - __('Backup process finished') - ) - ); - } catch (Exception $e) { - processException($e); - - $this->returnResponseException($e); - } - } - - /** - * exportAction - */ - public function exportAction(): void - { - try { - $this->setupApi(ActionsInterface::CONFIG_EXPORT_RUN); - - $password = $this->apiService->getParamString('password'); - $path = $this->apiService->getParamString('path', false, BACKUP_PATH); - - $this->eventDispatcher->notifyEvent( - 'run.export.start', - new Event( - $this, - EventMessage::factory() - ->addDescription(__u('sysPass XML export')) - ->addDetail(__u('Path'), $path) - ) - ); - - $exportService = $this->dic->get(XmlExportService::class); - $exportService->doExport($path, $password); - - - $this->eventDispatcher->notifyEvent( - 'run.export.end', - new Event( - $this, - EventMessage::factory() - ->addDescription(__u('Export process finished')) - ) - ); - - $exportFiles = [ - 'files' => [ - 'xml' => $exportService->getExportFile() - ] - ]; - - $this->returnResponse( - ApiResponse::makeSuccess( - $exportFiles, - null, - __('Export process finished') - ) - ); - } catch (Exception $e) { - processException($e); - - $this->returnResponseException($e); - } - } - - /** - * @throws InvalidClassException - */ - protected function initialize(): void - { - $this->apiService->setHelpClass(ConfigHelp::class); - } -} \ No newline at end of file