Blame view

sources/apps/user_ldap/lib/configuration.php 12.9 KB
31b7f2792   Kload   Upgrade to ownclo...
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
  <?php
  
  /**
   * ownCloud – LDAP Connection
   *
   * @author Arthur Schiwon
   * @copyright 2012, 2013 Arthur Schiwon blizzz@owncloud.com
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
   * License as published by the Free Software Foundation; either
   * version 3 of the License, or any later version.
   *
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
   *
   * You should have received a copy of the GNU Affero General Public
   * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
   *
   */
  
  namespace OCA\user_ldap\lib;
  
  class Configuration {
  
  	protected $configPrefix = null;
  	protected $configRead = false;
  
  	//settings
  	protected $config = array(
  		'ldapHost' => null,
  		'ldapPort' => null,
  		'ldapBackupHost' => null,
  		'ldapBackupPort' => null,
  		'ldapBase' => null,
  		'ldapBaseUsers' => null,
  		'ldapBaseGroups' => null,
  		'ldapAgentName' => null,
  		'ldapAgentPassword' => null,
  		'ldapTLS' => null,
  		'ldapNoCase' => null,
  		'turnOffCertCheck' => null,
  		'ldapIgnoreNamingRules' => null,
  		'ldapUserDisplayName' => null,
  		'ldapUserFilterObjectclass' => null,
  		'ldapUserFilterGroups' => null,
  		'ldapUserFilter' => null,
  		'ldapUserFilterMode' => null,
  		'ldapGroupFilter' => null,
  		'ldapGroupFilterMode' => null,
  		'ldapGroupFilterObjectclass' => null,
  		'ldapGroupFilterGroups' => null,
  		'ldapGroupDisplayName' => null,
  		'ldapGroupMemberAssocAttr' => null,
  		'ldapLoginFilter' => null,
  		'ldapLoginFilterMode' => null,
  		'ldapLoginFilterEmail' => null,
  		'ldapLoginFilterUsername' => null,
  		'ldapLoginFilterAttributes' => null,
  		'ldapQuotaAttribute' => null,
  		'ldapQuotaDefault' => null,
  		'ldapEmailAttribute' => null,
  		'ldapCacheTTL' => null,
  		'ldapUuidUserAttribute' => 'auto',
  		'ldapUuidGroupAttribute' => 'auto',
  		'ldapOverrideMainServer' => false,
  		'ldapConfigurationActive' => false,
  		'ldapAttributesForUserSearch' => null,
  		'ldapAttributesForGroupSearch' => null,
  		'homeFolderNamingRule' => null,
  		'hasPagedResultSupport' => false,
  		'hasMemberOfFilterSupport' => false,
  		'ldapExpertUsernameAttr' => null,
  		'ldapExpertUUIDUserAttr' => null,
  		'ldapExpertUUIDGroupAttr' => null,
  		'lastJpegPhotoLookup' => null,
6d9380f96   Cédric Dupont   Update sources OC...
79
80
  		'ldapNestedGroups' => false,
  		'ldapPagingSize' => null,
31b7f2792   Kload   Upgrade to ownclo...
81
  	);
6d9380f96   Cédric Dupont   Update sources OC...
82
83
84
85
86
  	/**
  	 * @param string $configPrefix
  	 * @param bool $autoRead
  	 */
  	public function __construct($configPrefix, $autoRead = true) {
31b7f2792   Kload   Upgrade to ownclo...
87
  		$this->configPrefix = $configPrefix;
6d9380f96   Cédric Dupont   Update sources OC...
88
  		if($autoRead) {
31b7f2792   Kload   Upgrade to ownclo...
89
90
91
  			$this->readConfiguration();
  		}
  	}
6d9380f96   Cédric Dupont   Update sources OC...
92
93
94
95
  	/**
  	 * @param string $name
  	 * @return mixed|void
  	 */
31b7f2792   Kload   Upgrade to ownclo...
96
97
98
99
100
  	public function __get($name) {
  		if(isset($this->config[$name])) {
  			return $this->config[$name];
  		}
  	}
6d9380f96   Cédric Dupont   Update sources OC...
101
102
103
104
  	/**
  	 * @param string $name
  	 * @param mixed $value
  	 */
31b7f2792   Kload   Upgrade to ownclo...
105
106
107
  	public function __set($name, $value) {
  		$this->setConfiguration(array($name => $value));
  	}
6d9380f96   Cédric Dupont   Update sources OC...
108
109
110
  	/**
  	 * @return array
  	 */
31b7f2792   Kload   Upgrade to ownclo...
111
112
113
114
115
  	public function getConfiguration() {
  		return $this->config;
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
116
  	 * set LDAP configuration with values delivered by an array, not read
31b7f2792   Kload   Upgrade to ownclo...
117
118
  	 * from configuration. It does not save the configuration! To do so, you
  	 * must call saveConfiguration afterwards.
6d9380f96   Cédric Dupont   Update sources OC...
119
  	 * @param array $config array that holds the config parameters in an associated
31b7f2792   Kload   Upgrade to ownclo...
120
  	 * array
6d9380f96   Cédric Dupont   Update sources OC...
121
122
  	 * @param array &$applied optional; array where the set fields will be given to
  	 * @return false|null
31b7f2792   Kload   Upgrade to ownclo...
123
124
125
126
127
128
129
  	 */
  	public function setConfiguration($config, &$applied = null) {
  		if(!is_array($config)) {
  			return false;
  		}
  
  		$cta = $this->getConfigTranslationArray();
6d9380f96   Cédric Dupont   Update sources OC...
130
131
132
133
134
  		foreach($config as $inputKey => $val) {
  			if(strpos($inputKey, '_') !== false && array_key_exists($inputKey, $cta)) {
  				$key = $cta[$inputKey];
  			} elseif(array_key_exists($inputKey, $this->config)) {
  				$key = $inputKey;
31b7f2792   Kload   Upgrade to ownclo...
135
136
137
138
139
140
141
142
143
144
  			} else {
  				continue;
  			}
  
  			$setMethod = 'setValue';
  			switch($key) {
  				case 'homeFolderNamingRule':
  					if(!empty($val) && strpos($val, 'attr:') === false) {
  						$val = 'attr:'.$val;
  					}
a293d369c   Kload   Update sources to...
145
  					break;
31b7f2792   Kload   Upgrade to ownclo...
146
147
148
149
150
151
152
153
154
155
156
  				case 'ldapBase':
  				case 'ldapBaseUsers':
  				case 'ldapBaseGroups':
  				case 'ldapAttributesForUserSearch':
  				case 'ldapAttributesForGroupSearch':
  				case 'ldapUserFilterObjectclass':
  				case 'ldapUserFilterGroups':
  				case 'ldapGroupFilterObjectclass':
  				case 'ldapGroupFilterGroups':
  				case 'ldapLoginFilterAttributes':
  					$setMethod = 'setMultiLine';
a293d369c   Kload   Update sources to...
157
158
159
160
  					break;
  			}
  			$this->$setMethod($key, $val);
  			if(is_array($applied)) {
6d9380f96   Cédric Dupont   Update sources OC...
161
  				$applied[] = $inputKey;
31b7f2792   Kload   Upgrade to ownclo...
162
163
164
165
166
167
168
169
170
171
172
173
174
  			}
  		}
  
  	}
  
  	public function readConfiguration() {
  		if(!$this->configRead && !is_null($this->configPrefix)) {
  			$cta = array_flip($this->getConfigTranslationArray());
  			foreach($this->config as $key => $val) {
  				if(!isset($cta[$key])) {
  					//some are determined
  					continue;
  				}
6d9380f96   Cédric Dupont   Update sources OC...
175
  				$dbKey = $cta[$key];
31b7f2792   Kload   Upgrade to ownclo...
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
  				switch($key) {
  					case 'ldapBase':
  					case 'ldapBaseUsers':
  					case 'ldapBaseGroups':
  					case 'ldapAttributesForUserSearch':
  					case 'ldapAttributesForGroupSearch':
  					case 'ldapUserFilterObjectclass':
  					case 'ldapUserFilterGroups':
  					case 'ldapGroupFilterObjectclass':
  					case 'ldapGroupFilterGroups':
  					case 'ldapLoginFilterAttributes':
  						$readMethod = 'getMultiLine';
  						break;
  					case 'ldapIgnoreNamingRules':
  						$readMethod = 'getSystemValue';
6d9380f96   Cédric Dupont   Update sources OC...
191
  						$dbKey = $key;
31b7f2792   Kload   Upgrade to ownclo...
192
193
194
195
196
197
198
199
200
201
202
203
  						break;
  					case 'ldapAgentPassword':
  						$readMethod = 'getPwd';
  						break;
  					case 'ldapUserDisplayName':
  					case 'ldapGroupDisplayName':
  						$readMethod = 'getLcValue';
  						break;
  					default:
  						$readMethod = 'getValue';
  						break;
  				}
6d9380f96   Cédric Dupont   Update sources OC...
204
  				$this->config[$key] = $this->$readMethod($dbKey);
31b7f2792   Kload   Upgrade to ownclo...
205
206
207
208
209
210
  			}
  			$this->configRead = true;
  		}
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
211
  	 * saves the current Configuration in the database
31b7f2792   Kload   Upgrade to ownclo...
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
  	 */
  	public function saveConfiguration() {
  		$cta = array_flip($this->getConfigTranslationArray());
  		foreach($this->config as $key => $value) {
  			switch ($key) {
  				case 'ldapAgentPassword':
  					$value = base64_encode($value);
  					break;
  				case 'ldapBase':
  				case 'ldapBaseUsers':
  				case 'ldapBaseGroups':
  				case 'ldapAttributesForUserSearch':
  				case 'ldapAttributesForGroupSearch':
  				case 'ldapUserFilterObjectclass':
  				case 'ldapUserFilterGroups':
  				case 'ldapGroupFilterObjectclass':
  				case 'ldapGroupFilterGroups':
  				case 'ldapLoginFilterAttributes':
  					if(is_array($value)) {
  						$value = implode("
  ", $value);
  					}
  					break;
  				//following options are not stored but detected, skip them
  				case 'ldapIgnoreNamingRules':
  				case 'hasPagedResultSupport':
  				case 'ldapUuidUserAttribute':
  				case 'ldapUuidGroupAttribute':
  					continue 2;
  			}
  			if(is_null($value)) {
  				$value = '';
  			}
  			$this->saveValue($cta[$key], $value);
  		}
  	}
6d9380f96   Cédric Dupont   Update sources OC...
248
249
250
251
252
253
  	/**
  	 * @param string $varName
  	 * @return array|string
  	 */
  	protected function getMultiLine($varName) {
  		$value = $this->getValue($varName);
31b7f2792   Kload   Upgrade to ownclo...
254
255
256
257
258
259
260
261
262
263
  		if(empty($value)) {
  			$value = '';
  		} else {
  			$value = preg_split('/\r
  |\r|
  /', $value);
  		}
  
  		return $value;
  	}
6d9380f96   Cédric Dupont   Update sources OC...
264
265
266
267
268
  	/**
  	 * @param string $varName
  	 * @param array|string $value
  	 */
  	protected function setMultiLine($varName, $value) {
31b7f2792   Kload   Upgrade to ownclo...
269
270
271
  		if(empty($value)) {
  			$value = '';
  		} else if (!is_array($value)) {
6d9380f96   Cédric Dupont   Update sources OC...
272
273
274
  			$value = preg_split('/\r
  |\r|
  |;/', $value);
31b7f2792   Kload   Upgrade to ownclo...
275
276
277
278
  			if($value === false) {
  				$value = '';
  			}
  		}
6d9380f96   Cédric Dupont   Update sources OC...
279
  		$this->setValue($varName, $value);
31b7f2792   Kload   Upgrade to ownclo...
280
  	}
6d9380f96   Cédric Dupont   Update sources OC...
281
282
283
284
285
286
  	/**
  	 * @param string $varName
  	 * @return string
  	 */
  	protected function getPwd($varName) {
  		return base64_decode($this->getValue($varName));
31b7f2792   Kload   Upgrade to ownclo...
287
  	}
6d9380f96   Cédric Dupont   Update sources OC...
288
289
290
291
292
293
  	/**
  	 * @param string $varName
  	 * @return string
  	 */
  	protected function getLcValue($varName) {
  		return mb_strtolower($this->getValue($varName), 'UTF-8');
31b7f2792   Kload   Upgrade to ownclo...
294
  	}
6d9380f96   Cédric Dupont   Update sources OC...
295
296
297
298
299
  	/**
  	 * @param string $varName
  	 * @return string
  	 */
  	protected function getSystemValue($varName) {
31b7f2792   Kload   Upgrade to ownclo...
300
  		//FIXME: if another system value is added, softcode the default value
6d9380f96   Cédric Dupont   Update sources OC...
301
  		return \OCP\Config::getSystemValue($varName, false);
31b7f2792   Kload   Upgrade to ownclo...
302
  	}
6d9380f96   Cédric Dupont   Update sources OC...
303
304
305
306
307
  	/**
  	 * @param string $varName
  	 * @return string
  	 */
  	protected function getValue($varName) {
31b7f2792   Kload   Upgrade to ownclo...
308
309
310
311
312
  		static $defaults;
  		if(is_null($defaults)) {
  			$defaults = $this->getDefaults();
  		}
  		return \OCP\Config::getAppValue('user_ldap',
6d9380f96   Cédric Dupont   Update sources OC...
313
314
  										$this->configPrefix.$varName,
  										$defaults[$varName]);
31b7f2792   Kload   Upgrade to ownclo...
315
  	}
6d9380f96   Cédric Dupont   Update sources OC...
316
317
318
319
320
321
  	/**
  	 * @param string $varName
  	 * @param mixed $value
  	 */
  	protected function setValue($varName, $value) {
  		$this->config[$varName] = $value;
31b7f2792   Kload   Upgrade to ownclo...
322
  	}
6d9380f96   Cédric Dupont   Update sources OC...
323
324
325
326
327
328
  	/**
  	 * @param string $varName
  	 * @param string $value
  	 * @return bool
  	 */
  	protected function saveValue($varName, $value) {
31b7f2792   Kload   Upgrade to ownclo...
329
  		return \OCP\Config::setAppValue('user_ldap',
6d9380f96   Cédric Dupont   Update sources OC...
330
  										$this->configPrefix.$varName,
31b7f2792   Kload   Upgrade to ownclo...
331
332
333
334
  										$value);
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
335
  	 * @return array an associative array with the default values. Keys are correspond
31b7f2792   Kload   Upgrade to ownclo...
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
  	 * to config-value entries in the database table
  	 */
  	public function getDefaults() {
  		return array(
  			'ldap_host'							=> '',
  			'ldap_port'							=> '',
  			'ldap_backup_host'					=> '',
  			'ldap_backup_port'					=> '',
  			'ldap_override_main_server'			=> '',
  			'ldap_dn'							=> '',
  			'ldap_agent_password'				=> '',
  			'ldap_base'							=> '',
  			'ldap_base_users'					=> '',
  			'ldap_base_groups'					=> '',
  			'ldap_userlist_filter'				=> '',
  			'ldap_user_filter_mode'				=> 0,
  			'ldap_userfilter_objectclass'		=> '',
  			'ldap_userfilter_groups'			=> '',
  			'ldap_login_filter'					=> '',
  			'ldap_login_filter_mode'			=> 0,
  			'ldap_loginfilter_email'			=> 0,
  			'ldap_loginfilter_username'			=> 1,
  			'ldap_loginfilter_attributes'		=> '',
  			'ldap_group_filter'					=> '',
  			'ldap_group_filter_mode'			=> 0,
  			'ldap_groupfilter_objectclass'		=> '',
  			'ldap_groupfilter_groups'			=> '',
  			'ldap_display_name'					=> 'displayName',
  			'ldap_group_display_name'			=> 'cn',
  			'ldap_tls'							=> 1,
  			'ldap_nocase'						=> 0,
  			'ldap_quota_def'					=> '',
  			'ldap_quota_attr'					=> '',
  			'ldap_email_attr'					=> '',
  			'ldap_group_member_assoc_attribute'	=> 'uniqueMember',
  			'ldap_cache_ttl'					=> 600,
  			'ldap_uuid_user_attribute'			=> 'auto',
  			'ldap_uuid_group_attribute'			=> 'auto',
  			'home_folder_naming_rule'			=> '',
  			'ldap_turn_off_cert_check'			=> 0,
  			'ldap_configuration_active'			=> 0,
  			'ldap_attributes_for_user_search'	=> '',
  			'ldap_attributes_for_group_search'	=> '',
  			'ldap_expert_username_attr'			=> '',
  			'ldap_expert_uuid_user_attr'		=> '',
  			'ldap_expert_uuid_group_attr'		=> '',
  			'has_memberof_filter_support'		=> 0,
  			'last_jpegPhoto_lookup'				=> 0,
6d9380f96   Cédric Dupont   Update sources OC...
384
385
  			'ldap_nested_groups'		        => 0,
  			'ldap_paging_size'					=> 500,
31b7f2792   Kload   Upgrade to ownclo...
386
387
388
389
  		);
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
390
  	 * @return array that maps internal variable names to database fields
31b7f2792   Kload   Upgrade to ownclo...
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
  	 */
  	public function getConfigTranslationArray() {
  		//TODO: merge them into one representation
  		static $array = array(
  			'ldap_host'							=> 'ldapHost',
  			'ldap_port'							=> 'ldapPort',
  			'ldap_backup_host'					=> 'ldapBackupHost',
  			'ldap_backup_port'					=> 'ldapBackupPort',
  			'ldap_override_main_server' 		=> 'ldapOverrideMainServer',
  			'ldap_dn'							=> 'ldapAgentName',
  			'ldap_agent_password'				=> 'ldapAgentPassword',
  			'ldap_base'							=> 'ldapBase',
  			'ldap_base_users'					=> 'ldapBaseUsers',
  			'ldap_base_groups'					=> 'ldapBaseGroups',
  			'ldap_userfilter_objectclass' 		=> 'ldapUserFilterObjectclass',
  			'ldap_userfilter_groups'	 		=> 'ldapUserFilterGroups',
  			'ldap_userlist_filter'				=> 'ldapUserFilter',
  			'ldap_user_filter_mode'				=> 'ldapUserFilterMode',
  			'ldap_login_filter'					=> 'ldapLoginFilter',
  			'ldap_login_filter_mode'			=> 'ldapLoginFilterMode',
  			'ldap_loginfilter_email'			=> 'ldapLoginFilterEmail',
  			'ldap_loginfilter_username'			=> 'ldapLoginFilterUsername',
  			'ldap_loginfilter_attributes'		=> 'ldapLoginFilterAttributes',
  			'ldap_group_filter'					=> 'ldapGroupFilter',
  			'ldap_group_filter_mode'			=> 'ldapGroupFilterMode',
  			'ldap_groupfilter_objectclass'		=> 'ldapGroupFilterObjectclass',
  			'ldap_groupfilter_groups'			=> 'ldapGroupFilterGroups',
  			'ldap_display_name'					=> 'ldapUserDisplayName',
  			'ldap_group_display_name'			=> 'ldapGroupDisplayName',
  			'ldap_tls'							=> 'ldapTLS',
  			'ldap_nocase'						=> 'ldapNoCase',
  			'ldap_quota_def'					=> 'ldapQuotaDefault',
  			'ldap_quota_attr'					=> 'ldapQuotaAttribute',
  			'ldap_email_attr'					=> 'ldapEmailAttribute',
  			'ldap_group_member_assoc_attribute'	=> 'ldapGroupMemberAssocAttr',
  			'ldap_cache_ttl'					=> 'ldapCacheTTL',
  			'home_folder_naming_rule' 			=> 'homeFolderNamingRule',
  			'ldap_turn_off_cert_check' 			=> 'turnOffCertCheck',
  			'ldap_configuration_active' 		=> 'ldapConfigurationActive',
  			'ldap_attributes_for_user_search' 	=> 'ldapAttributesForUserSearch',
  			'ldap_attributes_for_group_search'	=> 'ldapAttributesForGroupSearch',
  			'ldap_expert_username_attr' 		=> 'ldapExpertUsernameAttr',
  			'ldap_expert_uuid_user_attr' 		=> 'ldapExpertUUIDUserAttr',
  			'ldap_expert_uuid_group_attr'		=> 'ldapExpertUUIDGroupAttr',
  			'has_memberof_filter_support'		=> 'hasMemberOfFilterSupport',
  			'last_jpegPhoto_lookup'				=> 'lastJpegPhotoLookup',
6d9380f96   Cédric Dupont   Update sources OC...
437
438
  			'ldap_nested_groups'                => 'ldapNestedGroups',
  			'ldap_paging_size'                  => 'ldapPagingSize',
31b7f2792   Kload   Upgrade to ownclo...
439
440
441
  		);
  		return $array;
  	}
6d9380f96   Cédric Dupont   Update sources OC...
442
  }