-
Notifications
You must be signed in to change notification settings - Fork 0
/
uiowa_auth.module
107 lines (92 loc) · 3.86 KB
/
uiowa_auth.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
<?php
/**
* @file
* Primary module hooks for uiowa_auth module.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\uiowa_auth\UserLoginBlockPreRender;
/**
* Implements hook_form_FORM_ID_alter().
*/
function uiowa_auth_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Remove access to form fields to avoid users entering HawkID credentials.
$form['name']['#access'] = FALSE;
$form['pass']['#access'] = FALSE;
$form['actions']['#access'] = FALSE;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function uiowa_auth_form_user_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Adjust access to fields on the user edit form that are synced via SAML. The
// samlauth module does something similar to this but only for the current
// user editing their own account. We take some additional steps also.
if ($form['form_id']['#id'] === 'edit-user-form') {
/** @var \Drupal\user\UserInterface $account */
$account = user_load_by_name($form['account']['name']['#default_value']);
/** @var \Drupal\externalauth\Authmap $authmap */
$authmap = \Drupal::service('externalauth.authmap');
if ($account) {
// Always disable these fields.
$form['account']['current_pass']['#access'] = FALSE;
$form['account']['pass']['#access'] = FALSE;
$form['path']['#access'] = FALSE;
$form['account']['mail']['#disabled'] = TRUE;
$form['account']['mail']['#description'] = t('Email addresses are synced automatically after every successful HawkID login.');
if ((int) $account->id() === 1) {
$form['#disabled'] = TRUE;
$form['account']['name']['#description'] = t('User 1 information is randomized and locked for security.');
$form['account']['mail']['#description'] = t('User 1 information is randomized and locked for security.');
}
else {
if ($authmap->getUid($account->getAccountName(), 'samlauth') === FALSE) {
_uiowa_auth_alter_name_field($form);
}
else {
$form['account']['name']['#description'] = t('Usernames are synced automatically after every successful HawkID login.');
$form['account']['name']['#disabled'] = TRUE;
}
}
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function uiowa_auth_form_user_register_form_alter(&$form, FormStateInterface $form_state, $form_id) {
_uiowa_auth_alter_name_field($form);
// The email field is required for non-admins but an issue exists for core.
// @see: https://www.drupal.org/project/drupal/issues/2992848.
$form['account']['mail']['#access'] = FALSE;
$form['account']['mail']['#value'] = uniqid('email_') . '@uiowa.edu';
// Hide the password field and generate a random one since it is never used.
$pass = \Drupal::service('password_generator')->generate();
$form['account']['pass']['#access'] = FALSE;
$form['account']['pass']['#value'] = [
'pass1' => $pass,
'pass2' => $pass,
];
// Hide the path field. The defaults are fine.
$form['path']['#access'] = FALSE;
// @todo https://github.com/uiowa/uiowa/issues/5032
$form['account']['notify']['#access'] = FALSE;
}
/**
* Implements hook_block_view_BASE_BLOCK_ID_alter().
*/
function uiowa_auth_block_view_user_login_block_alter(array &$build, BlockPluginInterface $block) {
$build['#pre_render'][] = [UserLoginBlockPreRender::class, 'preRender'];
}
/**
* Helper to alter name field that defines a user mapping.
*
* @param array $form
* The form that contains the name field to modify.
*/
function _uiowa_auth_alter_name_field(array &$form) {
$form['account']['name']['#description'] = t('Enter the HawkID of the user you wish to add. HawkIDs can be found using the <a href=":url">UIowa directory</a>.', [
':url' => Url::fromUri('https://iam.uiowa.edu/whitepages/search')->toString(),
]);
}