Skip to content

Commit

Permalink
The error message is not user-friendly when adding duplicate permissi…
Browse files Browse the repository at this point in the history
…ons. (alibaba#12273)
  • Loading branch information
zhouchunhai committed Oct 31, 2024
1 parent e2d44f2 commit 7dd3e36
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,18 @@ public Object deletePermission(@RequestParam String role, @RequestParam String r
nacosRoleService.deletePermission(role, resource, action);
return RestResultUtils.success("delete permission ok!");
}

/**
* Judge whether a permission is duplicate.
*
* @param role the role
* @param resource the related resource
* @param action the related action
* @return true if duplicate, false otherwise
*/
@GetMapping
@Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + "permissions", action = ActionTypes.READ)
public Boolean isDuplicatePermission(@RequestParam String role, @RequestParam String resource, @RequestParam String action) {
return nacosRoleService.isDuplicatePermission(role, resource, action);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -370,5 +370,28 @@ public boolean hasGlobalAdminRole() {
authConfigs.setHasGlobalAdminRole(hasGlobalAdminRole);
return hasGlobalAdminRole;
}

/**
* judge whether the permission is duplicate.
*
* @param role role name
* @param resource resource
* @param action action
* @return true if duplicate, false otherwise
*/
public Boolean isDuplicatePermission(String role, String resource, String action) {
List<PermissionInfo> permissionInfos = getPermissions(role);
if (CollectionUtils.isEmpty(permissionInfos)) {
return Boolean.FALSE;
}
for (PermissionInfo permissionInfo : permissionInfos) {
boolean resourceMatch = StringUtils.equals(resource, permissionInfo.getResource());
boolean actionMatch = StringUtils.equals(action, permissionInfo.getAction()) || "rw".equals(permissionInfo.getAction());
if (resourceMatch && actionMatch) {
return Boolean.TRUE;
}
}
return Boolean.FALSE;
}

}

0 comments on commit 7dd3e36

Please sign in to comment.