-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmailhandler.module
More file actions
37 lines (31 loc) · 1.12 KB
/
mailhandler.module
File metadata and controls
37 lines (31 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
/**
* @file
* Hook implementations for Mailhandler module.
*/
use Drupal\Core\Entity\EntityInterface;
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function mailhandler_user_presave(EntityInterface $entity) {
/** @var \Drupal\user\UserInterface $entity */
if ($entity->hasField('mailhandler_gpg_key') && extension_loaded('gnupg')) {
/** @var \Drupal\user\UserInterface|null $original */
$original = $entity->original;
$gpg_key_field = $entity->get('mailhandler_gpg_key');
// Update the GPG keyring in case a new user has been created
// or the GPG key has been changed.
if (!$original || !$gpg_key_field->equals($original->get('mailhandler_gpg_key'))) {
// Initialize GnuPG resource.
$gpg = gnupg_init();
if ($original) {
// Delete the old key from GPG keyring.
gnupg_deletekey($gpg, $original->get('mailhandler_gpg_key')->fingerprint);
}
// Import the GPG public key.
$key_info = gnupg_import($gpg, $gpg_key_field->public_key);
// Update the fingerprint property.
$gpg_key_field->fingerprint = $key_info['fingerprint'];
}
}
}