Configuration.php 19 KB

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