-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ITKDev: Added custom pluing manager for audit log extensions
- Loading branch information
Showing
16 changed files
with
296 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
43
modules/os2forms_audit/src/Annotation/AuditLoggerProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = ''; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 6 additions & 7 deletions
13
...es/os2forms_audit/src/Plugin/Database.php → ...audit/src/Plugin/AuditLogger/Database.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()]); | ||
} | ||
|
||
} |
11 changes: 5 additions & 6 deletions
11
modules/os2forms_audit/src/Plugin/File.php → ...rms_audit/src/Plugin/AuditLogger/File.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 6 additions & 7 deletions
13
modules/os2forms_audit/src/Plugin/Loki.php → ...rms_audit/src/Plugin/AuditLogger/Loki.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
|
||
} |
Oops, something went wrong.