[release-2.19] Prevent null role while getting permissions (#6613)

This is an automated cherry-pick of #6612

/assign JohnNiang

```release-note
修复取消用户角色后无法正常渲染用户列表的问题
```
pull/6630/head v2.19.2
Halo Dev Bot 2024-09-06 22:15:52 +08:00 committed by GitHub
parent dfa10c06a4
commit 7f68de392a
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(() -> {