Skip to content

Commit

Permalink
The error message is not user-friendly when adding duplicate permission.
Browse files Browse the repository at this point in the history
  • Loading branch information
DirtyBit committed Nov 24, 2024
1 parent 7809764 commit aeafca2
Showing 1 changed file with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,11 @@ public void addPermission(String role, String resource, String action) {
if (!roleSet.contains(role)) {
throw new IllegalArgumentException("role " + role + " not found!");
}

if (hasDuplicatePermission(role, resource, action)) {
throw new IllegalArgumentException("permission already exists!");
}

permissionPersistService.addPermission(role, resource, action);
}

Expand Down Expand Up @@ -393,5 +398,27 @@ public boolean hasRoleWithUsername(@NotNull String role, @NotNull String usernam
return CollectionUtils.isNotEmpty(roleInfos) && roleInfos.stream()
.anyMatch(roleInfo -> role.equals(roleInfo.getRole()));
}

/**
* judge whether the permission is duplicate.
*
* @param role role name
* @param resource resource
* @param action action
* @return true if duplicate, false otherwise
*/
public boolean hasDuplicatePermission(String role, String resource, String action) {
List<PermissionInfo> permissionInfos = getPermissions(role);
if (CollectionUtils.isEmpty(permissionInfos)) {
return false;
}
return CollectionUtils.isNotEmpty(permissionInfos) && permissionInfos.stream()
.anyMatch(permissionInfo ->
StringUtils.equals(role, permissionInfo.getRole()) &&
StringUtils.equals(resource, permissionInfo.getResource()) &&
(StringUtils.equals(action, permissionInfo.getAction())
|| "rw".equals(permissionInfo.getAction()))
);
}

}

0 comments on commit aeafca2

Please sign in to comment.