Configuration.php 22 KB

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