forked from lucacracco/spid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spid.module
90 lines (78 loc) · 2.55 KB
/
spid.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
<?php
/**
* @file
* Contains spid.module.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\spid\SpidService;
use Drupal\user\UserInterface;
/**
* Implements hook_help().
*/
function spid_help($route_name) {
switch ($route_name) {
case 'spid.configure.metadata_idp':
$metadataFolder = \Drupal::config('spid.settings')
->get('idp_metadata_folder');
return '<p>' . t('Download IDPs metadata to %metadata_folder.', ['%metadata_folder' => $metadataFolder]) . '</p>';
}
}
/**
* Implements hook_user_presave().
*/
function spid_user_presave(UserInterface $account) {
// Hook into the user creation process from ExternalAuth::register() so that
// we don't need to save the new user a second time to add our SPID attribute
// values into the new user object. The way externalauth prefixes account
// names acts as a recursion stop, in case any called code (e.g. event) saves
// the account.
if ($account->isNew() && strpos($account->getAccountName(), 'spid_') === 0) {
/** @var \Drupal\spid\SpidServiceInterface $spid */
$spid = \Drupal::service('spid');
$spid->synchronizeUserAttributes($account, TRUE);
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function spid_form_user_form_alter(&$form, FormStateInterface $form_state, $form_id) {
/** @var \Drupal\externalauth\Authmap $authMap */
$authMap = \Drupal::service('externalauth.authmap');
/** @var \Drupal\Core\Session\AccountProxyInterface $user */
$user = \Drupal::currentUser();
/** @var \Drupal\Core\Config\ImmutableConfig $config */
$config = \Drupal::config('spid.settings');
if ($authMap->get($user->id(), 'spid')) {
$form['account']['mail']['#disabled'] = TRUE;
$form['account']['current_pass']['#access'] = FALSE;
$form['account']['pass']['#access'] = FALSE;
foreach (SpidService::getSpidAttributes() as $key => $attribute) {
if (($field = $config->get('user_' . $key)) !== 'none') {
$form[$field]['#disabled'] = TRUE;
}
}
}
}
/**
* Implements hook_theme().
*/
function spid_theme() {
return [
'spid_sp_access_button' => [
'template' => 'spid_sp_access_button',
'variables' => [
'img_folder' => NULL,
'size' => 'medium',
],
],
];
}
/**
* Implements hook_preprocess_spid_sp_access_button().
*/
function spid_preprocess_spid_sp_access_button(&$variables) {
$isTestenvEnabled = \Drupal::service('spid')
->isTestenvEnabled();
$variables['is_testenv_enabled'] = $isTestenvEnabled;
$variables['img_folder'] = \Drupal::moduleHandler()->getModule('spid')->getPath();
}