Completed
Push — stable8.2 ( b4bbd4...3350d6 )
by
unknown
59:02
created

Configuration::saveConfiguration()   D

Complexity

Conditions 19
Paths 49

Size

Total Lines 34
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 380
Metric Value
dl 0
loc 34
ccs 0
cts 31
cp 0
rs 4.9141
cc 19
eloc 28
nc 49
nop 0
crap 380

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @author Alexander Bergolth <[email protected]>
4
 * @author Arthur Schiwon <[email protected]>
5
 * @author Jörn Friedrich Dreyer <[email protected]>
6
 * @author Lukas Reschke <[email protected]>
7
 * @author Morris Jobke <[email protected]>
8
 * @author Robin McCorkell <[email protected]>
9
 *
10
 * @copyright Copyright (c) 2015, ownCloud, Inc.
11
 * @license AGPL-3.0
12
 *
13
 * This code is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License, version 3,
15
 * as published by the Free Software Foundation.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License, version 3,
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
24
 *
25
 */
26
27
namespace OCA\user_ldap\lib;
28
29
class Configuration {
30
31
	protected $configPrefix = null;
32
	protected $configRead = false;
33
34
	//settings
35
	protected $config = array(
36
		'ldapHost' => null,
37
		'ldapPort' => null,
38
		'ldapBackupHost' => null,
39
		'ldapBackupPort' => null,
40
		'ldapBase' => null,
41
		'ldapBaseUsers' => null,
42
		'ldapBaseGroups' => null,
43
		'ldapAgentName' => null,
44
		'ldapAgentPassword' => null,
45
		'ldapTLS' => null,
46
		'turnOffCertCheck' => null,
47
		'ldapIgnoreNamingRules' => null,
48
		'ldapUserDisplayName' => null,
49
		'ldapUserDisplayName2' => null,
50
		'ldapUserFilterObjectclass' => null,
51
		'ldapUserFilterGroups' => null,
52
		'ldapUserFilter' => null,
53
		'ldapUserFilterMode' => null,
54
		'ldapGroupFilter' => null,
55
		'ldapGroupFilterMode' => null,
56
		'ldapGroupFilterObjectclass' => null,
57
		'ldapGroupFilterGroups' => null,
58
		'ldapGroupDisplayName' => null,
59
		'ldapGroupMemberAssocAttr' => null,
60
		'ldapLoginFilter' => null,
61
		'ldapLoginFilterMode' => null,
62
		'ldapLoginFilterEmail' => null,
63
		'ldapLoginFilterUsername' => null,
64
		'ldapLoginFilterAttributes' => null,
65
		'ldapQuotaAttribute' => null,
66
		'ldapQuotaDefault' => null,
67
		'ldapEmailAttribute' => null,
68
		'ldapCacheTTL' => null,
69
		'ldapUuidUserAttribute' => 'auto',
70
		'ldapUuidGroupAttribute' => 'auto',
71
		'ldapOverrideMainServer' => false,
72
		'ldapConfigurationActive' => false,
73
		'ldapAttributesForUserSearch' => null,
74
		'ldapAttributesForGroupSearch' => null,
75
		'ldapExperiencedAdmin' => false,
76
		'homeFolderNamingRule' => null,
77
		'hasPagedResultSupport' => false,
78
		'hasMemberOfFilterSupport' => false,
79
		'useMemberOfToDetectMembership' => true,
80
		'ldapExpertUsernameAttr' => null,
81
		'ldapExpertUUIDUserAttr' => null,
82
		'ldapExpertUUIDGroupAttr' => null,
83
		'lastJpegPhotoLookup' => null,
84
		'ldapNestedGroups' => false,
85
		'ldapPagingSize' => null,
86
	);
87
88
	/**
89
	 * @param string $configPrefix
90
	 * @param bool $autoRead
91
	 */
92 115
	public function __construct($configPrefix, $autoRead = true) {
93 115
		$this->configPrefix = $configPrefix;
94 115
		if($autoRead) {
95
			$this->readConfiguration();
96
		}
97 115
	}
98
99
	/**
100
	 * @param string $name
101
	 * @return mixed|void
102
	 */
103 18
	public function __get($name) {
104 18
		if(isset($this->config[$name])) {
105 17
			return $this->config[$name];
106
		}
107 3
	}
108
109
	/**
110
	 * @param string $name
111
	 * @param mixed $value
112
	 */
113 2
	public function __set($name, $value) {
114 2
		$this->setConfiguration(array($name => $value));
115 2
	}
116
117
	/**
118
	 * @return array
119
	 */
120
	public function getConfiguration() {
121
		return $this->config;
122
	}
123
124
	/**
125
	 * set LDAP configuration with values delivered by an array, not read
126
	 * from configuration. It does not save the configuration! To do so, you
127
	 * must call saveConfiguration afterwards.
128
	 * @param array $config array that holds the config parameters in an associated
129
	 * array
130
	 * @param array &$applied optional; array where the set fields will be given to
131
	 * @return false|null
132
	 */
133 17
	public function setConfiguration($config, &$applied = null) {
134 17
		if(!is_array($config)) {
135
			return false;
136
		}
137
138 17
		$cta = $this->getConfigTranslationArray();
139 17
		foreach($config as $inputKey => $val) {
140 17
			if(strpos($inputKey, '_') !== false && array_key_exists($inputKey, $cta)) {
141
				$key = $cta[$inputKey];
142 17
			} elseif(array_key_exists($inputKey, $this->config)) {
143 17
				$key = $inputKey;
144 17
			} else {
145 2
				continue;
146
			}
147
148 17
			$setMethod = 'setValue';
149
			switch($key) {
150 17
				case 'ldapAgentPassword':
151 2
					$setMethod = 'setRawValue';
152 2
					break;
153 16
				case 'homeFolderNamingRule':
154 3
					$trimmedVal = trim($val);
155 3
					if(!empty($trimmedVal) && strpos($val, 'attr:') === false) {
156 1
						$val = 'attr:'.$trimmedVal;
157 1
					}
158 3
					break;
159 13
				case 'ldapBase':
160 13
				case 'ldapBaseUsers':
161 13
				case 'ldapBaseGroups':
162 13
				case 'ldapAttributesForUserSearch':
163 13
				case 'ldapAttributesForGroupSearch':
164 13
				case 'ldapUserFilterObjectclass':
165 13
				case 'ldapUserFilterGroups':
166 13
				case 'ldapGroupFilterObjectclass':
167 13
				case 'ldapGroupFilterGroups':
168 13
				case 'ldapLoginFilterAttributes':
169 12
					$setMethod = 'setMultiLine';
170 12
					break;
171
			}
172 17
			$this->$setMethod($key, $val);
173 17
			if(is_array($applied)) {
174 2
				$applied[] = $inputKey;
175 2
			}
176 17
		}
177
178 17
	}
179
180
	public function readConfiguration() {
181
		if(!$this->configRead && !is_null($this->configPrefix)) {
182
			$cta = array_flip($this->getConfigTranslationArray());
183
			foreach($this->config as $key => $val) {
184
				if(!isset($cta[$key])) {
185
					//some are determined
186
					continue;
187
				}
188
				$dbKey = $cta[$key];
189
				switch($key) {
190
					case 'ldapBase':
191
					case 'ldapBaseUsers':
192
					case 'ldapBaseGroups':
193
					case 'ldapAttributesForUserSearch':
194
					case 'ldapAttributesForGroupSearch':
195
					case 'ldapUserFilterObjectclass':
196
					case 'ldapUserFilterGroups':
197
					case 'ldapGroupFilterObjectclass':
198
					case 'ldapGroupFilterGroups':
199
					case 'ldapLoginFilterAttributes':
200
						$readMethod = 'getMultiLine';
201
						break;
202
					case 'ldapIgnoreNamingRules':
203
						$readMethod = 'getSystemValue';
204
						$dbKey = $key;
205
						break;
206
					case 'ldapAgentPassword':
207
						$readMethod = 'getPwd';
208
						break;
209
					case 'ldapUserDisplayName2':
210
					case 'ldapGroupDisplayName':
211
						$readMethod = 'getLcValue';
212
						break;
213
					case 'ldapUserDisplayName':
214
					default:
215
						// user display name does not lower case because
216
						// we rely on an upper case N as indicator whether to
217
						// auto-detect it or not. FIXME
218
						$readMethod = 'getValue';
219
						break;
220
				}
221
				$this->config[$key] = $this->$readMethod($dbKey);
222
			}
223
			$this->configRead = true;
224
		}
225
	}
226
227
	/**
228
	 * saves the current Configuration in the database
229
	 */
230
	public function saveConfiguration() {
231
		$cta = array_flip($this->getConfigTranslationArray());
232
		foreach($this->config as $key => $value) {
233
			switch ($key) {
234
				case 'ldapAgentPassword':
235
					$value = base64_encode($value);
236
					break;
237
				case 'ldapBase':
238
				case 'ldapBaseUsers':
239
				case 'ldapBaseGroups':
240
				case 'ldapAttributesForUserSearch':
241
				case 'ldapAttributesForGroupSearch':
242
				case 'ldapUserFilterObjectclass':
243
				case 'ldapUserFilterGroups':
244
				case 'ldapGroupFilterObjectclass':
245
				case 'ldapGroupFilterGroups':
246
				case 'ldapLoginFilterAttributes':
247
					if(is_array($value)) {
248
						$value = implode("\n", $value);
249
					}
250
					break;
251
				//following options are not stored but detected, skip them
252
				case 'ldapIgnoreNamingRules':
253
				case 'hasPagedResultSupport':
254
				case 'ldapUuidUserAttribute':
255
				case 'ldapUuidGroupAttribute':
256
					continue 2;
257
			}
258
			if(is_null($value)) {
259
				$value = '';
260
			}
261
			$this->saveValue($cta[$key], $value);
262
		}
263
	}
264
265
	/**
266
	 * @param string $varName
267
	 * @return array|string
268
	 */
269
	protected function getMultiLine($varName) {
270
		$value = $this->getValue($varName);
271
		if(empty($value)) {
272
			$value = '';
273
		} else {
274
			$value = preg_split('/\r\n|\r|\n/', $value);
275
		}
276
277
		return $value;
278
	}
279
280
	/**
281
	 * Sets multi-line values as arrays
282
	 * 
283
	 * @param string $varName name of config-key
284
	 * @param array|string $value to set
285
	 * @param boolean $trim Trim value? (default: false)
0 ignored issues
show
Bug introduced by
There is no parameter named $trim. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
286
	 */
287 12
	protected function setMultiLine($varName, $value) {
288 12
		if(empty($value)) {
289 2
			$value = '';
290 12
		} else if (!is_array($value)) {
291
			$value = preg_split('/\r\n|\r|\n|;/', $value);
292
			if($value === false) {
293
				$value = '';
294
			}
295
		}
296
297 12
		if(!is_array($value)) {
298 2
			$finalValue = trim($value);
299 2
		} else {
300 10
			$finalValue = [];
301 10
			foreach($value as $key => $val) {
302 10
				if(is_string($val)) {
303 10
					$val = trim($val);
304 10
					if(!empty($val)) {
305
						//accidental line breaks are not wanted and can cause
306
						// odd behaviour. Thus, away with them.
307 10
						$finalValue[] = $val;
308 10
					}
309 10
				} else {
310
					$finalValue[] = $val;
311
				}
312 10
			}
313
		}
314
315 12
		$this->setRawValue($varName, $finalValue);
316 12
	}
317
318
	/**
319
	 * @param string $varName
320
	 * @return string
321
	 */
322
	protected function getPwd($varName) {
323
		return base64_decode($this->getValue($varName));
324
	}
325
326
	/**
327
	 * @param string $varName
328
	 * @return string
329
	 */
330
	protected function getLcValue($varName) {
331
		return mb_strtolower($this->getValue($varName), 'UTF-8');
332
	}
333
334
	/**
335
	 * @param string $varName
336
	 * @return string
337
	 */
338
	protected function getSystemValue($varName) {
339
		//FIXME: if another system value is added, softcode the default value
340
		return \OCP\Config::getSystemValue($varName, false);
341
	}
342
343
	/**
344
	 * @param string $varName
345
	 * @return string
346
	 */
347
	protected function getValue($varName) {
348
		static $defaults;
349
		if(is_null($defaults)) {
350
			$defaults = $this->getDefaults();
351
		}
352
		return \OCP\Config::getAppValue('user_ldap',
353
										$this->configPrefix.$varName,
354
										$defaults[$varName]);
355
	}
356
357
	/**
358
	 * Sets a scalar value.
359
	 * 
360
	 * @param string $varName name of config key
361
	 * @param mixed $value to set
362
	 */
363 6
	protected function setValue($varName, $value) {
364 6
		if(is_string($value)) {
365 6
			$value = trim($value);
366 6
		}
367 6
		$this->config[$varName] = $value;
368 6
	}
369
370
	/**
371
	 * Sets a scalar value without trimming.
372
	 *
373
	 * @param string $varName name of config key
374
	 * @param mixed $value to set
375
	 */
376 13
	protected function setRawValue($varName, $value) {
377 13
		$this->config[$varName] = $value;
378 13
	}
379
380
	/**
381
	 * @param string $varName
382
	 * @param string $value
383
	 * @return bool
384
	 */
385
	protected function saveValue($varName, $value) {
386
		return \OCP\Config::setAppValue('user_ldap',
387
										$this->configPrefix.$varName,
388
										$value);
389
	}
390
391
	/**
392
	 * @return array an associative array with the default values. Keys are correspond
393
	 * to config-value entries in the database table
394
	 */
395
	public function getDefaults() {
396
		return array(
397
			'ldap_host'                         => '',
398
			'ldap_port'                         => '',
399
			'ldap_backup_host'                  => '',
400
			'ldap_backup_port'                  => '',
401
			'ldap_override_main_server'         => '',
402
			'ldap_dn'                           => '',
403
			'ldap_agent_password'               => '',
404
			'ldap_base'                         => '',
405
			'ldap_base_users'                   => '',
406
			'ldap_base_groups'                  => '',
407
			'ldap_userlist_filter'              => '',
408
			'ldap_user_filter_mode'             => 0,
409
			'ldap_userfilter_objectclass'       => '',
410
			'ldap_userfilter_groups'            => '',
411
			'ldap_login_filter'                 => '',
412
			'ldap_login_filter_mode'            => 0,
413
			'ldap_loginfilter_email'            => 0,
414
			'ldap_loginfilter_username'         => 1,
415
			'ldap_loginfilter_attributes'       => '',
416
			'ldap_group_filter'                 => '',
417
			'ldap_group_filter_mode'            => 0,
418
			'ldap_groupfilter_objectclass'      => '',
419
			'ldap_groupfilter_groups'           => '',
420
			'ldap_display_name'                 => 'displayName',
421
			'ldap_user_display_name_2'			=> '',
422
			'ldap_group_display_name'           => 'cn',
423
			'ldap_tls'                          => 0,
424
			'ldap_quota_def'                    => '',
425
			'ldap_quota_attr'                   => '',
426
			'ldap_email_attr'                   => '',
427
			'ldap_group_member_assoc_attribute' => 'uniqueMember',
428
			'ldap_cache_ttl'                    => 600,
429
			'ldap_uuid_user_attribute'          => 'auto',
430
			'ldap_uuid_group_attribute'         => 'auto',
431
			'home_folder_naming_rule'           => '',
432
			'ldap_turn_off_cert_check'          => 0,
433
			'ldap_configuration_active'         => 0,
434
			'ldap_attributes_for_user_search'   => '',
435
			'ldap_attributes_for_group_search'  => '',
436
			'ldap_expert_username_attr'         => '',
437
			'ldap_expert_uuid_user_attr'        => '',
438
			'ldap_expert_uuid_group_attr'       => '',
439
			'has_memberof_filter_support'       => 0,
440
			'use_memberof_to_detect_membership' => 1,
441
			'last_jpegPhoto_lookup'             => 0,
442
			'ldap_nested_groups'                => 0,
443
			'ldap_paging_size'                  => 500,
444
			'ldap_experienced_admin'            => 0,
445
		);
446
	}
447
448
	/**
449
	 * @return array that maps internal variable names to database fields
450
	 */
451 17
	public function getConfigTranslationArray() {
452
		//TODO: merge them into one representation
453
		static $array = array(
454
			'ldap_host'                         => 'ldapHost',
455
			'ldap_port'                         => 'ldapPort',
456
			'ldap_backup_host'                  => 'ldapBackupHost',
457
			'ldap_backup_port'                  => 'ldapBackupPort',
458
			'ldap_override_main_server'         => 'ldapOverrideMainServer',
459
			'ldap_dn'                           => 'ldapAgentName',
460
			'ldap_agent_password'               => 'ldapAgentPassword',
461
			'ldap_base'                         => 'ldapBase',
462
			'ldap_base_users'                   => 'ldapBaseUsers',
463
			'ldap_base_groups'                  => 'ldapBaseGroups',
464
			'ldap_userfilter_objectclass'       => 'ldapUserFilterObjectclass',
465
			'ldap_userfilter_groups'            => 'ldapUserFilterGroups',
466
			'ldap_userlist_filter'              => 'ldapUserFilter',
467
			'ldap_user_filter_mode'             => 'ldapUserFilterMode',
468
			'ldap_login_filter'                 => 'ldapLoginFilter',
469
			'ldap_login_filter_mode'            => 'ldapLoginFilterMode',
470
			'ldap_loginfilter_email'            => 'ldapLoginFilterEmail',
471
			'ldap_loginfilter_username'         => 'ldapLoginFilterUsername',
472
			'ldap_loginfilter_attributes'       => 'ldapLoginFilterAttributes',
473
			'ldap_group_filter'                 => 'ldapGroupFilter',
474
			'ldap_group_filter_mode'            => 'ldapGroupFilterMode',
475
			'ldap_groupfilter_objectclass'      => 'ldapGroupFilterObjectclass',
476
			'ldap_groupfilter_groups'           => 'ldapGroupFilterGroups',
477
			'ldap_display_name'                 => 'ldapUserDisplayName',
478
			'ldap_user_display_name_2'			=> 'ldapUserDisplayName2',
479
			'ldap_group_display_name'           => 'ldapGroupDisplayName',
480
			'ldap_tls'                          => 'ldapTLS',
481
			'ldap_quota_def'                    => 'ldapQuotaDefault',
482
			'ldap_quota_attr'                   => 'ldapQuotaAttribute',
483
			'ldap_email_attr'                   => 'ldapEmailAttribute',
484
			'ldap_group_member_assoc_attribute' => 'ldapGroupMemberAssocAttr',
485
			'ldap_cache_ttl'                    => 'ldapCacheTTL',
486
			'home_folder_naming_rule'           => 'homeFolderNamingRule',
487
			'ldap_turn_off_cert_check'          => 'turnOffCertCheck',
488
			'ldap_configuration_active'         => 'ldapConfigurationActive',
489
			'ldap_attributes_for_user_search'   => 'ldapAttributesForUserSearch',
490
			'ldap_attributes_for_group_search'  => 'ldapAttributesForGroupSearch',
491
			'ldap_expert_username_attr'         => 'ldapExpertUsernameAttr',
492
			'ldap_expert_uuid_user_attr'        => 'ldapExpertUUIDUserAttr',
493
			'ldap_expert_uuid_group_attr'       => 'ldapExpertUUIDGroupAttr',
494
			'has_memberof_filter_support'       => 'hasMemberOfFilterSupport',
495
			'use_memberof_to_detect_membership' => 'useMemberOfToDetectMembership',
496
			'last_jpegPhoto_lookup'             => 'lastJpegPhotoLookup',
497
			'ldap_nested_groups'                => 'ldapNestedGroups',
498
			'ldap_paging_size'                  => 'ldapPagingSize',
499
			'ldap_experienced_admin'            => 'ldapExperiencedAdmin'
500 17
		);
501 17
		return $array;
502
	}
503
504
}
505