From 59dc24baa687600bf9ee71843812fa50a405e0c7 Mon Sep 17 00:00:00 2001 From: Yasasr1 Date: Fri, 1 Nov 2024 16:27:38 +0530 Subject: [PATCH] Add method to update discovery config. --- .../service/OrganizationConfigManager.java | 14 +++++++++ .../OrganizationConfigManagerImpl.java | 29 +++++++++++++++++++ .../constant/OrganizationConfigConstants.java | 7 +++-- 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/components/org.wso2.carbon.identity.organization.config.service/src/main/java/org/wso2/carbon/identity/organization/config/service/OrganizationConfigManager.java b/components/org.wso2.carbon.identity.organization.config.service/src/main/java/org/wso2/carbon/identity/organization/config/service/OrganizationConfigManager.java index 1056790dc..76d41afa0 100644 --- a/components/org.wso2.carbon.identity.organization.config.service/src/main/java/org/wso2/carbon/identity/organization/config/service/OrganizationConfigManager.java +++ b/components/org.wso2.carbon.identity.organization.config.service/src/main/java/org/wso2/carbon/identity/organization/config/service/OrganizationConfigManager.java @@ -20,6 +20,7 @@ import org.wso2.carbon.identity.organization.config.service.exception.OrganizationConfigException; import org.wso2.carbon.identity.organization.config.service.model.DiscoveryConfig; +import org.wso2.carbon.identity.organization.management.service.exception.NotImplementedException; /** * Interface for organization configuration management. @@ -35,6 +36,19 @@ public interface OrganizationConfigManager { */ void addDiscoveryConfiguration(DiscoveryConfig discoveryConfig) throws OrganizationConfigException; + /** + * Update the discovery configuration of the primary organization. + * + * @param discoveryConfig The discovery configuration. + * @throws OrganizationConfigException The exception thrown when an error occurs while updating the discovery + * configuration. + */ + default void updateDiscoveryConfiguration(DiscoveryConfig discoveryConfig) throws OrganizationConfigException { + + throw new NotImplementedException("updateDiscoveryConfiguration method is not implemented in " + + this.getClass().getName()); + } + /** * Fetch the discovery configuration of the primary organization. * diff --git a/components/org.wso2.carbon.identity.organization.config.service/src/main/java/org/wso2/carbon/identity/organization/config/service/OrganizationConfigManagerImpl.java b/components/org.wso2.carbon.identity.organization.config.service/src/main/java/org/wso2/carbon/identity/organization/config/service/OrganizationConfigManagerImpl.java index 6ddd75597..7d5f74be6 100644 --- a/components/org.wso2.carbon.identity.organization.config.service/src/main/java/org/wso2/carbon/identity/organization/config/service/OrganizationConfigManagerImpl.java +++ b/components/org.wso2.carbon.identity.organization.config.service/src/main/java/org/wso2/carbon/identity/organization/config/service/OrganizationConfigManagerImpl.java @@ -38,6 +38,8 @@ import java.util.stream.Collectors; import static org.wso2.carbon.identity.configuration.mgt.core.constant.ConfigurationConstants.ErrorMessages.ERROR_CODE_RESOURCE_DOES_NOT_EXISTS; +import static org.wso2.carbon.identity.organization.config.service.constant.OrganizationConfigConstants.EMAIL_DOMAIN_BASED_SELF_SIGNUP_ENABLE; +import static org.wso2.carbon.identity.organization.config.service.constant.OrganizationConfigConstants.EMAIL_DOMAIN_ENABLE; import static org.wso2.carbon.identity.organization.config.service.constant.OrganizationConfigConstants.ErrorMessages.ERROR_CODE_DISCOVERY_CONFIG_CONFLICT; import static org.wso2.carbon.identity.organization.config.service.constant.OrganizationConfigConstants.ErrorMessages.ERROR_CODE_DISCOVERY_CONFIG_NOT_EXIST; import static org.wso2.carbon.identity.organization.config.service.constant.OrganizationConfigConstants.ErrorMessages.ERROR_CODE_DISCOVERY_CONFIG_UPDATE_NOT_ALLOWED; @@ -45,6 +47,7 @@ import static org.wso2.carbon.identity.organization.config.service.constant.OrganizationConfigConstants.ErrorMessages.ERROR_CODE_ERROR_DELETING_DISCOVERY_CONFIG; import static org.wso2.carbon.identity.organization.config.service.constant.OrganizationConfigConstants.ErrorMessages.ERROR_CODE_ERROR_RETRIEVING_DISCOVERY_CONFIG; import static org.wso2.carbon.identity.organization.config.service.constant.OrganizationConfigConstants.ErrorMessages.ERROR_CODE_INVALID_DISCOVERY_ATTRIBUTE; +import static org.wso2.carbon.identity.organization.config.service.constant.OrganizationConfigConstants.ErrorMessages.ERROR_CODE_INVALID_DISCOVERY_ATTRIBUTE_VALUES; import static org.wso2.carbon.identity.organization.config.service.constant.OrganizationConfigConstants.RESOURCE_NAME; import static org.wso2.carbon.identity.organization.config.service.constant.OrganizationConfigConstants.RESOURCE_TYPE_NAME; import static org.wso2.carbon.identity.organization.config.service.constant.OrganizationConfigConstants.SUPPORTED_DISCOVERY_ATTRIBUTE_KEYS; @@ -75,6 +78,26 @@ public void addDiscoveryConfiguration(DiscoveryConfig discoveryConfig) throws Or } } + @Override + public void updateDiscoveryConfiguration(DiscoveryConfig discoveryConfig) throws OrganizationConfigException { + + try { + if (!isDiscoveryConfigChangeAllowed()) { + throw handleClientException(ERROR_CODE_DISCOVERY_CONFIG_UPDATE_NOT_ALLOWED); + } + Optional resourceOptional = getDiscoveryResource(); + Resource resource = buildResourceFromValidationConfig(discoveryConfig); + if (!resourceOptional.isPresent()) { + getConfigurationManager().addResource(RESOURCE_TYPE_NAME, resource); + } else { + getConfigurationManager().replaceResource(RESOURCE_TYPE_NAME, resource); + } + + } catch (ConfigurationManagementException | OrganizationManagementServerException e) { + throw handleServerException(ERROR_CODE_ERROR_ADDING_DISCOVERY_CONFIG, e, getOrganizationId()); + } + } + @Override public DiscoveryConfig getDiscoveryConfiguration() throws OrganizationConfigException { @@ -158,6 +181,12 @@ private Resource buildResourceFromValidationConfig(DiscoveryConfig discoveryConf } configAttributes.put(key, property.getValue()); } + + if (Boolean.parseBoolean(configAttributes.get(EMAIL_DOMAIN_BASED_SELF_SIGNUP_ENABLE)) && + !Boolean.parseBoolean(configAttributes.get(EMAIL_DOMAIN_ENABLE))) { + throw handleClientException(ERROR_CODE_INVALID_DISCOVERY_ATTRIBUTE_VALUES); + } + List resourceAttributes = configAttributes.entrySet().stream() .filter(attribute -> attribute.getValue() != null && !"null".equals(attribute.getValue())) .map(attribute -> new Attribute(attribute.getKey(), attribute.getValue())) diff --git a/components/org.wso2.carbon.identity.organization.config.service/src/main/java/org/wso2/carbon/identity/organization/config/service/constant/OrganizationConfigConstants.java b/components/org.wso2.carbon.identity.organization.config.service/src/main/java/org/wso2/carbon/identity/organization/config/service/constant/OrganizationConfigConstants.java index bbf64eff2..26db537ac 100644 --- a/components/org.wso2.carbon.identity.organization.config.service/src/main/java/org/wso2/carbon/identity/organization/config/service/constant/OrganizationConfigConstants.java +++ b/components/org.wso2.carbon.identity.organization.config.service/src/main/java/org/wso2/carbon/identity/organization/config/service/constant/OrganizationConfigConstants.java @@ -18,7 +18,6 @@ package org.wso2.carbon.identity.organization.config.service.constant; -import java.util.Collections; import java.util.List; /** @@ -28,8 +27,10 @@ public class OrganizationConfigConstants { public static final String RESOURCE_TYPE_NAME = "ORGANIZATION_CONFIGURATION"; public static final String RESOURCE_NAME = "OrganizationDiscovery"; + public static final String EMAIL_DOMAIN_ENABLE = "emailDomain.enable"; + public static final String EMAIL_DOMAIN_BASED_SELF_SIGNUP_ENABLE = "emailDomainBasedSelfSignup.enable"; public static final List SUPPORTED_DISCOVERY_ATTRIBUTE_KEYS = - Collections.singletonList("emailDomain.enable"); + List.of(EMAIL_DOMAIN_ENABLE, EMAIL_DOMAIN_BASED_SELF_SIGNUP_ENABLE); private static final String ORGANIZATION_CONFIGURATION_ERROR_CODE_PREFIX = "OCM-"; /** @@ -47,6 +48,8 @@ public enum ErrorMessages { "The organization discovery configuration is already for available for the organization with id: %s."), ERROR_CODE_INVALID_DISCOVERY_ATTRIBUTE("60004", "Invalid organization discovery attribute.", "The organization discovery attribute with key: %s is not supported."), + ERROR_CODE_INVALID_DISCOVERY_ATTRIBUTE_VALUES("60005", "Invalid organization discovery attribute " + + "values.", "Provided organization discovery attribute value combination is not supported."), // Server errors. ERROR_CODE_ERROR_ADDING_DISCOVERY_CONFIG("65001", "Unable to add the organization discovery " +