Skip to content

Commit

Permalink
ITKDev: Started on audit log module
Browse files Browse the repository at this point in the history
  • Loading branch information
cableman committed Apr 25, 2024
1 parent 440a4f0 commit 488a30a
Show file tree
Hide file tree
Showing 21 changed files with 1,071 additions and 5 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# OS2Form Audit Module
# OS2Web Audit Module

OS2Form Audit is a Drupal module that helps track changes and perform audit on
OS2Form events.
OS2Web Audit is a Drupal module that helps track changes and perform audit on
OS2Web events.

## Requirements
-
Expand All @@ -12,7 +12,7 @@ OS2Form events.
## Features
-
- Detailed audit log entries.
- Support for all OS2Form entity types.
- Support for all OS2Web entity types.
- Easily extendable for custom use case.

## Installation
Expand All @@ -28,7 +28,7 @@ extent page (`/admin/modules`).

## Configuration

Navigate to `/admin/config/os2form/audit` to configure the module.
Navigate to `/admin/config/audit` to configure the module.

Some additional (brief) information on Usage, Extending/Overriding, and
Maintainers could go here.
Expand Down
4 changes: 4 additions & 0 deletions os2web_audit.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name: OS2web Audit
type: module
description: 'OS2web Audit Module (log all events to external service)'
core_version_requirement: ^8 || ^9 || ^10
5 changes: 5 additions & 0 deletions os2web_audit.links.menu.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
os2web_audit.admin_settings:
title: 'OS2web Audit settings'
parent: system.admin_config_system
description: 'Settings for the OS2web Audit module'
route_name: os2web_audit.plugin_settings_local_tasks
5 changes: 5 additions & 0 deletions os2web_audit.links.task.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
os2web_audit.plugin_settings_tasks:
title: 'Dynamic tasks'
route_name: os2web_audit.plugin_settings_local_tasks
base_route: os2web_audit.plugin_settings_local_tasks
deriver: Drupal\os2web_audit\Plugin\Derivative\LocalTask
6 changes: 6 additions & 0 deletions os2web_audit.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

/**
* @file
* The module file required by all modules.
*/
8 changes: 8 additions & 0 deletions os2web_audit.routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
os2web_audit.plugin_settings_local_tasks:
path: '/admin/config/os2web_audit/settings/{type}'
defaults:
_controller: '\Drupal\os2web_audit\Controller\LocalTasksController::dynamicTasks'
_title: 'Dynamic tasks'
type: ''
requirements:
_permission: 'administer site'
14 changes: 14 additions & 0 deletions os2web_audit.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
services:
plugin.manager.os2web_audit_logger:
class: Drupal\os2web_audit\Plugin\LoggerManager
parent: default_plugin_manager

os2web_audit.logger:
class: Drupal\os2web_audit\Service\Logger
arguments: ['@plugin.manager.os2web_audit_logger', '@config.factory']

os2web_audit.commands:
class: Drupal\os2web_audit\Commands\AuditLogDrushCommands
arguments: ['@os2web_audit.logger']
tags:
- { name: drush.command }
44 changes: 44 additions & 0 deletions src/Annotation/AuditLoggerProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Drupal\os2web_audit\Annotation;

use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;

/**
* 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 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 Translation|string $description = '';

}
44 changes: 44 additions & 0 deletions src/Commands/AuditLogDrushCommands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Drupal\os2web_audit\Commands;

use Drupal\os2web_audit\Service\Logger;
use Drush\Commands\DrushCommands;

/**
* Simple command to send log message into audit log.
*/
class AuditLogDrushCommands extends DrushCommands {

/**
* Os2webAuditDrushCommands constructor.
*
* @param \Drupal\os2web_audit\Service\Logger $auditLogger
* Audit logger service.
*/
public function __construct(
protected readonly Logger $auditLogger,
) {
parent::__construct();
}

/**
* Log a test message to the os2web_audit logger.
*
* @param string $log_message
* Message to be logged.
*
* @command audit:log
* @usage audit:log 'This is a test message.'
* Logs 'This is a test message.' to the os2web_audit logger.
*
* @throws \Drupal\Component\Plugin\Exception\PluginException
*/
public function logMessage(string $log_message = ''): void {
if (empty($log_message)) {
throw new \Exception('Log message cannot be empty.');
}
$this->auditLogger->log('test', time(), $log_message, ['from' => 'drush']);
}

}
58 changes: 58 additions & 0 deletions src/Controller/LocalTasksController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Drupal\os2web_audit\Controller;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Form\FormBuilderInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Class to handle local taks tabs callbacks.
*/
class LocalTasksController extends ControllerBase {

/**
* Default constructor.
*
* @param \Drupal\Core\Form\FormBuilderInterface $formBuilder
* Form builder object.
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* CConfiguration factory.
*/
public function __construct(
FormBuilderInterface $formBuilder,
ConfigFactoryInterface $configFactory,
) {
$this->formBuilder = $formBuilder;
$this->configFactory = $configFactory;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container): LocalTasksController|static {
return new static(
$container->get('form_builder'),
$container->get('config.factory'),
);
}

/**
* Get dynamic tasks.
*
* @param string|null $type
* The type of form to retrieve. Defaults to NULL.
*
* @return array
* An array containing the form definition.
*/
public function dynamicTasks(string $type = NULL): array {
if (empty($type)) {
return $this->formBuilder->getForm('\Drupal\os2web_audit\Form\SettingsForm');
}

return $this->formBuilder->getForm('\Drupal\os2web_audit\Form\PluginSettingsForm', $type);
}

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

namespace Drupal\os2web_audit\Form;

/**
* @file
* Abstract class for PluginSettingsForm implementation.
*/

use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\os2web_datalookup\Form\PluginSettingsFormInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Class for PluginSettingsForm implementation.
*/
class PluginSettingsForm extends ConfigFormBase implements PluginSettingsFormInterface {

/**
* The manager to be used for instantiating plugins.
*
* @var \Drupal\Component\Plugin\PluginManagerInterface
*/
protected PluginManagerInterface $manager;

/**
* {@inheritdoc}
*/
public function __construct(
ConfigFactoryInterface $config_factory,
PluginManagerInterface $manager,
) {
parent::__construct($config_factory);
$this->manager = $manager;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container): static {
return new static(
$container->get('config.factory'),
$container->get('plugin.manager.os2web_audit_logger')
);
}

/**
* {@inheritdoc}
*/
public static function getConfigName(): string {
return 'os2web_audit.plugin_settings';
}

/**
* {@inheritdoc}
*/
protected function getEditableConfigNames(): array {
return [$this->getConfigName()];
}

/**
* {@inheritdoc}
*/
public function getFormId(): string {
return $this->getConfigName() . '_settings_form';
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
$plugin_id = $form_state->getBuildInfo()['args'][0];
$instance = $this->getPluginInstance($plugin_id);
$form = $instance->buildConfigurationForm($form, $form_state);

return parent::buildForm($form, $form_state);
}

/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state): void {
$plugin_id = $form_state->getBuildInfo()['args'][0];
$instance = $this->getPluginInstance($plugin_id);
$instance->validateConfigurationForm($form, $form_state);
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
$plugin_id = $form_state->getBuildInfo()['args'][0];
$instance = $this->getPluginInstance($plugin_id);
$instance->submitConfigurationForm($form, $form_state);

$config = $this->config($this->getConfigName());
$config->set($plugin_id, $instance->getConfiguration());
$config->save();

parent::submitForm($form, $form_state);
}

/**
* Returns plugin instance for a given plugin id.
*
* @param string $plugin_id
* The plugin_id for the plugin instance.
*
* @return object
* Plugin instance.
*
* @throws \Drupal\Component\Plugin\Exception\PluginException
*/
public function getPluginInstance(string $plugin_id): object {
$configuration = $this->config($this->getConfigName())->get($plugin_id);

return $this->manager->createInstance($plugin_id, $configuration ?? []);
}

}
Loading

0 comments on commit 488a30a

Please sign in to comment.