diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c7370c3..0cd9e719 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,9 +12,8 @@ before starting to add changes. Use example [placed in the end of the page](#exa ## [Unreleased] - Adding Lat and Long fetching to DataAddress -- [#84](https://github.com/OS2Forms/os2forms/pull/84) - Added digital post test command. -- [#96](https://github.com/OS2Forms/os2forms/pull/96) +- Added digital post test command. +- Added new audit logging module (os2form_audit). ## [3.14.1] 2024-01-16 diff --git a/modules/os2forms_audit/README.md b/modules/os2forms_audit/README.md new file mode 100644 index 00000000..4607955a --- /dev/null +++ b/modules/os2forms_audit/README.md @@ -0,0 +1,42 @@ +# OS2Form Audit Module + +OS2Form Audit is a Drupal module that helps track changes and perform audit on +OS2Form events. + +## Requirements +- +- PHP 8.1 or higher +- Drupal 8 or 9 +- Composer for managing PHP dependencies + +## Features +- +- Detailed audit log entries. +- Support for all OS2Form entity types. +- Easily extendable for custom use case. + +## Installation + +### Composer + +### Drush + +### Admin UI + +Alternatively, you can enable this module via Drupal admin UI by visiting the +extent page (`/admin/modules`). + +## Configuration + +Navigate to `/admin/config/os2form/audit` to configure the module. + +Some additional (brief) information on Usage, Extending/Overriding, and +Maintainers could go here. + +## Usage + +Describe how to use this module after installation. + +## Extending and Overriding + +Describe how to extend or override this module's functionality. diff --git a/modules/os2forms_audit/os2forms_audit.info.yml b/modules/os2forms_audit/os2forms_audit.info.yml index 43864a34..297e6f4d 100644 --- a/modules/os2forms_audit/os2forms_audit.info.yml +++ b/modules/os2forms_audit/os2forms_audit.info.yml @@ -1,6 +1,4 @@ name: OS2Forms Audit type: module description: 'OS2Forms Audit Module (log all events to external service)' -core_version_requirement: ^8 || ^9 -dependencies: - - 'drupal:admin_audit_trail' +core_version_requirement: ^8 || ^9 || ^10 diff --git a/modules/os2forms_audit/os2forms_audit.links.menu.yml b/modules/os2forms_audit/os2forms_audit.links.menu.yml new file mode 100644 index 00000000..7032a261 --- /dev/null +++ b/modules/os2forms_audit/os2forms_audit.links.menu.yml @@ -0,0 +1,5 @@ +os2forms_audit.admin_settings: + title: 'OS2Forms Audit settings' + parent: system.admin_config_system + description: 'Settings for the OS2Forms Audit module' + route_name: os2forms_audit.admin_settings diff --git a/modules/os2forms_audit/os2forms_audit.module b/modules/os2forms_audit/os2forms_audit.module index e69de29b..50cce95b 100644 --- a/modules/os2forms_audit/os2forms_audit.module +++ b/modules/os2forms_audit/os2forms_audit.module @@ -0,0 +1,3 @@ +get('config.factory'), + $container->get('plugin.manager.os2forms_audit_logger') + ); + } + + /** + * The name of the configuration setting. + * + * @var string + */ + public static string $configName = 'os2forms_audit.settings'; + + /** + * {@inheritdoc} + */ + protected function getEditableConfigNames(): array { + return [self::$configName]; + } + + /** + * {@inheritdoc} + */ + public function getFormId(): string { + return 'os2forms_audit_admin_form'; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state): array { + $config = $this->config(self::$configName); + + $plugins = $this->loggerManager->getDefinitions(); + + $form['enabled'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Enabled'), + '#description' => $this->t('E'), + '#default_value' => $config->get('enabled'), + ]; + + return parent::buildForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state): void { + parent::submitForm($form, $form_state); + + $this->config(self::$configName) + ->set('enabled', $form_state->getValue('enabled')) + ->save(); + } + +} diff --git a/modules/os2forms_audit/src/Plugin/AuditLoggerInterface.php b/modules/os2forms_audit/src/Plugin/AuditLogger/AuditLoggerInterface.php similarity index 51% rename from modules/os2forms_audit/src/Plugin/AuditLoggerInterface.php rename to modules/os2forms_audit/src/Plugin/AuditLogger/AuditLoggerInterface.php index ca78507e..ed2ff916 100644 --- a/modules/os2forms_audit/src/Plugin/AuditLoggerInterface.php +++ b/modules/os2forms_audit/src/Plugin/AuditLogger/AuditLoggerInterface.php @@ -1,13 +1,16 @@ notice('Entity with ID @id is written.', ['@id' => $entity->id()]); + \Drupal::logger('os2forms_audit')->notice('Entity with ID @id is written.', ['@id' => $entity->id()]); } } diff --git a/modules/os2forms_audit/src/Plugin/File.php b/modules/os2forms_audit/src/Plugin/AuditLogger/File.php similarity index 66% rename from modules/os2forms_audit/src/Plugin/File.php rename to modules/os2forms_audit/src/Plugin/AuditLogger/File.php index e8a21762..e1af7270 100644 --- a/modules/os2forms_audit/src/Plugin/File.php +++ b/modules/os2forms_audit/src/Plugin/AuditLogger/File.php @@ -1,26 +1,25 @@ notice('Entity with ID @id is written.', ['@id' => $entity->id()]); + \Drupal::logger('os2forms_audit')->notice('Entity with ID @id is written.', ['@id' => $entity->id()]); } } diff --git a/modules/os2forms_audit/src/Plugin/LoggerManager.php b/modules/os2forms_audit/src/Plugin/LoggerManager.php new file mode 100644 index 00000000..2a761e71 --- /dev/null +++ b/modules/os2forms_audit/src/Plugin/LoggerManager.php @@ -0,0 +1,36 @@ +alterInfo('os2forms_audit_logger_info'); + $this->setCacheBackend($cache_backend, 'os2forms_audit_logger_plugins'); + } + +} diff --git a/modules/os2forms_audit/src/Service/Logger.php b/modules/os2forms_audit/src/Service/Logger.php index 1f6760e3..35c9dd51 100644 --- a/modules/os2forms_audit/src/Service/Logger.php +++ b/modules/os2forms_audit/src/Service/Logger.php @@ -1,5 +1,23 @@ loggerManager->getDefinitions(); + + } } diff --git a/modules/os2forms_audit/src/Service/LokiClient.php b/modules/os2forms_audit/src/Service/LokiClient.php index 16020685..cec555ed 100644 --- a/modules/os2forms_audit/src/Service/LokiClient.php +++ b/modules/os2forms_audit/src/Service/LokiClient.php @@ -3,16 +3,36 @@ namespace Drupal\os2forms_audit\Service; /** - * Class LokiClient + * Class LokiClient. * - * This is based/inspired by https://github.com/itspire/monolog-loki + * This is based/inspired by https://github.com/itspire/monolog-loki. */ class LokiClient { + /** + * @var string|null + */ protected ?string $entrypoint; + + /** + * Basic authentication username and password. + * + * @var array + */ protected array $basicAuth = []; + + /** + * Custom options for CURL command. + * + * @var array + */ protected array $customCurlOptions = []; + /** + * Curl handler. + * + * @var \CurlHandle|null + */ private ?\CurlHandle $connection = NULL; public function __construct( @@ -60,8 +80,8 @@ public function send(string $label, int $epoch, string $line, array $metadata = 'label' => $label, ], 'values' => [ - [ $epoch, $line, $metadata], - ] + [$epoch, $line, $metadata], + ], ], ]); }