diff --git a/Core/Lib/ExtendedController/LogAuditTrait.php b/Core/Lib/ExtendedController/LogAuditTrait.php index c81fd9de3..f25c64c9d 100644 --- a/Core/Lib/ExtendedController/LogAuditTrait.php +++ b/Core/Lib/ExtendedController/LogAuditTrait.php @@ -25,7 +25,7 @@ trait LogAuditTrait { public function createViewLogAudit(string $viewName = 'ListLogMessage') { - $this->addListView($viewName, 'LogMessage', 'history', 'fas fa-history'); + $this->addListView($viewName, 'LogAudit', 'history', 'fas fa-history'); $this->views[$viewName]->addOrderBy(['time'], 'date', 2); $this->views[$viewName]->addSearchFields(['context', 'message']); diff --git a/Core/Model/Asiento.php b/Core/Model/Asiento.php index bbd784de0..51d9d8f98 100644 --- a/Core/Model/Asiento.php +++ b/Core/Model/Asiento.php @@ -169,7 +169,8 @@ public function delete(): bool } // add audit log - Tools::log(self::AUDIT_CHANNEL)->warning('deleted-model', [ + $logAudit = new LogAudit(); + $logAudit->warning('deleted-model', [ '%model%' => $this->modelClassName(), '%key%' => $this->primaryColumnValue(), '%desc%' => $this->primaryDescription(), @@ -337,7 +338,8 @@ public function save(): bool } // add audit log - Tools::log(self::AUDIT_CHANNEL)->info('updated-model', [ + $logAudit = new LogAudit(); + $logAudit->info('updated-model', [ '%model%' => $this->modelClassName(), '%key%' => $this->primaryColumnValue(), '%desc%' => $this->primaryDescription(), diff --git a/Core/Model/Base/BusinessDocument.php b/Core/Model/Base/BusinessDocument.php index 3c5aab417..bb992e1de 100644 --- a/Core/Model/Base/BusinessDocument.php +++ b/Core/Model/Base/BusinessDocument.php @@ -22,6 +22,7 @@ use FacturaScripts\Core\Base\DataBase\DataBaseWhere; use FacturaScripts\Core\Base\Utils; use FacturaScripts\Core\DataSrc\Almacenes; +use FacturaScripts\Core\Model\LogAudit; use FacturaScripts\Core\Tools; use FacturaScripts\Dinamic\Lib\BusinessDocumentCode; use FacturaScripts\Dinamic\Model\Almacen; @@ -506,7 +507,8 @@ protected function saveUpdate(array $values = []): bool } // add audit log - Tools::log(self::AUDIT_CHANNEL)->info('updated-model', [ + $logAudit = new LogAudit(); + $logAudit->info('updated-model', [ '%model%' => $this->modelClassName(), '%key%' => $this->primaryColumnValue(), '%desc%' => $this->primaryDescription(), diff --git a/Core/Model/Base/TransformerDocument.php b/Core/Model/Base/TransformerDocument.php index dd060beac..6bc78ff8f 100644 --- a/Core/Model/Base/TransformerDocument.php +++ b/Core/Model/Base/TransformerDocument.php @@ -20,6 +20,7 @@ namespace FacturaScripts\Core\Model\Base; use FacturaScripts\Core\Base\DataBase\DataBaseWhere; +use FacturaScripts\Core\Model\LogAudit; use FacturaScripts\Core\Tools; use FacturaScripts\Dinamic\Lib\BusinessDocumentGenerator; use FacturaScripts\Dinamic\Model\DocTransformation; @@ -167,7 +168,8 @@ public function delete(): bool } // add audit log - Tools::log(self::AUDIT_CHANNEL)->warning('deleted-model', [ + $logAudit = new LogAudit(); + $logAudit->warning('deleted-model', [ '%model%' => $this->modelClassName(), '%key%' => $this->primaryColumnValue(), '%desc%' => $this->primaryDescription(), diff --git a/Core/Model/LogAudit.php b/Core/Model/LogAudit.php new file mode 100644 index 000000000..ea860b49c --- /dev/null +++ b/Core/Model/LogAudit.php @@ -0,0 +1,139 @@ +time = Tools::dateTime(); + } + + /** + * Returns the saved context as array. + * + * @return array + */ + public function context(): array + { + return json_decode(Tools::fixHtml($this->context), true); + } + + public function delete(): bool + { + Tools::log()->warning('cant-delete-audit-log'); + return false; + } + + public static function primaryColumn(): string + { + return 'id'; + } + + public static function tableName(): string + { + return 'logs_audit'; + } + + public function test(): bool + { + $this->channel = Tools::noHtml($this->channel); + $this->context = Tools::noHtml($this->context); + $this->message = Tools::noHtml($this->message); + if (strlen($this->message) > static::MAX_MESSAGE_LEN) { + $this->message = substr($this->message, 0, static::MAX_MESSAGE_LEN); + } + + $this->model = Tools::noHtml($this->model); + $this->modelcode = Tools::noHtml((string)$this->modelcode); + $this->uri = Tools::noHtml($this->uri); + + return parent::test(); + } + + /** + * @param string $message + * @param array $context + */ + public function warning(string $message, array $context = []): void + { + $this->level = 'warning'; + $this->log($message, $context); + } + + public function info(string $message, array $context = []): void + { + $this->level = 'info'; + $this->log($message, $context); + } + + private function log(string $message, array $context = []): void + { + $this->channel = self::AUDIT_CHANNEL; + $this->context = $this->getContext($context); + $this->idcontacto = $context['idcontacto'] ?? null; + $this->ip = Session::getClientIp(); + $this->message = Tools::lang()->trans($message, $context); + $this->model = $context['model-class'] ?? null; + $this->modelcode = $context['model-code'] ?? null; + $this->nick = $context['nick'] ?? Session::user()->nick; + $this->time = isset($context['time']) ? date('d-m-Y H:i:s', (int)$context['time']) : date('d-m-Y H:i:s'); + $this->uri = $context['uri'] ?? Session::get('uri'); + $this->save(); + } + + protected function saveUpdate(array $values = []): bool + { + Tools::log()->warning('cant-update-audit-log'); + return false; + } + + private function getContext(array $context) + { + return json_encode($context); + } +} diff --git a/Core/Table/logs_audit.xml b/Core/Table/logs_audit.xml new file mode 100644 index 000000000..e0c8e9297 --- /dev/null +++ b/Core/Table/logs_audit.xml @@ -0,0 +1,59 @@ + +