-
Notifications
You must be signed in to change notification settings - Fork 1
/
stanford_samlauth.module
102 lines (93 loc) · 2.99 KB
/
stanford_samlauth.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
<?php
/**
* @file
* Primary module hooks for Stanford SAML Authentication module.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
/**
* Implements hook_entity_base_field_info().
*/
function stanford_samlauth_entity_base_field_info(EntityTypeInterface $entity_type) {
$fields = [];
if ($entity_type->id() == 'user') {
$fields['affiliation'] = BaseFieldDefinition::create('string')
->setLabel(t('Affiliation'))
->setDescription(t("User's affiliation as defined by SAML data."))
->setStorageRequired(TRUE)
->setCardinality(-1);
}
return $fields;
}
/**
* Implements hook_menu_local_tasks_alter().
*/
function stanford_samlauth_menu_local_tasks_alter(&$data, $route_name, RefinableCacheableDependencyInterface &$cacheability) {
if ($route_name == 'user.login' || $route_name == 'user.register') {
$config = \Drupal::config('stanford_samlauth.settings');
if ($config->get('hide_local_login')) {
// Hide local tabs that have the "Recover Password" tab if local login
// isn't allowed.
unset($data['tabs']);
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function stanford_samlauth_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$link_text = \Drupal::config('samlauth.authentication')
->get('login_menu_item_title');
$form['#attached']['library'][] = 'stanford_samlauth/samlauth';
$form['saml'] = [
'#type' => 'html_tag',
'#weight' => -99,
'#tag' => 'a',
'#value' => $link_text ?: t('Stanford Login'),
'#attributes' => [
'rel' => 'nofollow',
'href' => '/saml/login',
'class' => [
'samlauth-login',
'su-button',
'decanter-button',
],
],
];
$config = \Drupal::config('stanford_samlauth.settings');
$form['login_title'] = [
'#type' => 'html_tag',
'#tag' => 'h1',
'#value' => t('Login'),
'#weight' => -999,
];
$form['intro_text'] = [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => t('Welcome back! Log in to access your website'),
'#weight' => -998,
];
// If configured to disallow local login, hide the local login form parts.
if ($config->get('hide_local_login')) {
unset($form['name'], $form['pass'], $form['actions']);
return;
}
// Moves the original form elements into a collapsed group.
$form['manual'] = [
'#type' => 'details',
'#title' => $config->get('local_login_fieldset_label') ?: t('Drupal Login'),
'#open' => $config->get('local_login_fieldset_open') ?: FALSE,
];
$form['manual']['name'] = $form['name'];
$form['manual']['pass'] = $form['pass'];
$form['manual']['actions'] = $form['actions'];
$form['manual']['actions']['reset'] = [
'#type' => 'link',
'#url' => Url::fromRoute('user.pass'),
'#title' => t('Reset Password'),
];
unset($form['name'], $form['pass'], $form['actions']);
}