Skip to content

Commit

Permalink
Improve getRoles of Role v2 management service
Browse files Browse the repository at this point in the history
  • Loading branch information
ChanikaRuchini committed Dec 8, 2023
1 parent c0ce673 commit bf821b2
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,19 @@ public void preGetRoles(Integer limit, Integer offset, String sortBy, String sor
}

@Override
public void postGetRoles(List<RoleBasicInfo> roleBasicInfoList, Integer limit, Integer offset,
public void postGetRoles(List<Role> roleInfoList, Integer limit, Integer offset,
String sortBy, String sortOrder, String tenantDomain)
throws IdentityRoleManagementException {

Iterator<RoleBasicInfo> iterator = roleBasicInfoList.iterator();
Iterator<Role> iterator = roleInfoList.iterator();
while (iterator.hasNext()) {
RoleBasicInfo roleBasicInfo = iterator.next();
if (APPLICATION.equalsIgnoreCase(roleBasicInfo.getAudience())) {
String applicationName = getApplicationName(roleBasicInfo.getAudienceId(), tenantDomain);
Role roleInfo = iterator.next();
if (APPLICATION.equalsIgnoreCase(roleInfo.getAudience())) {
String applicationName = getApplicationName(roleInfo.getAudienceId(), tenantDomain);
if (applicationName == null) {
iterator.remove();
}
roleBasicInfo.setAudienceName(applicationName);
roleInfo.setAudienceName(applicationName);
}
}
}
Expand All @@ -132,13 +132,13 @@ public void preGetRoles(String filter, Integer limit, Integer offset, String sor
}

@Override
public void postGetRoles(List<RoleBasicInfo> roleBasicInfoList, String filter, Integer limit, Integer offset,
public void postGetRoles(List<Role> roleInfoList, String filter, Integer limit, Integer offset,
String sortBy, String sortOrder, String tenantDomain)
throws IdentityRoleManagementException {

for (RoleBasicInfo roleBasicInfo : roleBasicInfoList) {
if (APPLICATION.equalsIgnoreCase(roleBasicInfo.getAudience())) {
roleBasicInfo.setAudienceName(getApplicationName(roleBasicInfo.getAudienceId(), tenantDomain));
for (Role roleInfo : roleInfoList) {
if (APPLICATION.equalsIgnoreCase(roleInfo.getAudience())) {
roleInfo.setAudienceName(getApplicationName(roleInfo.getAudienceId(), tenantDomain));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,32 +55,33 @@ RoleBasicInfo addRole(String roleName, List<String> userList, List<String> group
/**
* Retrieve available roles.
*
* @param limit Limit value.
* @param offset Offset value.
* @param sortBy SortBy value.
* @param sortOrder Sort order value.
* @param tenantDomain Tenant domain.
* @param limit Limit value.
* @param offset Offset value.
* @param sortBy SortBy value.
* @param sortOrder Sort order value.
* @param tenantDomain Tenant domain.
* @param requiredAttributes Required attributes.
* @return List of roles.
* @throws IdentityRoleManagementException IdentityRoleManagementException.
*/
List<RoleBasicInfo> getRoles(Integer limit, Integer offset, String sortBy, String sortOrder, String tenantDomain)
throws IdentityRoleManagementException;
List<Role> getRoles(Integer limit, Integer offset, String sortBy, String sortOrder, String tenantDomain,
List<String> requiredAttributes) throws IdentityRoleManagementException;

/**
* Retrieve available roles.
*
* @param filter Filter for the Role ID.
* @param limit Limit value.
* @param offset Offset value.
* @param sortBy SortBy value.
* @param sortOrder Sort order value.
* @param tenantDomain Tenant domain.
* @param filter Filter for the Role ID.
* @param limit Limit value.
* @param offset Offset value.
* @param sortBy SortBy value.
* @param sortOrder Sort order value.
* @param tenantDomain Tenant domain.
* @param requiredAttributes Required attributes.
* @return List of roles.
* @throws IdentityRoleManagementException IdentityRoleManagementException.
*/
List<RoleBasicInfo> getRoles(String filter, Integer limit, Integer offset, String sortBy, String sortOrder,
String tenantDomain)
throws IdentityRoleManagementException;
List<Role> getRoles(String filter, Integer limit, Integer offset, String sortBy, String sortOrder,
String tenantDomain, List<String> requiredAttributes) throws IdentityRoleManagementException;

/**
* Retrieve the given role.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ public RoleBasicInfo addRole(String roleName, List<String> userList, List<String
}

@Override
public List<RoleBasicInfo> getRoles(Integer limit, Integer offset, String sortBy, String sortOrder,
String tenantDomain) throws IdentityRoleManagementException {
public List<Role> getRoles(Integer limit, Integer offset, String sortBy, String sortOrder, String tenantDomain,
List<String> requiredAttributes) throws IdentityRoleManagementException {

List<RoleManagementListener> roleManagementListenerList = RoleManagementServiceComponentHolder.getInstance()
.getRoleManagementListenerList();
Expand All @@ -151,23 +151,23 @@ public List<RoleBasicInfo> getRoles(Integer limit, Integer offset, String sortBy
.getInstance();
roleManagementEventPublisherProxy.publishPreGetRolesWithException(limit, offset, sortBy, sortOrder,
tenantDomain);
List<RoleBasicInfo> roleBasicInfoList = roleDAO.getRoles(limit, offset, sortBy, sortOrder, tenantDomain);
List<Role> roleList = roleDAO.getRoles(limit, offset, sortBy, sortOrder, tenantDomain, requiredAttributes);
roleManagementEventPublisherProxy.publishPostGetRoles(limit, offset, sortBy, sortOrder, tenantDomain);
for (RoleManagementListener roleManagementListener : roleManagementListenerList) {
if (roleManagementListener.isEnable()) {
roleManagementListener.postGetRoles(roleBasicInfoList, limit, offset, sortBy, sortOrder,
roleManagementListener.postGetRoles(roleList, limit, offset, sortBy, sortOrder,
tenantDomain);
}
}
if (log.isDebugEnabled()) {
log.debug(String.format("%s get roles successfully.", getUser(tenantDomain)));
}
return roleBasicInfoList;
return roleList;
}

@Override
public List<RoleBasicInfo> getRoles(String filter, Integer limit, Integer offset, String sortBy, String sortOrder,
String tenantDomain)
public List<Role> getRoles(String filter, Integer limit, Integer offset, String sortBy, String sortOrder,
String tenantDomain, List<String> requiredAttributes)
throws IdentityRoleManagementException {

List<RoleManagementListener> roleManagementListenerList = RoleManagementServiceComponentHolder.getInstance()
Expand All @@ -182,19 +182,19 @@ public List<RoleBasicInfo> getRoles(String filter, Integer limit, Integer offset
roleManagementEventPublisherProxy.publishPreGetRolesWithException(filter, limit, offset, sortBy, sortOrder,
tenantDomain);
List<ExpressionNode> expressionNodes = getExpressionNodes(filter);
List<RoleBasicInfo> roleBasicInfoList = roleDAO.getRoles(expressionNodes, limit, offset, sortBy,
sortOrder, tenantDomain);
List<Role> roleInfoList = roleDAO.getRoles(expressionNodes, limit, offset, sortBy,
sortOrder, tenantDomain, requiredAttributes);
roleManagementEventPublisherProxy.publishPostGetRoles(filter, limit, offset, sortBy, sortOrder, tenantDomain);
for (RoleManagementListener roleManagementListener : roleManagementListenerList) {
if (roleManagementListener.isEnable()) {
roleManagementListener.postGetRoles(roleBasicInfoList, filter, limit, offset, sortBy, sortOrder,
roleManagementListener.postGetRoles(roleInfoList, filter, limit, offset, sortBy, sortOrder,
tenantDomain);
}
}
if (log.isDebugEnabled()) {
log.debug(String.format("%s get filtered roles successfully.", getUser(tenantDomain)));
}
return roleBasicInfoList;
return roleInfoList;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,31 +56,34 @@ RoleBasicInfo addRole(String roleName, List<String> userList, List<String> group
/**
* Retrieve available roles.
*
* @param limit Limit value.
* @param offset Offset value.
* @param sortBy SortBy value.
* @param sortOrder Sort order value.
* @param tenantDomain Tenant domain.
* @param limit Limit value.
* @param offset Offset value.
* @param sortBy SortBy value.
* @param sortOrder Sort order value.
* @param tenantDomain Tenant domain.
* @param requiredAttributes Required attributes.
* @return List of roles.
* @throws IdentityRoleManagementException IdentityRoleManagementException.
*/
List<RoleBasicInfo> getRoles(Integer limit, Integer offset, String sortBy, String sortOrder, String tenantDomain)
List<Role> getRoles(Integer limit, Integer offset, String sortBy, String sortOrder, String tenantDomain,
List<String> requiredAttributes)
throws IdentityRoleManagementException;

/**
* Retrieve available roles matching the provided filter.
*
* @param expressionNodes List of expressionNodes.
* @param limit Limit value.
* @param offset Offset value.
* @param sortBy SortBy value.
* @param sortOrder Sort order value.
* @param tenantDomain Tenant domain.
* @param expressionNodes List of expressionNodes.
* @param limit Limit value.
* @param offset Offset value.
* @param sortBy SortBy value.
* @param sortOrder Sort order value.
* @param tenantDomain Tenant domain.
* @param requiredAttributes Required attributes.
* @return List of roles.
* @throws IdentityRoleManagementException IdentityRoleManagementException.
*/
List<RoleBasicInfo> getRoles(List<ExpressionNode> expressionNodes, Integer limit, Integer offset, String sortBy,
String sortOrder, String tenantDomain)
List<Role> getRoles(List<ExpressionNode> expressionNodes, Integer limit, Integer offset, String sortBy,
String sortOrder, String tenantDomain, List<String> requiredAttributes)
throws IdentityRoleManagementException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ public class RoleDAOImpl implements RoleDAO {
private final GroupIDResolver groupIDResolver = new GroupIDResolver();
private final UserIDResolver userIDResolver = new UserIDResolver();
private final Set<String> systemRoles = getSystemRoles();
private final String USERS = "users";
private final String GROUPS = "groups";
private final String PERMISSIONS = "permissions";
private final String ASSOCIATED_APPLICATIONS = "associatedApplications";

@Override
public RoleBasicInfo addRole(String roleName, List<String> userList, List<String> groupList,
Expand Down Expand Up @@ -280,8 +284,8 @@ public RoleBasicInfo addRole(String roleName, List<String> userList, List<String
}

@Override
public List<RoleBasicInfo> getRoles(Integer limit, Integer offset, String sortBy, String sortOrder,
String tenantDomain) throws IdentityRoleManagementException {
public List<Role> getRoles(Integer limit, Integer offset, String sortBy, String sortOrder, String tenantDomain,
List<String> requiredAttributes) throws IdentityRoleManagementException {

int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
limit = validateLimit(limit);
Expand All @@ -300,12 +304,12 @@ public List<RoleBasicInfo> getRoles(Integer limit, Integer offset, String sortBy
throw new IdentityRoleManagementServerException(UNEXPECTED_SERVER_ERROR.getCode(),
"Error while listing roles in tenantDomain: " + tenantDomain, e);
}
return roles;
return getRolesRequestedAttributes(roles, requiredAttributes, tenantDomain);
}

@Override
public List<RoleBasicInfo> getRoles(List<ExpressionNode> expressionNodes, Integer limit, Integer offset,
String sortBy, String sortOrder, String tenantDomain)
public List<Role> getRoles(List<ExpressionNode> expressionNodes, Integer limit, Integer offset, String sortBy,
String sortOrder, String tenantDomain, List<String> requiredAttributes)
throws IdentityRoleManagementException {

int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
Expand Down Expand Up @@ -335,7 +339,50 @@ public List<RoleBasicInfo> getRoles(List<ExpressionNode> expressionNodes, Intege
throw new IdentityRoleManagementServerException(RoleConstants.Error.UNEXPECTED_SERVER_ERROR.getCode(),
"Error while listing roles in tenantDomain: " + tenantDomain, e);
}
return roles;
return getRolesRequestedAttributes(roles, requiredAttributes, tenantDomain);
}

private List<Role> getRolesRequestedAttributes(List<RoleBasicInfo> roles, List<String> requiredAttributes,
String tenantDomain)
throws IdentityRoleManagementException {

List<Role> rolesList = new ArrayList();
for (RoleBasicInfo roleBasicInfo : roles) {
Role role = new Role();
role.setId(roleBasicInfo.getId());
role.setName(roleBasicInfo.getName());
role.setAudienceId(roleBasicInfo.getAudienceId());
role.setAudienceName(roleBasicInfo.getAudienceName());
role.setAudience(roleBasicInfo.getAudience());
if (requiredAttributes != null && !requiredAttributes.isEmpty()) {
if (requiredAttributes.contains(USERS)) {
role.setUsers(getUserListOfRole(roleBasicInfo.getId(), tenantDomain));
}
if (requiredAttributes.contains(GROUPS)) {
role.setGroups(getGroupListOfRole(roleBasicInfo.getId(), tenantDomain));
role.setIdpGroups(getIdpGroupListOfRole(roleBasicInfo.getId(), tenantDomain));
}
if (requiredAttributes.contains(PERMISSIONS)) {
if (isSharedRole(roleBasicInfo.getId(), tenantDomain)) {
role.setPermissions(getPermissionsOfSharedRole(roleBasicInfo.getId(), tenantDomain));
} else {
role.setPermissions(getPermissions(roleBasicInfo.getId(), tenantDomain));
}
}
if (requiredAttributes.contains(ASSOCIATED_APPLICATIONS)) {
if (ORGANIZATION.equals(roleBasicInfo.getAudience())) {
role.setAssociatedApplications(getAssociatedAppsById(roleBasicInfo.getId(), tenantDomain));
} else if (APPLICATION.equals(roleBasicInfo.getAudience())) {
List<AssociatedApplication> associatedApplications = new ArrayList<>();
associatedApplications.add(new AssociatedApplication(roleBasicInfo.getAudienceId(),
roleBasicInfo.getAudienceName()));
role.setAssociatedApplications(associatedApplications);
}
}
}
rolesList.add(role);
}
return rolesList;
}

@Override
Expand Down Expand Up @@ -3169,7 +3216,7 @@ private void deleteSharedHybridRoles(String roleId, int mainTenantId, Connection
try (ResultSet resultSet = selectStatement.executeQuery()) {
while (resultSet.next()) {
idsToDelete.add(new AbstractMap.SimpleEntry<>(
resultSet.getInt(1), resultSet.getInt(2)));
resultSet.getInt(1), resultSet.getInt(2)));
}
}
try (NamedPreparedStatement deleteStatement = new NamedPreparedStatement(connection, DELETE_SHARED_ROLE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void preGetRoles(Integer limit, Integer offset, String sortBy, String sor
}

@Override
public void postGetRoles(List<RoleBasicInfo> roleBasicInfoList, Integer limit, Integer offset, String sortBy,
public void postGetRoles(List<Role> roleBasicInfoList, Integer limit, Integer offset, String sortBy,
String sortOrder, String tenantDomain) throws IdentityRoleManagementException {

}
Expand All @@ -66,7 +66,7 @@ public void preGetRoles(String filter, Integer limit, Integer offset, String sor
}

@Override
public void postGetRoles(List<RoleBasicInfo> roleBasicInfoList, String filter, Integer limit, Integer offset,
public void postGetRoles(List<Role> roleInfoList, String filter, Integer limit, Integer offset,
String sortBy, String sortOrder, String tenantDomain)
throws IdentityRoleManagementException {

Expand Down
Loading

0 comments on commit bf821b2

Please sign in to comment.