123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612 |
- <?php
- namespace OCA\User_LDAP;
- class Configuration {
- public const AVATAR_PREFIX_DEFAULT = 'default';
- public const AVATAR_PREFIX_NONE = 'none';
- public const AVATAR_PREFIX_DATA_ATTRIBUTE = 'data:';
- public const LDAP_SERVER_FEATURE_UNKNOWN = 'unknown';
- public const LDAP_SERVER_FEATURE_AVAILABLE = 'available';
- public const LDAP_SERVER_FEATURE_UNAVAILABLE = 'unavailable';
-
- protected $configPrefix;
-
- protected $configRead = false;
-
- protected array $unsavedChanges = [];
-
- protected $config = [
- 'ldapHost' => null,
- 'ldapPort' => null,
- 'ldapBackupHost' => null,
- 'ldapBackupPort' => null,
- 'ldapBackgroundHost' => null,
- 'ldapBackgroundPort' => null,
- 'ldapBase' => null,
- 'ldapBaseUsers' => null,
- 'ldapBaseGroups' => null,
- 'ldapAgentName' => null,
- 'ldapAgentPassword' => null,
- 'ldapTLS' => null,
- 'turnOffCertCheck' => null,
- 'ldapIgnoreNamingRules' => null,
- 'ldapUserDisplayName' => null,
- 'ldapUserDisplayName2' => null,
- 'ldapUserAvatarRule' => null,
- 'ldapGidNumber' => null,
- 'ldapUserFilterObjectclass' => null,
- 'ldapUserFilterGroups' => null,
- 'ldapUserFilter' => null,
- 'ldapUserFilterMode' => null,
- 'ldapGroupFilter' => null,
- 'ldapGroupFilterMode' => null,
- 'ldapGroupFilterObjectclass' => null,
- 'ldapGroupFilterGroups' => null,
- 'ldapGroupDisplayName' => null,
- 'ldapGroupMemberAssocAttr' => null,
- 'ldapLoginFilter' => null,
- 'ldapLoginFilterMode' => null,
- 'ldapLoginFilterEmail' => null,
- 'ldapLoginFilterUsername' => null,
- 'ldapLoginFilterAttributes' => null,
- 'ldapQuotaAttribute' => null,
- 'ldapQuotaDefault' => null,
- 'ldapEmailAttribute' => null,
- 'ldapCacheTTL' => null,
- 'ldapUuidUserAttribute' => 'auto',
- 'ldapUuidGroupAttribute' => 'auto',
- 'ldapOverrideMainServer' => false,
- 'ldapConfigurationActive' => false,
- 'ldapAttributesForUserSearch' => null,
- 'ldapAttributesForGroupSearch' => null,
- 'ldapExperiencedAdmin' => false,
- 'homeFolderNamingRule' => null,
- 'hasMemberOfFilterSupport' => false,
- 'useMemberOfToDetectMembership' => true,
- 'ldapExpertUsernameAttr' => null,
- 'ldapExpertUUIDUserAttr' => null,
- 'ldapExpertUUIDGroupAttr' => null,
- 'markRemnantsAsDisabled' => false,
- 'lastJpegPhotoLookup' => null,
- 'ldapNestedGroups' => false,
- 'ldapPagingSize' => null,
- 'turnOnPasswordChange' => false,
- 'ldapDynamicGroupMemberURL' => null,
- 'ldapDefaultPPolicyDN' => null,
- 'ldapExtStorageHomeAttribute' => null,
- 'ldapMatchingRuleInChainState' => self::LDAP_SERVER_FEATURE_UNKNOWN,
- 'ldapConnectionTimeout' => 15,
- 'ldapAttributePhone' => null,
- 'ldapAttributeWebsite' => null,
- 'ldapAttributeAddress' => null,
- 'ldapAttributeTwitter' => null,
- 'ldapAttributeFediverse' => null,
- 'ldapAttributeOrganisation' => null,
- 'ldapAttributeRole' => null,
- 'ldapAttributeHeadline' => null,
- 'ldapAttributeBiography' => null,
- 'ldapAdminGroup' => '',
- ];
- public function __construct(string $configPrefix, bool $autoRead = true) {
- $this->configPrefix = $configPrefix;
- if ($autoRead) {
- $this->readConfiguration();
- }
- }
-
- public function __get($name) {
- if (isset($this->config[$name])) {
- return $this->config[$name];
- }
- return null;
- }
-
- public function __set($name, $value) {
- $this->setConfiguration([$name => $value]);
- }
- public function getConfiguration(): array {
- return $this->config;
- }
-
- public function setConfiguration(array $config, array &$applied = null): void {
- $cta = $this->getConfigTranslationArray();
- foreach ($config as $inputKey => $val) {
- if (str_contains($inputKey, '_') && array_key_exists($inputKey, $cta)) {
- $key = $cta[$inputKey];
- } elseif (array_key_exists($inputKey, $this->config)) {
- $key = $inputKey;
- } else {
- continue;
- }
- $setMethod = 'setValue';
- switch ($key) {
- case 'ldapAgentPassword':
- $setMethod = 'setRawValue';
- break;
- case 'homeFolderNamingRule':
- $trimmedVal = trim($val);
- if ($trimmedVal !== '' && !str_contains($val, 'attr:')) {
- $val = 'attr:'.$trimmedVal;
- }
- break;
- case 'ldapBase':
- case 'ldapBaseUsers':
- case 'ldapBaseGroups':
- case 'ldapAttributesForUserSearch':
- case 'ldapAttributesForGroupSearch':
- case 'ldapUserFilterObjectclass':
- case 'ldapUserFilterGroups':
- case 'ldapGroupFilterObjectclass':
- case 'ldapGroupFilterGroups':
- case 'ldapLoginFilterAttributes':
- $setMethod = 'setMultiLine';
- break;
- }
- $this->$setMethod($key, $val);
- if (is_array($applied)) {
- $applied[] = $inputKey;
-
- }
- $this->unsavedChanges[$key] = $key;
- }
- }
- public function readConfiguration(): void {
- if (!$this->configRead) {
- $cta = array_flip($this->getConfigTranslationArray());
- foreach ($this->config as $key => $val) {
- if (!isset($cta[$key])) {
-
- continue;
- }
- $dbKey = $cta[$key];
- switch ($key) {
- case 'ldapBase':
- case 'ldapBaseUsers':
- case 'ldapBaseGroups':
- case 'ldapAttributesForUserSearch':
- case 'ldapAttributesForGroupSearch':
- case 'ldapUserFilterObjectclass':
- case 'ldapUserFilterGroups':
- case 'ldapGroupFilterObjectclass':
- case 'ldapGroupFilterGroups':
- case 'ldapLoginFilterAttributes':
- $readMethod = 'getMultiLine';
- break;
- case 'ldapIgnoreNamingRules':
- $readMethod = 'getSystemValue';
- $dbKey = $key;
- break;
- case 'ldapAgentPassword':
- $readMethod = 'getPwd';
- break;
- case 'ldapUserDisplayName2':
- case 'ldapGroupDisplayName':
- $readMethod = 'getLcValue';
- break;
- case 'ldapUserDisplayName':
- default:
-
-
-
- $readMethod = 'getValue';
- break;
- }
- $this->config[$key] = $this->$readMethod($dbKey);
- }
- $this->configRead = true;
- }
- }
-
- public function saveConfiguration(): void {
- $cta = array_flip($this->getConfigTranslationArray());
- $changed = false;
- foreach ($this->unsavedChanges as $key) {
- $value = $this->config[$key];
- switch ($key) {
- case 'ldapAgentPassword':
- $value = base64_encode($value);
- break;
- case 'ldapBase':
- case 'ldapBaseUsers':
- case 'ldapBaseGroups':
- case 'ldapAttributesForUserSearch':
- case 'ldapAttributesForGroupSearch':
- case 'ldapUserFilterObjectclass':
- case 'ldapUserFilterGroups':
- case 'ldapGroupFilterObjectclass':
- case 'ldapGroupFilterGroups':
- case 'ldapLoginFilterAttributes':
- if (is_array($value)) {
- $value = implode("\n", $value);
- }
- break;
-
- case 'ldapIgnoreNamingRules':
- case 'ldapUuidUserAttribute':
- case 'ldapUuidGroupAttribute':
- continue 2;
- }
- if (is_null($value)) {
- $value = '';
- }
- $changed = true;
- $this->saveValue($cta[$key], $value);
- }
- if ($changed) {
- $this->saveValue('_lastChange', (string)time());
- }
- $this->unsavedChanges = [];
- }
-
- protected function getMultiLine($varName) {
- $value = $this->getValue($varName);
- if (empty($value)) {
- $value = '';
- } else {
- $value = preg_split('/\r\n|\r|\n/', $value);
- }
- return $value;
- }
-
- protected function setMultiLine(string $varName, $value): void {
- if (empty($value)) {
- $value = '';
- } elseif (!is_array($value)) {
- $value = preg_split('/\r\n|\r|\n|;/', $value);
- if ($value === false) {
- $value = '';
- }
- }
- if (!is_array($value)) {
- $finalValue = trim($value);
- } else {
- $finalValue = [];
- foreach ($value as $key => $val) {
- if (is_string($val)) {
- $val = trim($val);
- if ($val !== '') {
-
-
- $finalValue[] = $val;
- }
- } else {
- $finalValue[] = $val;
- }
- }
- }
- $this->setRawValue($varName, $finalValue);
- }
- protected function getPwd(string $varName): string {
- return base64_decode($this->getValue($varName));
- }
- protected function getLcValue(string $varName): string {
- return mb_strtolower($this->getValue($varName), 'UTF-8');
- }
- protected function getSystemValue(string $varName): string {
-
- return \OC::$server->getConfig()->getSystemValue($varName, false);
- }
- protected function getValue(string $varName): string {
- static $defaults;
- if (is_null($defaults)) {
- $defaults = $this->getDefaults();
- }
- return \OC::$server->getConfig()->getAppValue('user_ldap',
- $this->configPrefix.$varName,
- $defaults[$varName]);
- }
-
- protected function setValue(string $varName, $value): void {
- if (is_string($value)) {
- $value = trim($value);
- }
- $this->config[$varName] = $value;
- }
-
- protected function setRawValue(string $varName, $value): void {
- $this->config[$varName] = $value;
- }
- protected function saveValue(string $varName, string $value): bool {
- \OC::$server->getConfig()->setAppValue(
- 'user_ldap',
- $this->configPrefix.$varName,
- $value
- );
- return true;
- }
-
- public function getDefaults(): array {
- return [
- 'ldap_host' => '',
- 'ldap_port' => '',
- 'ldap_backup_host' => '',
- 'ldap_backup_port' => '',
- 'ldap_background_host' => '',
- 'ldap_background_port' => '',
- 'ldap_override_main_server' => '',
- 'ldap_dn' => '',
- 'ldap_agent_password' => '',
- 'ldap_base' => '',
- 'ldap_base_users' => '',
- 'ldap_base_groups' => '',
- 'ldap_userlist_filter' => '',
- 'ldap_user_filter_mode' => 0,
- 'ldap_userfilter_objectclass' => '',
- 'ldap_userfilter_groups' => '',
- 'ldap_login_filter' => '',
- 'ldap_login_filter_mode' => 0,
- 'ldap_loginfilter_email' => 0,
- 'ldap_loginfilter_username' => 1,
- 'ldap_loginfilter_attributes' => '',
- 'ldap_group_filter' => '',
- 'ldap_group_filter_mode' => 0,
- 'ldap_groupfilter_objectclass' => '',
- 'ldap_groupfilter_groups' => '',
- 'ldap_gid_number' => 'gidNumber',
- 'ldap_display_name' => 'displayName',
- 'ldap_user_display_name_2' => '',
- 'ldap_group_display_name' => 'cn',
- 'ldap_tls' => 0,
- 'ldap_quota_def' => '',
- 'ldap_quota_attr' => '',
- 'ldap_email_attr' => '',
- 'ldap_group_member_assoc_attribute' => '',
- 'ldap_cache_ttl' => 600,
- 'ldap_uuid_user_attribute' => 'auto',
- 'ldap_uuid_group_attribute' => 'auto',
- 'home_folder_naming_rule' => '',
- 'ldap_turn_off_cert_check' => 0,
- 'ldap_configuration_active' => 0,
- 'ldap_attributes_for_user_search' => '',
- 'ldap_attributes_for_group_search' => '',
- 'ldap_expert_username_attr' => '',
- 'ldap_expert_uuid_user_attr' => '',
- 'ldap_expert_uuid_group_attr' => '',
- 'has_memberof_filter_support' => 0,
- 'use_memberof_to_detect_membership' => 1,
- 'ldap_mark_remnants_as_disabled' => 0,
- 'last_jpegPhoto_lookup' => 0,
- 'ldap_nested_groups' => 0,
- 'ldap_paging_size' => 500,
- 'ldap_turn_on_pwd_change' => 0,
- 'ldap_experienced_admin' => 0,
- 'ldap_dynamic_group_member_url' => '',
- 'ldap_default_ppolicy_dn' => '',
- 'ldap_user_avatar_rule' => 'default',
- 'ldap_ext_storage_home_attribute' => '',
- 'ldap_matching_rule_in_chain_state' => self::LDAP_SERVER_FEATURE_UNKNOWN,
- 'ldap_connection_timeout' => 15,
- 'ldap_attr_phone' => '',
- 'ldap_attr_website' => '',
- 'ldap_attr_address' => '',
- 'ldap_attr_twitter' => '',
- 'ldap_attr_fediverse' => '',
- 'ldap_attr_organisation' => '',
- 'ldap_attr_role' => '',
- 'ldap_attr_headline' => '',
- 'ldap_attr_biography' => '',
- 'ldap_admin_group' => '',
- ];
- }
-
- public function getConfigTranslationArray(): array {
-
- static $array = [
- 'ldap_host' => 'ldapHost',
- 'ldap_port' => 'ldapPort',
- 'ldap_backup_host' => 'ldapBackupHost',
- 'ldap_backup_port' => 'ldapBackupPort',
- 'ldap_background_host' => 'ldapBackgroundHost',
- 'ldap_background_port' => 'ldapBackgroundPort',
- 'ldap_override_main_server' => 'ldapOverrideMainServer',
- 'ldap_dn' => 'ldapAgentName',
- 'ldap_agent_password' => 'ldapAgentPassword',
- 'ldap_base' => 'ldapBase',
- 'ldap_base_users' => 'ldapBaseUsers',
- 'ldap_base_groups' => 'ldapBaseGroups',
- 'ldap_userfilter_objectclass' => 'ldapUserFilterObjectclass',
- 'ldap_userfilter_groups' => 'ldapUserFilterGroups',
- 'ldap_userlist_filter' => 'ldapUserFilter',
- 'ldap_user_filter_mode' => 'ldapUserFilterMode',
- 'ldap_user_avatar_rule' => 'ldapUserAvatarRule',
- 'ldap_login_filter' => 'ldapLoginFilter',
- 'ldap_login_filter_mode' => 'ldapLoginFilterMode',
- 'ldap_loginfilter_email' => 'ldapLoginFilterEmail',
- 'ldap_loginfilter_username' => 'ldapLoginFilterUsername',
- 'ldap_loginfilter_attributes' => 'ldapLoginFilterAttributes',
- 'ldap_group_filter' => 'ldapGroupFilter',
- 'ldap_group_filter_mode' => 'ldapGroupFilterMode',
- 'ldap_groupfilter_objectclass' => 'ldapGroupFilterObjectclass',
- 'ldap_groupfilter_groups' => 'ldapGroupFilterGroups',
- 'ldap_gid_number' => 'ldapGidNumber',
- 'ldap_display_name' => 'ldapUserDisplayName',
- 'ldap_user_display_name_2' => 'ldapUserDisplayName2',
- 'ldap_group_display_name' => 'ldapGroupDisplayName',
- 'ldap_tls' => 'ldapTLS',
- 'ldap_quota_def' => 'ldapQuotaDefault',
- 'ldap_quota_attr' => 'ldapQuotaAttribute',
- 'ldap_email_attr' => 'ldapEmailAttribute',
- 'ldap_group_member_assoc_attribute' => 'ldapGroupMemberAssocAttr',
- 'ldap_cache_ttl' => 'ldapCacheTTL',
- 'home_folder_naming_rule' => 'homeFolderNamingRule',
- 'ldap_turn_off_cert_check' => 'turnOffCertCheck',
- 'ldap_configuration_active' => 'ldapConfigurationActive',
- 'ldap_attributes_for_user_search' => 'ldapAttributesForUserSearch',
- 'ldap_attributes_for_group_search' => 'ldapAttributesForGroupSearch',
- 'ldap_expert_username_attr' => 'ldapExpertUsernameAttr',
- 'ldap_expert_uuid_user_attr' => 'ldapExpertUUIDUserAttr',
- 'ldap_expert_uuid_group_attr' => 'ldapExpertUUIDGroupAttr',
- 'has_memberof_filter_support' => 'hasMemberOfFilterSupport',
- 'use_memberof_to_detect_membership' => 'useMemberOfToDetectMembership',
- 'ldap_mark_remnants_as_disabled' => 'markRemnantsAsDisabled',
- 'last_jpegPhoto_lookup' => 'lastJpegPhotoLookup',
- 'ldap_nested_groups' => 'ldapNestedGroups',
- 'ldap_paging_size' => 'ldapPagingSize',
- 'ldap_turn_on_pwd_change' => 'turnOnPasswordChange',
- 'ldap_experienced_admin' => 'ldapExperiencedAdmin',
- 'ldap_dynamic_group_member_url' => 'ldapDynamicGroupMemberURL',
- 'ldap_default_ppolicy_dn' => 'ldapDefaultPPolicyDN',
- 'ldap_ext_storage_home_attribute' => 'ldapExtStorageHomeAttribute',
- 'ldap_matching_rule_in_chain_state' => 'ldapMatchingRuleInChainState',
- 'ldapIgnoreNamingRules' => 'ldapIgnoreNamingRules',
- 'ldap_connection_timeout' => 'ldapConnectionTimeout',
- 'ldap_attr_phone' => 'ldapAttributePhone',
- 'ldap_attr_website' => 'ldapAttributeWebsite',
- 'ldap_attr_address' => 'ldapAttributeAddress',
- 'ldap_attr_twitter' => 'ldapAttributeTwitter',
- 'ldap_attr_fediverse' => 'ldapAttributeFediverse',
- 'ldap_attr_organisation' => 'ldapAttributeOrganisation',
- 'ldap_attr_role' => 'ldapAttributeRole',
- 'ldap_attr_headline' => 'ldapAttributeHeadline',
- 'ldap_attr_biography' => 'ldapAttributeBiography',
- 'ldap_admin_group' => 'ldapAdminGroup',
- ];
- return $array;
- }
-
- public function resolveRule(string $rule): array {
- if ($rule === 'avatar') {
- return $this->getAvatarAttributes();
- }
- throw new \RuntimeException('Invalid rule');
- }
- public function getAvatarAttributes(): array {
- $value = $this->ldapUserAvatarRule ?: self::AVATAR_PREFIX_DEFAULT;
- $defaultAttributes = ['jpegphoto', 'thumbnailphoto'];
- if ($value === self::AVATAR_PREFIX_NONE) {
- return [];
- }
- if (str_starts_with($value, self::AVATAR_PREFIX_DATA_ATTRIBUTE)) {
- $attribute = trim(substr($value, strlen(self::AVATAR_PREFIX_DATA_ATTRIBUTE)));
- if ($attribute === '') {
- return $defaultAttributes;
- }
- return [strtolower($attribute)];
- }
- if ($value !== self::AVATAR_PREFIX_DEFAULT) {
- \OC::$server->getLogger()->warning('Invalid config value to ldapUserAvatarRule; falling back to default.');
- }
- return $defaultAttributes;
- }
-
- public function usesLdapi(): bool {
- return (substr($this->config['ldapHost'], 0, strlen('ldapi://')) === 'ldapi://');
- }
- }
|