-
Notifications
You must be signed in to change notification settings - Fork 30
/
Config.php
297 lines (254 loc) · 9.03 KB
/
Config.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
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
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\LoginLdap;
use Exception;
use Piwik\Config as PiwikConfig;
use Piwik\Container\StaticContainer;
use Piwik\Plugins\LoginLdap\Ldap\Client;
use Piwik\Plugins\LoginLdap\Ldap\ServerInfo;
use Piwik\Log\LoggerInterface;
/**
* Utility class with methods to manage LoginLdap INI configuration.
*/
class Config
{
public static $defaultConfig = array(
'use_ldap_for_authentication' => 1,
'synchronize_users_after_login' => 1,
'enable_synchronize_access_from_ldap' => 0,
'new_user_default_sites_view_access' => '',
'user_email_suffix' => '',
'append_user_email_suffix_to_username' => 1,
'required_member_of' => '',
'required_member_of_field' => 'memberOf',
'ldap_user_filter' => '',
'ldap_user_id_field' => 'uid',
'ldap_mail_field' => 'mail',
'ldap_password_field' => 'userPassword',
'ldap_view_access_field' => 'view',
'ldap_admin_access_field' => 'admin',
'ldap_superuser_access_field' => 'superuser',
'strip_domain_from_web_auth' => 1,
'use_webserver_auth' => 0,
'user_access_attribute_server_specification_delimiter' => ';',
'user_access_attribute_server_separator' => ':',
'instance_name' => '',
'ldap_network_timeout' => Client::DEFAULT_TIMEOUT_SECS,
'enable_password_confirmation' => 0
);
// for backwards compatibility
public static $alternateOptionNames = array(
'user_email_suffix' => array('usernameSuffix'),
'required_member_of' => array('memberOf'),
'ldap_user_filter' => array('filter'),
'ldap_user_id_field' => array('userIdField'),
'ldap_mail_field' => array('mailField'),
'use_webserver_auth' => array('useKerberos'),
);
/**
* Returns an INI option value that is stored in the `[LoginLdap]` config section.
*
* If alternate option names exist for the option name, they will be used as fallbacks.
*
* @param $optionName
* @return mixed
*/
public static function getConfigOption($optionName)
{
return self::getConfigOptionFrom(PiwikConfig::getInstance()->LoginLdap, $optionName);
}
public static function getConfigOptionFrom($config, $optionName)
{
if (isset($config[$optionName])) {
return $config[$optionName];
} else if (isset(self::$alternateOptionNames[$optionName])) {
foreach (self::$alternateOptionNames[$optionName] as $alternateName) {
if (isset($config[$alternateName])) {
return $config[$alternateName];
}
}
return self::getDefaultConfigOptionValue($optionName);
} else {
return self::getDefaultConfigOptionValue($optionName);
}
}
public static function getDefaultConfigOptionValue($optionName)
{
return @self::$defaultConfig[$optionName];
}
public static function isAccessSynchronizationEnabled()
{
return self::getConfigOption('enable_synchronize_access_from_ldap');
}
public static function getDefaultSitesToGiveViewAccessTo()
{
return self::getConfigOption('new_user_default_sites_view_access');
}
public static function getRequiredMemberOf()
{
return self::getConfigOption('required_member_of');
}
public static function getRequiredMemberOfField()
{
return self::getConfigOption('required_member_of_field');
}
public static function getLdapUserFilter()
{
return self::getConfigOption('ldap_user_filter');
}
public static function getLdapUserIdField()
{
return self::getConfigOption('ldap_user_id_field');
}
public static function getLdapMailField()
{
return self::getConfigOption('ldap_mail_field');
}
public static function getLdapPasswordField()
{
return self::getConfigOption('ldap_password_field');
}
public static function getLdapUserEmailSuffix()
{
return self::getConfigOption('user_email_suffix');
}
public static function getLdapViewAccessField()
{
return self::getConfigOption('ldap_view_access_field');
}
public static function getLdapAdminAccessField()
{
return self::getConfigOption('ldap_admin_access_field');
}
public static function getSuperUserAccessField()
{
return self::getConfigOption('ldap_superuser_access_field');
}
public static function shouldUseWebServerAuthentication()
{
return self::getConfigOption('use_webserver_auth') == 1;
}
public static function getUserAccessAttributeServerSpecificationDelimiter()
{
return self::getConfigOption('user_access_attribute_server_specification_delimiter');
}
public static function getUserAccessAttributeServerSiteListSeparator()
{
return self::getConfigOption('user_access_attribute_server_separator');
}
public static function getDesignatedPiwikInstanceName()
{
return self::getConfigOption('instance_name');
}
public static function getUseLdapForAuthentication()
{
return self::getConfigOption('use_ldap_for_authentication') == 1;
}
public static function getShouldSynchronizeUsersAfterLogin()
{
return self::getConfigOption('synchronize_users_after_login') == 1;
}
public static function getLdapNetworkTimeout()
{
return self::getConfigOption('ldap_network_timeout');
}
public static function shouldAppendUserEmailSuffixToUsername()
{
return self::getConfigOption('append_user_email_suffix_to_username') == 1;
}
public static function getStripDomainFromWebAuth()
{
return self::getConfigOption('strip_domain_from_web_auth') == 1;
}
public static function getServerConfig($server)
{
$configName = 'LoginLdap_' . preg_replace('/[^a-zA-Z0-9_-]/', '', $server);
return PiwikConfig::getInstance()->__get($configName);
}
public static function getServerNameList()
{
return self::getConfigOption('servers');
}
/**
* Returns a list of {@link ServerInfo} instances describing the LDAP servers
* that should be connected to.
*
* @return ServerInfo[]
*/
public static function getConfiguredLdapServers()
{
$serverNameList = self::getServerNameList();
if (empty($serverNameList)) {
$server = ServerInfo::makeFromOldConfig();
$serverHost = $server->getServerHostname();
if (empty($serverHost)) {
return array();
} else {
return array($server);
}
} else {
if (is_string($serverNameList)) {
$serverNameList = explode(',', $serverNameList);
}
$servers = array();
foreach ($serverNameList as $name) {
try {
$servers[] = ServerInfo::makeConfigured($name);
} catch (Exception $ex) {
/** @var LoggerInterface $logger */
$logger = StaticContainer::get(LoggerInterface::class);
$logger->debug("LoginLdap\\Config::{func}: LDAP server info '{name}' is configured incorrectly: {message}", array(
'func' => __FUNCTION__,
'name' => $name,
'message' => $ex->getMessage(),
'exception' => $ex
));
}
}
return $servers;
}
}
public static function getPluginOptionValuesWithDefaults()
{
$result = self::$defaultConfig;
foreach ($result as $name => $ignore) {
$actualValue = Config::getConfigOption($name);
// special check for useKerberos which can be a string
if ($name == 'use_webserver_auth'
&& $actualValue === 'false'
) {
$actualValue = 0;
}
if (isset($actualValue)) {
$result[$name] = $actualValue;
}
}
return $result;
}
public static function savePluginOptions($config)
{
$loginLdap = PiwikConfig::getInstance()->LoginLdap;
foreach (self::$defaultConfig as $name => $value) {
if (isset($config[$name])) {
$loginLdap[$name] = $config[$name];
}
}
PiwikConfig::getInstance()->LoginLdap = $loginLdap;
PiwikConfig::getInstance()->forceSave();
}
public static function saveLdapServerConfigs($servers)
{
$serverNames = array();
foreach ($servers as $serverInfo) {
ServerInfo::saveServerConfig($serverInfo, $forceSave = false);
$serverNames[] = $serverInfo['name'];
}
PiwikConfig::getInstance()->LoginLdap['servers']= $serverNames;
PiwikConfig::getInstance()->forceSave();
}
}