Configuration.php 22 KB

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