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 binding relationship. #12926

Merged
merged 6 commits into from
Dec 6, 2024
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 @@ -1307,8 +1307,7 @@ void testBuildFindConfigInfoStateSql() {
TableConstant.CONFIG_INFO);
String select = configInfoMapper.select(Arrays.asList("id", "data_id", "group_id", "tenant_id", "gmt_modified"),
Arrays.asList("data_id", "group_id", "tenant_id"));
assertEquals(
"SELECT id,data_id,group_id,tenant_id,gmt_modified FROM config_info WHERE data_id = ? AND group_id = ? AND tenant_id = ?",
assertEquals("SELECT id,data_id,group_id,tenant_id,gmt_modified FROM config_info WHERE data_id = ? AND group_id = ? AND tenant_id = ?",
select);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,12 @@ public void addRole(String role, String username) {
throw new IllegalArgumentException(
"role '" + AuthConstants.GLOBAL_ADMIN_ROLE + "' is not permitted to create!");
}

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

rolePersistService.addRole(role, username);
roleSet.add(role);
}
Expand Down Expand Up @@ -394,5 +400,23 @@ public Result<Boolean> isDuplicatePermission(String role, String resource, Strin
}
return Result.success(Boolean.FALSE);
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,15 @@ void duplicatePermission() {
when(spy.getPermissions("admin")).thenReturn(permissionInfos);
spy.isDuplicatePermission("admin", "test", "r");
}

@Test
void isUserBoundToRole() {
String role = "TEST";
String userName = "nacos";
assertFalse(nacosRoleService.isUserBoundToRole("", userName));
assertFalse(nacosRoleService.isUserBoundToRole(role, ""));
assertFalse(nacosRoleService.isUserBoundToRole("", null));
assertFalse(nacosRoleService.isUserBoundToRole(null, ""));
assertFalse(nacosRoleService.isUserBoundToRole(role, userName));
}
}
Loading