[UI] Support user creation
[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.onap.portalsdk.core.controller.RestrictedBaseController;
26 import org.onap.portalsdk.core.domain.Role;
27 import org.onap.portalsdk.core.domain.User;
28 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
29 import org.onap.portalsdk.core.onboarding.exception.CipherUtilException;
30 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
31 import org.onap.portalsdk.core.service.RoleService;
32 import org.onap.portalsdk.core.service.UserProfileService;
33 import org.onap.portalsdk.core.web.support.UserUtils;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.http.HttpStatus;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.stereotype.Controller;
38 import org.springframework.web.bind.annotation.RequestBody;
39 import org.springframework.web.bind.annotation.RequestMapping;
40 import org.springframework.web.bind.annotation.RequestMethod;
41
42 @Controller
43 @RequestMapping("/api/v1/user")
44 public class UserController extends RestrictedBaseController {
45
46     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(UserController.class);
47
48     @Autowired
49     UserProfileService userService;
50
51     @Autowired
52     RoleService roleService;
53
54     public UserController() {
55         super();
56     }
57
58     @RequestMapping(value = { "/" }, method = RequestMethod.POST)
59     public ResponseEntity<User> createUser(@RequestBody User user) {
60         try {
61             return new ResponseEntity<>(createUser(user.getFirstName(), user.getLoginId(), user.getLoginPwd()),
62                     HttpStatus.OK);
63         } catch (Exception e) {
64             LOGGER.error(EELFLoggerDelegate.errorLogger, "Creation of user failed. " + UserUtils.getStackTrace(e));
65             return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
66         }
67     }
68
69     private User createUser(String firstName, String loginId, String loginPwd) throws IOException, CipherUtilException {
70         User newUser = new User();
71         newUser.setActive(true);
72         newUser.setCreated(new Date());
73         newUser.setFirstName(firstName);
74         newUser.setInternal(false);
75         newUser.setLoginId(loginId);
76         newUser.setOrgUserId(loginId);
77         newUser.setLoginPwd(CipherUtil.encryptPKC(loginPwd, System.getenv("ENCRYPTION_KEY")));
78         newUser.setModified(new Date());
79         newUser.setModifiedId(1L);
80         newUser.setOnline(true);
81         newUser.setTimeZoneId(10L);
82         userService.saveUser(newUser);
83         Role actualRole = null;
84         List<Role> roles = roleService.getActiveRoles(null);
85         for (Role role : roles) {
86             if (role.getName().equals("Blueprint Validation UI user")) {
87                 actualRole = role;
88                 break;
89             }
90         }
91         if (actualRole == null) {
92             throw new RuntimeException("Blueprint Validation UI user role does not exist");
93         }
94         SortedSet<Role> actualRoles = new TreeSet<Role>();
95         actualRoles.add(actualRole);
96         newUser.setRoles(actualRoles);
97         userService.saveUser(newUser);
98         return newUser;
99     }
100
101 }