configuration.php 14 KB

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