From de4f2ffb5f4780aa2dd7880f21ffd49f22ed4904 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Thu, 26 Jan 2023 21:54:13 +0100 Subject: [PATCH] org -> unit change, some new functionality --- Admin/Routes/Cli.php | 13 +++ Controller/BackendController.php | 8 +- Controller/CliController.php | 122 +++++++++++++++++++++++++ Models/Audit.php | 8 ++ Models/AuditMapper.php | 1 + Theme/Backend/audit-list.tpl.php | 4 +- Theme/Cli/blockchain.tpl.php | 15 +++ tests/Controller/ApiControllerTest.php | 2 +- 8 files changed, 163 insertions(+), 10 deletions(-) create mode 100644 Admin/Routes/Cli.php create mode 100644 Controller/CliController.php create mode 100644 Theme/Cli/blockchain.tpl.php diff --git a/Admin/Routes/Cli.php b/Admin/Routes/Cli.php new file mode 100644 index 0000000..fca73df --- /dev/null +++ b/Admin/Routes/Cli.php @@ -0,0 +1,13 @@ + [ + [ + 'dest' => '\Modules\Admin\Controller\CliController:cliGenerateBlockchain', + 'verb' => RouteVerb::ANY, + ], + ], +]; diff --git a/Controller/BackendController.php b/Controller/BackendController.php index 67953ab..031d44a 100755 --- a/Controller/BackendController.php +++ b/Controller/BackendController.php @@ -104,13 +104,7 @@ final class BackendController extends Controller /** @var \Model\Setting[] $exportTemplates */ $exportTemplates = $this->app->appSettings->get( - names: [ - SettingsEnum::DEFAULT_PDF_EXPORT_TEMPLATE, - SettingsEnum::DEFAULT_EXCEL_EXPORT_TEMPLATE, - SettingsEnum::DEFAULT_CSV_EXPORT_TEMPLATE, - SettingsEnum::DEFAULT_WORD_EXPORT_TEMPLATE, - SettingsEnum::DEFAULT_EMAIL_EXPORT_TEMPLATE, - ], + names: [SettingsEnum::DEFAULT_LIST_EXPORTS], module: 'Admin' ); diff --git a/Controller/CliController.php b/Controller/CliController.php new file mode 100644 index 0000000..5406b75 --- /dev/null +++ b/Controller/CliController.php @@ -0,0 +1,122 @@ +app->l11nManager, $request, $response); + $view->setTemplate('/Modules/Auditor/Theme/Cli/blockchain'); + + $first = AuditMapper::get() + ->where('blockchain', null) + ->sort('id', OrderType::ASC) + ->limit(1) + ->execute(); + + if ($first->getId() === 1) { + $first = AuditMapper::get() + ->where('id', $first->getId() + 1) + ->execute(); + } + + $count = 0; + if (!($first instanceof NullAudit)) { + $last = AuditMapper::get() + ->sort('id', OrderType::DESC) + ->limit(1) + ->execute(); + + $previous = AuditMapper::get() + ->where('id', $first->getId() - 1) + ->execute(); + + $current = $first; + $endLastBatchId = $first->getId() - 1; + + while ($current->getId() !== 0 && $current->getId() <= $last->getId()) { + $batch = AuditMapper::getAll() + ->where('id', $endLastBatchId, '>') + ->sort('id', OrderType::ASC) + ->limit(50) + ->execute(); + + foreach ($batch as $audit) { + $current = $audit; + + $current->blockchain = \md5( + $previous->blockchain + . $current->getId() + . $current->createdBy->getId() + . $current->createdAt->format('Y-m-d H:i:s') + . $current->type + . $current->trigger + . $current->module + . $current->ref + . $current->old + . $current->new + . $current->content + ); + + AuditMapper::update()->with('blockchain')->execute($current); + ++$count; + + $previous = $current; + } + + $endLastBatchId = $current->getId(); + } + + } + + $view->setData('count', $count); + + return $view; + } +} diff --git a/Models/Audit.php b/Models/Audit.php index 5e7a2c0..1850407 100755 --- a/Models/Audit.php +++ b/Models/Audit.php @@ -119,6 +119,14 @@ class Audit */ public int $ip = 0; + /** + * Blockchain. + * + * @var null|string + * @since 1.0.0 + */ + public ?string $blockchain = null; + /** * Constructor. * diff --git a/Models/AuditMapper.php b/Models/AuditMapper.php index be20602..cef491e 100755 --- a/Models/AuditMapper.php +++ b/Models/AuditMapper.php @@ -45,6 +45,7 @@ final class AuditMapper extends DataMapperFactory 'auditor_audit_content' => ['name' => 'auditor_audit_content', 'type' => 'string', 'internal' => 'content'], 'auditor_audit_old' => ['name' => 'auditor_audit_old', 'type' => 'compress', 'internal' => 'old'], 'auditor_audit_new' => ['name' => 'auditor_audit_new', 'type' => 'compress', 'internal' => 'new'], + 'auditor_audit_blockchain' => ['name' => 'auditor_audit_blockchain', 'type' => 'string', 'internal' => 'blockchain', 'readonly' => true], ]; /** diff --git a/Theme/Backend/audit-list.tpl.php b/Theme/Backend/audit-list.tpl.php index 95bfaf9..f54aee0 100755 --- a/Theme/Backend/audit-list.tpl.php +++ b/Theme/Backend/audit-list.tpl.php @@ -15,14 +15,14 @@ declare(strict_types=1); use phpOMS\Uri\UriFactory; /** - * @var \phpOMS\Views\View $this + * @var \Web\Backend\Views\TableView $this * @var \Modules\Audit\Models\Audit[] $audits */ $audits = $this->getData('audits') ?? []; $tableView = $this->getData('tableView'); $tableView->id = 'auditList'; -$tableView->baseUri = 'admin/audit/list'; +$tableView->baseUri = '{/lang}/{/app}/admin/audit/list'; $tableView->exportUri = '{/api}auditor/list/export'; $tableView->setObjects($audits); diff --git a/Theme/Cli/blockchain.tpl.php b/Theme/Cli/blockchain.tpl.php new file mode 100644 index 0000000..a593f5a --- /dev/null +++ b/Theme/Cli/blockchain.tpl.php @@ -0,0 +1,15 @@ +getData('count') ?? 0) . ' elements.' . "\n"; diff --git a/tests/Controller/ApiControllerTest.php b/tests/Controller/ApiControllerTest.php index cdcc312..575c9d3 100755 --- a/tests/Controller/ApiControllerTest.php +++ b/tests/Controller/ApiControllerTest.php @@ -53,7 +53,7 @@ final class ApiControllerTest extends \PHPUnit\Framework\TestCase }; $this->app->dbPool = $GLOBALS['dbpool']; - $this->app->orgId = 1; + $this->app->unitId = 1; $this->app->accountManager = new AccountManager($GLOBALS['session']); $this->app->appSettings = new CoreSettings(); $this->app->moduleManager = new ModuleManager($this->app, __DIR__ . '/../../../../Modules/');