-
Notifications
You must be signed in to change notification settings - Fork 1
/
surfconext.module
458 lines (395 loc) · 14 KB
/
surfconext.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
<?php
/**
* @file
* SURFconext authentication module for Drupal.
*
* SURFconext Drupal module enables federated authentication on the SURFcontext
* service based on the SAML2.0 standard.
*/
/**
* Implements hook_menu().
*/
function surfconext_menu() {
$items = array();
$items['admin/config/people/surfconext'] = array(
'title' => 'SURFconext settings',
'description' => 'Control the various settings of SURFconext federated login.',
'page callback' => 'drupal_get_form',
'page arguments' => array('surfconext_settings_form'),
'access arguments' => array('administer surfconext'),
'type' => MENU_LOCAL_TASK | MENU_NORMAL_ITEM,
);
$items['user/login/surfconext'] = array(
'title' => 'Log in',
'access callback' => 'user_is_anonymous',
'page callback' => '_surfconext_login',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_theme().
*/
function surfconext_theme() {
return array(
'surfconext_login_link' => array(
'file' => 'surfconext.theme.inc',
'variables' => array(),
),
'surfconext_info' => array(
'file' => 'surfconext.theme.inc',
'variables' => array(),
),
);
}
/**
* Implements hook_mediamosa_ck_acl_alter().
*/
function surfconext_mediamosa_ck_acl_alter(&$acl) {
// Set realm.
// Get the SURFconext instance.
$surfconext = SurfConext::get();
if ($surfconext) {
// Get unique ID which we can use as realm.
$realm = $surfconext->getAttibuteUsedForRealm();
if (!empty($realm)) {
$acl['acl_realm'] = $realm;
}
}
}
/**
* Implements hook_mediamosa_sb_theme_user_url_alter().
*/
function surfconext_mediamosa_sb_theme_user_url_alter(&$user_link) {
$user_link = 'user/login/surfconext';
}
/**
* Implements hook_help().
*/
function surfconext_help($path, $arg) {
switch ($path) {
case 'admin/config/people/surfconext':
return '<p>' . t('You can configure SimpleSAML and SURFconext settings on this form. However, default settings should work fine. You can enable the SimpleSAML login at the bottom of the form. Be careful though, enabling the switch will.') . '</p>';
}
}
/**
* Implements hook_permission().
*/
function surfconext_permission() {
return array(
'administer surfconext' => array(
'title' => t('Administer SURFconext'),
'description' => t('Warning: Give to trusted roles only; this permission has security implications.'),
),
);
}
/**
* Implements hook_cron().
*/
function surfconext_cron() {
// Call our cron for surfconext.
SurfConext::cronSurfConext();
}
/**
* Implements hook_init().
*/
function surfconext_init() {
// Get the SURFconext instance.
$surfconext = SurfConext::get();
if ($surfconext) {
// Init SURFconext.
$surfconext->init();
}
}
/**
* Implements hook_user_logout().
*/
function surfconext_user_logout($account) {
// Get the SURFconext instance.
$surfconext = SurfConext::get();
if ($surfconext) {
// Init SURFconext.
$surfconext->drupalLogoff($account);
}
}
/**
* Implements settings for the module.
*
* @see surfconext_settings_form_validate()
*/
function surfconext_settings_form() {
$form = array();
$form['simplesaml'] = array(
'#type' => 'fieldset',
'#title' => 'SimpleSAML settings',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['simplesaml']['surfconext_simplesaml_basedir'] = array(
'#type' => 'textfield',
'#title' => t('Installation directory'),
'#default_value' => SurfConext::getSimpleSamlBasedir(),
'#description' => t("The base directory of simpleSAMLphp library. Absolute path with no trailing slash. Default: '/var/simplesamlphp'."),
'#required' => TRUE,
);
$form['simplesaml']['surfconext_authsource'] = array(
'#type' => 'textfield',
'#title' => t('Authentication source for this SP'),
'#default_value' => variable_get('surfconext_authsource', 'default-sp'),
'#description' => t("The name of the source to use from /var/simplesamlphp/config/authsources.php. Default: 'default-sp'."),
'#required' => TRUE,
);
$form['surfconext_attributes'] = array(
'#type' => 'fieldset',
'#title' => 'SURFconext attributes',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['surfconext_attributes']['surfconext_attribute_name_unique_id'] = array(
'#type' => 'textfield',
'#title' => t('SURFconext attribute: UID'),
'#default_value' => SurfConext::getAttributeNameUniqueID(),
'#description' => t("Unique id, used for Drupal uid. Default: @default", array('@default' => SurfConext::ATTRIBUTE_NAME_UNIQUE_ID)),
);
$form['surfconext_attributes']['surfconext_attribute_name_displayname'] = array(
'#type' => 'textfield',
'#title' => t('SURFconext attribute: DisplayName'),
'#default_value' => SurfConext::getAttributeNameDisplayName(),
'#description' => t("Display Name, used for Drupal profile. Default: @default", array('@default' => SurfConext::ATTRIBUTE_NAME_DISPLAYNAME)),
);
$form['surfconext_attributes']['surfconext_attribute_name_mail'] = array(
'#type' => 'textfield',
'#title' => t('SURFconext attribute: Mail'),
'#default_value' => SurfConext::getAttributeNameMail(),
'#description' => t("E-Mail address, stored in Drupal profile. Default: @default", array('@default' => SurfConext::ATTRIBUTE_NAME_MAIL)),
);
$form['surfconext'] = array(
'#type' => 'fieldset',
'#title' => 'SURFconext settings',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => t('SimpleSAMLphp needs an updated idp metadata file. You can fetch this file with !simplesaml_cron module, or you can use Drupal to update the file. In this case, fill in the following settings. For more information see the README.txt of this module.',
array('!simplesaml_cron' => l(t('SimpleSAMLphp cron'), 'http://simplesamlphp.org/docs/stable/simplesamlphp-automated_metadata'))),
);
$form['surfconext']['surfconext_metadata_idp_url'] = array(
'#type' => 'textfield',
'#title' => t('SURFconext IdP metadata URL'),
'#default_value' => SurfConext::getSurfConextMetadataIdpUrl(),
'#description' => t("Enter the URL of the IdP metadata provider. Default: 'https://engine.surfconext.nl/authentication/idp/metadata'."),
);
$form['surfconext']['surfconext_metadata_idp_filename'] = array(
'#type' => 'textfield',
'#title' => t('SURFconext IdP metadata file location'),
'#default_value' => SurfConext::getSurfConextMetadataIdpFilename(),
'#description' => t("This is the location where the file of the SURFconext IdP metadata is saved. Current real path location: '@realpath'. Default: 'private://saml20-idp-remote.php'. You might need to set the 'private file system path' when using private:// on the !link.",
array(
'@realpath' => drupal_realpath(SurfConext::getSurfConextMetadataIdpFilename()),
'!link' => l(t('file system configuration page'), 'admin/config/media/file-system'),
)
),
);
$form['surfconext']['surfconext_metadata_refresh_interval'] = array(
'#type' => 'textfield',
'#title' => t('SURFconext IdP refresh'),
'#default_value' => SurfConext::getSurfConextMetadataRefreshInterval(),
'#description' => t('The number of minutes before the SURFconext IdP metadata is refreshed. The SURFconext metadata expires every 48 hours, therefor its required to refresh the metadata at least every 48 hours. Default is 12 hours (720 minutes).'),
);
$form['surfconext']['surfconext_metadata_refresh'] = array(
'#type' => 'submit',
'#value' => t('Refresh IdP metadata file'),
);
$form['enable'] = array(
'#type' => 'fieldset',
'#title' => t('Master switch'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['enable']['surfconext_enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Enable authentication via SURFconext'),
'#default_value' => SurfConext::isEnabled(),
'#description' => t('Enabling the SURFconext login will not disable the normal Drupal login. !link for Drupal to prevent user to login onto Drupal.', array('!link' => l(t('Disable account creation'), 'admin/config/people/accounts'))),
);
$form['#validate'][] = 'surfconext_settings_form_validate';
$form['#submit'][] = 'surfconext_settings_form_submit';
return system_settings_form($form);
}
/**
* Validate settings form.
*
* @see surfconext_settings_form()
*/
function surfconext_settings_form_validate($form, &$form_state) {
if (isset($form_state['values']['surfconext_metadata_idp_filename'])) {
// Check if the IdP file is writable.
$surfconext_metadata_idp_filename = $form_state['values']['surfconext_metadata_idp_filename'];
// Is the location of the IdP file writeable?
if (!empty($surfconext_metadata_idp_filename) && !is_writable(dirname(drupal_realpath($surfconext_metadata_idp_filename)))) {
drupal_set_message(
t("Warning: the location of the IdP metadata file '@file' is not writable. If the file is located under private://, then check the file settings for the 'Private file system path' !here.",
array(
'@file' => drupal_realpath($surfconext_metadata_idp_filename),
'!here' => l(t('here'), 'admin/config/media/file-system'),
)
),
'error'
);
}
else {
// Remove the last update time so file is pulled again.
variable_del('surfconext_metadata_ipd_last_update');
}
}
}
/**
* Process the submit from the settings form.
*/
function surfconext_settings_form_submit($form, &$form_state) {
// Get values.
$values = $form_state['values'];
// Refresh the metadata?
if ($values['op'] == t('Refresh IdP metadata file')) {
try {
// Refresh the metadata.
SurfConext::refreshIdPMetadataFile();
// Success.
drupal_set_message(t('Metadata refreshed'));
}
catch (Exception $e) {
drupal_set_message(t('Metadata failed to refresh, check logs', 'error'));
}
// Prevent the form from being saved.
drupal_goto('admin/config/people/surfconext');
}
}
/**
* Implements hook_form_alter().
*/
function surfconext_form_alter(&$form, $form_state, $form_id) {
// Must be enabled.
if (!SurfConext::isEnabled()) {
return;
}
if (in_array($form_id, array('user_login_block', 'user_account_form'))) {
try {
$xml = simplexml_load_string('<?xml version="1.0" encoding="utf-8"?>' . $form['links']['#markup']);
$items = array();
foreach ($xml->xpath('ul/li') as $item) {
if (isset($item->a)) {
$title = (string) $item->a->attributes()->title;
$href = (string) $item->a->attributes()->href;
$text = (string) $item->a;
$items[] = l($text, $href, array('attributes' => array('title' => $title)));
}
}
// Add our item.
$items[] = l(t('SURFconext Log In'), 'user/login/surfconext', array('attributes' => array('title' => t('Login using the SURFconext federated login.'))));
// Use theme_item_list() again to rebuild.
$form['links'] = array('#markup' => theme('item_list', array('items' => $items)));
}
catch (Exception $e) {
surfconext::log('Unable to add federated SURFconext link to form.');
}
}
}
/**
* Implements hook_block_info().
*/
function surfconext_block_info() {
return array(
'surfconext_login_link' => array(
'info' => t('SURFconext login'),
'cache' => DRUPAL_NO_CACHE,
),
'surfconext_info' => array(
'info' => t('SURFconext info'),
'cache' => DRUPAL_NO_CACHE,
),
);
}
/**
* Implements hook_block_view().
*/
function surfconext_block_view($delta = '') {
if (!SurfConext::isEnabled()) {
// Exit without executing.
return;
}
$block = array();
switch ($delta) {
case 'surfconext_login_link':
$block = array(
'subject' => t('SURFconext login'),
'content' => array(
'#theme' => 'surfconext_login_link',
),
);
break;
case 'surfconext_info':
$block = array(
'subject' => t('SURFconext authentication info'),
'content' => array(
'#theme' => 'surfconext_info',
),
);
break;
}
return $block;
}
/**
* Show the SURFconext page.
*/
function _surfconext_login() {
// Get the SURFconext instance.
$surfconext = SurfConext::get();
if ($surfconext) {
// Show login page SURFconext.
$surfconext->login();
}
}
/**
* Implements hook_form_alter().
*/
function surfconext_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
global $user;
if ((!in_array('administrator', $user->roles) && ($user->uid != 1))) {
$form['account']['pass']['#access'] = FALSE;
$form['account']['mail']['#access'] = FALSE;
$form['account']['mail']['#access'] = FALSE;
$form['account']['current_pass_required_values']['#access'] = FALSE;
$form['account']['current_pass']['#access'] = FALSE;
$form['display_name']['#access'] = FALSE;
}
}
/**
* Implements hook_user_login().
*/
function surfconext_user_login(&$edit, $account) {
if ((in_array('administrator', $account->roles) || ($account->uid == 1))) {
return;
}
$surfconext = SurfConext::get();
$display_name = $surfconext->getAttribute($surfconext->getAttributeNameDisplayName());
$email = $surfconext->getAttribute($surfconext->getAttributeNameMail());
$uid = $surfconext->getAttribute($surfconext->getAttributeNameUniqueID());
SurfConext::debug("User %user uid:%uid (email:%email) logged in. displayname: %displayname.", array(
'%user' => $account->name,
'%uid' => $uid,
'%email' => $email,
'%displayname' => $display_name,
));
// Check if the DisplayName is changed since last login.
if (($account->display_name != $display_name) ||
// Also if email is empty the fill it with the simplesaml value.
((empty($account->mail) && !empty($email)))) {
$account->display_name['und'][0]['value'] = $display_name;
$edit = (array) $account;
$edit['display_name'] = $account->display_name;
// For now we only update empty email fields, as email changes has
// more consequences.
if (empty($account->mail)) {
$edit['mail'] = $email;
}
user_save($account, $edit);
}
}