Skip to content

Commit

Permalink
ITKDev: Added custom pluing manager for audit log extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
cableman committed Apr 18, 2024
1 parent a496578 commit 27b2579
Show file tree
Hide file tree
Showing 16 changed files with 296 additions and 32 deletions.
5 changes: 2 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
42 changes: 42 additions & 0 deletions modules/os2forms_audit/README.md
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 1 addition & 3 deletions modules/os2forms_audit/os2forms_audit.info.yml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions modules/os2forms_audit/os2forms_audit.links.menu.yml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions modules/os2forms_audit/os2forms_audit.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php


7 changes: 7 additions & 0 deletions modules/os2forms_audit/os2forms_audit.routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
os2forms_audit.admin_settings:
path: '/admin/config/os2forms_audit/settings'
defaults:
_form: '\Drupal\os2forms_audit\Form\SettingsForm'
_title: 'OS2Forms Audit settings'
requirements:
_permission: 'administer site'
4 changes: 4 additions & 0 deletions modules/os2forms_audit/os2forms_audit.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
services:
plugin.manager.os2forms_audit_logger:
class: Drupal\os2forms_audit\Plugin\LoggerManager
parent: default_plugin_manager
43 changes: 43 additions & 0 deletions modules/os2forms_audit/src/Annotation/AuditLoggerProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Drupal\os2forms_audit\Annotation;

use Drupal\Component\Annotation\Plugin;

/**
* Defines a AuditLoggerProvider annotation object.
*
* @see plugin_api
*
* @Annotation
*/
class AuditLoggerProvider extends Plugin {

/**
* The plugin ID.
*
* @var string
*/
public string $id;

/**
* The human-readable name of the consent storage.
*
* @var \Drupal\Core\Annotation\Translation
*
* @ingroup plugin_translatable
*/
public \Drupal\Core\Annotation\Translation $title;

/**
* A brief description of the consent storage.
*
* This will be shown when adding or configuring this consent storage.
*
* @var \Drupal\Core\Annotation\Translation|string
*
* @ingroup plugin_translatable
*/
public \Drupal\Core\Annotation\Translation|string $description = '';

}
88 changes: 88 additions & 0 deletions modules/os2forms_audit/src/Form/SettingsForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Drupal\os2forms_audit\Form;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\os2forms_audit\Plugin\LoggerManager;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Class SettingsForm.
*
* This is the settings for the module.
*/
class SettingsForm extends ConfigFormBase {

/**
* {@inheritdoc}
*/
public function __construct(
ConfigFactoryInterface $configFactory,
private readonly LoggerManager $loggerManager
) {
parent::__construct($configFactory);
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->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();
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?php

namespace Drupal\os2forms_audit\Plugin;
namespace Drupal\os2forms_audit\Plugin\AuditLogger;

use Drupal\Component\Plugin\ConfigurableInterface;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Plugin\PluginFormInterface;

/**
* Interface for AuditLogger plugins.
*/
interface AuditLoggerInterface {
interface AuditLoggerInterface extends PluginInspectionInterface {

/**
* Write the entity data.
Expand All @@ -16,4 +19,5 @@ interface AuditLoggerInterface {
* The entity to be audited.
*/
public function write(EntityInterface $entity);

}
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
<?php

namespace Drupal\os2forms_audit\Plugin;
namespace Drupal\os2forms_audit\Plugin\AuditLogger;

use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Entity\EntityInterface;
use Psr\Log\LogLevel;
use Drupal\Core\Plugin\PluginBase;

/**
* Stores entities in the database.
*
* @AuditLogger(
* @AuditLoggerProvider(
* id = "database",
* title = @Translation("Database logger"),
* description = @Translation("Store entity data in the database.")
* )
*/
class DatabaseEntityWriter extends PluginBase implements AuditLoggerInterface {
class Database extends PluginBase implements AuditLoggerInterface {

/**
* {@inheritdoc}
*/
public function write(EntityInterface $entity) {
public function write(EntityInterface $entity): void {
// Code to write the $entity data to a file as an audit entry.

// Then log the action like this:
\Drupal::logger('os2form_audit')->notice('Entity with ID @id is written.', ['@id' => $entity->id()]);
\Drupal::logger('os2forms_audit')->notice('Entity with ID @id is written.', ['@id' => $entity->id()]);
}

}
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
<?php

namespace AuditLoggerInterface;
namespace Drupal\os2forms_audit\Plugin\AuditLogger;

use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\os2forms_audit\Plugin\AuditLoggerInterface;
use Drupal\Core\Plugin\PluginBase;

/**
* Writes entities to a file.
*
* @AuditLogger(
* @AuditLoggerProvider(
* id = "file",
* title = @Translation("File logger"),
* description = @Translation("Writes entities to a file.")
* )
*/
class FileEntityWriter extends PluginBase implements AuditLoggerInterface {
class File extends PluginBase implements AuditLoggerInterface {

/**
* {@inheritdoc}
*/
public function write(EntityInterface $entity) {
public function write(EntityInterface $entity): void {
// Code to write the entity to a file.
// This is just a placeholder and won't write the data.
file_put_contents('path_to_your_file.txt', serialize($entity));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
<?php

namespace Drupal\os2forms_audit\Plugin;
namespace Drupal\os2forms_audit\Plugin\AuditLogger;

use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Entity\EntityInterface;
use Psr\Log\LogLevel;
use Drupal\Core\Plugin\PluginBase;

/**
* Stores entities in the database.
*
* @AuditLogger(
* @AuditLoggerProvider(
* id = "loki",
* title = @Translation("Loki logger"),
* description = @Translation("Store entity data in Loki.")
* )
*/
class DatabaseEntityWriter extends PluginBase implements AuditLoggerInterface {
class Loki extends PluginBase implements AuditLoggerInterface {

/**
* {@inheritdoc}
*/
public function write(EntityInterface $entity) {
public function write(EntityInterface $entity): void {
// Code to write the $entity data to a file as an audit entry.

// Then log the action like this:
\Drupal::logger('os2form_audit')->notice('Entity with ID @id is written.', ['@id' => $entity->id()]);
\Drupal::logger('os2forms_audit')->notice('Entity with ID @id is written.', ['@id' => $entity->id()]);
}

}
36 changes: 36 additions & 0 deletions modules/os2forms_audit/src/Plugin/LoggerManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Drupal\os2forms_audit\Plugin;

use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;

/**
* Provides the Fruit plugin manager.
*
* @see \Drupal\os2forms_audit\Annotation\AuditLoggerProvider
* @see \Drupal\os2forms_audit\Plugin\AuditLogger\AuditLoggerInterface
* @see plugin_api
*/
class LoggerManager extends DefaultPluginManager {

/**
* Constructor for FruitManager objects.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {

parent::__construct(
'Plugin/AuditLogger',
$namespaces,
$module_handler,
'Drupal\os2forms_audit\Plugin\AuditLogger\AuditLoggerInterface',
'Drupal\os2forms_audit\Annotation\AuditLoggerProvider',
);


$this->alterInfo('os2forms_audit_logger_info');
$this->setCacheBackend($cache_backend, 'os2forms_audit_logger_plugins');
}

}
Loading

0 comments on commit 27b2579

Please sign in to comment.