Skip to content

Commit

Permalink
[ISSUE #12773] Fix unfriendly message when adding duplicate permissio…
Browse files Browse the repository at this point in the history
…ns or binding relationship. (#12926)

* Fix unfriendly message when adding duplicate roles.

* unit test.

* correct code style.

* correct code style.

* correct code style.
  • Loading branch information
DirtyBit64 authored Dec 6, 2024
1 parent ee96df6 commit d4f5421
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
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));
}
}

0 comments on commit d4f5421

Please sign in to comment.