1
0

configuration.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. 'ldapNoCase' => null,
  43. 'turnOffCertCheck' => null,
  44. 'ldapIgnoreNamingRules' => null,
  45. 'ldapUserDisplayName' => null,
  46. 'ldapUserFilterObjectclass' => null,
  47. 'ldapUserFilterGroups' => null,
  48. 'ldapUserFilter' => null,
  49. 'ldapUserFilterMode' => null,
  50. 'ldapGroupFilter' => null,
  51. 'ldapGroupFilterMode' => null,
  52. 'ldapGroupFilterObjectclass' => null,
  53. 'ldapGroupFilterGroups' => null,
  54. 'ldapGroupDisplayName' => null,
  55. 'ldapGroupMemberAssocAttr' => null,
  56. 'ldapLoginFilter' => null,
  57. 'ldapLoginFilterMode' => null,
  58. 'ldapLoginFilterEmail' => null,
  59. 'ldapLoginFilterUsername' => null,
  60. 'ldapLoginFilterAttributes' => null,
  61. 'ldapQuotaAttribute' => null,
  62. 'ldapQuotaDefault' => null,
  63. 'ldapEmailAttribute' => null,
  64. 'ldapCacheTTL' => null,
  65. 'ldapUuidUserAttribute' => 'auto',
  66. 'ldapUuidGroupAttribute' => 'auto',
  67. 'ldapOverrideMainServer' => false,
  68. 'ldapConfigurationActive' => false,
  69. 'ldapAttributesForUserSearch' => null,
  70. 'ldapAttributesForGroupSearch' => null,
  71. 'ldapExperiencedAdmin' => false,
  72. 'homeFolderNamingRule' => null,
  73. 'hasPagedResultSupport' => false,
  74. 'hasMemberOfFilterSupport' => false,
  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 'ldapUserDisplayName':
  192. case 'ldapGroupDisplayName':
  193. $readMethod = 'getLcValue';
  194. break;
  195. default:
  196. $readMethod = 'getValue';
  197. break;
  198. }
  199. $this->config[$key] = $this->$readMethod($dbKey);
  200. }
  201. $this->configRead = true;
  202. }
  203. }
  204. /**
  205. * saves the current Configuration in the database
  206. */
  207. public function saveConfiguration() {
  208. $cta = array_flip($this->getConfigTranslationArray());
  209. foreach($this->config as $key => $value) {
  210. switch ($key) {
  211. case 'ldapAgentPassword':
  212. $value = base64_encode($value);
  213. break;
  214. case 'ldapBase':
  215. case 'ldapBaseUsers':
  216. case 'ldapBaseGroups':
  217. case 'ldapAttributesForUserSearch':
  218. case 'ldapAttributesForGroupSearch':
  219. case 'ldapUserFilterObjectclass':
  220. case 'ldapUserFilterGroups':
  221. case 'ldapGroupFilterObjectclass':
  222. case 'ldapGroupFilterGroups':
  223. case 'ldapLoginFilterAttributes':
  224. if(is_array($value)) {
  225. $value = implode("\n", $value);
  226. }
  227. break;
  228. //following options are not stored but detected, skip them
  229. case 'ldapIgnoreNamingRules':
  230. case 'hasPagedResultSupport':
  231. case 'ldapUuidUserAttribute':
  232. case 'ldapUuidGroupAttribute':
  233. continue 2;
  234. }
  235. if(is_null($value)) {
  236. $value = '';
  237. }
  238. $this->saveValue($cta[$key], $value);
  239. }
  240. }
  241. /**
  242. * @param string $varName
  243. * @return array|string
  244. */
  245. protected function getMultiLine($varName) {
  246. $value = $this->getValue($varName);
  247. if(empty($value)) {
  248. $value = '';
  249. } else {
  250. $value = preg_split('/\r\n|\r|\n/', $value);
  251. }
  252. return $value;
  253. }
  254. /**
  255. * @param string $varName
  256. * @param array|string $value
  257. */
  258. protected function setMultiLine($varName, $value) {
  259. if(empty($value)) {
  260. $value = '';
  261. } else if (!is_array($value)) {
  262. $value = preg_split('/\r\n|\r|\n|;/', $value);
  263. if($value === false) {
  264. $value = '';
  265. }
  266. }
  267. $this->setValue($varName, $value);
  268. }
  269. /**
  270. * @param string $varName
  271. * @return string
  272. */
  273. protected function getPwd($varName) {
  274. return base64_decode($this->getValue($varName));
  275. }
  276. /**
  277. * @param string $varName
  278. * @return string
  279. */
  280. protected function getLcValue($varName) {
  281. return mb_strtolower($this->getValue($varName), 'UTF-8');
  282. }
  283. /**
  284. * @param string $varName
  285. * @return string
  286. */
  287. protected function getSystemValue($varName) {
  288. //FIXME: if another system value is added, softcode the default value
  289. return \OCP\Config::getSystemValue($varName, false);
  290. }
  291. /**
  292. * @param string $varName
  293. * @return string
  294. */
  295. protected function getValue($varName) {
  296. static $defaults;
  297. if(is_null($defaults)) {
  298. $defaults = $this->getDefaults();
  299. }
  300. return \OCP\Config::getAppValue('user_ldap',
  301. $this->configPrefix.$varName,
  302. $defaults[$varName]);
  303. }
  304. /**
  305. * @param string $varName
  306. * @param mixed $value
  307. */
  308. protected function setValue($varName, $value) {
  309. $this->config[$varName] = $value;
  310. }
  311. /**
  312. * @param string $varName
  313. * @param string $value
  314. * @return bool
  315. */
  316. protected function saveValue($varName, $value) {
  317. return \OCP\Config::setAppValue('user_ldap',
  318. $this->configPrefix.$varName,
  319. $value);
  320. }
  321. /**
  322. * @return array an associative array with the default values. Keys are correspond
  323. * to config-value entries in the database table
  324. */
  325. public function getDefaults() {
  326. return array(
  327. 'ldap_host' => '',
  328. 'ldap_port' => '',
  329. 'ldap_backup_host' => '',
  330. 'ldap_backup_port' => '',
  331. 'ldap_override_main_server' => '',
  332. 'ldap_dn' => '',
  333. 'ldap_agent_password' => '',
  334. 'ldap_base' => '',
  335. 'ldap_base_users' => '',
  336. 'ldap_base_groups' => '',
  337. 'ldap_userlist_filter' => '',
  338. 'ldap_user_filter_mode' => 0,
  339. 'ldap_userfilter_objectclass' => '',
  340. 'ldap_userfilter_groups' => '',
  341. 'ldap_login_filter' => '',
  342. 'ldap_login_filter_mode' => 0,
  343. 'ldap_loginfilter_email' => 0,
  344. 'ldap_loginfilter_username' => 1,
  345. 'ldap_loginfilter_attributes' => '',
  346. 'ldap_group_filter' => '',
  347. 'ldap_group_filter_mode' => 0,
  348. 'ldap_groupfilter_objectclass' => '',
  349. 'ldap_groupfilter_groups' => '',
  350. 'ldap_display_name' => 'displayName',
  351. 'ldap_group_display_name' => 'cn',
  352. 'ldap_tls' => 1,
  353. 'ldap_nocase' => 0,
  354. 'ldap_quota_def' => '',
  355. 'ldap_quota_attr' => '',
  356. 'ldap_email_attr' => '',
  357. 'ldap_group_member_assoc_attribute' => 'uniqueMember',
  358. 'ldap_cache_ttl' => 600,
  359. 'ldap_uuid_user_attribute' => 'auto',
  360. 'ldap_uuid_group_attribute' => 'auto',
  361. 'home_folder_naming_rule' => '',
  362. 'ldap_turn_off_cert_check' => 0,
  363. 'ldap_configuration_active' => 0,
  364. 'ldap_attributes_for_user_search' => '',
  365. 'ldap_attributes_for_group_search' => '',
  366. 'ldap_expert_username_attr' => '',
  367. 'ldap_expert_uuid_user_attr' => '',
  368. 'ldap_expert_uuid_group_attr' => '',
  369. 'has_memberof_filter_support' => 0,
  370. 'last_jpegPhoto_lookup' => 0,
  371. 'ldap_nested_groups' => 0,
  372. 'ldap_paging_size' => 500,
  373. 'ldap_experienced_admin' => 0,
  374. );
  375. }
  376. /**
  377. * @return array that maps internal variable names to database fields
  378. */
  379. public function getConfigTranslationArray() {
  380. //TODO: merge them into one representation
  381. static $array = array(
  382. 'ldap_host' => 'ldapHost',
  383. 'ldap_port' => 'ldapPort',
  384. 'ldap_backup_host' => 'ldapBackupHost',
  385. 'ldap_backup_port' => 'ldapBackupPort',
  386. 'ldap_override_main_server' => 'ldapOverrideMainServer',
  387. 'ldap_dn' => 'ldapAgentName',
  388. 'ldap_agent_password' => 'ldapAgentPassword',
  389. 'ldap_base' => 'ldapBase',
  390. 'ldap_base_users' => 'ldapBaseUsers',
  391. 'ldap_base_groups' => 'ldapBaseGroups',
  392. 'ldap_userfilter_objectclass' => 'ldapUserFilterObjectclass',
  393. 'ldap_userfilter_groups' => 'ldapUserFilterGroups',
  394. 'ldap_userlist_filter' => 'ldapUserFilter',
  395. 'ldap_user_filter_mode' => 'ldapUserFilterMode',
  396. 'ldap_login_filter' => 'ldapLoginFilter',
  397. 'ldap_login_filter_mode' => 'ldapLoginFilterMode',
  398. 'ldap_loginfilter_email' => 'ldapLoginFilterEmail',
  399. 'ldap_loginfilter_username' => 'ldapLoginFilterUsername',
  400. 'ldap_loginfilter_attributes' => 'ldapLoginFilterAttributes',
  401. 'ldap_group_filter' => 'ldapGroupFilter',
  402. 'ldap_group_filter_mode' => 'ldapGroupFilterMode',
  403. 'ldap_groupfilter_objectclass' => 'ldapGroupFilterObjectclass',
  404. 'ldap_groupfilter_groups' => 'ldapGroupFilterGroups',
  405. 'ldap_display_name' => 'ldapUserDisplayName',
  406. 'ldap_group_display_name' => 'ldapGroupDisplayName',
  407. 'ldap_tls' => 'ldapTLS',
  408. 'ldap_nocase' => 'ldapNoCase',
  409. 'ldap_quota_def' => 'ldapQuotaDefault',
  410. 'ldap_quota_attr' => 'ldapQuotaAttribute',
  411. 'ldap_email_attr' => 'ldapEmailAttribute',
  412. 'ldap_group_member_assoc_attribute' => 'ldapGroupMemberAssocAttr',
  413. 'ldap_cache_ttl' => 'ldapCacheTTL',
  414. 'home_folder_naming_rule' => 'homeFolderNamingRule',
  415. 'ldap_turn_off_cert_check' => 'turnOffCertCheck',
  416. 'ldap_configuration_active' => 'ldapConfigurationActive',
  417. 'ldap_attributes_for_user_search' => 'ldapAttributesForUserSearch',
  418. 'ldap_attributes_for_group_search' => 'ldapAttributesForGroupSearch',
  419. 'ldap_expert_username_attr' => 'ldapExpertUsernameAttr',
  420. 'ldap_expert_uuid_user_attr' => 'ldapExpertUUIDUserAttr',
  421. 'ldap_expert_uuid_group_attr' => 'ldapExpertUUIDGroupAttr',
  422. 'has_memberof_filter_support' => 'hasMemberOfFilterSupport',
  423. 'last_jpegPhoto_lookup' => 'lastJpegPhotoLookup',
  424. 'ldap_nested_groups' => 'ldapNestedGroups',
  425. 'ldap_paging_size' => 'ldapPagingSize',
  426. 'ldap_experienced_admin' => 'ldapExperiencedAdmin'
  427. );
  428. return $array;
  429. }
  430. }