Skip to content

Commit

Permalink
Merge pull request #5094 from shashimalcse/methdo-to-resolve-audience…
Browse files Browse the repository at this point in the history
…-role

Add method to resolve role audience
  • Loading branch information
AnuradhaSK authored Oct 27, 2023
2 parents 3b9f58e + 66c8d9a commit 372177d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -401,4 +401,14 @@ Map<String, String> getMainRoleToSharedRoleMappingsBySubOrg(List<String> roleIds
*/
List<String> 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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.
*
Expand All @@ -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);
}
}

0 comments on commit 372177d

Please sign in to comment.