Skip to content

Commit

Permalink
Merge pull request #757 from madurangasiriwardena/export
Browse files Browse the repository at this point in the history
Add improved user profile export functionality
  • Loading branch information
madurangasiriwardena authored Sep 19, 2023
2 parents e912bc9 + d6a3b98 commit ab5156e
Show file tree
Hide file tree
Showing 19 changed files with 1,204 additions and 211 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
org.wso2.carbon.user.core.*; version="${carbon.kernel.package.import.version.range}",
org.wso2.carbon.context; version="${carbon.kernel.package.import.version.range}",
org.wso2.carbon.consent.mgt.core.*; version="${carbon.consent.mgt.version.range}",
org.wso2.carbon.identity.user.profile.mgt.*; version="${carbon.identity.framework.imp.pkg.version.range}",
</Import-Package>
<Export-Package>
!org.wso2.carbon.identity.user.export.core.internal,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.identity.user.export.core.internal;

import org.wso2.carbon.consent.mgt.core.ConsentManager;
import org.wso2.carbon.identity.user.export.core.service.UserInformationProvider;
import org.wso2.carbon.identity.user.profile.mgt.association.federation.FederatedAssociationManager;
import org.wso2.carbon.user.core.service.RealmService;

import java.util.ArrayList;
import java.util.List;

/**
* Data holder for User Profile Export service.
*/
public class UserProfileExportDataHolder {

private static RealmService realmService;
private static FederatedAssociationManager federatedAssociationManager;
private static ConsentManager consentManager;

public static List<UserInformationProvider> getUserInformationProviders() {

return userInformationProviders;
}

public static void setUserInformationProviders(
List<UserInformationProvider> userInformationProviders) {

UserProfileExportDataHolder.userInformationProviders = userInformationProviders;
}

private static List<UserInformationProvider> userInformationProviders = new ArrayList<>();


/**
* Get RealmService instance.
*
* @return RealmService instance.
*/
public static RealmService getRealmService() {

return realmService;
}

/**
* Set RealmService instance.
*
* @param realmService RealmService instance.
*/
public static void setRealmService(RealmService realmService) {

UserProfileExportDataHolder.realmService = realmService;
}

/**
* Set FederatedAssociationManager instance.
*
* @param federatedAssociationManager FederatedAssociationManager instance.
*/
public static void setFederatedAssociationManager(FederatedAssociationManager federatedAssociationManager) {

UserProfileExportDataHolder.federatedAssociationManager = federatedAssociationManager;
}

/**
* Get FederatedAssociationManager instance.
*
* @return FederatedAssociationManager instance.
*/
public static FederatedAssociationManager getFederatedAssociationManager() {

return federatedAssociationManager;
}

/**
* Get consent manager instance.
*
* @return Consent Manager
*/
public static ConsentManager getConsentManager() {

return consentManager;
}

/**
* Set consent manager instance.
*
* @param consentManager consent manager
*/
public static void setConsentManager(ConsentManager consentManager) {

UserProfileExportDataHolder.consentManager = consentManager;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/*
* Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.identity.user.export.core.internal;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.wso2.carbon.consent.mgt.core.ConsentManager;
import org.wso2.carbon.identity.user.export.core.internal.service.impl.BasicUserInformationProvider;
import org.wso2.carbon.identity.user.export.core.internal.service.impl.ConsentInformationProvider;
import org.wso2.carbon.identity.user.export.core.internal.service.impl.SecurityInformationProvider;
import org.wso2.carbon.identity.user.export.core.internal.service.impl.UserInformationServiceImpl;
import org.wso2.carbon.identity.user.export.core.service.UserInformationProvider;
import org.wso2.carbon.identity.user.export.core.service.UserInformationService;
import org.wso2.carbon.identity.user.export.core.service.impl.LinkedAccountsProvider;
import org.wso2.carbon.identity.user.export.core.service.impl.UserProfileInformationProvider;
import org.wso2.carbon.identity.user.profile.mgt.association.federation.FederatedAssociationManager;
import org.wso2.carbon.user.core.service.RealmService;

/**
* User profile export service component.
*/
@Component(
name = "user.profile.export.service",
immediate = true
)
public class UserProfileExportServiceComponent {

private static final Log LOG = LogFactory.getLog(UserProfileExportServiceComponent.class);

@Activate
protected void activate(ComponentContext ctxt) {

try {
UserInformationServiceImpl userInformationService = new UserInformationServiceImpl();
ctxt.getBundleContext().registerService(UserInformationService.class.getName(), userInformationService,
null);

UserProfileInformationProvider userProfileInfoProvider = new UserProfileInformationProvider();
ctxt.getBundleContext().registerService(UserInformationProvider.class.getName(), userProfileInfoProvider,
null);

LinkedAccountsProvider federatedAccountsProvider = new LinkedAccountsProvider();
ctxt.getBundleContext().registerService(UserInformationProvider.class.getName(), federatedAccountsProvider,
null);

BasicUserInformationProvider basicUserInformationProvider = new BasicUserInformationProvider();
ctxt.getBundleContext().registerService(UserInformationProvider.class.getName(), basicUserInformationProvider,
null);

ConsentInformationProvider consentInformationProvider = new ConsentInformationProvider();
ctxt.getBundleContext().registerService(UserInformationProvider.class.getName(), consentInformationProvider,
null);

SecurityInformationProvider securityInformationProvider = new SecurityInformationProvider();
ctxt.getBundleContext().registerService(UserInformationProvider.class.getName(), securityInformationProvider,
null);
} catch (Exception e) {
LOG.error("Error while activating UserProfileExportServiceComponent", e);
}
}

@Deactivate
protected void deactivate(ComponentContext ctxt) {

LOG.debug("User profile export service bundle is deactivated.");
}

@Reference(
name = "user.realm.service",
service = RealmService.class,
cardinality = ReferenceCardinality.MANDATORY,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetRealmService"
)
protected void setRealmService(RealmService rlmService) {

if (rlmService != null) {
LOG.debug("Realm service initialized.");
}
UserProfileExportDataHolder.setRealmService(rlmService);
}

protected void unsetRealmService(RealmService realmService) {

UserProfileExportDataHolder.setRealmService(null);
}


@Reference(
name = "federation.association.manager.component",
service = FederatedAssociationManager.class,
cardinality = ReferenceCardinality.MANDATORY,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetFederatedAssociationManagerService"
)
protected void setFederatedAssociationManagerService(FederatedAssociationManager
federatedAssociationManagerService) {

UserProfileExportDataHolder.setFederatedAssociationManager(federatedAssociationManagerService);
}

protected void unsetFederatedAssociationManagerService(FederatedAssociationManager
federatedAssociationManagerService) {

UserProfileExportDataHolder.setFederatedAssociationManager(null);
}


@Reference(
name = "consent.manager",
service = ConsentManager.class,
cardinality = ReferenceCardinality.MANDATORY,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetConsentManager")
public void setConsentManager(ConsentManager consentManager) {

if (LOG.isDebugEnabled()) {
LOG.debug("Setting the ConsentManager Service");
}
UserProfileExportDataHolder.setConsentManager(consentManager);
}

public void unsetConsentManager(ConsentManager consentManager) {

if (LOG.isDebugEnabled()) {
LOG.debug("Unsetting the ConsentManager Service");
}
UserProfileExportDataHolder.setConsentManager(null);
}

@Reference(
name = "user.export.attribute.provider",
service = UserInformationProvider.class,
cardinality = ReferenceCardinality.MULTIPLE,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetUserAttributeProvider"
)
public void setUserAttributeProvider(UserInformationProvider userInformationProvider) {
UserProfileExportDataHolder.getUserInformationProviders().add(userInformationProvider);
}

public void unsetUserAttributeProvider(UserInformationProvider userInformationProvider) {
UserProfileExportDataHolder.getUserInformationProviders().remove(userInformationProvider);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 Inc. licenses this file to you under the Apache License,
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
Expand All @@ -19,9 +19,38 @@
package org.wso2.carbon.identity.user.export.core.internal.service.impl;

import org.wso2.carbon.identity.core.handler.AbstractIdentityHandler;
import org.wso2.carbon.identity.user.export.core.UserExportException;
import org.wso2.carbon.identity.user.export.core.internal.UserProfileExportDataHolder;
import org.wso2.carbon.identity.user.export.core.service.UserInformationProvider;
import org.wso2.carbon.user.api.UserRealm;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.core.UserStoreManager;
import org.wso2.carbon.user.core.common.AbstractUserStoreManager;
import org.wso2.carbon.user.core.service.RealmService;

public abstract class AbstractUserInformationProvider extends AbstractIdentityHandler implements
UserInformationProvider {


protected static final String WSO2_CLAIM_URI = "http://wso2.org/claims/";
protected static final String WSO2_IDENTITY_CLAIM_URI = "http://wso2.org/claims/identity/";
protected static final String WSO2_RUN_TIME_CLAIM_URI = "http://wso2.org/claims/runtime/";

protected UserStoreManager getUserStoreManager(int tenantId, String userStoreDomain) throws UserExportException {

AbstractUserStoreManager userStoreManager;
try {
RealmService realmService = UserProfileExportDataHolder.getRealmService();
UserRealm userRealm = realmService.getTenantUserRealm(tenantId);
userStoreManager = (AbstractUserStoreManager) userRealm.getUserStoreManager();
} catch (UserStoreException e) {
throw new UserExportException("Error while getting userstore", e);
}
return userStoreManager.getSecondaryUserStoreManager(userStoreDomain);
}

protected String getTenantDomain(int tenantId) throws UserStoreException {

return UserProfileExportDataHolder.getRealmService().getTenantManager().getDomain(tenantId);
}
}
Loading

0 comments on commit ab5156e

Please sign in to comment.