-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail_verification.module
85 lines (78 loc) · 2.58 KB
/
email_verification.module
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
<?php
/**
* @file
* Contains email_verification.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Url;
use Drupal\Component\Utility\Html;
/**
* Implements hook_help().
*/
function email_verification_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the email_verification module.
case 'help.page.email_verification':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Verify User Emails before registration') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_form_alter().
*/
function email_verification_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// If email verify flag absent in url then redirect it to user verification
// form.
if ($form_id == 'user_register_form') {
// Only work for Anonymous users.
if (!\Drupal::currentUser()->isAnonymous()) {
return;
}
$form['#cache'] = ['max-age' => 0];
$verify = \Drupal::request()->query->get('verify');
if (empty($verify)) {
$targetUrl = Url::fromRoute('email_verification.user_verification_form')->toString();
$response = new RedirectResponse($targetUrl, 301);
$response->send();
return;
}
// Once md5 has present with actual email in url then compare it
// to make sure its same email address.
$email = \Drupal::request()->query->get('email');
$config = \Drupal::configFactory()->getEditable('email_verification.settings');
$salt = $config->get('user_email_verification_salt') ?? 'email';
$key = md5($salt . $email);
// If not same then redirect back to user verification screen.
if ($key == $verify) {
$form['account']['mail']['#default_value'] = $email;
// Set field to read only.
$form['account']['mail']['#attributes']['readonly'] = 'readonly';
}
else {
$targetUrl = Url::fromRoute('email_verification.user_verification_form')->toString();
$response = new RedirectResponse($targetUrl, 301);
$response->send();
exit;
}
}
}
/**
* Implements hook_mail().
*/
function email_verification_mail($key, &$message, $params) {
$options = [
'langcode' => $message['langcode'],
];
switch ($key) {
case 'email_verification':
$message['from'] = \Drupal::config('system.site')->get('mail');
$message['subject'] = t('@title', ['@title' => $params['title']], $options);
$message['body'][] = Html::escape($params['message']);
break;
}
}