3 * * ============LICENSE_START=======================================================
5 * * ================================================================================
6 * * Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
7 * * ================================================================================
8 * * Licensed under the Apache License, Version 2.0 (the "License");
9 * * you may not use this file except in compliance with the License.
10 * * You may obtain a copy of the License at
12 * * http://www.apache.org/licenses/LICENSE-2.0
14 * * Unless required by applicable law or agreed to in writing, software
15 * * distributed under the License is distributed on an "AS IS" BASIS,
16 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * * See the License for the specific language governing permissions and
18 * * limitations under the License.
19 * * ============LICENSE_END=========================================================
23 package org.onap.dcaegen2.platform.mod.controllers;
25 import org.onap.dcaegen2.platform.mod.exceptions.UserNotFoundException;
26 import org.onap.dcaegen2.platform.mod.models.GenericResponse;
27 import org.onap.dcaegen2.platform.mod.models.ModUser;
28 import org.onap.dcaegen2.platform.mod.models.UpdateUserRequest;
29 import org.onap.dcaegen2.platform.mod.security.services.UserDetailsServiceImpl;
30 import org.onap.dcaegen2.platform.mod.services.MODUserDetailService;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.http.HttpStatus;
33 import org.springframework.http.ResponseEntity;
34 import org.springframework.security.access.prepost.PreAuthorize;
35 import org.springframework.security.core.userdetails.UserDetails;
36 import org.springframework.web.bind.annotation.*;
38 import javax.validation.Valid;
39 import java.util.List;
48 @RequestMapping("/api/users")
49 @CrossOrigin(origins = "*")
50 public class UserController {
53 private MODUserDetailService modUserDetailService;
56 private UserDetailsServiceImpl userDetailsService;
58 @PreAuthorize("hasRole('ADMIN')")
59 @GetMapping("/getAll")
60 @ResponseStatus(HttpStatus.OK)
61 public List<ModUser> getAllUsers() {
62 return modUserDetailService.findAll();
65 @PreAuthorize("hasRole('ADMIN') or hasRole('USER')")
66 @GetMapping("/{username}")
67 public UserDetails getUser(@PathVariable String username) {
68 return userDetailsService.loadUserByUsername(username);
71 @PreAuthorize("hasRole('ADMIN')")
72 @PatchMapping("/admin/{username}")
73 public ModUser adminUpdateUserProfile(@PathVariable String username, @RequestBody @Valid UpdateUserRequest
74 userRequest, @RequestHeader (name="Authorization") String token) {
75 return userDetailsService.adminUpdateUser(username, userRequest, token);
78 @PreAuthorize("hasRole('USER') or hasRole('DEVELOPER')")
79 @PatchMapping("/user/{username}")
80 public ModUser userUpdateOwnProfile(@PathVariable String username, @RequestBody @Valid UpdateUserRequest
81 userRequest, @RequestHeader (name="Authorization") String token) {
82 return userDetailsService.userUpdateOwnProfile(username, userRequest, token);
85 @PreAuthorize("hasRole('ADMIN')")
86 @DeleteMapping("/{username}")
87 public ResponseEntity<?> deleteUser(@PathVariable String username) {
88 modUserDetailService.deleteUserByUsername(username);
89 return ResponseEntity.ok(new GenericResponse("User " + username + " was removed"));
93 @ResponseStatus(HttpStatus.NOT_FOUND)
94 public void userNotFoundHandler(UserNotFoundException ex) {