2 * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 * not use this file except in compliance with the License. You may obtain
6 * a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 * implied. See the License for the specific language governing
14 * permissions and limitations under the License.
17 package org.akraino.validation.ui.controller;
19 import java.io.IOException;
20 import java.util.Date;
21 import java.util.List;
22 import java.util.SortedSet;
23 import java.util.TreeSet;
25 import org.akraino.validation.ui.data.UserData;
26 import org.onap.portalsdk.core.controller.RestrictedBaseController;
27 import org.onap.portalsdk.core.domain.Role;
28 import org.onap.portalsdk.core.domain.User;
29 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
30 import org.onap.portalsdk.core.onboarding.exception.CipherUtilException;
31 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
32 import org.onap.portalsdk.core.service.RoleService;
33 import org.onap.portalsdk.core.service.UserProfileService;
34 import org.onap.portalsdk.core.web.support.UserUtils;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.http.HttpStatus;
37 import org.springframework.http.ResponseEntity;
38 import org.springframework.stereotype.Controller;
39 import org.springframework.web.bind.annotation.RequestBody;
40 import org.springframework.web.bind.annotation.RequestMapping;
41 import org.springframework.web.bind.annotation.RequestMethod;
44 @RequestMapping("/api/v1/user")
45 public class UserController extends RestrictedBaseController {
47 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(UserController.class);
50 UserProfileService userService;
53 RoleService roleService;
55 public UserController() {
59 @RequestMapping(value = { "/" }, method = RequestMethod.GET)
60 public ResponseEntity<List<User>> getUsers() {
62 return new ResponseEntity<>(userService.findAllActive(), HttpStatus.OK);
63 } catch (Exception e) {
64 LOGGER.error(EELFLoggerDelegate.errorLogger,
65 "Error when trying to get users. " + UserUtils.getStackTrace(e));
67 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
70 @RequestMapping(value = { "/" }, method = RequestMethod.POST)
71 public ResponseEntity<Boolean> updateUser(@RequestBody User user) {
73 User actualUser = null;
74 List<User> actualUsers = userService.findAllActive();
75 for (User tempUser : actualUsers) {
76 if (tempUser.getLoginId().equals(user.getLoginId())) {
77 actualUser = tempUser;
80 if (actualUser == null) {
81 throw new RuntimeException("User does not exist");
83 actualUser.setLoginPwd(CipherUtil.encryptPKC(user.getLoginPwd(), System.getenv("ENCRYPTION_KEY")));
84 userService.saveUser(actualUser);
85 return new ResponseEntity<>(true, HttpStatus.OK);
86 } catch (Exception e) {
87 LOGGER.error(EELFLoggerDelegate.errorLogger, "Update of user failed. " + UserUtils.getStackTrace(e));
88 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
92 @RequestMapping(value = { "/create" }, method = RequestMethod.POST)
93 public ResponseEntity<User> postUser(@RequestBody UserData userData) {
95 return new ResponseEntity<>(createUser(userData.getUser(), userData.getRole()), HttpStatus.OK);
96 } catch (Exception e) {
97 LOGGER.error(EELFLoggerDelegate.errorLogger, "Creation of user failed. " + UserUtils.getStackTrace(e));
98 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
102 @RequestMapping(value = { "/updatepassword" }, method = RequestMethod.POST)
103 public ResponseEntity<Boolean> updatePassword(@RequestBody UserData userData) {
105 User actualUser = null;
106 List<User> actualUsers = userService.findAllActive();
107 for (User tempUser : actualUsers) {
108 if (tempUser.getLoginId().equals(userData.getUser().getLoginId())) {
109 actualUser = tempUser;
112 if (actualUser == null) {
113 throw new RuntimeException("User does not exist");
115 if (!CipherUtil.decryptPKC(actualUser.getLoginPwd(), System.getenv("ENCRYPTION_KEY"))
116 .equals(userData.getUser().getLoginPwd())) {
117 throw new RuntimeException("Wrong password");
119 actualUser.setLoginPwd(CipherUtil.encryptPKC(userData.getNewPwd(), System.getenv("ENCRYPTION_KEY")));
120 userService.saveUser(actualUser);
121 return new ResponseEntity<>(true, HttpStatus.OK);
122 } catch (Exception e) {
123 LOGGER.error(EELFLoggerDelegate.errorLogger, "Creation of user failed. " + UserUtils.getStackTrace(e));
124 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
128 private User createUser(User user, String roleName) throws IOException, CipherUtilException {
129 User newUser = new User();
130 newUser.setActive(true);
131 newUser.setCreated(new Date());
132 newUser.setFirstName(user.getFirstName());
133 newUser.setInternal(false);
134 newUser.setLoginId(user.getLoginId());
135 newUser.setOrgUserId(user.getLoginId());
136 newUser.setLoginPwd(CipherUtil.encryptPKC(user.getLoginPwd(), System.getenv("ENCRYPTION_KEY")));
137 newUser.setModified(new Date());
138 newUser.setModifiedId(1L);
139 newUser.setOnline(true);
140 newUser.setTimeZoneId(10L);
141 userService.saveUser(newUser);
142 Role actualRole = null;
143 List<Role> roles = roleService.getActiveRoles(null);
144 for (Role role : roles) {
145 if (role.getName().equals(roleName)) {
150 if (actualRole == null) {
151 throw new RuntimeException("User role does not exist");
153 SortedSet<Role> actualRoles = new TreeSet<Role>();
154 actualRoles.add(actualRole);
155 newUser.setRoles(actualRoles);
156 userService.saveUser(newUser);