forked from ubc/mediawiki-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomHooks.php
81 lines (63 loc) · 3 KB
/
CustomHooks.php
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
<?php
use Exception;
use IMSGlobal\Caliper\entities\agent\Person;
use CaliperExtension\caliper\CaliperSensor;
# If LDAP environment variables are defined, enable additional customization
if (getenv('LDAP_SERVER') || getenv('LDAP_BASE_DN') || getenv('LDAP_SEARCH_STRINGS') || getenv('LDAP_SEARCH_ATTRS')) {
// Remove the change password link from Preferences page.
// ref: https://stackoverflow.com/questions/16893589/prevent-users-from-changing-their-passwords-in-mediawiki
// note: many of the hooks mentioned in the stackoverflow post above have been deprecated
$wgHooks['GetPreferences'][] = 'RemovePasswordChangeLink';
function RemovePasswordChangeLink($user, &$preferences) {
unset($preferences['password']);
return true;
}
///////////////////////////////////////////////////////////////////////////////
$wgHooks['AuthChangeFormFields'][] = 'ChangeAuthFormFields';
function ChangeAuthFormFields($requests, $fieldInfo, &$formDescriptor, $action) {
global $wgCookiePrefix;
// Remove "local" domain option from login page
unset($formDescriptor['domain']['options']['local']);
// Remove username from cookies to avoid prefilling the field with wiki username.
// Users should authenticate with usernames in LDAP.
unset($_COOKIE[$wgCookiePrefix.'UserName']);
return true;
}
# if Caliper is setup, use a custom actor with puid from LDAP
if (getenv('CALIPER_HOST') && getenv('CALIPER_API_KEY')) {
$wgHooks['SetCaliperActorObject'][] = 'SetCaliperActor';
// This is the username MediaWiki will use.
function SetCaliperActor(&$actor, &$user) {
global $wgDBprefix;
if ($actor !== null) {
return true;
} else if (!$user->isLoggedIn() || !$user->getId()) {
return false;
}
$puid = null;
$userId = $user->getId();
$dbr = wfGetDB(DB_REPLICA);
$res = $dbr->select(
array('ucead' => $wgDBprefix.'user_cwl_extended_account_data'), // tables
array('ucead.puid'), // fields
array('ucead.user_id' => $userId, 'ucead.account_status' => 1), // where clause
__METHOD__, // caller function name
array('LIMIT' => 1) // options. fetch first row only
);
foreach ($res as $row) {
$puid = $row->puid;
}
if (!$puid) {
return false;
}
$caliperLDAPActorHomepage = rtrim(loadenv('CALIPER_LDAP_ACTOR_HOMEPAGE', ''), '/');
$actor = (new Person( $caliperLDAPActorHomepage . "/" . $puid ))
->setName($user->getName())
->setDateCreated(CaliperSensor::mediawikiTimestampToDateTime($user->getRegistration()));
return true;
}
}
} // end customization for LDAP authentication
#####################
## End LDAP customization
#####################