[UI] Handling users and passwords
[validation.git] / ui / src / main / java / org / akraino / validation / ui / controller / UserController.java
1 /*
2  * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.akraino.validation.ui.controller;
18
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;
24
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;
42
43 @Controller
44 @RequestMapping("/api/v1/user")
45 public class UserController extends RestrictedBaseController {
46
47     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(UserController.class);
48
49     @Autowired
50     UserProfileService userService;
51
52     @Autowired
53     RoleService roleService;
54
55     public UserController() {
56         super();
57     }
58
59     @RequestMapping(value = { "/" }, method = RequestMethod.GET)
60     public ResponseEntity<List<User>> getUsers() {
61         try {
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));
66         }
67         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
68     }
69
70     @RequestMapping(value = { "/" }, method = RequestMethod.POST)
71     public ResponseEntity<Boolean> updateUser(@RequestBody User user) {
72         try {
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;
78                 }
79             }
80             if (actualUser == null) {
81                 throw new RuntimeException("User does not exist");
82             }
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);
89         }
90     }
91
92     @RequestMapping(value = { "/create" }, method = RequestMethod.POST)
93     public ResponseEntity<User> postUser(@RequestBody UserData userData) {
94         try {
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);
99         }
100     }
101
102     @RequestMapping(value = { "/updatepassword" }, method = RequestMethod.POST)
103     public ResponseEntity<Boolean> updatePassword(@RequestBody UserData userData) {
104         try {
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;
110                 }
111             }
112             if (actualUser == null) {
113                 throw new RuntimeException("User does not exist");
114             }
115             if (!CipherUtil.decryptPKC(actualUser.getLoginPwd(), System.getenv("ENCRYPTION_KEY"))
116                     .equals(userData.getUser().getLoginPwd())) {
117                 throw new RuntimeException("Wrong password");
118             }
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);
125         }
126     }
127
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)) {
146                 actualRole = role;
147                 break;
148             }
149         }
150         if (actualRole == null) {
151             throw new RuntimeException("User role does not exist");
152         }
153         SortedSet<Role> actualRoles = new TreeSet<Role>();
154         actualRoles.add(actualRole);
155         newUser.setRoles(actualRoles);
156         userService.saveUser(newUser);
157         return newUser;
158     }
159
160 }