Skip to content

Commit

Permalink
ITKDev: Added remote ip to log lines
Browse files Browse the repository at this point in the history
  • Loading branch information
cableman committed Dec 11, 2024
1 parent 40d9229 commit a9d9ee1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 38 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

- Add new module to track user accessing webform submissions.
- Added remote ip to all log lines.

## [0.1.1] - 2024-11-19

Expand Down
55 changes: 18 additions & 37 deletions modules/os2web_audit_entity/os2web_audit_entity.module
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\webform_revisions\Entity\WebformRevisionsSubmission;

const OS2WEB_AUDIT_ENTITY_TYPES = [
'file',
];

const OS2WEB_AUDIT_ENTITY_API_USER_ROLES = [
'os2forms_rest_api_user',
'os2forms_rest_api_user_write',
Expand All @@ -34,29 +30,19 @@ function os2web_audit_entity_entity_update(EntityInterface $entity): void {
*/
function os2web_audit_entity_entity_delete(EntityInterface $entity): void {
// Your code to handle the entity delete event.
$t = 1;
if ($entity->bundle() == 'webform') {
# Try to check for _cpr field for extra logging information.
$t = 1;
}

}

// Should it be more configurable in relation to types.
// Storage used instead of load, because this hook is trigger by UI and API
function os2web_audit_entity_entity_storage_load(array $entities, $entity_type): void {
// file (REQUEST_URI)

foreach ($entities as $entity) {
if (in_array($entity_type, OS2WEB_AUDIT_ENTITY_TYPES)) {
$account = \Drupal::currentUser();

$data = ['API' => FALSE];
if (os2web_audit_entity_is_api_user($account)) {
$data['API'] = 'true';
}

}
if ($entity_type == 'file') {
/** @var \Drupal\file\Entity\File $entity */
$fid = $entity->id();
$uri = $entity->getFileUri();
$msg = sprintf('File (%d) accessed. Uri "%s"', $fid, $uri);
os2web_audit_entity_log($msg);
}
}
}

/**
Expand All @@ -65,15 +51,7 @@ function os2web_audit_entity_entity_storage_load(array $entities, $entity_type):
* @param array<WebformRevisionsSubmission> $submissions
*/
function os2web_audit_entity_webform_post_load_data(array $submissions): void {

foreach ($submissions as $submission) {
$account = \Drupal::currentUser();

$apiUser = FALSE;
if (os2web_audit_entity_is_api_user($account)) {
$apiUser = TRUE;
}

# Try to check for _cpr field for extra logging information.
$personal = '';
$submissionData = $submission->getData();
Expand All @@ -86,7 +64,7 @@ function os2web_audit_entity_webform_post_load_data(array $submissions): void {
}

$msg = sprintf('Webform submission (%d) looked up. %sWebform id "%s".', $submission->id(), $personal, $submission->getWebform()->id());
os2web_audit_entity_log($msg, $submission->getWebform()->id(), ['userType' => $apiUser ? OS2WEB_AUDIT_ENTITY_USER_API : OS2WEB_AUDIT_ENTITY_USER_WEB]);
os2web_audit_entity_log($msg);
}
}

Expand All @@ -111,15 +89,18 @@ function os2web_audit_entity_is_api_user(AccountInterface $account): bool {
*
* @param string $message
* Message to log.
* @param string $mail
* Identify users by e-mail address.
* @param array<string, string> $metadata
* Optional metadata to set.
*/
function os2web_audit_entity_log(string $message, string $mail, array $metadata = []): void {
function os2web_audit_entity_log(string $message): void {
/** @var \Drupal\os2web_audit\Service\Logger $logger */
$logger = \Drupal::service('os2web_audit.logger');

$metadata['userId'] = $mail;
$account = \Drupal::currentUser();
$apiUser = FALSE;
if (os2web_audit_entity_is_api_user($account)) {
$apiUser = TRUE;
}

$metadata['userId'] = $account->getEmail();
$metadata['userType'] = $apiUser ? OS2WEB_AUDIT_ENTITY_USER_API : OS2WEB_AUDIT_ENTITY_USER_WEB;
$logger->info('Entity', $message, FALSE, $metadata);
}
2 changes: 1 addition & 1 deletion os2web_audit.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ services:

os2web_audit.logger:
class: Drupal\os2web_audit\Service\Logger
arguments: ['@plugin.manager.os2web_audit_logger', '@config.factory', '@current_user', '@logger.factory']
arguments: ['@plugin.manager.os2web_audit_logger', '@config.factory', '@current_user', '@logger.factory', '@request_stack']
9 changes: 9 additions & 0 deletions src/Service/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Drupal\os2web_audit\Form\PluginSettingsForm;
use Drupal\os2web_audit\Form\SettingsForm;
use Drupal\os2web_audit\Plugin\LoggerManager;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* Class Logger.
Expand All @@ -24,6 +25,7 @@ public function __construct(
private readonly ConfigFactoryInterface $configFactory,
private readonly AccountProxyInterface $currentUser,
private readonly LoggerChannelFactoryInterface $watchdog,
private readonly RequestStack $requestStack,
) {
}

Expand Down Expand Up @@ -86,6 +88,13 @@ private function log(string $type, int $timestamp, string $line, bool $logUser =
$metadata['userId'] = $this->currentUser->getEmail();
}

// Log request IP for information more information.
$request = $this->requestStack->getCurrentRequest();
$ip_address = $request->getClientIp();
if (!is_null($ip_address)) {
$line .= sprintf(' Remote ip: %s',$ip_address);
}

try {
/** @var \Drupal\os2web_audit\Plugin\AuditLogger\AuditLoggerInterface $logger */
$logger = $this->loggerManager->createInstance($plugin_id, $configuration ?? []);
Expand Down

0 comments on commit a9d9ee1

Please sign in to comment.