From d5dcf44f27d1a040c59f16b8b273e7d1a579b557 Mon Sep 17 00:00:00 2001 From: Kamidu Sachith Date: Fri, 16 Oct 2015 18:39:34 +0530 Subject: [PATCH 1/5] Change the service registration --- .../backend-oauth-authenticator/pom.xml | 12 ++++++---- .../OauthAuthenticatorServiceComponent.java | 24 +++++++++++-------- .../authenticator/OAuthAuthenticator.java | 5 ++-- .../pom.xml | 7 ++++++ pom.xml | 5 ++++ 5 files changed, 35 insertions(+), 18 deletions(-) diff --git a/components/identity-extensions/backend-oauth-authenticator/pom.xml b/components/identity-extensions/backend-oauth-authenticator/pom.xml index e148f5fdf3..a071805ad4 100644 --- a/components/identity-extensions/backend-oauth-authenticator/pom.xml +++ b/components/identity-extensions/backend-oauth-authenticator/pom.xml @@ -87,12 +87,14 @@ ${pom.artifactId} ${pom.artifactId} + ${carbon.device.mgt.version} + Identity backend Bundle + org.wso2.carbon.identity.authenticator.backend.oauth.internal.OauthAuthenticatorServiceComponent - org.wso2.sample.authenticator.internal + org.wso2.org.wso2.carbon.identity.authenticator.backend.oauth.internal - !org.wso2.sample.authenticator.internal, - org.wso2.sample.authenticator.*, + org.wso2.carbon.identity.authenticator.backend.oauth.* javax.servlet.http, @@ -112,8 +114,8 @@ org.wso2.carbon.core.services.authentication, org.wso2.carbon.identity.oauth2.stub, org.wso2.carbon.identity.oauth2.stub.dto, - org.wso2.carbon.base, - org.wso2.carbon.utils + org.wso2.carbon.base, + org.wso2.carbon.utils diff --git a/components/identity-extensions/backend-oauth-authenticator/src/main/java/org/wso2/carbon/identity/authenticator/backend/oauth/internal/OauthAuthenticatorServiceComponent.java b/components/identity-extensions/backend-oauth-authenticator/src/main/java/org/wso2/carbon/identity/authenticator/backend/oauth/internal/OauthAuthenticatorServiceComponent.java index 59577ac633..7076afd4a6 100755 --- a/components/identity-extensions/backend-oauth-authenticator/src/main/java/org/wso2/carbon/identity/authenticator/backend/oauth/internal/OauthAuthenticatorServiceComponent.java +++ b/components/identity-extensions/backend-oauth-authenticator/src/main/java/org/wso2/carbon/identity/authenticator/backend/oauth/internal/OauthAuthenticatorServiceComponent.java @@ -20,25 +20,27 @@ package org.wso2.carbon.identity.authenticator.backend.oauth.internal; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.osgi.service.component.ComponentContext; +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; import org.wso2.carbon.core.services.authentication.CarbonServerAuthenticator; import org.wso2.carbon.identity.authenticator.backend.oauth.OauthAuthenticator; -/** - * @scr.component component.name="org.wso2.carbon.identity.authenticator.backend.oauth.OauthAuthenticator" immediate="true" - */ -@SuppressWarnings("unused") -public class OauthAuthenticatorServiceComponent { +public class OauthAuthenticatorServiceComponent implements BundleActivator { + + private ServiceRegistration pipServiceRegRef; private static final Log log = LogFactory.getLog(OauthAuthenticatorServiceComponent .class); - protected void activate(ComponentContext ctxt) { + @Override + public void start(BundleContext bundleContext) throws Exception { + log.info("Initiating"); try { OauthAuthenticator oauthAuthenticator = new OauthAuthenticator(); - ctxt.getBundleContext().registerService(CarbonServerAuthenticator.class.getName(), - oauthAuthenticator, null); + pipServiceRegRef = bundleContext.registerService(CarbonServerAuthenticator.class.getName(), + oauthAuthenticator, null); if (log.isDebugEnabled()) { log.debug("OAuth Authenticator bundle is activated"); } @@ -47,10 +49,12 @@ public class OauthAuthenticatorServiceComponent { } } - protected void deactivate(ComponentContext ctxt) { + @Override + public void stop(BundleContext bundleContext) throws Exception { if (log.isDebugEnabled()) { log.debug("OAuth Authenticator bundle is deactivated"); } + pipServiceRegRef.unregister(); } } diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/OAuthAuthenticator.java b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/OAuthAuthenticator.java index 241e7de9b4..1119f3ac60 100644 --- a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/OAuthAuthenticator.java +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/OAuthAuthenticator.java @@ -45,7 +45,6 @@ public class OAuthAuthenticator implements WebappAuthenticator { private static final String RESOURCE_KEY = "resource"; private static APITokenAuthenticator authenticator = new APITokenAuthenticator(); - private static final Log log = LogFactory.getLog(OAuthAuthenticator.class); @Override @@ -81,8 +80,8 @@ public class OAuthAuthenticator implements WebappAuthenticator { authenticationInfo.setStatus(Status.CONTINUE); } String apiVersion = tokenizer.nextToken(); - String authLevel = authenticator.getResourceAuthenticationScheme(context, apiVersion, requestUri, requestMethod); - //String authLevel = "any"; + //String authLevel = authenticator.getResourceAuthenticationScheme(context, apiVersion, requestUri, requestMethod); + String authLevel = "any"; try { if (Constants.NO_MATCHING_AUTH_SCHEME.equals(authLevel)) { AuthenticationFrameworkUtil.handleNoMatchAuthScheme(request, response, requestMethod, apiVersion, diff --git a/features/oauth-extensions/org.wso2.carbon.device.mgt.oauth.extensions.feature/pom.xml b/features/oauth-extensions/org.wso2.carbon.device.mgt.oauth.extensions.feature/pom.xml index b75edcb2d3..9d72ffbe15 100644 --- a/features/oauth-extensions/org.wso2.carbon.device.mgt.oauth.extensions.feature/pom.xml +++ b/features/oauth-extensions/org.wso2.carbon.device.mgt.oauth.extensions.feature/pom.xml @@ -45,6 +45,10 @@ org.wso2.carbon.devicemgt org.wso2.carbon.device.mgt.oauth.extensions + + org.wso2.carbon.devicemgt + org.wso2.carbon.identity.authenticator.backend.oauth + @@ -73,6 +77,9 @@ org.wso2.carbon.devicemgt:org.wso2.carbon.device.mgt.oauth.extensions:${carbon.device.mgt.version} + + org.wso2.carbon.devicemgt:org.wso2.carbon.identity.authenticator.backend.oauth:${carbon.device.mgt.version} + org.wso2.carbon.core.server:${carbon.kernel.version} diff --git a/pom.xml b/pom.xml index 12b522c5bb..2152418327 100644 --- a/pom.xml +++ b/pom.xml @@ -247,6 +247,11 @@ + + org.wso2.carbon.devicemgt + org.wso2.carbon.identity.authenticator.backend.oauth + ${carbon.device.mgt.version} + From ddbd2c537243690453418ed66d7f117a3c7cf1cf Mon Sep 17 00:00:00 2001 From: Kamidu Sachith Date: Sat, 17 Oct 2015 20:03:19 +0530 Subject: [PATCH 2/5] Java 8 support --- .../backend-oauth-authenticator/pom.xml | 22 +------------------ 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/components/identity-extensions/backend-oauth-authenticator/pom.xml b/components/identity-extensions/backend-oauth-authenticator/pom.xml index a071805ad4..f626686a0d 100644 --- a/components/identity-extensions/backend-oauth-authenticator/pom.xml +++ b/components/identity-extensions/backend-oauth-authenticator/pom.xml @@ -96,27 +96,7 @@ org.wso2.carbon.identity.authenticator.backend.oauth.* - - javax.servlet.http, - org.apache.commons.logging, - org.wso2.carbon.identity.application.authentication.framework.*, - org.wso2.carbon.identity.oauth2, - org.wso2.carbon.identity.oauth2.dto, - org.wso2.carbon.user.core.service, - org.wso2.carbon.utils.multitenancy, - org.apache.axis2.client, - org.apache.axis2.context, - org.apache.axis2.transport.http, - org.apache.commons.httpclient, - org.osgi.framework, - org.osgi.service.component, - org.wso2.carbon.core.security, - org.wso2.carbon.core.services.authentication, - org.wso2.carbon.identity.oauth2.stub, - org.wso2.carbon.identity.oauth2.stub.dto, - org.wso2.carbon.base, - org.wso2.carbon.utils - + From cab888d3211b55ef2c321170aba0b4e6c62b9866 Mon Sep 17 00:00:00 2001 From: inosh-perera Date: Sun, 18 Oct 2015 17:26:32 +0530 Subject: [PATCH 3/5] fixing issues in oauth app creation, SAML. Moving service provider configuratiions to config files --- .../DynamicClientRegistrationServiceImpl.java | 15 +++++++--- .../profile/RegistrationProfile.java | 28 +++++++++++++++++ .../JaggeryOAuthConfigurationSettings.java | 30 +++++++++++++++++++ .../DynamicClientWebAppRegistrationUtil.java | 17 ++++++++++- 4 files changed, 85 insertions(+), 5 deletions(-) diff --git a/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/src/main/java/org/wso2/carbon/dynamic/client/registration/impl/DynamicClientRegistrationServiceImpl.java b/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/src/main/java/org/wso2/carbon/dynamic/client/registration/impl/DynamicClientRegistrationServiceImpl.java index 31aa091c26..0679b5f1e2 100644 --- a/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/src/main/java/org/wso2/carbon/dynamic/client/registration/impl/DynamicClientRegistrationServiceImpl.java +++ b/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/src/main/java/org/wso2/carbon/dynamic/client/registration/impl/DynamicClientRegistrationServiceImpl.java @@ -52,12 +52,11 @@ public class DynamicClientRegistrationServiceImpl implements DynamicClientRegist private static final String BASIC_AUTHENTICATOR = "BasicAuthenticator"; private static final String BASIC = "basic"; private static final String LOCAL = "local"; - private static final String ASSERTION_CONSUMER_URI = "https://localhost:9443/mdm/sso/acs"; - private static final String AUDIENCE = "https://null:9443/oauth2/token"; private static final Log log = LogFactory.getLog(DynamicClientRegistrationService.class); private static final String AUTH_TYPE_OAUTH_2 = "oauth2"; private static final String OAUTH_CONSUMER_SECRET = "oauthConsumerSecret"; private static final int STEP_ORDER = 1; + private static final String OAUTH_VERSION = "OAuth-2.0"; @Override public OAuthApplicationInfo registerOAuthApplication(RegistrationProfile profile) throws @@ -122,6 +121,9 @@ public class DynamicClientRegistrationServiceImpl implements DynamicClientRegist String grantType = profile.getGrantType(); String callbackUrl = profile.getCallbackUrl(); boolean isSaaSApp = profile.isSaasApp(); + String audience = profile.getAudience(); + String assertionConsumerURL = profile.getAssertionConsumerURL(); + String recepientValidationURL = profile.getRecepientValidationURL(); if (userId == null || userId.isEmpty()) { return null; @@ -177,6 +179,7 @@ public class DynamicClientRegistrationServiceImpl implements DynamicClientRegist oAuthConsumerApp.setApplicationName(applicationName); oAuthConsumerApp.setCallbackUrl(callbackUrl); oAuthConsumerApp.setGrantTypes(grantType); + oAuthConsumerApp.setOAuthVersion(OAUTH_VERSION); if (log.isDebugEnabled()) { log.debug("Creating OAuth App " + applicationName); } @@ -216,9 +219,13 @@ public class DynamicClientRegistrationServiceImpl implements DynamicClientRegist SAMLSSOServiceProviderDTO samlssoServiceProviderDTO = new SAMLSSOServiceProviderDTO(); samlssoServiceProviderDTO.setIssuer(MDM); - samlssoServiceProviderDTO.setAssertionConsumerUrl(ASSERTION_CONSUMER_URI); + samlssoServiceProviderDTO.setAssertionConsumerUrls(new String[] {assertionConsumerURL}); samlssoServiceProviderDTO.setDoSignResponse(true); - samlssoServiceProviderDTO.setRequestedAudiences(new String[] { AUDIENCE }); + samlssoServiceProviderDTO.setRequestedAudiences(new String[] { audience }); + samlssoServiceProviderDTO.setDefaultAssertionConsumerUrl(assertionConsumerURL); + samlssoServiceProviderDTO.setRequestedRecipients(new String[] {recepientValidationURL}); + samlssoServiceProviderDTO.setDoSignAssertions(true); + SAMLSSOConfigAdmin configAdmin = new SAMLSSOConfigAdmin(getConfigSystemRegistry()); configAdmin.addRelyingPartyServiceProvider(samlssoServiceProviderDTO); diff --git a/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/src/main/java/org/wso2/carbon/dynamic/client/registration/profile/RegistrationProfile.java b/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/src/main/java/org/wso2/carbon/dynamic/client/registration/profile/RegistrationProfile.java index f755e9ed23..2cc1cce5a4 100644 --- a/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/src/main/java/org/wso2/carbon/dynamic/client/registration/profile/RegistrationProfile.java +++ b/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/src/main/java/org/wso2/carbon/dynamic/client/registration/profile/RegistrationProfile.java @@ -41,6 +41,34 @@ public class RegistrationProfile { private String tokenScope; private String grantType; private boolean saasApp; + private String audience; + + public String getRecepientValidationURL() { + return recepientValidationURL; + } + + public void setRecepientValidationURL(String recepientValidationURL) { + this.recepientValidationURL = recepientValidationURL; + } + + public String getAssertionConsumerURL() { + return assertionConsumerURL; + } + + public void setAssertionConsumerURL(String assertionConsumerURL) { + this.assertionConsumerURL = assertionConsumerURL; + } + + private String recepientValidationURL; + private String assertionConsumerURL; + + public String getAudience() { + return audience; + } + + public void setAudience(String audience) { + this.audience = audience; + } public boolean isSaasApp() { return saasApp; diff --git a/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/dto/JaggeryOAuthConfigurationSettings.java b/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/dto/JaggeryOAuthConfigurationSettings.java index 32f8b9bcc9..c46e82e013 100644 --- a/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/dto/JaggeryOAuthConfigurationSettings.java +++ b/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/dto/JaggeryOAuthConfigurationSettings.java @@ -32,6 +32,36 @@ public class JaggeryOAuthConfigurationSettings { private String callbackURL; private String tokenScope; private boolean requireDynamicClientRegistration; + private String audience; + private String assertionConsumerURL; + private String recepientValidationURL; + + @XmlElement(name = "assertionConsumerURL", required = true) + public String getAssertionConsumerURL() { + return assertionConsumerURL; + } + + public void setAssertionConsumerURL(String assertionConsumerURL) { + this.assertionConsumerURL = assertionConsumerURL; + } + + @XmlElement(name = "recepientValidationURL", required = true) + public String getRecepientValidationURL() { + return recepientValidationURL; + } + + public void setRecepientValidationURL(String recepientValidationURL) { + this.recepientValidationURL = recepientValidationURL; + } + + @XmlElement(name = "audience", required = true) + public String getAudience() { + return audience; + } + + public void setAudience(String audience) { + this.audience = audience; + } @XmlElement(name = "saasApp", required = true) public boolean isSaasApp() { diff --git a/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/util/DynamicClientWebAppRegistrationUtil.java b/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/util/DynamicClientWebAppRegistrationUtil.java index 5c826b338b..9540d0e9e8 100644 --- a/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/util/DynamicClientWebAppRegistrationUtil.java +++ b/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/util/DynamicClientWebAppRegistrationUtil.java @@ -52,8 +52,11 @@ public class DynamicClientWebAppRegistrationUtil { private final static String OAUTH_PARAM_TOKEN_SCOPE = "tokenScope"; private final static String OAUTH_PARAM_SAAS_APP = "saasApp"; private final static String OAUTH_PARAM_CALLBACK_URL = "callbackURL"; + private final static String AUDIENCE = "audience"; + private final static String ASSERTION_CONSUMER_URL = "assertionConsumerURL"; + private final static String RECEPIENT_VALIDATION_URL = "recepientValidationURL"; private static final String JAGGERY_APP_OAUTH_CONFIG_PATH = - "config" + File.separator + "oauth.json"; + "config" + File.separator + "service-provider.json"; private static final Log log = LogFactory.getLog(DynamicClientWebAppRegistrationUtil.class); @@ -192,6 +195,9 @@ public class DynamicClientWebAppRegistrationUtil { registrationProfile.setClientName(webAppName); registrationProfile.setSaasApp(jaggeryOAuthConfigurationSettings.isSaasApp()); registrationProfile.setOwner(DynamicClientWebAppRegistrationUtil.getUserName()); + registrationProfile.setAudience(jaggeryOAuthConfigurationSettings.getAudience()); + registrationProfile.setAssertionConsumerURL(jaggeryOAuthConfigurationSettings.getAssertionConsumerURL()); + registrationProfile.setRecepientValidationURL(jaggeryOAuthConfigurationSettings.getRecepientValidationURL()); if (jaggeryOAuthConfigurationSettings.getCallbackURL() != null) { registrationProfile.setCallbackUrl(jaggeryOAuthConfigurationSettings.getCallbackURL()); } else { @@ -244,6 +250,15 @@ public class DynamicClientWebAppRegistrationUtil { case DynamicClientWebAppRegistrationUtil.OAUTH_PARAM_CALLBACK_URL: jaggeryOAuthConfigurationSettings.setCallbackURL(reader.nextString()); break; + case DynamicClientWebAppRegistrationUtil.AUDIENCE: + jaggeryOAuthConfigurationSettings.setAudience(reader.nextString()); + break; + case DynamicClientWebAppRegistrationUtil.ASSERTION_CONSUMER_URL: + jaggeryOAuthConfigurationSettings.setAssertionConsumerURL(reader.nextString()); + break; + case DynamicClientWebAppRegistrationUtil.RECEPIENT_VALIDATION_URL: + jaggeryOAuthConfigurationSettings.setRecepientValidationURL(reader.nextString()); + break; } } return jaggeryOAuthConfigurationSettings; From c74e27102f90ea979cde2238ca0cdbdf572ccdb2 Mon Sep 17 00:00:00 2001 From: prabathabey Date: Sun, 18 Oct 2015 18:16:52 +0530 Subject: [PATCH 4/5] Fixing issues in adding license configs --- .../mgt/registry/RegistryBasedLicenseManager.java | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/registry/RegistryBasedLicenseManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/registry/RegistryBasedLicenseManager.java index 7eb9f200a1..a70f62d79e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/registry/RegistryBasedLicenseManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/registry/RegistryBasedLicenseManager.java @@ -107,15 +107,8 @@ public class RegistryBasedLicenseManager implements LicenseManager { @Override public void addLicense(final String deviceType, final License license) throws LicenseManagementException { try { - GenericArtifact artifact = this.getGenericArtifact(deviceType, license.getLanguage()); - if (artifact != null) { - if (log.isDebugEnabled()) { - log.debug("Generic artifact is null for '" + deviceType + "' device type. Hence license does not " + - "have content"); - } - return; - } - artifact = artifactManager.newGovernanceArtifact(new QName("http://www.wso2.com", deviceType)); + GenericArtifact artifact = + artifactManager.newGovernanceArtifact(new QName("http://www.wso2.com", deviceType)); artifact.setAttribute(DeviceManagementConstants.LicenseProperties.NAME, license.getName()); artifact.setAttribute(DeviceManagementConstants.LicenseProperties.VERSION, license.getVersion()); artifact.setAttribute(DeviceManagementConstants.LicenseProperties.PROVIDER, license.getProvider()); From c46c4f1be9b6f58bc8a56d1e8d1a7844a8a7b026 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Mon, 19 Oct 2015 20:34:02 +0530 Subject: [PATCH 5/5] Fixing the policy update issues --- .../mgt/core/mgt/impl/PolicyManagerImpl.java | 72 ++++++++++++++++--- .../policy/mgt/core/PolicyDAOTestCase.java | 5 ++ 2 files changed, 67 insertions(+), 10 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java index 263817802b..e563d8c631 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -106,7 +106,7 @@ public class PolicyManagerImpl implements PolicyManager { policyDAO.addPolicyCriteriaProperties(policy.getPolicyCriterias()); } - if(policy.isActive()){ + if (policy.isActive()) { policyDAO.activatePolicy(policy.getId()); } PolicyManagementDAOFactory.commitTransaction(); @@ -135,18 +135,52 @@ public class PolicyManagerImpl implements PolicyManager { try { // Previous policy needs to be obtained before begining the transaction - Policy previousPolicy = getPolicy(policy.getId()); + Policy previousPolicy = this.getPolicy(policy.getId()); PolicyManagementDAOFactory.beginTransaction(); // This will keep track of the policies updated. policyDAO.recordUpdatedPolicy(policy); + + List existingFeaturesList = new ArrayList<>(); + List newFeaturesList = new ArrayList<>(); + List temp = new ArrayList<>(); + + List updatedFeatureList = policy.getProfile().getProfileFeaturesList(); + + List existingProfileFeaturesList = previousPolicy.getProfile().getProfileFeaturesList(); + + // Checks for the existing features + for (ProfileFeature feature : updatedFeatureList) { + for (ProfileFeature fe : existingProfileFeaturesList) { + if (feature.getFeatureCode().equalsIgnoreCase(fe.getFeatureCode())) { + existingFeaturesList.add(feature); + temp.add(feature.getFeatureCode()); + } + } + } + + // Checks for the new features + for (ProfileFeature feature : updatedFeatureList) { + if (!temp.contains(feature.getFeatureCode())) { + newFeaturesList.add(feature); + } + } + + int profileId = previousPolicy.getProfile().getProfileId(); + policy.getProfile().setProfileId(profileId); + policy.setProfileId(profileId); + Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); + policy.getProfile().setUpdatedDate(currentTimestamp); + policyDAO.updatePolicy(policy); profileDAO.updateProfile(policy.getProfile()); - featureDAO.updateProfileFeatures(policy.getProfile().getProfileFeaturesList(), policy.getProfile() - .getProfileId()); - policyDAO.deleteAllPolicyRelatedConfigs(policy.getId()); + featureDAO.updateProfileFeatures(existingFeaturesList, profileId); + if (!newFeaturesList.isEmpty()) { + featureDAO.addProfileFeatures(newFeaturesList, profileId); + } + policyDAO.deleteAllPolicyRelatedConfigs(policy.getId()); if (policy.getUsers() != null) { @@ -478,20 +512,24 @@ public class PolicyManagerImpl implements PolicyManager { roleNames = policyDAO.getPolicyAppliedRoles(policyId); userNames = policyDAO.getPolicyAppliedUsers(policyId); - Profile profile = profileDAO.getProfile(policy.getProfileId()); - policy.setProfile(profile); + //Profile profile = profileDAO.getProfile(policy.getProfileId()); + + policy.setRoles(roleNames); policy.setUsers(userNames); } catch (PolicyManagerDAOException e) { throw new PolicyManagementException("Error occurred while getting the policy related to policy ID (" + policyId + ")", e); - } catch (ProfileManagerDAOException e) { - throw new PolicyManagementException("Error occurred while getting the profile related to policy ID (" + - policyId + ")", e); +// } catch (ProfileManagerDAOException e) { +// throw new PolicyManagementException("Error occurred while getting the profile related to policy ID (" + +// policyId + ")", e); } catch (SQLException e) { throw new PolicyManagementException("Error occurred while opening a connection to the data source", e); +// } catch (ProfileManagementException e) { +// throw new PolicyManagementException("Error occurred while getting the profile related to policy ID (" + +// policyId + ")", e); } finally { PolicyManagementDAOFactory.closeConnection(); } @@ -499,6 +537,20 @@ public class PolicyManagerImpl implements PolicyManager { // This is done because connection close in below method too. deviceList = this.getPolicyAppliedDevicesIds(policyId); policy.setDevices(deviceList); + + try { + // PolicyManagementDAOFactory.openConnection(); + Profile profile = profileManager.getProfile(policy.getProfileId()); + policy.setProfile(profile); + } catch (ProfileManagementException e) { + throw new PolicyManagementException("Error occurred while getting the profile related to policy ID (" + + policyId + ")", e); +// } catch (SQLException e) { +// throw new PolicyManagementException("Error occurred while opening a connection to the data source", e); +// } finally { +// PolicyManagementDAOFactory.closeConnection(); + } + return policy; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java index b17fda051c..931d0b4253 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java @@ -314,6 +314,11 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { users.add("Udara"); users.add("Dileesha"); policy.setUsers(users); + + Profile profile2 = ProfileCreator.getProfile3(FeatureCreator.getFeatureList4()); + + Profile pf = new Profile(); + pap.updatePolicy(policy); pap.activatePolicy(policy.getId()); }