diff --git a/components/role-mgt/org.wso2.carbon.identity.role.v2.mgt.core/src/main/java/org/wso2/carbon/identity/role/v2/mgt/core/dao/RoleDAO.java b/components/role-mgt/org.wso2.carbon.identity.role.v2.mgt.core/src/main/java/org/wso2/carbon/identity/role/v2/mgt/core/dao/RoleDAO.java index c069bc7503ff..ccb3bf9823d9 100644 --- a/components/role-mgt/org.wso2.carbon.identity.role.v2.mgt.core/src/main/java/org/wso2/carbon/identity/role/v2/mgt/core/dao/RoleDAO.java +++ b/components/role-mgt/org.wso2.carbon.identity.role.v2.mgt.core/src/main/java/org/wso2/carbon/identity/role/v2/mgt/core/dao/RoleDAO.java @@ -401,4 +401,14 @@ Map getMainRoleToSharedRoleMappingsBySubOrg(List roleIds */ List getAssociatedApplicationIdsByRoleId(String roleId, String tenantDomain) throws IdentityRoleManagementException; + + /** + * Get role audience ref id. + * + * @param audience Audience. + * @param audienceId Audience ID. + * @return audience ref id. + * @throws IdentityRoleManagementException IdentityRoleManagementException. + */ + int getRoleAudienceRefId(String audience, String audienceId) throws IdentityRoleManagementException; } diff --git a/components/role-mgt/org.wso2.carbon.identity.role.v2.mgt.core/src/main/java/org/wso2/carbon/identity/role/v2/mgt/core/dao/RoleDAOImpl.java b/components/role-mgt/org.wso2.carbon.identity.role.v2.mgt.core/src/main/java/org/wso2/carbon/identity/role/v2/mgt/core/dao/RoleDAOImpl.java index ecb2366bcd3c..438d649a782a 100644 --- a/components/role-mgt/org.wso2.carbon.identity.role.v2.mgt.core/src/main/java/org/wso2/carbon/identity/role/v2/mgt/core/dao/RoleDAOImpl.java +++ b/components/role-mgt/org.wso2.carbon.identity.role.v2.mgt.core/src/main/java/org/wso2/carbon/identity/role/v2/mgt/core/dao/RoleDAOImpl.java @@ -1725,6 +1725,34 @@ private int getRoleAudienceRefId(String audience, String audienceId, Connection return id; } + @Override + public int getRoleAudienceRefId(String audience, String audienceId) throws IdentityRoleManagementException { + + int id = -1; + try (Connection connection = IdentityDatabaseUtil.getUserDBConnection(false); + NamedPreparedStatement statement = new NamedPreparedStatement(connection, GET_ROLE_AUDIENCE_SQL)) { + + statement.setString(RoleConstants.RoleTableColumns.UM_AUDIENCE, audience); + statement.setString(RoleConstants.RoleTableColumns.UM_AUDIENCE_ID, audienceId); + try (ResultSet resultSet = statement.executeQuery()) { + while (resultSet.next()) { + id = resultSet.getInt(1); + } + // Create new audience. + if (id == -1) { + createRoleAudience(audience, audienceId, connection); + return getRoleAudienceRefId(audience, audienceId, connection); + } + } + } catch (SQLException e) { + String errorMessage = + "Error while resolving the role audiences for the given audience: " + audience + + " and audienceId : " + audienceId; + throw new IdentityRoleManagementServerException(UNEXPECTED_SERVER_ERROR.getCode(), errorMessage, e); + } + return id; + } + /** * Create role audience. * diff --git a/components/role-mgt/org.wso2.carbon.identity.role.v2.mgt.core/src/main/java/org/wso2/carbon/identity/role/v2/mgt/core/util/RoleManagementUtils.java b/components/role-mgt/org.wso2.carbon.identity.role.v2.mgt.core/src/main/java/org/wso2/carbon/identity/role/v2/mgt/core/util/RoleManagementUtils.java index 85012ac6341f..dfe5c172ad1e 100644 --- a/components/role-mgt/org.wso2.carbon.identity.role.v2.mgt.core/src/main/java/org/wso2/carbon/identity/role/v2/mgt/core/util/RoleManagementUtils.java +++ b/components/role-mgt/org.wso2.carbon.identity.role.v2.mgt.core/src/main/java/org/wso2/carbon/identity/role/v2/mgt/core/util/RoleManagementUtils.java @@ -22,6 +22,9 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.CarbonConstants; import org.wso2.carbon.identity.role.v2.mgt.core.RoleConstants; +import org.wso2.carbon.identity.role.v2.mgt.core.dao.RoleDAO; +import org.wso2.carbon.identity.role.v2.mgt.core.dao.RoleMgtDAOFactory; +import org.wso2.carbon.identity.role.v2.mgt.core.exception.IdentityRoleManagementException; import java.util.Locale; @@ -32,6 +35,8 @@ public class RoleManagementUtils { private static final Log log = LogFactory.getLog(RoleManagementUtils.class); + private static final RoleDAO roleDAO = RoleMgtDAOFactory.getInstance().getRoleDAO(); + /** * Checks whether the given role is an internal or application role. * @@ -45,4 +50,17 @@ public static boolean isHybridRole(String roleName) { roleName.toLowerCase(Locale.ENGLISH).startsWith((RoleConstants.APPLICATION_DOMAIN + CarbonConstants.DOMAIN_SEPARATOR).toLowerCase(Locale.ENGLISH)); } + + /** + * Resolve role audience ref id. + * + * @param audience Audience. + * @param audienceId Audience ID. + * @return audience ref id. + * @throws IdentityRoleManagementException IdentityRoleManagementException. + */ + public static int resolveAudienceRefId(String audience, String audienceId) throws IdentityRoleManagementException { + + return roleDAO.getRoleAudienceRefId(audience, audienceId); + } }