-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrowd_auth.module
108 lines (90 loc) · 3.16 KB
/
crowd_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
108
<?php
/**
* @file
* Contains crowd_auth.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\crowd_auth\Entity\Server;
use Drupal\user\Entity\User;
use Drupal\crowd_auth\CrowdUserException;
define('CROWD_AUTH_PROVIDER', 'crowd_auth');
/**
* Implements hook_help().
*/
function crowd_auth_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the crowd_auth module.
case 'help.page.crowd_auth':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Atlassian Crowd Authentication Provider') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_form_FORM_ID_alter(). for user_login_form.
*/
function crowd_auth_form_user_login_form_alter(&$form, $form_state, $form_id) {
_crowd_auth_form_user_login_alter($form, $form_state, 'user_login');
}
/**
* Implements hook_form_FORM_ID_alter(). for user_login_block.
*/
function crowd_auth_form_user_login_block_alter(&$form, &$form_state) {
_crowd_auth_form_user_login_alter($form, $form_state, 'user_login_block');
}
function _crowd_auth_form_user_login_alter(&$form, &$form_state, $form_id) {
if (count(\Drupal::service('crowd.servers')->getEnabledServers()) > 0) {
$key = array_search('::validateAuthentication', $form['#validate']);
$form['#validate'][$key] = 'crowd_auth_core_override_user_login_authenticate_validate';
array_splice($form['#validate'], $key + 1, 0, 'crowd_auth_user_login_authenticate_validate');
}
}
function crowd_auth_core_override_user_login_authenticate_validate($form, &$form_state) {
if (!empty($form_state->get('uid'))) {
return;
}
$original_form = $form_state->getFormObject();
$original_form->validateAuthentication($form, $form_state);
}
function crowd_auth_user_login_authenticate_validate($form, &$form_state) {
$servers = \Drupal::service('crowd.servers')->getEnabledServers();
foreach ($servers as $server) {
if ($server->connect()) {
try {
$result = $server->registerAndLogin($form_state->getValue('name'), $form_state->getValue('pass'));
if (is_object($result)) {
$form_state->set('uid', $result->id());
}
} catch (CrowdUserException $ex) {
return;
}
}
}
}
function crowd_auth_form_user_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
$account = $form_state->getFormObject()->getEntity();
$authMap = \Drupal::service('externalauth.authmap')->getAuthData($account->id(), CROWD_AUTH_PROVIDER);
if (!$authMap) {
return;
}
$data = unserialize($authMap['data']);
$server = \Drupal::service('crowd.servers')->getServerById($data['server_id']);
if (!$server) {
return;
}
$form['account']['pass']['#access']
= $form['account']['current_pass']['#access']
= $form['account']['mail']['#access']
= $form['account']['name']['#access']
= FALSE;
foreach ($form['account']['roles']['#options'] as $roleId => $role) {
if ($role = $server->getMappingForLocalGroup($roleId)) {
$form['account']['roles'][$roleId] = [
'#default_value' => ($account->hasRole($role->id())) ? $roleId : 0,
'#disabled' => 1
];
}
}
}