You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

configuration.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <?php
  2. /**
  3. * @author Alexander Bergolth <leo@strike.wu.ac.at>
  4. * @author Arthur Schiwon <blizzz@owncloud.com>
  5. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  6. * @author Lukas Reschke <lukas@owncloud.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  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. namespace OCA\user_ldap\lib;
  27. class Configuration {
  28. protected $configPrefix = null;
  29. protected $configRead = false;
  30. //settings
  31. protected $config = array(
  32. 'ldapHost' => null,
  33. 'ldapPort' => null,
  34. 'ldapBackupHost' => null,
  35. 'ldapBackupPort' => null,
  36. 'ldapBase' => null,
  37. 'ldapBaseUsers' => null,
  38. 'ldapBaseGroups' => null,
  39. 'ldapAgentName' => null,
  40. 'ldapAgentPassword' => null,
  41. 'ldapTLS' => null,
  42. 'turnOffCertCheck' => null,
  43. 'ldapIgnoreNamingRules' => null,
  44. 'ldapUserDisplayName' => null,
  45. 'ldapUserFilterObjectclass' => null,
  46. 'ldapUserFilterGroups' => null,
  47. 'ldapUserFilter' => null,
  48. 'ldapUserFilterMode' => null,
  49. 'ldapGroupFilter' => null,
  50. 'ldapGroupFilterMode' => null,
  51. 'ldapGroupFilterObjectclass' => null,
  52. 'ldapGroupFilterGroups' => null,
  53. 'ldapGroupDisplayName' => null,
  54. 'ldapGroupMemberAssocAttr' => null,
  55. 'ldapLoginFilter' => null,
  56. 'ldapLoginFilterMode' => null,
  57. 'ldapLoginFilterEmail' => null,
  58. 'ldapLoginFilterUsername' => null,
  59. 'ldapLoginFilterAttributes' => null,
  60. 'ldapQuotaAttribute' => null,
  61. 'ldapQuotaDefault' => null,
  62. 'ldapEmailAttribute' => null,
  63. 'ldapCacheTTL' => null,
  64. 'ldapUuidUserAttribute' => 'auto',
  65. 'ldapUuidGroupAttribute' => 'auto',
  66. 'ldapOverrideMainServer' => false,
  67. 'ldapConfigurationActive' => false,
  68. 'ldapAttributesForUserSearch' => null,
  69. 'ldapAttributesForGroupSearch' => null,
  70. 'ldapExperiencedAdmin' => false,
  71. 'homeFolderNamingRule' => null,
  72. 'hasPagedResultSupport' => false,
  73. 'hasMemberOfFilterSupport' => false,
  74. 'useMemberOfToDetectMembership' => true,
  75. 'ldapExpertUsernameAttr' => null,
  76. 'ldapExpertUUIDUserAttr' => null,
  77. 'ldapExpertUUIDGroupAttr' => null,
  78. 'lastJpegPhotoLookup' => null,
  79. 'ldapNestedGroups' => false,
  80. 'ldapPagingSize' => null,
  81. );
  82. /**
  83. * @param string $configPrefix
  84. * @param bool $autoRead
  85. */
  86. public function __construct($configPrefix, $autoRead = true) {
  87. $this->configPrefix = $configPrefix;
  88. if($autoRead) {
  89. $this->readConfiguration();
  90. }
  91. }
  92. /**
  93. * @param string $name
  94. * @return mixed|void
  95. */
  96. public function __get($name) {
  97. if(isset($this->config[$name])) {
  98. return $this->config[$name];
  99. }
  100. }
  101. /**
  102. * @param string $name
  103. * @param mixed $value
  104. */
  105. public function __set($name, $value) {
  106. $this->setConfiguration(array($name => $value));
  107. }
  108. /**
  109. * @return array
  110. */
  111. public function getConfiguration() {
  112. return $this->config;
  113. }
  114. /**
  115. * set LDAP configuration with values delivered by an array, not read
  116. * from configuration. It does not save the configuration! To do so, you
  117. * must call saveConfiguration afterwards.
  118. * @param array $config array that holds the config parameters in an associated
  119. * array
  120. * @param array &$applied optional; array where the set fields will be given to
  121. * @return false|null
  122. */
  123. public function setConfiguration($config, &$applied = null) {
  124. if(!is_array($config)) {
  125. return false;
  126. }
  127. $cta = $this->getConfigTranslationArray();
  128. foreach($config as $inputKey => $val) {
  129. if(strpos($inputKey, '_') !== false && array_key_exists($inputKey, $cta)) {
  130. $key = $cta[$inputKey];
  131. } elseif(array_key_exists($inputKey, $this->config)) {
  132. $key = $inputKey;
  133. } else {
  134. continue;
  135. }
  136. $setMethod = 'setValue';
  137. switch($key) {
  138. case 'homeFolderNamingRule':
  139. if(!empty($val) && strpos($val, 'attr:') === false) {
  140. $val = 'attr:'.$val;
  141. }
  142. break;
  143. case 'ldapBase':
  144. case 'ldapBaseUsers':
  145. case 'ldapBaseGroups':
  146. case 'ldapAttributesForUserSearch':
  147. case 'ldapAttributesForGroupSearch':
  148. case 'ldapUserFilterObjectclass':
  149. case 'ldapUserFilterGroups':
  150. case 'ldapGroupFilterObjectclass':
  151. case 'ldapGroupFilterGroups':
  152. case 'ldapLoginFilterAttributes':
  153. $setMethod = 'setMultiLine';
  154. break;
  155. }
  156. $this->$setMethod($key, $val);
  157. if(is_array($applied)) {
  158. $applied[] = $inputKey;
  159. }
  160. }
  161. }
  162. public function readConfiguration() {
  163. if(!$this->configRead && !is_null($this->configPrefix)) {
  164. $cta = array_flip($this->getConfigTranslationArray());
  165. foreach($this->config as $key => $val) {
  166. if(!isset($cta[$key])) {
  167. //some are determined
  168. continue;
  169. }
  170. $dbKey = $cta[$key];
  171. switch($key) {
  172. case 'ldapBase':
  173. case 'ldapBaseUsers':
  174. case 'ldapBaseGroups':
  175. case 'ldapAttributesForUserSearch':
  176. case 'ldapAttributesForGroupSearch':
  177. case 'ldapUserFilterObjectclass':
  178. case 'ldapUserFilterGroups':
  179. case 'ldapGroupFilterObjectclass':
  180. case 'ldapGroupFilterGroups':
  181. case 'ldapLoginFilterAttributes':
  182. $readMethod = 'getMultiLine';
  183. break;
  184. case 'ldapIgnoreNamingRules':
  185. $readMethod = 'getSystemValue';
  186. $dbKey = $key;
  187. break;
  188. case 'ldapAgentPassword':
  189. $readMethod = 'getPwd';
  190. break;
  191. case 'ldapGroupDisplayName':
  192. $readMethod = 'getLcValue';
  193. break;
  194. case 'ldapUserDisplayName':
  195. default:
  196. // user display name does not lower case because
  197. // we rely on an upper case N as indicator whether to
  198. // auto-detect it or not. FIXME
  199. $readMethod = 'getValue';
  200. break;
  201. }
  202. $this->config[$key] = $this->$readMethod($dbKey);
  203. }
  204. $this->configRead = true;
  205. }
  206. }
  207. /**
  208. * saves the current Configuration in the database
  209. */
  210. public function saveConfiguration() {
  211. $cta = array_flip($this->getConfigTranslationArray());
  212. foreach($this->config as $key => $value) {
  213. switch ($key) {
  214. case 'ldapAgentPassword':
  215. $value = base64_encode($value);
  216. break;
  217. case 'ldapBase':
  218. case 'ldapBaseUsers':
  219. case 'ldapBaseGroups':
  220. case 'ldapAttributesForUserSearch':
  221. case 'ldapAttributesForGroupSearch':
  222. case 'ldapUserFilterObjectclass':
  223. case 'ldapUserFilterGroups':
  224. case 'ldapGroupFilterObjectclass':
  225. case 'ldapGroupFilterGroups':
  226. case 'ldapLoginFilterAttributes':
  227. if(is_array($value)) {
  228. $value = implode("\n", $value);
  229. }
  230. break;
  231. //following options are not stored but detected, skip them
  232. case 'ldapIgnoreNamingRules':
  233. case 'hasPagedResultSupport':
  234. case 'ldapUuidUserAttribute':
  235. case 'ldapUuidGroupAttribute':
  236. continue 2;
  237. }
  238. if(is_null($value)) {
  239. $value = '';
  240. }
  241. $this->saveValue($cta[$key], $value);
  242. }
  243. }
  244. /**
  245. * @param string $varName
  246. * @return array|string
  247. */
  248. protected function getMultiLine($varName) {
  249. $value = $this->getValue($varName);
  250. if(empty($value)) {
  251. $value = '';
  252. } else {
  253. $value = preg_split('/\r\n|\r|\n/', $value);
  254. }
  255. return $value;
  256. }
  257. /**
  258. * @param string $varName
  259. * @param array|string $value
  260. */
  261. protected function setMultiLine($varName, $value) {
  262. if(empty($value)) {
  263. $value = '';
  264. } else if (!is_array($value)) {
  265. $value = preg_split('/\r\n|\r|\n|;/', $value);
  266. if($value === false) {
  267. $value = '';
  268. }
  269. }
  270. $this->setValue($varName, $value);
  271. }
  272. /**
  273. * @param string $varName
  274. * @return string
  275. */
  276. protected function getPwd($varName) {
  277. return base64_decode($this->getValue($varName));
  278. }
  279. /**
  280. * @param string $varName
  281. * @return string
  282. */
  283. protected function getLcValue($varName) {
  284. return mb_strtolower($this->getValue($varName), 'UTF-8');
  285. }
  286. /**
  287. * @param string $varName
  288. * @return string
  289. */
  290. protected function getSystemValue($varName) {
  291. //FIXME: if another system value is added, softcode the default value
  292. return \OCP\Config::getSystemValue($varName, false);
  293. }
  294. /**
  295. * @param string $varName
  296. * @return string
  297. */
  298. protected function getValue($varName) {
  299. static $defaults;
  300. if(is_null($defaults)) {
  301. $defaults = $this->getDefaults();
  302. }
  303. return \OCP\Config::getAppValue('user_ldap',
  304. $this->configPrefix.$varName,
  305. $defaults[$varName]);
  306. }
  307. /**
  308. * @param string $varName
  309. * @param mixed $value
  310. */
  311. protected function setValue($varName, $value) {
  312. $this->config[$varName] = $value;
  313. }
  314. /**
  315. * @param string $varName
  316. * @param string $value
  317. * @return bool
  318. */
  319. protected function saveValue($varName, $value) {
  320. return \OCP\Config::setAppValue('user_ldap',
  321. $this->configPrefix.$varName,
  322. $value);
  323. }
  324. /**
  325. * @return array an associative array with the default values. Keys are correspond
  326. * to config-value entries in the database table
  327. */
  328. public function getDefaults() {
  329. return array(
  330. 'ldap_host' => '',
  331. 'ldap_port' => '',
  332. 'ldap_backup_host' => '',
  333. 'ldap_backup_port' => '',
  334. 'ldap_override_main_server' => '',
  335. 'ldap_dn' => '',
  336. 'ldap_agent_password' => '',
  337. 'ldap_base' => '',
  338. 'ldap_base_users' => '',
  339. 'ldap_base_groups' => '',
  340. 'ldap_userlist_filter' => '',
  341. 'ldap_user_filter_mode' => 0,
  342. 'ldap_userfilter_objectclass' => '',
  343. 'ldap_userfilter_groups' => '',
  344. 'ldap_login_filter' => '',
  345. 'ldap_login_filter_mode' => 0,
  346. 'ldap_loginfilter_email' => 0,
  347. 'ldap_loginfilter_username' => 1,
  348. 'ldap_loginfilter_attributes' => '',
  349. 'ldap_group_filter' => '',
  350. 'ldap_group_filter_mode' => 0,
  351. 'ldap_groupfilter_objectclass' => '',
  352. 'ldap_groupfilter_groups' => '',
  353. 'ldap_display_name' => 'displayName',
  354. 'ldap_group_display_name' => 'cn',
  355. 'ldap_tls' => 0,
  356. 'ldap_quota_def' => '',
  357. 'ldap_quota_attr' => '',
  358. 'ldap_email_attr' => '',
  359. 'ldap_group_member_assoc_attribute' => 'uniqueMember',
  360. 'ldap_cache_ttl' => 600,
  361. 'ldap_uuid_user_attribute' => 'auto',
  362. 'ldap_uuid_group_attribute' => 'auto',
  363. 'home_folder_naming_rule' => '',
  364. 'ldap_turn_off_cert_check' => 0,
  365. 'ldap_configuration_active' => 0,
  366. 'ldap_attributes_for_user_search' => '',
  367. 'ldap_attributes_for_group_search' => '',
  368. 'ldap_expert_username_attr' => '',
  369. 'ldap_expert_uuid_user_attr' => '',
  370. 'ldap_expert_uuid_group_attr' => '',
  371. 'has_memberof_filter_support' => 0,
  372. 'use_memberof_to_detect_membership' => 1,
  373. 'last_jpegPhoto_lookup' => 0,
  374. 'ldap_nested_groups' => 0,
  375. 'ldap_paging_size' => 500,
  376. 'ldap_experienced_admin' => 0,
  377. );
  378. }
  379. /**
  380. * @return array that maps internal variable names to database fields
  381. */
  382. public function getConfigTranslationArray() {
  383. //TODO: merge them into one representation
  384. static $array = array(
  385. 'ldap_host' => 'ldapHost',
  386. 'ldap_port' => 'ldapPort',
  387. 'ldap_backup_host' => 'ldapBackupHost',
  388. 'ldap_backup_port' => 'ldapBackupPort',
  389. 'ldap_override_main_server' => 'ldapOverrideMainServer',
  390. 'ldap_dn' => 'ldapAgentName',
  391. 'ldap_agent_password' => 'ldapAgentPassword',
  392. 'ldap_base' => 'ldapBase',
  393. 'ldap_base_users' => 'ldapBaseUsers',
  394. 'ldap_base_groups' => 'ldapBaseGroups',
  395. 'ldap_userfilter_objectclass' => 'ldapUserFilterObjectclass',
  396. 'ldap_userfilter_groups' => 'ldapUserFilterGroups',
  397. 'ldap_userlist_filter' => 'ldapUserFilter',
  398. 'ldap_user_filter_mode' => 'ldapUserFilterMode',
  399. 'ldap_login_filter' => 'ldapLoginFilter',
  400. 'ldap_login_filter_mode' => 'ldapLoginFilterMode',
  401. 'ldap_loginfilter_email' => 'ldapLoginFilterEmail',
  402. 'ldap_loginfilter_username' => 'ldapLoginFilterUsername',
  403. 'ldap_loginfilter_attributes' => 'ldapLoginFilterAttributes',
  404. 'ldap_group_filter' => 'ldapGroupFilter',
  405. 'ldap_group_filter_mode' => 'ldapGroupFilterMode',
  406. 'ldap_groupfilter_objectclass' => 'ldapGroupFilterObjectclass',
  407. 'ldap_groupfilter_groups' => 'ldapGroupFilterGroups',
  408. 'ldap_display_name' => 'ldapUserDisplayName',
  409. 'ldap_group_display_name' => 'ldapGroupDisplayName',
  410. 'ldap_tls' => 'ldapTLS',
  411. 'ldap_quota_def' => 'ldapQuotaDefault',
  412. 'ldap_quota_attr' => 'ldapQuotaAttribute',
  413. 'ldap_email_attr' => 'ldapEmailAttribute',
  414. 'ldap_group_member_assoc_attribute' => 'ldapGroupMemberAssocAttr',
  415. 'ldap_cache_ttl' => 'ldapCacheTTL',
  416. 'home_folder_naming_rule' => 'homeFolderNamingRule',
  417. 'ldap_turn_off_cert_check' => 'turnOffCertCheck',
  418. 'ldap_configuration_active' => 'ldapConfigurationActive',
  419. 'ldap_attributes_for_user_search' => 'ldapAttributesForUserSearch',
  420. 'ldap_attributes_for_group_search' => 'ldapAttributesForGroupSearch',
  421. 'ldap_expert_username_attr' => 'ldapExpertUsernameAttr',
  422. 'ldap_expert_uuid_user_attr' => 'ldapExpertUUIDUserAttr',
  423. 'ldap_expert_uuid_group_attr' => 'ldapExpertUUIDGroupAttr',
  424. 'has_memberof_filter_support' => 'hasMemberOfFilterSupport',
  425. 'use_memberof_to_detect_membership' => 'useMemberOfToDetectMembership',
  426. 'last_jpegPhoto_lookup' => 'lastJpegPhotoLookup',
  427. 'ldap_nested_groups' => 'ldapNestedGroups',
  428. 'ldap_paging_size' => 'ldapPagingSize',
  429. 'ldap_experienced_admin' => 'ldapExperiencedAdmin'
  430. );
  431. return $array;
  432. }
  433. }