use application tenant domain as app owner tenant domain if the app owner deleted
@@ -915,6 +915,7 @@ public class ApplicationMgtUtil {
* Get user's tenant domain.
*
* @param tenantDomain The tenant domain which user is trying to access.
+ * This is the same tenant that application resides.
* @param username The username of the user.
* @return The tenant domain where the user resides.
* @throws IdentityApplicationManagementException Error when user cannot be resolved.
@@ -922,13 +923,44 @@ public class ApplicationMgtUtil {
public static String getUserTenantDomain(String tenantDomain, String username)
throws IdentityApplicationManagementException {
- if (DOMAIN_QUALIFIED_REGISTRY_SYSTEM_USERNAME.equals(username) ||
- !ApplicationManagementServiceComponentHolder.getInstance().isOrganizationManagementEnabled()) {
+ try {
+ if (useApplicationTenantDomainAsUserTenantDomain(tenantDomain, username)) {
return tenantDomain;
- } else {
- return getUser(tenantDomain, username).orElseThrow(() -> new IdentityApplicationManagementException(
- "Error resolving user.")).getTenantDomain();
}
+ Optional<User> user = getUser(tenantDomain, username);
+ if (!user.isPresent()) {
+ /*
+ This situation occur when the application creator is deleted. At that point,
+ set the tenant domain of the application as the user's tenant domain.
+ */
+ return tenantDomain;
+ }
+ return user.get().getTenantDomain();
+ } catch (UserStoreException e) {
+ throw new IdentityApplicationManagementException("Error while retrieving tenant.", e);
+ }
+ }
+
+ private static boolean useApplicationTenantDomainAsUserTenantDomain(String tenantDomain, String username)
+ throws UserStoreException {
+
+ if (MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain) ||
+ DOMAIN_QUALIFIED_REGISTRY_SYSTEM_USERNAME.equals(username) ||
+ !ApplicationManagementServiceComponentHolder.getInstance().isOrganizationManagementEnabled()) {
+ return true;
+ }
+ /*
+ If the tenant doesn't have an associated organization, return the application tenant
+ as the user's tenant domain.
+ */
+ int tenantID = IdentityTenantUtil.getTenantId(tenantDomain);
+ Tenant tenant = ApplicationManagementServiceComponentHolder.getInstance().getRealmService()
+ .getTenantManager().getTenant(tenantID);
+ String accessedOrganizationId = tenant.getAssociatedOrganizationUUID();
+ if (accessedOrganizationId == null) {
+ return true;
+ }
+ return false;
}
/**
