-
Notifications
You must be signed in to change notification settings - Fork 11
/
oe_authentication.install
118 lines (105 loc) · 4.03 KB
/
oe_authentication.install
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
<?php
/**
* @file
* OpenEuropa Authentication install file.
*/
declare(strict_types=1);
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\cas\Model\EmailAssignment;
/**
* Implements hook_install().
*/
function oe_authentication_install(): void {
if (\Drupal::isConfigSyncing() === FALSE) {
// Set default settings for CAS.
\Drupal::configFactory()->getEditable('cas.settings')
->set('server.hostname', 'ecas.ec.europa.eu')
->set('server.path', '/cas')
->set('server.version', '3.0')
->set('forced_login.enabled', TRUE)
->set('forced_login.paths.pages', '/user/login')
->set('logout.cas_logout', TRUE)
->set('logout.logout_destination', '<front>')
->set('user_accounts.prevent_normal_login', TRUE)
->set('user_accounts.auto_register', TRUE)
->set('user_accounts.restrict_password_management', TRUE)
->set('user_accounts.restrict_email_management', TRUE)
->set('user_accounts.auto_register_follow_registration_policy', TRUE)
// We need to add a default email domain so that we can trigger
// the appropriate event to handle emails.
->set('user_accounts.email_assignment_strategy', EmailAssignment::Standard->value)
->set('user_accounts.email_hostname', 'example.com')
->set('login_link_enabled', TRUE)
->set('login_link_label', 'EU Login')
->set('error_handling.login_failure_page', '/')
->set('error_handling.message_prevent_normal_login', 'This account must log in using <a href="[cas:login-url]">EU Login</a>.')
->set('error_handling.message_restrict_password_management', 'The requested account is associated with EU Login and its password cannot be managed from this website.')
->set('login_success_message', 'You have been logged in.')
->save();
\Drupal::messenger()->addMessage('Please be aware that oe_authentication makes changes to the following configuration: CAS settings.');
}
}
/**
* Add custom eu login fields.
*/
function oe_authentication_update_8001(): void {
$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."),
],
];
foreach ($custom_fields as $weight => $field) {
$storage_definition = BaseFieldDefinition::create('string')
->setLabel($field['name'])
->setDescription($field['description'])
->setSettings([
'default_value' => '',
'max_length' => 255,
])
->setDisplayOptions('form', [
'weight' => $weight,
'region' => 'content',
]);
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition($field['name'], 'user', 'oe_authentication', $storage_definition);
}
}
/**
* Install oe_authentication_user_fields module.
*/
function oe_authentication_update_8002(): void {
$entity_definition_manager = \Drupal::entityDefinitionUpdateManager();
// Change the base fields provider.
$field_names = [
'field_oe_firstname',
'field_oe_lastname',
'field_oe_department',
'field_oe_organisation',
];
foreach ($field_names as $field_name) {
$field_storage_definition = $entity_definition_manager->getFieldStorageDefinition($field_name, 'user');
$field_storage_definition->setProvider('oe_authentication_user_fields');
$entity_definition_manager->updateFieldStorageDefinition($field_storage_definition);
}
// Changing the base field provider requires container invalidation.
\Drupal::service('kernel')->invalidateContainer();
// Install the new sub-module.
\Drupal::service('module_installer')->install(['oe_authentication_user_fields']);
}