Skip to content

Commit

Permalink
BE: RBAC: Add missing RBAC action, fix possible exceptions on unknown…
Browse files Browse the repository at this point in the history
… actions (#3810)
  • Loading branch information
Haarolean committed May 11, 2023
1 parent 1c35ded commit fdd9ad9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
import org.springframework.security.core.context.SecurityContext;
Expand All @@ -23,15 +26,12 @@

@RestController

This comment has been minimized.

Copy link
@zece14zece

zece14zece Jun 21, 2023

лол

@RequiredArgsConstructor
@Slf4j
public class AccessController implements AuthorizationApi {

private final AccessControlService accessControlService;

public Mono<ResponseEntity<AuthenticationInfoDTO>> getUserAuthInfo(ServerWebExchange exchange) {
AuthenticationInfoDTO dto = new AuthenticationInfoDTO();
dto.setRbacEnabled(accessControlService.isRbacEnabled());
UserInfoDTO userInfo = new UserInfoDTO();

Mono<List<UserPermissionDTO>> permissions = accessControlService.getUser()
.map(user -> accessControlService.getRoles()
.stream()
Expand All @@ -49,13 +49,11 @@ public Mono<ResponseEntity<AuthenticationInfoDTO>> getUserAuthInfo(ServerWebExch
return userName
.zipWith(permissions)
.map(data -> {
userInfo.setUsername(data.getT1());
userInfo.setPermissions(data.getT2());

dto.setUserInfo(userInfo);
var dto = new AuthenticationInfoDTO(accessControlService.isRbacEnabled());
dto.setUserInfo(new UserInfoDTO(data.getT1(), data.getT2()));
return dto;
})
.switchIfEmpty(Mono.just(dto))
.switchIfEmpty(Mono.just(new AuthenticationInfoDTO(accessControlService.isRbacEnabled())))
.map(ResponseEntity::ok);
}

Expand All @@ -70,11 +68,22 @@ private List<UserPermissionDTO> mapPermissions(List<Permission> permissions, Lis
dto.setActions(permission.getActions()
.stream()
.map(String::toUpperCase)
.map(ActionDTO::valueOf)
.map(this::mapAction)
.filter(Objects::nonNull)
.collect(Collectors.toList()));
return dto;
})
.collect(Collectors.toList());
}

@Nullable
private ActionDTO mapAction(String name) {
try {
return ActionDTO.fromValue(name);
} catch (IllegalArgumentException e) {
log.warn("Unknown Action [{}], skipping", name);
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3452,6 +3452,7 @@ components:
- MESSAGES_READ
- MESSAGES_PRODUCE
- MESSAGES_DELETE
- RESTART

ResourceType:
type: string
Expand Down

0 comments on commit fdd9ad9

Please sign in to comment.