forked from harlor/ldap_profile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ldap_profile.module
66 lines (57 loc) · 2.12 KB
/
ldap_profile.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
<?php
use Drupal\Core\Form\FormStateInterface;
use Drupal\ldap_servers\Mapping;
/**
* Implements hook_ldap_user_attributes_alter()
*
* Add profile fields to ldap Drupal mappings.
*/
function ldap_profile_ldap_user_attributes_alter(array &$available_user_attrs, array &$params) {
$entityFieldManager = \Drupal::getContainer()->get('entity_field.manager');
$profile_fields = $entityFieldManager->getFieldStorageDefinitions('profile');
foreach ($profile_fields as $field_name => $field_instance) {
$field_id = sprintf("[profile.%s]", $field_name);
if (!array_key_exists($field_id, $available_user_attrs)) {
$available_user_attrs[$field_id] = new Mapping(
$field_id,
t('Profile field: @label', ['@label' => $field_instance->getLabel()]),
TRUE,
FALSE,
[],
'ldap_user',
'ldap_user'
);
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Disable fields in profile form that are synced with LDAP
*/
function ldap_profile_form_profile_profile_edit_form_alter(&$form, FormStateInterface $form_state) {
$config = \Drupal::config('ldap_user.settings');
$field_provider = \Drupal::service('ldap_user.field_provider');
$user_processor = \Drupal::service('ldap.drupal_user_processor');
if ($config->get('drupalAcctProvisionServer')) {
$server = \Drupal::entityTypeManager()->getStorage('ldap_server')->load($config->get('drupalAcctProvisionServer'));
$field_provider->loadAttributes($user_processor::PROVISION_TO_DRUPAL, $server);
}
$event = $user_processor::EVENT_SYNC_TO_DRUPAL_USER;
$mappings = $field_provider->getConfigurableAttributesSyncedOnEvent($event);
$fields = [];
foreach ($mappings as $key => $mapping) {
$field = explode('.', substr($key, 1, -1));
if (count($field) == 2) {
$entity_type = $field[0];
$field_name = $field[1];
$fields[$entity_type][] = $field_name;
}
}
if (array_key_exists('profile', $fields)) {
foreach ($fields['profile'] as $field_name) {
$form[$field_name]['#disabled'] = TRUE;
$form[$field_name]['#description'] .= ' ' . t('This field is filled by LDAP.');
}
}
}