Configuration.php 21 KB

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