aboutsummaryrefslogtreecommitdiffstats
path: root/lib/src/main/java/org/onap/portal/bff/controller/UsersController.java
blob: f67809bc1ef25ae601de46856bed8e2fb8e46c5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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);
  }
}