This repository has been archived by the owner on Jul 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdpc_user_management.module
313 lines (285 loc) · 10.1 KB
/
dpc_user_management.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
/**
* This file is used for hooks
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\dpc_user_management\Controller\EventsLogController;
use Drupal\dpc_user_management\Controller\MailchimpController;
use Drupal\user\Entity\User;
use Drupal\dpc_user_management\Traits\HandlesEmailDomainGroupMembership;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\Request;
/**
* Implements hook_help().
*
* @param $route_name
* @param RouteMatchInterface $route_match
*
* @return \Drupal\Core\StringTranslation\TranslatableMarkup
*/
function dpc_user_management_help($route_name, RouteMatchInterface $route_match)
{
switch ($route_name) {
case 'help.page.dpc_user_management':
return t('
<h2>User management module information.</h2>
');
}
}
/**
* Implemets hook_schema()
*/
function dpc_user_management_schema()
{
$schema = [];
$schema['dpc_group_events'] = [
'description' => 'Table for DPC Group membership events.',
'fields' => [
'id' => [
'description' => 'The primary key to store unique information.',
'type' => 'serial',
'not null' => true,
],
'uid' => [
'description' => 'The primary identifier for a User.',
'type' => 'int',
'unsigned' => true,
'not null' => true,
],
'gid' => [
'description' => 'The primary identifier for a Group.',
'type' => 'int',
'unsigned' => true,
'not null' => true,
],
'name' => [
'description' => 'The name of the event',
'type' => 'varchar',
'length' => 32,
'not null' => true,
'default' => '',
],
'status' => [
'description' => 'The status of the event',
'type' => 'varchar',
'length' => 32,
'not null' => false,
],
'created' => [
'type' => 'int',
'not null' => false,
'size' => 'normal',
],
'changed' => [
'type' => 'int',
'not null' => false,
'size' => 'normal',
],
],
'primary key' => [
'id',
],
];
$schema[\Drupal\dpc_user_management\Controller\DeletedGroupController::$table_name] =
\Drupal\dpc_user_management\Controller\DeletedGroupController::$schema;
$schema[\Drupal\dpc_user_management\Controller\UserImportController::$table_name] =
\Drupal\dpc_user_management\Controller\UserImportController::$schema;
return $schema;
}
/**
* Add records to the events table
*
* @param $name "event name"
* @param $gid "group id"
* @param $uid "user id"
*
* @throws Exception
*/
function dpc_log_event($name, $gid, $uid)
{
$connection = \Drupal::database();
$connection->insert('dpc_group_events')->fields([
'name' => $name,
'uid' => $uid,
'gid' => $gid,
'status' => 'pending',
'created' => strtotime('now')
])->execute();
}
/**
* Implements hook_cron()
*
* @throws Exception
*/
function dpc_user_management_cron(){
$mc_controller = new MailchimpController('cron');
$request = new Request();
$mc_controller->syncAudience($request);
/**
* Processes all Event Record on cron call
*/
$EventsLog = new EventsLogController();
$EventsLog->processUnprocessedRecords();
}
/**
* Implements hook_entity_prepare_form().
*
* @param EntityInterface $entity
* @param $operation
* @param FormStateInterface $form_state
*/
function dpc_user_management_user_prepare_form(
EntityInterface $entity,
$operation,
FormStateInterface $form_state
) {
if ($operation == 'default') {
/** @var \Drupal\Core\Field\FieldItemList $addresses */
try {
$addresses = $entity->get('field_email_addresses');
// Add the primary email to the list of email addresses if it is empty
if (empty($addresses->getValue())) {
$addresses->setValue([
[
'value' => $entity->mail->value,
'status' => 'verified',
'is_primary' => 1
]
]);
}
$mc_audience_status = $entity->get('field_mailchimp_audience_status');
if (empty($mc_audience_status->getValue())) {
$mc_audience_status->setValue('Not subscribed');
}
} catch (\Exception $exception) {
}
}
}
/**
* Implements hook_alter_form().
*
* @param $form
* @param FormStateInterface $form_state
* @param string $form_id
*/
function dpc_user_management_form_alter(&$form, FormStateInterface $form_state, $form_id)
{
if ($form_id === 'user_form') {
// remove the default email field
unset($form['account']['mail']);
// add asset library
$form['#attached']['library'][] = 'dpc_user_management/user_profile';
// add validation
$form['#validate'][] = 'dpc_user_management_email_validation';
$mc_config = \Drupal::config('dpc_mailchimp.settings');
if (!$mc_config->get('audience_id') || ! $mc_config->get('api_key')) {
unset($form['field_mailchimp_audience_status']);
}
if (isset($form['field_mailchimp_audience_status'])) {
$form['field_mailchimp_audience_status']['widget'][0]['value']['#attributes']['disabled'] = TRUE;
}
if (!\Drupal::currentUser()->hasPermission('administer group fields')) {
unset($form['jse_access']);
}
}
if ($form_id === 'user_register_form') {
// hide email addresses form on registration
unset($form['field_email_addresses']);
unset($form['field_mailchimp_audience_status']);
if (!\Drupal::currentUser()->hasPermission('administer group fields')) {
unset($form['jse_access']);
}
}
if ($form_id === 'mailchimp_audience_settings') {
$form['#attached']['library'][] = 'dpc_user_management/mailchimp';
}
}
/**
* Implements hook_entity_type_build()
*
* @param EntityTypeInterface[] $entity_types
*/
function dpc_user_management_entity_type_build(&$entity_types)
{
// User Entity class override
if (isset($entity_types['user'])) {
$entity_types['user']->setClass('Drupal\dpc_user_management\UserEntity');
}
if (isset($entity_types['group'])) {
$entity_types['group']->setClass('Drupal\dpc_user_management\GroupEntity');
}
}
/**
* @param $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
*/
function dpc_user_management_email_validation($form, &$form_state)
{
if ($form_state->hasValue('field_email_addresses')) {
$addresses = $form_state->getValue('field_email_addresses');
$has_primary_email_set = in_array(true, array_column($addresses, 'is_primary'));
foreach ($addresses as $address) {
// check if primary email is removed
if (is_array($address)) {
if ($address['is_primary'] && empty($address['value']) && !$has_primary_email_set) {
return $form_state->setErrorByName('field_email_addresses',
"Set another address as Primary before removing your Primary address");
}
if (empty($address['status']) && !empty($address['value'])) {
$email = $address['value'];
$db = \Drupal::database();
$address_exists = $db
->query("select * from {user__field_email_addresses} where field_email_addresses_value = '$email'")
->fetchAll();
if (!empty($address_exists)) {
return $form_state->setErrorByName('field_email_addresses', "The email $email is already in use.");
};
}
}
}
}
}
/**
* Implements hook_user_login
*
* @param \Drupal\user\Entity\User $account
*/
function dpc_user_management_user_login($account)
{
// On users first login, add them to a group if they have a whitelisted domain
if ($account->getLastAccessedTime() == 0) {
HandlesEmailDomainGroupMembership::addUserToGroups(User::load($account->id()), $account->getEmail());
}
// Set a warning for users with Invalidated emails
$addresses = $account->field_email_addresses->getValue();
if ($addresses) {
foreach ($addresses as $address) {
if ($address['status'] === 'unverified') {
$user_profile_url = Url::fromRoute('entity.user.edit_form', ['user' => $account->id()])->toString();
$message = "You have an unverified email address, this may affect your ability to view some content. Go to your <a href='$user_profile_url'>account settings</a> page to re-verify or remove the email address.";
$message = new TranslatableMarkup ('@message', ['@message' => Markup::create($message)]);
\Drupal::messenger()->addWarning($message);
}
}
}
}
/**
* Adds "is_primary" field to email addresses
* Implements hook_update_N
*/
function dpc_user_management_update_8001()
{
$database = \Drupal::database();
$schema = $database->schema();
$schema->addField('user__field_email_addresses', 'field_email_addresses_is_primary', [
'type' => 'int',
'size' => 'tiny',
'default' => 0,
'not null' => true,
]);
}