Skip to content

Commit

Permalink
Improve user sharing service with new DB schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
sadilchamishka committed Oct 24, 2023
1 parent aae841d commit b47485e
Show file tree
Hide file tree
Showing 18 changed files with 446 additions and 352 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<parent>
<groupId>org.wso2.carbon.identity.organization.management</groupId>
<artifactId>identity-organization-management</artifactId>
<version>1.3.76-SNAPSHOT</version>
<version>1.3.89-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -48,6 +48,10 @@
<groupId>org.wso2.carbon.identity.organization.management</groupId>
<artifactId>org.wso2.carbon.identity.organization.management.ext</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.organization.management</groupId>
<artifactId>org.wso2.carbon.identity.organization.management.role.management.service</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.core</artifactId>
Expand Down Expand Up @@ -119,13 +123,17 @@
org.osgi.framework; version="${osgi.framework.imp.pkg.version.range}",
org.osgi.service.component; version="${osgi.service.component.imp.pkg.version.range}",
org.wso2.carbon.context;version="${carbon.kernel.package.import.version.range}",
org.wso2.carbon.database.utils.jdbc;version="${org.wso2.carbon.database.utils.version.range}",
org.wso2.carbon.database.utils.jdbc.exceptions;version="${org.wso2.carbon.database.utils.version.range}",
org.wso2.carbon.identity.core;version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.core.util;version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.organization.management.service; version="${org.wso2.identity.organization.mgt.core.imp.pkg.version.range}",
org.wso2.carbon.identity.organization.management.service.util;version="${org.wso2.identity.organization.mgt.imp.pkg.version.range}",
org.wso2.carbon.identity.organization.management.service.exception;version="${org.wso2.identity.organization.mgt.imp.pkg.version.range}",
org.wso2.carbon.identity.organization.management.service.model; version="${org.wso2.identity.organization.mgt.core.imp.pkg.version.range}",
org.wso2.carbon.identity.organization.management.service.constant;version="${org.wso2.identity.organization.mgt.core.imp.pkg.version.range}",
org.wso2.carbon.identity.organization.management.role.management.service;version="${org.wso2.identity.organization.mgt.imp.pkg.version.range}",
org.wso2.carbon.identity.organization.management.role.management.service.models;version="${org.wso2.identity.organization.mgt.imp.pkg.version.range}",
org.wso2.carbon.user.api;version="${carbon.user.api.imp.pkg.version.range}",
org.wso2.carbon.user.core;version="${carbon.kernel.package.import.version.range}",
org.wso2.carbon.user.core.common;version="${carbon.kernel.package.import.version.range}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

package org.wso2.carbon.identity.organization.management.organization.user.sharing;

import org.wso2.carbon.identity.organization.management.organization.user.sharing.models.SharedUserAssociation;
import org.wso2.carbon.identity.organization.management.organization.user.sharing.models.UserAssociation;
import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException;

/**
Expand All @@ -27,57 +27,55 @@
public interface OrganizationUserSharingService {

/**
* Creates the association between the shared user and the actual user in the shared organization.
* Creates the association between the shared user and the actual user in the organization.
*
* @param realUserId Actual user ID of the user in the parent organization.
* @param userResidentOrgId The organization ID where the user's identity is managed.
* @param sharedOrgId Organization ID of the user shared organization.
* @param orgId Organization ID of the user is shared.
* @param associatedUserId Actual user who is associated for a shared user.
* @param associatedOrgId The organization ID associated user.
* @throws OrganizationManagementException If an error occurs while creating the organization user association.
*/
void shareOrganizationUser(String realUserId, String userResidentOrgId, String sharedOrgId)
void shareOrganizationUser(String orgId, String associatedUserId, String associatedOrgId)
throws OrganizationManagementException;

/**
* UnShare all the shared users for the given user.
*
* @param realUserId The ID of the user.
* @param userResidentOrgId The ID of the organization where the user is managed.
* @return True if the organization user associations are deleted successfully.
* @throws OrganizationManagementException If an error occurs while deleting the organization user associations.
* @param associatedUserId The ID of the associated user.
* @param associatedOrgId The ID of the organization where the user is managed.
* @return True if the user associations are deleted successfully.
* @throws OrganizationManagementException If an error occurs while deleting the user associations.
*/
boolean unShareOrganizationUsers(String realUserId, String userResidentOrgId)
boolean unShareOrganizationUsers(String associatedUserId, String associatedOrgId)
throws OrganizationManagementException;

/**
* Delete the organization user association of the shared user.
*
* @param sharedUserId The ID of the shared user.
* @param userResidentOrgId The ID of organization where the user's identity is managed.
* @param userId The ID of the user.
* @param associatedOrgId The ID of organization where the user's identity is managed.
* @return True if the organization user association is deleted successfully.
* @throws OrganizationManagementException If an error occurs while deleting the organization user association.
*/
boolean deleteOrganizationUserAssociationOfSharedUser(String sharedUserId, String userResidentOrgId)
throws OrganizationManagementException;
boolean deleteUserAssociation(String userId, String associatedOrgId) throws OrganizationManagementException;

/**
* Get the shared user association of the user.
* Get the user association of the associated user in a given organization.
*
* @param realUserId The actual ID of the user.
* @param sharedOrganizationId The organization ID of the user.
* @return The shared user association of the user.
* @throws OrganizationManagementException If an error occurs while retrieving the shared user association.
* @param associatedUserId The ID of the user who is associated to the organization.
* @param orgId The organization ID of the user.
* @return The user association of the associated user within a given organization.
* @throws OrganizationManagementException If an error occurs while retrieving the user association.
*/
SharedUserAssociation getSharedUserAssociationOfUser(String realUserId, String sharedOrganizationId)
UserAssociation getUserAssociationOfAssociatedUserByOrgId(String associatedUserId, String orgId)
throws OrganizationManagementException;

/**
* Get the shared user association of a shared user.
* Get the user association of a user.
*
* @param sharedUserId The user ID of the shared user.
* @param sharedOrganizationId The organization ID of the user.
* @return The shared user association of the shared user.
* @throws OrganizationManagementException If an error occurs while retrieving the shared user association.
* @param userId The ID of user.
* @param orgId The organization ID of the user.
* @return The user association of the user.
* @throws OrganizationManagementException If an error occurs while retrieving the user association.
*/
SharedUserAssociation getSharedUserAssociationOfSharedUser(String sharedUserId, String sharedOrganizationId)
throws OrganizationManagementException;
UserAssociation getUserAssociation(String userId, String orgId) throws OrganizationManagementException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.wso2.carbon.identity.organization.management.organization.user.sharing.dao.OrganizationUserSharingDAO;
import org.wso2.carbon.identity.organization.management.organization.user.sharing.dao.OrganizationUserSharingDAOImpl;
import org.wso2.carbon.identity.organization.management.organization.user.sharing.internal.OrganizationUserSharingDataHolder;
import org.wso2.carbon.identity.organization.management.organization.user.sharing.models.SharedUserAssociation;
import org.wso2.carbon.identity.organization.management.organization.user.sharing.models.UserAssociation;
import org.wso2.carbon.identity.organization.management.service.OrganizationManager;
import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException;
import org.wso2.carbon.user.api.UserRealm;
Expand Down Expand Up @@ -50,76 +50,71 @@ public class OrganizationUserSharingServiceImpl implements OrganizationUserShari
private final OrganizationUserSharingDAO organizationUserSharingDAO = new OrganizationUserSharingDAOImpl();

@Override
public void shareOrganizationUser(String realUserId, String userResidentOrgId, String sharedOrgId)
public void shareOrganizationUser(String orgId, String associatedUserId, String associatedOrgId)
throws OrganizationManagementException {

try {
int userResidentTenantId =
IdentityTenantUtil.getTenantId(getOrganizationManager().resolveTenantDomain(userResidentOrgId));
AbstractUserStoreManager userStoreManager = getAbstractUserStoreManager(userResidentTenantId);
String userName = userStoreManager.getUser(realUserId, null).getUsername();
int associatedUserTenantId =
IdentityTenantUtil.getTenantId(getOrganizationManager().resolveTenantDomain(associatedOrgId));
AbstractUserStoreManager userStoreManager = getAbstractUserStoreManager(associatedUserTenantId);
String userName = userStoreManager.getUser(associatedUserId, null).getUsername();

HashMap<String, String> userClaims = new HashMap<>();
userClaims.put(CLAIM_MANAGED_ORGANIZATION, userResidentOrgId);
userClaims.put(CLAIM_MANAGED_ORGANIZATION, associatedOrgId);
userClaims.put(ID_CLAIM_READ_ONLY, "true");
UserCoreUtil.setSkipPasswordPatternValidationThreadLocal(true);

int sharedOrgTenantId =
IdentityTenantUtil.getTenantId(getOrganizationManager().resolveTenantDomain(sharedOrgId));
userStoreManager = getAbstractUserStoreManager(sharedOrgTenantId);

userName = "sub-" + userName;
int tenantId = IdentityTenantUtil.getTenantId(getOrganizationManager().resolveTenantDomain(orgId));
userStoreManager = getAbstractUserStoreManager(tenantId);
userStoreManager.addUser(userName, generatePassword(), null, userClaims, DEFAULT_PROFILE);
String sharedUserId = userStoreManager.getUserIDFromUserName(userName);
organizationUserSharingDAO.createOrganizationUserAssociation(realUserId, userResidentOrgId, sharedUserId,
sharedOrgId);
String userId = userStoreManager.getUserIDFromUserName(userName);
organizationUserSharingDAO.createOrganizationUserAssociation(userId, orgId, associatedUserId,
associatedOrgId);
} catch (UserStoreException e) {
throw handleServerException(ERROR_CODE_ERROR_CREATE_SHARED_USER, e, sharedOrgId);
throw handleServerException(ERROR_CODE_ERROR_CREATE_SHARED_USER, e, orgId);
}
}

@Override
public boolean unShareOrganizationUsers(String realUserId, String userResidentOrgId)
public boolean unShareOrganizationUsers(String associatedUserId, String associatedOrgId)
throws OrganizationManagementException {

List<SharedUserAssociation> sharedUserAssociationList =
organizationUserSharingDAO.getOrganizationUserAssociationsOfUser(realUserId, userResidentOrgId);
List<UserAssociation> userAssociationList =
organizationUserSharingDAO.getUserAssociationsOfAssociatedUser(associatedUserId, associatedOrgId);
// Removing the shared users from the shared organizations.
for (SharedUserAssociation sharedUserAssociation : sharedUserAssociationList) {
String sharedOrganizationId = sharedUserAssociation.getSharedOrganizationId();
String tenantDomain = getOrganizationManager().resolveTenantDomain(sharedOrganizationId);
for (UserAssociation userAssociation : userAssociationList) {
String organizationId = userAssociation.getOrganizationId();
String tenantDomain = getOrganizationManager().resolveTenantDomain(organizationId);
int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
try {
AbstractUserStoreManager sharedOrgUserStoreManager = getAbstractUserStoreManager(tenantId);
sharedOrgUserStoreManager.deleteUserWithID(sharedUserAssociation.getSharedUserId());
sharedOrgUserStoreManager.deleteUserWithID(userAssociation.getUserId());
} catch (UserStoreException e) {
throw handleServerException(ERROR_CODE_ERROR_DELETE_SHARED_USER, e,
sharedUserAssociation.getSharedUserId(), sharedOrganizationId);
userAssociation.getUserId(), organizationId);
}
}
return true;
}

@Override
public boolean deleteOrganizationUserAssociationOfSharedUser(String sharedUserId, String userResidentOrgId)
throws OrganizationManagementException {
public boolean deleteUserAssociation(String userId, String associatedOrgId) throws OrganizationManagementException {

return organizationUserSharingDAO.deleteOrganizationUserAssociationOfSharedUser(sharedUserId,
userResidentOrgId);
return organizationUserSharingDAO.deleteUserAssociationOfUserByAssociatedOrg(userId, associatedOrgId);
}

@Override
public SharedUserAssociation getSharedUserAssociationOfUser(String realUserId, String sharedOrganizationId)
public UserAssociation getUserAssociationOfAssociatedUserByOrgId(String associatedUserId, String orgId)
throws OrganizationManagementException {

return organizationUserSharingDAO.getOrganizationUserAssociation(realUserId, sharedOrganizationId);
return organizationUserSharingDAO.getUserAssociationOfAssociatedUserByOrgId(associatedUserId, orgId);
}

@Override
public SharedUserAssociation getSharedUserAssociationOfSharedUser(String sharedUserId, String sharedOrganizationId)
public UserAssociation getUserAssociation(String sharedUserId, String sharedOrganizationId)
throws OrganizationManagementException {

return organizationUserSharingDAO.getSharedUserAssociationOfSharedUser(sharedUserId, sharedOrganizationId);
return organizationUserSharingDAO.getUserAssociation(sharedUserId, sharedOrganizationId);
}

private AbstractUserStoreManager getAbstractUserStoreManager(int tenantId) throws UserStoreException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,31 @@
*/
public class SQLConstants {

public static final String CREATE_ORGANIZATION_USER_ASSOCIATION = "INSERT INTO IDN_ORG_USER_ASSOCIATION(" +
"SHARED_USER_ID, SUB_ORG_ID, REAL_USER_ID, USER_RESIDENT_ORG_ID) VALUES(?, ?, ?, ?)";
public static final String CREATE_ORGANIZATION_USER_ASSOCIATION = "INSERT INTO UM_ORG_USER_ASSOCIATION(" +
"UM_USER_ID, UM_ORG_ID, UM_ASSOCIATED_USER_ID, UM_ASSOCIATED_ORG_ID) VALUES(?, ?, ?, ?)";
public static final String DELETE_ORGANIZATION_USER_ASSOCIATION_FOR_SHARED_USER = "DELETE FROM " +
"IDN_ORG_USER_ASSOCIATION WHERE SHARED_USER_ID = ? AND USER_RESIDENT_ORG_ID = ?";
public static final String DELETE_ORGANIZATION_USER_ASSOCIATIONS_FOR_USER = "DELETE FROM " +
"IDN_ORG_USER_ASSOCIATION WHERE REAL_USER_ID = ? AND USER_RESIDENT_ORG_ID = ?";
public static final String GET_ORGANIZATION_USER_ASSOCIATIONS_FOR_USER = "SELECT SHARED_USER_ID, SUB_ORG_ID " +
"FROM IDN_ORG_USER_ASSOCIATION WHERE REAL_USER_ID = ? AND USER_RESIDENT_ORG_ID = ?";
public static final String GET_ORGANIZATION_USER_ASSOCIATION_FOR_USER_AT_SHARED_ORG = "SELECT SHARED_USER_ID, " +
"SUB_ORG_ID FROM IDN_ORG_USER_ASSOCIATION WHERE REAL_USER_ID = ? AND SUB_ORG_ID = ?";
"UM_ORG_USER_ASSOCIATION WHERE UM_USER_ID = ? AND UM_ASSOCIATED_ORG_ID = ?";
public static final String DELETE_ORGANIZATION_USER_ASSOCIATIONS_FOR_ROOT_USER = "DELETE FROM " +
"UM_ORG_USER_ASSOCIATION WHERE UM_ASSOCIATED_USER_ID = ? AND UM_ASSOCIATED_ORG_ID = ?";
public static final String GET_ORGANIZATION_USER_ASSOCIATIONS_FOR_USER = "SELECT UM_USER_ID, UM_ORG_ID, " +
"UM_ASSOCIATED_USER_ID, UM_ASSOCIATED_ORG_ID " +
"FROM UM_ORG_USER_ASSOCIATION WHERE UM_ASSOCIATED_USER_ID = ? AND UM_ASSOCIATED_ORG_ID = ?";
public static final String GET_ORGANIZATION_USER_ASSOCIATION_FOR_ROOT_USER_IN_ORG = "SELECT UM_USER_ID, " +
"UM_ORG_ID, UM_ASSOCIATED_USER_ID, UM_ASSOCIATED_ORG_ID FROM UM_ORG_USER_ASSOCIATION " +
"WHERE UM_ASSOCIATED_USER_ID = ? AND UM_ORG_ID = ?";

public static final String GET_ORGANIZATION_USER_ASSOCIATIONS_FOR_SHARED_USER = "SELECT REAL_USER_ID, " +
"USER_RESIDENT_ORG_ID FROM IDN_ORG_USER_ASSOCIATION WHERE SHARED_USER_ID = ? AND SUB_ORG_ID = ?";
public static final String GET_ORGANIZATION_USER_ASSOCIATIONS_FOR_SHARED_USER = "SELECT UM_USER_ID, UM_ORG_ID, " +
"UM_ASSOCIATED_USER_ID, UM_ASSOCIATED_ORG_ID FROM UM_ORG_USER_ASSOCIATION " +
"WHERE UM_USER_ID = ? AND UM_ORG_ID = ?";
/**
* SQL placeholders related to organization user sharing SQL operations.
*/
public static final class SQLPlaceholders {

public static final String COLUMN_NAME_SHARED_USER_ID = "SHARED_USER_ID";
public static final String COLUMN_NAME_SUB_ORG_ID = "SUB_ORG_ID";
public static final String COLUMN_NAME_REAL_USER_ID = "REAL_USER_ID";
public static final String COLUMN_NAME_USER_RESIDENT_ORG_ID = "USER_RESIDENT_ORG_ID";
public static final String COLUMN_NAME_USER_ID = "UM_USER_ID";
public static final String COLUMN_NAME_ORG_ID = "UM_ORG_ID";
public static final String COLUMN_NAME_ASSOCIATED_USER_ID = "UM_ASSOCIATED_USER_ID";
public static final String COLUMN_NAME_ASSOCIATED_ORG_ID = "UM_ASSOCIATED_ORG_ID";
}

}
Loading

0 comments on commit b47485e

Please sign in to comment.