1212import java .util .Collection ;
1313import java .util .Collections ;
1414import java .util .List ;
15+ import java .util .Objects ;
1516import java .util .stream .Collectors ;
17+ import javax .annotation .Nullable ;
1618import lombok .RequiredArgsConstructor ;
19+ import lombok .extern .slf4j .Slf4j ;
1720import org .springframework .http .ResponseEntity ;
1821import org .springframework .security .core .context .ReactiveSecurityContextHolder ;
1922import org .springframework .security .core .context .SecurityContext ;
2326
2427@ RestController
2528@ RequiredArgsConstructor
29+ @ Slf4j
2630public class AccessController implements AuthorizationApi {
2731
2832 private final AccessControlService accessControlService ;
2933
3034 public Mono <ResponseEntity <AuthenticationInfoDTO >> getUserAuthInfo (ServerWebExchange exchange ) {
31- AuthenticationInfoDTO dto = new AuthenticationInfoDTO ();
32- dto .setRbacEnabled (accessControlService .isRbacEnabled ());
33- UserInfoDTO userInfo = new UserInfoDTO ();
34-
3535 Mono <List <UserPermissionDTO >> permissions = accessControlService .getUser ()
3636 .map (user -> accessControlService .getRoles ()
3737 .stream ()
@@ -49,13 +49,11 @@ public Mono<ResponseEntity<AuthenticationInfoDTO>> getUserAuthInfo(ServerWebExch
4949 return userName
5050 .zipWith (permissions )
5151 .map (data -> {
52- userInfo .setUsername (data .getT1 ());
53- userInfo .setPermissions (data .getT2 ());
54-
55- dto .setUserInfo (userInfo );
52+ var dto = new AuthenticationInfoDTO (accessControlService .isRbacEnabled ());
53+ dto .setUserInfo (new UserInfoDTO (data .getT1 (), data .getT2 ()));
5654 return dto ;
5755 })
58- .switchIfEmpty (Mono .just (dto ))
56+ .switchIfEmpty (Mono .just (new AuthenticationInfoDTO ( accessControlService . isRbacEnabled ()) ))
5957 .map (ResponseEntity ::ok );
6058 }
6159
@@ -70,11 +68,22 @@ private List<UserPermissionDTO> mapPermissions(List<Permission> permissions, Lis
7068 dto .setActions (permission .getActions ()
7169 .stream ()
7270 .map (String ::toUpperCase )
73- .map (ActionDTO ::valueOf )
71+ .map (this ::mapAction )
72+ .filter (Objects ::nonNull )
7473 .collect (Collectors .toList ()));
7574 return dto ;
7675 })
7776 .collect (Collectors .toList ());
7877 }
7978
79+ @ Nullable
80+ private ActionDTO mapAction (String name ) {
81+ try {
82+ return ActionDTO .fromValue (name );
83+ } catch (IllegalArgumentException e ) {
84+ log .warn ("Unknown Action [{}], skipping" , name );
85+ return null ;
86+ }
87+ }
88+
8089}
0 commit comments