Skip to content

Commit

Permalink
use new util method to check whether the tenant is an org
Browse files Browse the repository at this point in the history
  • Loading branch information
AnuradhaSK committed Oct 17, 2023
1 parent 3a78578 commit 120d6fe
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.core.util.IdentityUtil;
import org.wso2.carbon.identity.organization.management.service.OrganizationManager;
import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException;
import org.wso2.carbon.identity.organization.management.service.util.OrganizationManagementUtil;
import org.wso2.carbon.identity.role.v2.mgt.core.AssociatedApplication;
import org.wso2.carbon.identity.role.v2.mgt.core.GroupBasicInfo;
import org.wso2.carbon.identity.role.v2.mgt.core.IdentityRoleManagementException;
Expand All @@ -38,13 +38,11 @@
import org.wso2.carbon.identity.role.v2.mgt.core.RoleManagementService;
import org.wso2.carbon.identity.role.v2.mgt.core.UserBasicInfo;
import org.wso2.carbon.identity.role.v2.mgt.core.util.UserIDResolver;
import org.wso2.carbon.identity.scim2.common.internal.SCIMCommonComponentHolder;
import org.wso2.carbon.identity.scim2.common.utils.SCIMCommonConstants;
import org.wso2.carbon.identity.scim2.common.utils.SCIMCommonUtils;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.core.UserCoreConstants;
import org.wso2.carbon.user.core.common.AbstractUserStoreManager;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import org.wso2.charon3.core.exceptions.BadRequestException;
import org.wso2.charon3.core.exceptions.CharonException;
import org.wso2.charon3.core.exceptions.ConflictException;
Expand Down Expand Up @@ -76,7 +74,6 @@
import java.util.stream.Collectors;

import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_ORGANIZATION_NOT_FOUND_FOR_TENANT;
import static org.wso2.carbon.identity.role.v2.mgt.core.RoleConstants.Error.INVALID_PERMISSION;
import static org.wso2.carbon.identity.role.v2.mgt.core.RoleConstants.Error.OPERATION_FORBIDDEN;
import static org.wso2.carbon.identity.role.v2.mgt.core.RoleConstants.Error.ROLE_NOT_FOUND;
Expand Down Expand Up @@ -112,7 +109,7 @@ public RoleV2 createRole(RoleV2 role)

try {
if (!isRoleModificationAllowedForTenant(tenantDomain)) {
throw new BadRequestException("Role creation is not allowed for sub-organizations.",
throw new BadRequestException("Role creation is not allowed for organizations.",
ResponseCodeConstants.INVALID_VALUE);
}
// Check if the role already exists.
Expand Down Expand Up @@ -271,7 +268,7 @@ public void deleteRole(String roleID) throws CharonException, NotFoundException,

try {
if (!isRoleModificationAllowedForTenant(tenantDomain)) {
throw new BadRequestException("Role deletion is not allowed for sub-organizations.",
throw new BadRequestException("Role deletion is not allowed for organizations.",
ResponseCodeConstants.INVALID_VALUE);
}
roleManagementService.deleteRole(roleID, tenantDomain);
Expand Down Expand Up @@ -364,15 +361,15 @@ public RoleV2 patchRole(String roleId, Map<String, List<PatchOperation>> patchOp

if (CollectionUtils.isNotEmpty(displayNameOperations)) {
if (!isRoleModificationAllowedForTenant(tenantDomain)) {
throw new BadRequestException("Role name modification is not allowed for sub-organizations.",
throw new BadRequestException("Role name modification is not allowed for organizations.",
ResponseCodeConstants.INVALID_VALUE);
}
String newRoleName = (String) displayNameOperations.get(displayNameOperations.size() - 1).getValues();
updateRoleName(roleId, currentRoleName, newRoleName);
}
if (CollectionUtils.isNotEmpty(permissionOperations)) {
if (!isRoleModificationAllowedForTenant(tenantDomain)) {
throw new BadRequestException("Role's permission change is not allowed for sub-organizations.",
throw new BadRequestException("Role's permission change is not allowed for organizations.",
ResponseCodeConstants.INVALID_VALUE);
}
updatePermissions(roleId, permissionOperations);
Expand Down Expand Up @@ -568,7 +565,7 @@ private void doUpdateRoleName(RoleV2 oldRole, RoleV2 newRole)
// Update role name.
try {
if (!isRoleModificationAllowedForTenant(tenantDomain)) {
throw new BadRequestException("Role name update is not allowed for sub-organizations.",
throw new BadRequestException("Role name update is not allowed for organizations.",
ResponseCodeConstants.INVALID_VALUE);
}
roleManagementService.updateRoleName(oldRole.getId(), newRoleDisplayName, tenantDomain);
Expand Down Expand Up @@ -692,7 +689,7 @@ private void doUpdatePermissions(RoleV2 oldRole, RoleV2 newRole) throws BadReque
// Update the role with added permissions and deleted permissions.
if (isNotEmpty(deletePermissionValuesList) || isNotEmpty(addedPermissionValuesList)) {
if (!isRoleModificationAllowedForTenant(tenantDomain)) {
throw new BadRequestException("Role's permission modification is not allowed for sub-organizations.",
throw new BadRequestException("Role's permission modification is not allowed for organizations.",
ResponseCodeConstants.INVALID_VALUE);
}
if (LOG.isDebugEnabled()) {
Expand Down Expand Up @@ -1133,29 +1130,9 @@ private boolean isUsersAttributeRequired(Map<String, Boolean> requiredAttributes
private boolean isRoleModificationAllowedForTenant(String tenantDomain) throws CharonException {

try {
if (MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
return true;
}
String organizationId = getOrganizationManager().resolveOrganizationId(tenantDomain);
if (StringUtils.isBlank(organizationId)) {
// There couldn't be any sub-org without an org id. Therefore, returning true.
return true;
}
return getOrganizationManager().isPrimaryOrganization(organizationId);
return !OrganizationManagementUtil.isOrganization(tenantDomain);
} catch (OrganizationManagementException e) {
if (ERROR_CODE_ORGANIZATION_NOT_FOUND_FOR_TENANT.getCode().equals(e.getErrorCode())) {
if (LOG.isDebugEnabled()) {
LOG.debug("Organization not found for the tenant: " + tenantDomain);
}
// There couldn't be any sub-org without an org id. Therefore, returning true.
return true;
}
throw new CharonException("Error while checking whether the organization is primary.", e);
throw new CharonException("Error while checking whether the tenant is an organization.", e);
}
}

private static OrganizationManager getOrganizationManager() {

return SCIMCommonComponentHolder.getOrganizationManager();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.wso2.carbon.identity.core.util.IdentityCoreInitializedEvent;
import org.wso2.carbon.identity.core.util.IdentityUtil;
import org.wso2.carbon.identity.event.handler.AbstractEventHandler;
import org.wso2.carbon.identity.organization.management.service.OrganizationManager;
import org.wso2.carbon.identity.role.mgt.core.RoleManagementService;
import org.wso2.carbon.identity.scim2.common.extenstion.SCIMUserStoreErrorResolver;
import org.wso2.carbon.identity.scim2.common.handlers.SCIMClaimOperationEventHandler;
Expand Down Expand Up @@ -316,21 +315,6 @@ protected void unsetScimUserStoreErrorResolver(SCIMUserStoreErrorResolver scimUs
SCIMCommonComponentHolder.removeScimUserStoreErrorResolver(scimUserStoreErrorResolver);
}

@Reference(name = "identity.organization.management.component",
service = OrganizationManager.class,
cardinality = ReferenceCardinality.MANDATORY,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetOrganizationManager")
protected void setOrganizationManager(OrganizationManager organizationManager) {

SCIMCommonComponentHolder.setOrganizationManager(organizationManager);
}

protected void unsetOrganizationManager(OrganizationManager organizationManager) {

SCIMCommonComponentHolder.setOrganizationManager(null);
}

@Deactivate
protected void deactivate(ComponentContext context) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.wso2.carbon.identity.scim2.common.internal;

import org.wso2.carbon.identity.claim.metadata.mgt.ClaimMetadataManagementService;
import org.wso2.carbon.identity.organization.management.service.OrganizationManager;
import org.wso2.carbon.identity.scim2.common.extenstion.SCIMUserStoreErrorResolver;
import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.user.mgt.RolePermissionManagementService;
Expand All @@ -40,7 +39,6 @@ public class SCIMCommonComponentHolder {
private static RolePermissionManagementService rolePermissionManagementService;
private static RoleManagementService roleManagementService;
private static org.wso2.carbon.identity.role.v2.mgt.core.RoleManagementService roleManagementServiceV2;
private static OrganizationManager organizationManager;
private static final List<SCIMUserStoreErrorResolver> scimUserStoreErrorResolvers = new ArrayList<>();

/**
Expand Down Expand Up @@ -160,24 +158,4 @@ public static void removeScimUserStoreErrorResolver(SCIMUserStoreErrorResolver s
scimUserStoreErrorResolvers.remove(scimUserStoreErrorResolver);
scimUserStoreErrorResolvers.sort(Comparator.comparing(SCIMUserStoreErrorResolver::getOrder).reversed());
}

/**
* Get organization manager.
*
* @return OrganizationManager.
*/
public static OrganizationManager getOrganizationManager() {

return organizationManager;
}

/**
* Set organization manager.
*
* @param organizationManager OrganizationManager.
*/
public static void setOrganizationManager(OrganizationManager organizationManager) {

SCIMCommonComponentHolder.organizationManager = organizationManager;
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@
<commons.lang.version>20030203.000129</commons.lang.version>
<identity.governance.version>1.8.12</identity.governance.version>
<charon.version>4.0.10</charon.version>
<org.wso2.carbon.identity.organization.management.core.version>1.0.65
<org.wso2.carbon.identity.organization.management.core.version>1.0.70
</org.wso2.carbon.identity.organization.management.core.version>
<!--Maven Plugin Version-->
<maven.compiler.plugin.version>2.3.1</maven.compiler.plugin.version>
Expand Down

0 comments on commit 120d6fe

Please sign in to comment.