-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
1,071 additions
and
5 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,4 @@ | ||
name: OS2web Audit | ||
type: module | ||
description: 'OS2web Audit Module (log all events to external service)' | ||
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 @@ | ||
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 |
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 @@ | ||
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 |
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,6 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* The module file required by all modules. | ||
*/ |
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,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' |
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,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 } |
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,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 = ''; | ||
|
||
} |
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,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']); | ||
} | ||
|
||
} |
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,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); | ||
} | ||
|
||
} |
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,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 ?? []); | ||
} | ||
|
||
} |
Oops, something went wrong.