Prevent null role while getting permissions (#6612)

#### What type of PR is this?

/kind bug
/area core
/milestone 2.20.x

#### What this PR does / why we need it:

This PR filters blank role name while granting roles for an user to prevent null role in permissions.

#### Which issue(s) this PR fixes:

Fixes https://github.com/halo-dev/halo/issues/6604

#### Does this PR introduce a user-facing change?

```release-note
修复取消用户角色后无法正常渲染用户列表的问题
```
pull/6622/head
John Niang 2024-09-06 22:01:52 +08:00 committed by GitHub
parent cf2837d744
commit 93ffb7d8ea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 1 deletions

View File

@ -771,7 +771,11 @@ public class UserEndpoint implements CustomEndpoint {
.map(user -> {
var username = user.getMetadata().getName();
var roles = Optional.ofNullable(usernameRolesMap.get(username))
.map(roleNames -> roleNames.stream().map(roleMap::get).toList())
.map(roleNames -> roleNames.stream()
.map(roleMap::get)
.filter(Objects::nonNull)
.toList()
)
.orElseGet(List::of);
return new ListedUser(user, roles);
})

View File

@ -133,6 +133,7 @@ public class UserServiceImpl implements UserService {
var mutableRoles = new HashSet<>(roles);
mutableRoles.removeAll(existingRoles);
return mutableRoles.stream()
.filter(StringUtils::hasText)
.map(roleName -> RoleBinding.create(username, roleName));
}).flatMap(client::create))
.then(Mono.defer(() -> {