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

[ISSUE #12773] Fix unfriendly message when adding duplicate permissions or roles. #12915

Closed
wants to merge 1 commit into from
Closed
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 @@ -235,6 +235,12 @@ public void addRole(String role, String username) {
throw new IllegalArgumentException(
"role '" + AuthConstants.GLOBAL_ADMIN_ROLE + "' is not permitted to create!");
}

if (hasRoleWithUsername(role, username)) {
throw new IllegalArgumentException(
"user '" + username + "' already bound to the role '" + role + "' !");
}

rolePersistService.addRole(role, username);
roleSet.add(role);
}
Expand Down Expand Up @@ -296,6 +302,11 @@ public void addPermission(String role, String resource, String action) {
if (!roleSet.contains(role)) {
throw new IllegalArgumentException("role " + role + " not found!");
}

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

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

Expand Down Expand Up @@ -370,5 +381,42 @@ public boolean hasGlobalAdminRole() {
authConfigs.setHasGlobalAdminRole(hasGlobalAdminRole);
return hasGlobalAdminRole;
}


/**
* check if the user is already bound to the role.
*
* @return true if the user is already bound to the role.
*/
public boolean hasRoleWithUsername(String role, String username) {
Page<RoleInfo> roleInfoPage = rolePersistService.getRolesByUserNameAndRoleName(username,
role, DEFAULT_PAGE_NO, Integer.MAX_VALUE);
if (roleInfoPage == null) {
return false;
}
List<RoleInfo> roleInfos = roleInfoPage.getPageItems();
return CollectionUtils.isNotEmpty(roleInfos) && roleInfos.stream()
.anyMatch(roleInfo -> role.equals(roleInfo.getRole()));
}

/**
* check if the permission is already exists.
*
* @param role role name
* @param resource resource
* @param action action
* @return true if duplicate, false otherwise
*/
public boolean hasPermission(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())));
}

}
Loading