Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added user audit module #2

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

- First version of the module
- Added submodule to log user CUD events.

[Unreleased]: https://github.com/OS2web/os2web_audit/compare/develop...HEAD
6 changes: 6 additions & 0 deletions modules/os2web_audit_user/os2web_audit_user.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: "OS2web Audit logging users"
description: "Logs CUD events for users"
type: module
core_version_requirement: ^8 || ^9 || ^10
dependencies:
- os2web_audit:os2web_audit
68 changes: 68 additions & 0 deletions modules/os2web_audit_user/os2web_audit_user.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/**
* @file
* Audit log user CUD and login/logout events.
*/

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\UserInterface;

/**
* Implements hook_ENTITY_TYPE_insert().
*/
function os2web_audit_user_user_insert(EntityInterface $entity): void {
/** @var \Drupal\user\Entity\User $entity */
$msg = sprintf('User created with rolles: %s', implode(', ', $entity->getRoles()));
os2web_audit_user_log($msg, $entity->getEmail());
}

/**
* Implements hook_ENTITY_TYPE_update().
*/
function os2web_audit_user_user_update(EntityInterface $entity): void {
/** @var \Drupal\user\Entity\User $entity */
$msg = sprintf('User updated with rolles: %s', implode(', ', $entity->getRoles()));
os2web_audit_user_log($msg, $entity->getEmail());
}

/**
* Implements hook_ENTITY_TYPE_delete().
*/
function os2web_audit_user_user_delete(EntityInterface $entity): void {
/** @var \Drupal\user\Entity\User $entity */
os2web_audit_user_log('User deleted', $entity->getEmail());
}

/**
* Implements hook_user_login().
*/
function os2web_audit_user_user_login(UserInterface $account): void {
os2web_audit_user_log('User logged in', $account->getEmail());
}

/**
* Implements hook_user_logout().
*/
function os2web_audit_user_user_logout(AccountInterface $account): void {
os2web_audit_user_log('User logged out', $account->getEmail());
}

/**
* Simple logger wrapper.
*
* @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_user_log(string $message, string $mail, array $metadata = []): void {
/** @var \Drupal\os2web_audit\Service\Logger $logger */
$logger = \Drupal::service('os2web_audit.logger');

$metadata['userId'] = $mail;
$logger->info('User', $message, FALSE, $metadata);
}
Loading