forked from openeuropa/oe_authentication
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoe_authentication.module
113 lines (101 loc) · 3.22 KB
/
oe_authentication.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
<?php
/**
* @file
* OpenEuropa Authentication module.
*/
declare(strict_types = 1);
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
/**
* Implements hook_user_cancel_methods_alter().
*
* Remove the option to delete users while keeping the option to block them.
*/
function oe_authentication_user_cancel_methods_alter(&$methods) {
if (\Drupal::currentUser()->id() == 1) {
return;
}
$restricted_options = [
'user_cancel_reassign',
'user_cancel_delete',
];
foreach ($restricted_options as $restricted_option) {
unset($methods[$restricted_option]);
}
}
/**
* Implements hook_entity_base_field_info().
*
* Add custom EULogin fields.
*/
function oe_authentication_entity_base_field_info(EntityTypeInterface $entity_type) {
if ($entity_type->id() != 'user') {
return [];
}
$custom_fields = [
'7' => [
'machine_name' => 'field_oe_firstname',
'name' => t('First Name'),
'description' => t("User's first name."),
],
'8' => [
'machine_name' => 'field_oe_lastname',
'name' => t('Last Name'),
'description' => t("User's last name."),
],
'9' => [
'machine_name' => 'field_oe_department',
'name' => t('Department'),
'description' => t("User's department."),
],
'10' => [
'machine_name' => 'field_oe_organisation',
'name' => t('Organisation'),
'description' => t("User's organisation."),
],
];
$fields = [];
foreach ($custom_fields as $weight => $field) {
$fields[$field['machine_name']] = BaseFieldDefinition::create('string')
->setLabel($field['name'])
->setDescription($field['description'])
->setSettings([
'default_value' => '',
'max_length' => 255,
])
->setDisplayOptions('form', [
'weight' => $weight,
'region' => 'content',
]);
}
return $fields;
}
/**
* Implements hook_menu_local_actions_alter().
*
* Change the title for the Add Cas users link added by the Cas module.
*/
function oe_authentication_menu_local_actions_alter(&$local_actions) {
if (isset($local_actions['cas.bulk_add_cas_users'])) {
$local_actions['cas.bulk_add_cas_users']['title'] = t('Add EU Login user');
}
}
/**
* Implements hook_form_form_id_alter().
*
* Replace CAS from strings in the "bulk add users" form.
*/
function oe_authentication_form_bulk_add_cas_users_alter(&$form, &$form_state, $form_id) {
if (isset($form['intro']['#markup'])) {
$form['intro']['#markup'] = t('Use this form to pre-register one or more users, allowing them to log in using EU Login.');
}
if (isset($form['cas_usernames']['#title'])) {
$form['cas_usernames']['#title'] = t('EU Login username(s)');
}
if (isset($form['roles']['#description'])) {
$form['roles']['#description'] = t('Optionally assign one or more roles to each user. Note that if you have EU Login configured to assign roles during automatic registration on login, those will be ignored.');
}
if (isset($form['extra_info']['#markup'])) {
$form['extra_info']['#markup'] = t('Note that because EU Login attributes are only available when a user has logged in, any role or field assignment based on attributes will not be available using this form.');
}
}