-
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
3 changed files
with
71 additions
and
0 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,7 @@ | ||
name: "OS2web Audit Users" | ||
description: "Logs CUD events for users" | ||
type: module | ||
core_version_requirement: ^8 || ^9 || ^10 | ||
dependencies: | ||
- os2web_audit:os2web_audit | ||
|
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,63 @@ | ||
<?php | ||
|
||
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 $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); | ||
} |