-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtfa.module
More file actions
83 lines (72 loc) · 2.68 KB
/
tfa.module
File metadata and controls
83 lines (72 loc) · 2.68 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
<?php
/**
* @file
* Contains tfa.module.
*/
use Drupal\Component\Render\PlainTextOutput;
use Drupal\Core\Session\AccountInterface;
use Drupal\block\Entity\Block;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Component\Utility\Html;
/**
* Implements hook_help().
*/
function tfa_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the tfa module.
case 'help.page.tfa':
$output = '';
$output .= '<h3>' . \Drupal::translation()->translate('About') . '</h3>';
$output .= '<p>' . \Drupal::translation()->translate('Pluggable provider of second factor authentication for Drupal. For more information, see the online documentation for the <a href=":tfa">Two-factor Authentication</a> module.',
[
':tfa' => 'https://www.drupal.org/project/tfa',
]) . '</p>';
return $output;
}
}
/**
* Implements hook_block_access().
*
* Remove access to the core user_login_block so we can replace with the TFA
* login block.
*/
function tfa_block_access(Block $block, $operation, AccountInterface $account) {
if (\Drupal::config('tfa.settings')->get('enabled') && $block->getPluginId() === 'user_login_block') {
return AccessResult::forbidden();
}
}
/**
* Implements hook_mail().
*/
function tfa_mail($key, &$message, $params) {
if ($key === 'tfa_send_code') {
$options = array(
'langcode' => $params['langcode'],
);
$email = \Drupal::config('system.site')->get('mail');
$message['from'] = $email;
$message['subject'] = t('@title', array('@title' => $params['title']), $options);
$message['body'][] = Html::escape($params['message']);
}
else {
$token_service = \Drupal::token();
$language_manager = \Drupal::languageManager();
$variables = ['user' => $params['account']];
$language = $language_manager->getLanguage($params['account']->getPreferredLangcode());
$original_language = $language_manager->getConfigOverrideLanguage();
$language_manager->setConfigOverrideLanguage($language);
$tfa_config = \Drupal::config('tfa.settings');
$token_options = [
'langcode' => $message['langcode'],
'clear' => TRUE,
];
// Configuration mapping key matches the hook_mail() $key.
$subject = $tfa_config->get("mail.{$key}.subject");
$subject = $token_service->replace($subject, $variables, $token_options);
$message['subject'] = PlainTextOutput::renderFromHtml($subject);
$body = $tfa_config->get("mail.{$key}.body");
$message['body'][] = $token_service->replace($body, $variables, $token_options);
$language_manager->setConfigOverrideLanguage($original_language);
}
}