This repository was archived by the owner on Jan 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagic_code.module
More file actions
176 lines (145 loc) · 4.63 KB
/
magic_code.module
File metadata and controls
176 lines (145 loc) · 4.63 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
* @file
* Module file for the magic_code module.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\magic_code\MagicCodeCollector;
use Drupal\magic_code\MagicCodeManagerInterface;
use Drupal\user\UserInterface;
/**
* Implements hook_cron().
*/
function magic_code_cron() {
/** @var MagicCodeCollector $collector */
$collector = \Drupal::service('magic_code.collector');
$logger = \Drupal::logger('magic_code');
$limit = 0;
// Delete a batch of expired codes.
if (!empty($expiredCodes = $collector->collectExpired($limit))) {
$count = count($expiredCodes);
$collector->deleteMultipleMagicCodes($expiredCodes);
$logger->notice('Deleted @count expired magic codes in cron.', [
'@count' => $count,
]);
}
}
/**
* Implements hook_entity_update().
*
* Revokes all magic codes for updated user.
*/
function magic_code_entity_update(EntityInterface $entity) {
if (!$entity instanceof UserInterface) {
return;
}
/** @var MagicCodeCollector $collector */
$collector = \Drupal::service('magic_code.collector');
$logger = \Drupal::logger('magic_code');
if (!empty($codes = $collector->collectForAccount($entity))) {
$count = count($codes);
foreach ($codes as $code) {
$code->revoke()->save();
}
$logger->notice('Revoked @count magic codes for user @id due to user entity update.', [
'@count' => $count,
'@id' => $entity->id(),
]);
}
}
/**
* Implements hook_mail_alter().
*/
function magic_code_mail_alter(&$message) {
if (strpos($message['id'], 'user_') === 0) {
_magic_code_replace_mail_tokens($message);
}
}
/**
* Replace custom tokens in mail message.
*
* This function can be used by other modules.
*/
function _magic_code_replace_mail_tokens(&$message) {
$variables = ['user' => $message['params']['account']];
// @see user_mail().
$languageManager = \Drupal::languageManager();
$langcode = $message['langcode'];
$language = $languageManager->getLanguage($langcode);
$originalLanguage = $languageManager->getConfigOverrideLanguage();
$languageManager->setConfigOverrideLanguage($language);
$tokenOptions = [
'langcode' => $langcode,
'key' => $message['key'],
'clear' => TRUE,
'body' => $message['body'],
'callback' => 'magic_code_user_mail_tokens',
];
// Replace tokens in the message body.
foreach($message['body'] as $key => $bodyStr) {
$message['body'][$key] = \Drupal::token()->replace($bodyStr, $variables, $tokenOptions);
}
$languageManager->setConfigOverrideLanguage($originalLanguage);
}
/**
* Custon token callback for user mail tokens.
*/
function magic_code_user_mail_tokens(&$replacements, $data, $options) {
$negotiator = \Drupal::service('consumer.negotiator');
$consumer = $negotiator->negotiateFromRequest();
$body = $options['body'];
if (isset($data['user']) && isset($consumer)) {
$operations = _magic_code_get_available_tokens();
/** @var MagicCodeManagerInterface $repository */
$manager = \Drupal::service('magic_code.manager');
$tokens = [
'magic-code-login_mail-only' => ['operation' => 'login'],
'magic-code-register_mail-only' => ['operation' => 'register'],
'magic-code-reset-password_mail-only' => ['operation' => 'reset-password'],
'magic-code-user-cancel_mail-only' => ['operation' => 'user-cancel'],
];
$tokens = [];
foreach ($operations as $operation) {
$key = sprintf('magic-code-%s_mail-only', $operation);
$tokens[$key] = ['operation' => $operation];
}
$options = [
'langcode' => $options['langcode'],
'key' => $options['key'],
];
// Register token replacements.
foreach ($tokens as $tokenName => $params) {
$operation = $params['operation'];
$fullToken = '[user:'. $tokenName . ']';
// Only replace token, if it actually exists, otherwise we
// would generate magic codes that are not used.
$match = FALSE;
foreach($body as $bodyStr) {
if (str_contains($bodyStr, $fullToken)) {
$match = TRUE;
break;
}
}
if (!$match) {
continue;
}
$email = array_key_exists('email', $params) ? $params['email'] : NULL;
$code = $manager->createNew($operation, $data['user'], $consumer, $email);
$replacements[$fullToken] = $code->get('value')->getString();
}
}
}
/**
* Get the available operations for which a
* token should be created.
*/
function _magic_code_get_available_tokens() {
$operations = [
'login',
'register',
'set-password',
'cancel-account',
];
\Drupal::moduleHandler()->alter('magic_code_user_mail_token_operations', $operations);
return $operations;
}