Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add deletable flag to admin-ui role object #888 #900

Merged
merged 3 commits into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
public class AdminRole {
private String role;
private String description;
private Boolean deletable;

public String getRole() {
return role;
Expand All @@ -22,6 +23,13 @@ public void setDescription(String description) {
this.description = description;
}

public Boolean getDeletable() {
return deletable;
}

public void setDeletable(Boolean deletable) {
this.deletable = deletable;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -40,6 +48,7 @@ public String toString() {
return "AdminRole{" +
"role='" + role + '\'' +
", description='" + description + '\'' +
", deletable='" + deletable + '\'' +
'}';
}
}
3 changes: 3 additions & 0 deletions jans-config-api/docs/jans-config-api-swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6471,6 +6471,9 @@ components:
description:
type: string
description: role description
deletable:
type: boolean
description: can we delete the role?
AdminPermission:
type: object
description: Admin permission
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ public List<AdminRole> getRoles() throws ApplicationException {
}
}

private AdminRole getRoleObjByName(String role) throws ApplicationException {
try {
AdminConf adminConf = entryManager.find(AdminConf.class, CONFIG_DN);
List<AdminRole> roles = adminConf.getDynamic().getRoles().stream().filter(ele -> ele.getRole().equals(role)).collect(Collectors.toList());
if (roles.isEmpty()) {
log.error(ErrorResponse.ROLE_NOT_FOUND.getDescription());
throw new ApplicationException(Response.Status.BAD_REQUEST.getStatusCode(), ErrorResponse.ROLE_NOT_FOUND.getDescription());
}
return roles.get(0);
} catch (ApplicationException e) {
log.error(ErrorResponse.GET_ADMIUI_ROLES_ERROR.getDescription());
throw e;
} catch (Exception e) {
log.error(ErrorResponse.GET_ADMIUI_ROLES_ERROR.getDescription(), e);
throw new ApplicationException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ErrorResponse.GET_ADMIUI_ROLES_ERROR.getDescription());
}
}

public List<AdminRole> addRole(AdminRole roleArg) throws ApplicationException {
try {
AdminConf adminConf = entryManager.find(AdminConf.class, CONFIG_DN);
Expand Down Expand Up @@ -103,6 +121,11 @@ public List<AdminRole> deleteRole(String role) throws ApplicationException {
}

List<AdminRole> roles = adminConf.getDynamic().getRoles();
if (isFalse(getRoleObjByName(role).getDeletable())) {
log.error(ErrorResponse.ROLE_MARKED_UNDELETABLE.getDescription());
throw new ApplicationException(Response.Status.BAD_REQUEST.getStatusCode(), ErrorResponse.ROLE_MARKED_UNDELETABLE.getDescription());
}

roles.removeIf(ele -> ele.getRole().equals(role));

adminConf.getDynamic().setRoles(roles);
Expand Down Expand Up @@ -287,13 +310,20 @@ public List<RolePermissionMapping> mapPermissionsToRole(RolePermissionMapping ro
public List<RolePermissionMapping> removePermissionsFromRole(RolePermissionMapping rolePermissionMappingArg) throws ApplicationException {
try {
AdminConf adminConf = entryManager.find(AdminConf.class, CONFIG_DN);
if (isFalse(getRoleObjByName(role).getDeletable())) {
log.error(ErrorResponse.ROLE_MARKED_UNDELETABLE.getDescription());
throw new ApplicationException(Response.Status.BAD_REQUEST.getStatusCode(), ErrorResponse.ROLE_MARKED_UNDELETABLE.getDescription());
}
List<RolePermissionMapping> roleScopeMapping = adminConf.getDynamic().getRolePermissionMapping()
.stream().filter(ele -> !ele.getRole().equalsIgnoreCase(rolePermissionMappingArg.getRole()))
.collect(Collectors.toList());
adminConf.getDynamic().setRolePermissionMapping(roleScopeMapping);
entryManager.merge(adminConf);

return adminConf.getDynamic().getRolePermissionMapping();
} catch (ApplicationException e) {
log.error(ErrorResponse.ERROR_IN_DELETING_ROLE_PERMISSION.getDescription());
throw e;
} catch (Exception e) {
log.error(ErrorResponse.ERROR_IN_DELETING_ROLE_PERMISSION.getDescription(), e);
throw new ApplicationException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ErrorResponse.ERROR_IN_DELETING_ROLE_PERMISSION.getDescription());
Expand Down Expand Up @@ -321,4 +351,11 @@ private void validateRolePermissionMapping(AdminConf adminConf, RolePermissionMa
throw new ApplicationException(Response.Status.BAD_REQUEST.getStatusCode(), ErrorResponse.PERMISSION_NOT_FOUND.getDescription());
}
}

private static boolean isFalse(Boolean bool) {
if (bool == null) {
return true;
}
return bool.booleanValue() ? false : true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public enum ErrorResponse {
SAVE_ADMIUI_PERMISSIONS_ERROR("Error in saving Admin UI permissions."),
EDIT_ADMIUI_PERMISSIONS_ERROR("Error in editing Admin UI permissions."),
DELETE_ADMIUI_PERMISSIONS_ERROR("Error in deleting Admin UI permissions."),
ROLE_MARKED_UNDELETABLE("Role cannot be deleted. Please set ‘deletable’ property of role to true."),
UNABLE_TO_DELETE_ROLE_MAPPED_TO_PERMISSIONS("Role is mapped to permissions so cannot be deleted. Please remove the permissions mapped before deleting the role."),
UNABLE_TO_DELETE_PERMISSION_MAPPED_TO_ROLE("Permission is mapped to role so cannot be deleted. Please remove the permission mapped to the role before deleting it.");

Expand Down