diff options
Diffstat (limited to 'lib/src/main/java/org/onap/portal/bff/controller/UsersController.java')
-rw-r--r-- | lib/src/main/java/org/onap/portal/bff/controller/UsersController.java | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/lib/src/main/java/org/onap/portal/bff/controller/UsersController.java b/lib/src/main/java/org/onap/portal/bff/controller/UsersController.java new file mode 100644 index 0000000..f67809b --- /dev/null +++ b/lib/src/main/java/org/onap/portal/bff/controller/UsersController.java @@ -0,0 +1,145 @@ +/* + * + * Copyright (c) 2022. Deutsche Telekom AG + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * + * + */ + +package org.onap.portal.bff.controller; + +import io.vavr.collection.List; +import org.onap.portal.bff.config.PortalBffConfig; +import org.onap.portal.bff.openapi.server.api.UsersApi; +import org.onap.portal.bff.openapi.server.model.CreateUserRequestApiDto; +import org.onap.portal.bff.openapi.server.model.RoleApiDto; +import org.onap.portal.bff.openapi.server.model.RoleListResponseApiDto; +import org.onap.portal.bff.openapi.server.model.UpdateUserPasswordRequestApiDto; +import org.onap.portal.bff.openapi.server.model.UpdateUserRequestApiDto; +import org.onap.portal.bff.openapi.server.model.UserListResponseApiDto; +import org.onap.portal.bff.openapi.server.model.UserResponseApiDto; +import org.onap.portal.bff.services.KeycloakService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.server.ServerWebExchange; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +@RestController +public class UsersController extends AbstractBffController implements UsersApi { + + public static final String CREATE = "USER_CREATE"; + public static final String GET = "USER_GET"; + public static final String UPDATE = "USER_UPDATE"; + public static final String DELETE = "USER_DELETE"; + public static final String LIST = "USER_LIST"; + public static final String UPDATE_PASSWORD = "USER_UPDATE_PASSWORD"; + public static final String UPDATE_ROLES = "USER_UPDATE_ROLES"; + public static final String LIST_ROLES = "USER_LIST_ROLES"; + public static final String LIST_AVAILABLE_ROLES = "USER_LIST_AVAILABLE_ROLES"; + + private final KeycloakService keycloakService; + + @Autowired + public UsersController(PortalBffConfig bffConfig, KeycloakService keycloakService) { + super(bffConfig); + this.keycloakService = keycloakService; + } + + @Override + public Mono<ResponseEntity<UserResponseApiDto>> createUser( + Mono<CreateUserRequestApiDto> requestMono, String xRequestId, ServerWebExchange exchange) { + return checkRoleAccess(CREATE, exchange) + .then(requestMono.flatMap(request -> keycloakService.createUser(request, xRequestId))) + .map(ResponseEntity::ok); + } + + @Override + public Mono<ResponseEntity<UserResponseApiDto>> getUser( + String userId, String xRequestId, ServerWebExchange exchange) { + return checkRoleAccess(GET, exchange) + .then(keycloakService.getUser(userId, xRequestId)) + .map(ResponseEntity::ok); + } + + @Override + public Mono<ResponseEntity<Void>> updateUser( + String userId, + Mono<UpdateUserRequestApiDto> requestMono, + String xRequestId, + ServerWebExchange exchange) { + return checkRoleAccess(UPDATE, exchange) + .then(requestMono) + .flatMap(request -> keycloakService.updateUser(userId, request, xRequestId)) + .map(ResponseEntity::ok); + } + + @Override + public Mono<ResponseEntity<Void>> deleteUser( + String userId, String xRequestId, ServerWebExchange exchange) { + return checkRoleAccess(DELETE, exchange) + .then(keycloakService.deleteUser(userId, xRequestId)) + .thenReturn(ResponseEntity.noContent().build()); + } + + @Override + public Mono<ResponseEntity<UserListResponseApiDto>> listUsers( + Integer page, Integer pageSize, String xRequestId, ServerWebExchange exchange) { + + return checkRoleAccess(LIST, exchange) + .then(keycloakService.listUsers(page, pageSize, xRequestId)) + .map(ResponseEntity::ok); + } + + @Override + public Mono<ResponseEntity<Void>> updatePassword( + String userId, + Mono<UpdateUserPasswordRequestApiDto> requestMono, + String xRequestId, + ServerWebExchange exchange) { + return checkRoleAccess(UPDATE_PASSWORD, exchange) + .then(requestMono) + .flatMap(request -> keycloakService.updateUserPassword(userId, request)) + .thenReturn(ResponseEntity.noContent().build()); + } + + @Override + public Mono<ResponseEntity<RoleListResponseApiDto>> listAvailableRoles( + String userId, String xRequestId, ServerWebExchange exchange) { + return checkRoleAccess(LIST_AVAILABLE_ROLES, exchange) + .then(keycloakService.getAvailableRoles(userId, xRequestId)) + .map(ResponseEntity::ok); + } + + @Override + public Mono<ResponseEntity<RoleListResponseApiDto>> listAssignedRoles( + String userId, String xRequestId, ServerWebExchange exchange) { + return checkRoleAccess(LIST_ROLES, exchange) + .then(keycloakService.getAssignedRoles(userId, xRequestId)) + .map(ResponseEntity::ok); + } + + @Override + public Mono<ResponseEntity<RoleListResponseApiDto>> updateAssignedRoles( + String userId, String xRequestId, Flux<RoleApiDto> rolesFlux, ServerWebExchange exchange) { + return checkRoleAccess(UPDATE_ROLES, exchange) + .then(rolesFlux.collectList()) + .map(List::ofAll) + .flatMap(roles -> keycloakService.updateAssignedRoles(userId, roles, xRequestId)) + .map(ResponseEntity::ok); + } +} |