Configuration.php 22 KB

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