Configuration.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Alexander Bergolth <leo@strike.wu.ac.at>
  6. * @author Alex Weirig <alex.weirig@technolink.lu>
  7. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  11. * @author Lennart Rosam <hello@takuto.de>
  12. * @author Lukas Reschke <lukas@statuscode.ch>
  13. * @author Marc Hefter <marchefter@march42.net>
  14. * @author Morris Jobke <hey@morrisjobke.de>
  15. * @author Robin McCorkell <robin@mccorkell.me.uk>
  16. * @author Roeland Jago Douma <roeland@famdouma.nl>
  17. * @author Roger Szabo <roger.szabo@web.de>
  18. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  19. * @author Xuanwo <xuanwo@yunify.com>
  20. *
  21. * @license AGPL-3.0
  22. *
  23. * This code is free software: you can redistribute it and/or modify
  24. * it under the terms of the GNU Affero General Public License, version 3,
  25. * as published by the Free Software Foundation.
  26. *
  27. * This program is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. * GNU Affero General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU Affero General Public License, version 3,
  33. * along with this program. If not, see <http://www.gnu.org/licenses/>
  34. *
  35. */
  36. namespace OCA\User_LDAP;
  37. use Psr\Log\LoggerInterface;
  38. /**
  39. * @property string $ldapHost
  40. * @property string $ldapPort
  41. * @property string $ldapBackupHost
  42. * @property string $ldapBackupPort
  43. * @property string $ldapBackgroundHost
  44. * @property string $ldapBackgroundPort
  45. * @property array|'' $ldapBase
  46. * @property array|'' $ldapBaseUsers
  47. * @property array|'' $ldapBaseGroups
  48. * @property string $ldapAgentName
  49. * @property string $ldapAgentPassword
  50. * @property string $ldapTLS
  51. * @property string $turnOffCertCheck
  52. * @property string $ldapIgnoreNamingRules
  53. * @property string $ldapUserDisplayName
  54. * @property string $ldapUserDisplayName2
  55. * @property string $ldapUserAvatarRule
  56. * @property string $ldapGidNumber
  57. * @property array|'' $ldapUserFilterObjectclass
  58. * @property array|'' $ldapUserFilterGroups
  59. * @property string $ldapUserFilter
  60. * @property string $ldapUserFilterMode
  61. * @property string $ldapGroupFilter
  62. * @property string $ldapGroupFilterMode
  63. * @property array|'' $ldapGroupFilterObjectclass
  64. * @property array|'' $ldapGroupFilterGroups
  65. * @property string $ldapGroupDisplayName
  66. * @property string $ldapGroupMemberAssocAttr
  67. * @property string $ldapLoginFilter
  68. * @property string $ldapLoginFilterMode
  69. * @property string $ldapLoginFilterEmail
  70. * @property string $ldapLoginFilterUsername
  71. * @property array|'' $ldapLoginFilterAttributes
  72. * @property string $ldapQuotaAttribute
  73. * @property string $ldapQuotaDefault
  74. * @property string $ldapEmailAttribute
  75. * @property string $ldapCacheTTL
  76. * @property string $ldapUuidUserAttribute
  77. * @property string $ldapUuidGroupAttribute
  78. * @property string $ldapOverrideMainServer
  79. * @property string $ldapConfigurationActive
  80. * @property array|'' $ldapAttributesForUserSearch
  81. * @property array|'' $ldapAttributesForGroupSearch
  82. * @property string $ldapExperiencedAdmin
  83. * @property string $homeFolderNamingRule
  84. * @property string $hasMemberOfFilterSupport
  85. * @property string $useMemberOfToDetectMembership
  86. * @property string $ldapExpertUsernameAttr
  87. * @property string $ldapExpertUUIDUserAttr
  88. * @property string $ldapExpertUUIDGroupAttr
  89. * @property string $markRemnantsAsDisabled
  90. * @property string $lastJpegPhotoLookup
  91. * @property string $ldapNestedGroups
  92. * @property string $ldapPagingSize
  93. * @property string $turnOnPasswordChange
  94. * @property string $ldapDynamicGroupMemberURL
  95. * @property string $ldapDefaultPPolicyDN
  96. * @property string $ldapExtStorageHomeAttribute
  97. * @property string $ldapMatchingRuleInChainState
  98. * @property string $ldapConnectionTimeout
  99. * @property string $ldapAttributePhone
  100. * @property string $ldapAttributeWebsite
  101. * @property string $ldapAttributeAddress
  102. * @property string $ldapAttributeTwitter
  103. * @property string $ldapAttributeFediverse
  104. * @property string $ldapAttributeOrganisation
  105. * @property string $ldapAttributeRole
  106. * @property string $ldapAttributeHeadline
  107. * @property string $ldapAttributeBiography
  108. * @property string $ldapAdminGroup
  109. */
  110. class Configuration {
  111. public const AVATAR_PREFIX_DEFAULT = 'default';
  112. public const AVATAR_PREFIX_NONE = 'none';
  113. public const AVATAR_PREFIX_DATA_ATTRIBUTE = 'data:';
  114. public const LDAP_SERVER_FEATURE_UNKNOWN = 'unknown';
  115. public const LDAP_SERVER_FEATURE_AVAILABLE = 'available';
  116. public const LDAP_SERVER_FEATURE_UNAVAILABLE = 'unavailable';
  117. /**
  118. * @var string
  119. */
  120. protected $configPrefix;
  121. /**
  122. * @var bool
  123. */
  124. protected $configRead = false;
  125. /**
  126. * @var string[]
  127. */
  128. protected array $unsavedChanges = [];
  129. /**
  130. * @var array<string, mixed> settings
  131. */
  132. protected $config = [
  133. 'ldapHost' => null,
  134. 'ldapPort' => null,
  135. 'ldapBackupHost' => null,
  136. 'ldapBackupPort' => null,
  137. 'ldapBackgroundHost' => null,
  138. 'ldapBackgroundPort' => null,
  139. 'ldapBase' => null,
  140. 'ldapBaseUsers' => null,
  141. 'ldapBaseGroups' => null,
  142. 'ldapAgentName' => null,
  143. 'ldapAgentPassword' => null,
  144. 'ldapTLS' => null,
  145. 'turnOffCertCheck' => null,
  146. 'ldapIgnoreNamingRules' => null,
  147. 'ldapUserDisplayName' => null,
  148. 'ldapUserDisplayName2' => null,
  149. 'ldapUserAvatarRule' => null,
  150. 'ldapGidNumber' => null,
  151. 'ldapUserFilterObjectclass' => null,
  152. 'ldapUserFilterGroups' => null,
  153. 'ldapUserFilter' => null,
  154. 'ldapUserFilterMode' => null,
  155. 'ldapGroupFilter' => null,
  156. 'ldapGroupFilterMode' => null,
  157. 'ldapGroupFilterObjectclass' => null,
  158. 'ldapGroupFilterGroups' => null,
  159. 'ldapGroupDisplayName' => null,
  160. 'ldapGroupMemberAssocAttr' => null,
  161. 'ldapLoginFilter' => null,
  162. 'ldapLoginFilterMode' => null,
  163. 'ldapLoginFilterEmail' => null,
  164. 'ldapLoginFilterUsername' => null,
  165. 'ldapLoginFilterAttributes' => null,
  166. 'ldapQuotaAttribute' => null,
  167. 'ldapQuotaDefault' => null,
  168. 'ldapEmailAttribute' => null,
  169. 'ldapCacheTTL' => null,
  170. 'ldapUuidUserAttribute' => 'auto',
  171. 'ldapUuidGroupAttribute' => 'auto',
  172. 'ldapOverrideMainServer' => false,
  173. 'ldapConfigurationActive' => false,
  174. 'ldapAttributesForUserSearch' => null,
  175. 'ldapAttributesForGroupSearch' => null,
  176. 'ldapExperiencedAdmin' => false,
  177. 'homeFolderNamingRule' => null,
  178. 'hasMemberOfFilterSupport' => false,
  179. 'useMemberOfToDetectMembership' => true,
  180. 'ldapExpertUsernameAttr' => null,
  181. 'ldapExpertUUIDUserAttr' => null,
  182. 'ldapExpertUUIDGroupAttr' => null,
  183. 'markRemnantsAsDisabled' => false,
  184. 'lastJpegPhotoLookup' => null,
  185. 'ldapNestedGroups' => false,
  186. 'ldapPagingSize' => null,
  187. 'turnOnPasswordChange' => false,
  188. 'ldapDynamicGroupMemberURL' => null,
  189. 'ldapDefaultPPolicyDN' => null,
  190. 'ldapExtStorageHomeAttribute' => null,
  191. 'ldapMatchingRuleInChainState' => self::LDAP_SERVER_FEATURE_UNKNOWN,
  192. 'ldapConnectionTimeout' => 15,
  193. 'ldapAttributePhone' => null,
  194. 'ldapAttributeWebsite' => null,
  195. 'ldapAttributeAddress' => null,
  196. 'ldapAttributeTwitter' => null,
  197. 'ldapAttributeFediverse' => null,
  198. 'ldapAttributeOrganisation' => null,
  199. 'ldapAttributeRole' => null,
  200. 'ldapAttributeHeadline' => null,
  201. 'ldapAttributeBiography' => null,
  202. 'ldapAdminGroup' => '',
  203. ];
  204. public function __construct(string $configPrefix, bool $autoRead = true) {
  205. $this->configPrefix = $configPrefix;
  206. if ($autoRead) {
  207. $this->readConfiguration();
  208. }
  209. }
  210. /**
  211. * @param string $name
  212. * @return mixed|null
  213. */
  214. public function __get($name) {
  215. if (isset($this->config[$name])) {
  216. return $this->config[$name];
  217. }
  218. return null;
  219. }
  220. /**
  221. * @param string $name
  222. * @param mixed $value
  223. */
  224. public function __set($name, $value) {
  225. $this->setConfiguration([$name => $value]);
  226. }
  227. public function getConfiguration(): array {
  228. return $this->config;
  229. }
  230. /**
  231. * set LDAP configuration with values delivered by an array, not read
  232. * from configuration. It does not save the configuration! To do so, you
  233. * must call saveConfiguration afterwards.
  234. * @param array $config array that holds the config parameters in an associated
  235. * array
  236. * @param array &$applied optional; array where the set fields will be given to
  237. */
  238. public function setConfiguration(array $config, ?array &$applied = null): void {
  239. $cta = $this->getConfigTranslationArray();
  240. foreach ($config as $inputKey => $val) {
  241. if (str_contains($inputKey, '_') && array_key_exists($inputKey, $cta)) {
  242. $key = $cta[$inputKey];
  243. } elseif (array_key_exists($inputKey, $this->config)) {
  244. $key = $inputKey;
  245. } else {
  246. continue;
  247. }
  248. $setMethod = 'setValue';
  249. switch ($key) {
  250. case 'ldapAgentPassword':
  251. $setMethod = 'setRawValue';
  252. break;
  253. case 'homeFolderNamingRule':
  254. $trimmedVal = trim($val);
  255. if ($trimmedVal !== '' && !str_contains($val, 'attr:')) {
  256. $val = 'attr:'.$trimmedVal;
  257. }
  258. break;
  259. case 'ldapBase':
  260. case 'ldapBaseUsers':
  261. case 'ldapBaseGroups':
  262. case 'ldapAttributesForUserSearch':
  263. case 'ldapAttributesForGroupSearch':
  264. case 'ldapUserFilterObjectclass':
  265. case 'ldapUserFilterGroups':
  266. case 'ldapGroupFilterObjectclass':
  267. case 'ldapGroupFilterGroups':
  268. case 'ldapLoginFilterAttributes':
  269. $setMethod = 'setMultiLine';
  270. break;
  271. }
  272. $this->$setMethod($key, $val);
  273. if (is_array($applied)) {
  274. $applied[] = $inputKey;
  275. // storing key as index avoids duplication, and as value for simplicity
  276. }
  277. $this->unsavedChanges[$key] = $key;
  278. }
  279. }
  280. public function readConfiguration(): void {
  281. if (!$this->configRead) {
  282. $cta = array_flip($this->getConfigTranslationArray());
  283. foreach ($this->config as $key => $val) {
  284. if (!isset($cta[$key])) {
  285. //some are determined
  286. continue;
  287. }
  288. $dbKey = $cta[$key];
  289. switch ($key) {
  290. case 'ldapBase':
  291. case 'ldapBaseUsers':
  292. case 'ldapBaseGroups':
  293. case 'ldapAttributesForUserSearch':
  294. case 'ldapAttributesForGroupSearch':
  295. case 'ldapUserFilterObjectclass':
  296. case 'ldapUserFilterGroups':
  297. case 'ldapGroupFilterObjectclass':
  298. case 'ldapGroupFilterGroups':
  299. case 'ldapLoginFilterAttributes':
  300. $readMethod = 'getMultiLine';
  301. break;
  302. case 'ldapIgnoreNamingRules':
  303. $readMethod = 'getSystemValue';
  304. $dbKey = $key;
  305. break;
  306. case 'ldapAgentPassword':
  307. $readMethod = 'getPwd';
  308. break;
  309. case 'ldapUserDisplayName2':
  310. case 'ldapGroupDisplayName':
  311. $readMethod = 'getLcValue';
  312. break;
  313. case 'ldapUserDisplayName':
  314. default:
  315. // user display name does not lower case because
  316. // we rely on an upper case N as indicator whether to
  317. // auto-detect it or not. FIXME
  318. $readMethod = 'getValue';
  319. break;
  320. }
  321. $this->config[$key] = $this->$readMethod($dbKey);
  322. }
  323. $this->configRead = true;
  324. }
  325. }
  326. /**
  327. * saves the current config changes in the database
  328. */
  329. public function saveConfiguration(): void {
  330. $cta = array_flip($this->getConfigTranslationArray());
  331. $changed = false;
  332. foreach ($this->unsavedChanges as $key) {
  333. $value = $this->config[$key];
  334. switch ($key) {
  335. case 'ldapAgentPassword':
  336. $value = base64_encode($value);
  337. break;
  338. case 'ldapBase':
  339. case 'ldapBaseUsers':
  340. case 'ldapBaseGroups':
  341. case 'ldapAttributesForUserSearch':
  342. case 'ldapAttributesForGroupSearch':
  343. case 'ldapUserFilterObjectclass':
  344. case 'ldapUserFilterGroups':
  345. case 'ldapGroupFilterObjectclass':
  346. case 'ldapGroupFilterGroups':
  347. case 'ldapLoginFilterAttributes':
  348. if (is_array($value)) {
  349. $value = implode("\n", $value);
  350. }
  351. break;
  352. //following options are not stored but detected, skip them
  353. case 'ldapIgnoreNamingRules':
  354. case 'ldapUuidUserAttribute':
  355. case 'ldapUuidGroupAttribute':
  356. continue 2;
  357. }
  358. if (is_null($value)) {
  359. $value = '';
  360. }
  361. $changed = true;
  362. $this->saveValue($cta[$key], $value);
  363. }
  364. if ($changed) {
  365. $this->saveValue('_lastChange', (string)time());
  366. }
  367. $this->unsavedChanges = [];
  368. }
  369. /**
  370. * @param string $varName
  371. * @return array|string
  372. */
  373. protected function getMultiLine($varName) {
  374. $value = $this->getValue($varName);
  375. if (empty($value)) {
  376. $value = '';
  377. } else {
  378. $value = preg_split('/\r\n|\r|\n/', $value);
  379. }
  380. return $value;
  381. }
  382. /**
  383. * Sets multi-line values as arrays
  384. *
  385. * @param string $varName name of config-key
  386. * @param array|string $value to set
  387. */
  388. protected function setMultiLine(string $varName, $value): void {
  389. if (empty($value)) {
  390. $value = '';
  391. } elseif (!is_array($value)) {
  392. $value = preg_split('/\r\n|\r|\n|;/', $value);
  393. if ($value === false) {
  394. $value = '';
  395. }
  396. }
  397. if (!is_array($value)) {
  398. $finalValue = trim($value);
  399. } else {
  400. $finalValue = [];
  401. foreach ($value as $key => $val) {
  402. if (is_string($val)) {
  403. $val = trim($val);
  404. if ($val !== '') {
  405. //accidental line breaks are not wanted and can cause
  406. // odd behaviour. Thus, away with them.
  407. $finalValue[] = $val;
  408. }
  409. } else {
  410. $finalValue[] = $val;
  411. }
  412. }
  413. }
  414. $this->setRawValue($varName, $finalValue);
  415. }
  416. protected function getPwd(string $varName): string {
  417. return base64_decode($this->getValue($varName));
  418. }
  419. protected function getLcValue(string $varName): string {
  420. return mb_strtolower($this->getValue($varName), 'UTF-8');
  421. }
  422. protected function getSystemValue(string $varName): string {
  423. //FIXME: if another system value is added, softcode the default value
  424. return \OC::$server->getConfig()->getSystemValue($varName, false);
  425. }
  426. protected function getValue(string $varName): string {
  427. static $defaults;
  428. if (is_null($defaults)) {
  429. $defaults = $this->getDefaults();
  430. }
  431. return \OC::$server->getConfig()->getAppValue('user_ldap',
  432. $this->configPrefix.$varName,
  433. $defaults[$varName]);
  434. }
  435. /**
  436. * Sets a scalar value.
  437. *
  438. * @param string $varName name of config key
  439. * @param mixed $value to set
  440. */
  441. protected function setValue(string $varName, $value): void {
  442. if (is_string($value)) {
  443. $value = trim($value);
  444. }
  445. $this->config[$varName] = $value;
  446. }
  447. /**
  448. * Sets a scalar value without trimming.
  449. *
  450. * @param string $varName name of config key
  451. * @param mixed $value to set
  452. */
  453. protected function setRawValue(string $varName, $value): void {
  454. $this->config[$varName] = $value;
  455. }
  456. protected function saveValue(string $varName, string $value): bool {
  457. \OC::$server->getConfig()->setAppValue(
  458. 'user_ldap',
  459. $this->configPrefix.$varName,
  460. $value
  461. );
  462. return true;
  463. }
  464. /**
  465. * @return array an associative array with the default values. Keys are correspond
  466. * to config-value entries in the database table
  467. */
  468. public function getDefaults(): array {
  469. return [
  470. 'ldap_host' => '',
  471. 'ldap_port' => '',
  472. 'ldap_backup_host' => '',
  473. 'ldap_backup_port' => '',
  474. 'ldap_background_host' => '',
  475. 'ldap_background_port' => '',
  476. 'ldap_override_main_server' => '',
  477. 'ldap_dn' => '',
  478. 'ldap_agent_password' => '',
  479. 'ldap_base' => '',
  480. 'ldap_base_users' => '',
  481. 'ldap_base_groups' => '',
  482. 'ldap_userlist_filter' => '',
  483. 'ldap_user_filter_mode' => 0,
  484. 'ldap_userfilter_objectclass' => '',
  485. 'ldap_userfilter_groups' => '',
  486. 'ldap_login_filter' => '',
  487. 'ldap_login_filter_mode' => 0,
  488. 'ldap_loginfilter_email' => 0,
  489. 'ldap_loginfilter_username' => 1,
  490. 'ldap_loginfilter_attributes' => '',
  491. 'ldap_group_filter' => '',
  492. 'ldap_group_filter_mode' => 0,
  493. 'ldap_groupfilter_objectclass' => '',
  494. 'ldap_groupfilter_groups' => '',
  495. 'ldap_gid_number' => 'gidNumber',
  496. 'ldap_display_name' => 'displayName',
  497. 'ldap_user_display_name_2' => '',
  498. 'ldap_group_display_name' => 'cn',
  499. 'ldap_tls' => 0,
  500. 'ldap_quota_def' => '',
  501. 'ldap_quota_attr' => '',
  502. 'ldap_email_attr' => '',
  503. 'ldap_group_member_assoc_attribute' => '',
  504. 'ldap_cache_ttl' => 600,
  505. 'ldap_uuid_user_attribute' => 'auto',
  506. 'ldap_uuid_group_attribute' => 'auto',
  507. 'home_folder_naming_rule' => '',
  508. 'ldap_turn_off_cert_check' => 0,
  509. 'ldap_configuration_active' => 0,
  510. 'ldap_attributes_for_user_search' => '',
  511. 'ldap_attributes_for_group_search' => '',
  512. 'ldap_expert_username_attr' => '',
  513. 'ldap_expert_uuid_user_attr' => '',
  514. 'ldap_expert_uuid_group_attr' => '',
  515. 'has_memberof_filter_support' => 0,
  516. 'use_memberof_to_detect_membership' => 1,
  517. 'ldap_mark_remnants_as_disabled' => 0,
  518. 'last_jpegPhoto_lookup' => 0,
  519. 'ldap_nested_groups' => 0,
  520. 'ldap_paging_size' => 500,
  521. 'ldap_turn_on_pwd_change' => 0,
  522. 'ldap_experienced_admin' => 0,
  523. 'ldap_dynamic_group_member_url' => '',
  524. 'ldap_default_ppolicy_dn' => '',
  525. 'ldap_user_avatar_rule' => 'default',
  526. 'ldap_ext_storage_home_attribute' => '',
  527. 'ldap_matching_rule_in_chain_state' => self::LDAP_SERVER_FEATURE_UNKNOWN,
  528. 'ldap_connection_timeout' => 15,
  529. 'ldap_attr_phone' => '',
  530. 'ldap_attr_website' => '',
  531. 'ldap_attr_address' => '',
  532. 'ldap_attr_twitter' => '',
  533. 'ldap_attr_fediverse' => '',
  534. 'ldap_attr_organisation' => '',
  535. 'ldap_attr_role' => '',
  536. 'ldap_attr_headline' => '',
  537. 'ldap_attr_biography' => '',
  538. 'ldap_admin_group' => '',
  539. ];
  540. }
  541. /**
  542. * @return array that maps internal variable names to database fields
  543. */
  544. public function getConfigTranslationArray(): array {
  545. //TODO: merge them into one representation
  546. static $array = [
  547. 'ldap_host' => 'ldapHost',
  548. 'ldap_port' => 'ldapPort',
  549. 'ldap_backup_host' => 'ldapBackupHost',
  550. 'ldap_backup_port' => 'ldapBackupPort',
  551. 'ldap_background_host' => 'ldapBackgroundHost',
  552. 'ldap_background_port' => 'ldapBackgroundPort',
  553. 'ldap_override_main_server' => 'ldapOverrideMainServer',
  554. 'ldap_dn' => 'ldapAgentName',
  555. 'ldap_agent_password' => 'ldapAgentPassword',
  556. 'ldap_base' => 'ldapBase',
  557. 'ldap_base_users' => 'ldapBaseUsers',
  558. 'ldap_base_groups' => 'ldapBaseGroups',
  559. 'ldap_userfilter_objectclass' => 'ldapUserFilterObjectclass',
  560. 'ldap_userfilter_groups' => 'ldapUserFilterGroups',
  561. 'ldap_userlist_filter' => 'ldapUserFilter',
  562. 'ldap_user_filter_mode' => 'ldapUserFilterMode',
  563. 'ldap_user_avatar_rule' => 'ldapUserAvatarRule',
  564. 'ldap_login_filter' => 'ldapLoginFilter',
  565. 'ldap_login_filter_mode' => 'ldapLoginFilterMode',
  566. 'ldap_loginfilter_email' => 'ldapLoginFilterEmail',
  567. 'ldap_loginfilter_username' => 'ldapLoginFilterUsername',
  568. 'ldap_loginfilter_attributes' => 'ldapLoginFilterAttributes',
  569. 'ldap_group_filter' => 'ldapGroupFilter',
  570. 'ldap_group_filter_mode' => 'ldapGroupFilterMode',
  571. 'ldap_groupfilter_objectclass' => 'ldapGroupFilterObjectclass',
  572. 'ldap_groupfilter_groups' => 'ldapGroupFilterGroups',
  573. 'ldap_gid_number' => 'ldapGidNumber',
  574. 'ldap_display_name' => 'ldapUserDisplayName',
  575. 'ldap_user_display_name_2' => 'ldapUserDisplayName2',
  576. 'ldap_group_display_name' => 'ldapGroupDisplayName',
  577. 'ldap_tls' => 'ldapTLS',
  578. 'ldap_quota_def' => 'ldapQuotaDefault',
  579. 'ldap_quota_attr' => 'ldapQuotaAttribute',
  580. 'ldap_email_attr' => 'ldapEmailAttribute',
  581. 'ldap_group_member_assoc_attribute' => 'ldapGroupMemberAssocAttr',
  582. 'ldap_cache_ttl' => 'ldapCacheTTL',
  583. 'home_folder_naming_rule' => 'homeFolderNamingRule',
  584. 'ldap_turn_off_cert_check' => 'turnOffCertCheck',
  585. 'ldap_configuration_active' => 'ldapConfigurationActive',
  586. 'ldap_attributes_for_user_search' => 'ldapAttributesForUserSearch',
  587. 'ldap_attributes_for_group_search' => 'ldapAttributesForGroupSearch',
  588. 'ldap_expert_username_attr' => 'ldapExpertUsernameAttr',
  589. 'ldap_expert_uuid_user_attr' => 'ldapExpertUUIDUserAttr',
  590. 'ldap_expert_uuid_group_attr' => 'ldapExpertUUIDGroupAttr',
  591. 'has_memberof_filter_support' => 'hasMemberOfFilterSupport',
  592. 'use_memberof_to_detect_membership' => 'useMemberOfToDetectMembership',
  593. 'ldap_mark_remnants_as_disabled' => 'markRemnantsAsDisabled',
  594. 'last_jpegPhoto_lookup' => 'lastJpegPhotoLookup',
  595. 'ldap_nested_groups' => 'ldapNestedGroups',
  596. 'ldap_paging_size' => 'ldapPagingSize',
  597. 'ldap_turn_on_pwd_change' => 'turnOnPasswordChange',
  598. 'ldap_experienced_admin' => 'ldapExperiencedAdmin',
  599. 'ldap_dynamic_group_member_url' => 'ldapDynamicGroupMemberURL',
  600. 'ldap_default_ppolicy_dn' => 'ldapDefaultPPolicyDN',
  601. 'ldap_ext_storage_home_attribute' => 'ldapExtStorageHomeAttribute',
  602. 'ldap_matching_rule_in_chain_state' => 'ldapMatchingRuleInChainState',
  603. 'ldapIgnoreNamingRules' => 'ldapIgnoreNamingRules', // sysconfig
  604. 'ldap_connection_timeout' => 'ldapConnectionTimeout',
  605. 'ldap_attr_phone' => 'ldapAttributePhone',
  606. 'ldap_attr_website' => 'ldapAttributeWebsite',
  607. 'ldap_attr_address' => 'ldapAttributeAddress',
  608. 'ldap_attr_twitter' => 'ldapAttributeTwitter',
  609. 'ldap_attr_fediverse' => 'ldapAttributeFediverse',
  610. 'ldap_attr_organisation' => 'ldapAttributeOrganisation',
  611. 'ldap_attr_role' => 'ldapAttributeRole',
  612. 'ldap_attr_headline' => 'ldapAttributeHeadline',
  613. 'ldap_attr_biography' => 'ldapAttributeBiography',
  614. 'ldap_admin_group' => 'ldapAdminGroup',
  615. ];
  616. return $array;
  617. }
  618. /**
  619. * @throws \RuntimeException
  620. */
  621. public function resolveRule(string $rule): array {
  622. if ($rule === 'avatar') {
  623. return $this->getAvatarAttributes();
  624. }
  625. throw new \RuntimeException('Invalid rule');
  626. }
  627. public function getAvatarAttributes(): array {
  628. $value = $this->ldapUserAvatarRule ?: self::AVATAR_PREFIX_DEFAULT;
  629. $defaultAttributes = ['jpegphoto', 'thumbnailphoto'];
  630. if ($value === self::AVATAR_PREFIX_NONE) {
  631. return [];
  632. }
  633. if (str_starts_with($value, self::AVATAR_PREFIX_DATA_ATTRIBUTE)) {
  634. $attribute = trim(substr($value, strlen(self::AVATAR_PREFIX_DATA_ATTRIBUTE)));
  635. if ($attribute === '') {
  636. return $defaultAttributes;
  637. }
  638. return [strtolower($attribute)];
  639. }
  640. if ($value !== self::AVATAR_PREFIX_DEFAULT) {
  641. \OCP\Server::get(LoggerInterface::class)->warning('Invalid config value to ldapUserAvatarRule; falling back to default.');
  642. }
  643. return $defaultAttributes;
  644. }
  645. /**
  646. * Returns TRUE if the ldapHost variable starts with 'ldapi://'
  647. */
  648. public function usesLdapi(): bool {
  649. $host = $this->config['ldapHost'];
  650. return is_string($host) && (substr($host, 0, strlen('ldapi://')) === 'ldapi://');
  651. }
  652. }