Merge "adding catalog-service for mod2"
[dcaegen2/platform.git] / mod2 / auth-service / src / main / java / org / onap / dcaegen2 / platform / mod / controllers / UserController.java
1 /*
2  *
3  *  * ============LICENSE_START=======================================================
4  *  *  org.onap.dcae
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
11  *  *
12  *  *       http://www.apache.org/licenses/LICENSE-2.0
13  *  *
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=========================================================
20  *
21  */
22
23 package org.onap.dcaegen2.platform.mod.controllers;
24
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.*;
37
38 import javax.validation.Valid;
39 import java.util.List;
40
41
42 /**
43  * @author
44  * @date 09/08/2020
45  * User Operations
46  */
47 @RestController
48 @RequestMapping("/api/users")
49 @CrossOrigin(origins = "*")
50 public class UserController {
51
52     @Autowired
53     private MODUserDetailService modUserDetailService;
54
55     @Autowired
56     private UserDetailsServiceImpl userDetailsService;
57
58     @PreAuthorize("hasRole('ADMIN')")
59     @GetMapping("/getAll")
60     @ResponseStatus(HttpStatus.OK)
61     public List<ModUser> getAllUsers() {
62         return modUserDetailService.findAll();
63     }
64
65     @PreAuthorize("hasRole('ADMIN') or hasRole('USER')")
66     @GetMapping("/{username}")
67     public UserDetails getUser(@PathVariable String username) {
68         return userDetailsService.loadUserByUsername(username);
69     }
70
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);
76     }
77
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);
83     }
84
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"));
90     }
91
92     @ExceptionHandler
93     @ResponseStatus(HttpStatus.NOT_FOUND)
94     public void userNotFoundHandler(UserNotFoundException ex) {
95     }
96 }