Configuration.php 22 KB

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