fixed some sonar code smells
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / controller / UserController.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 package org.onap.portalapp.portal.controller;
39
40 import javax.servlet.http.HttpServletRequest;
41
42 import org.onap.portalapp.controller.EPRestrictedBaseController;
43 import org.onap.portalapp.portal.domain.EPUser;
44 import org.onap.portalapp.portal.ecomp.model.PortalRestResponse;
45 import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum;
46 import org.onap.portalapp.portal.logging.aop.EPAuditLog;
47 import org.onap.portalapp.portal.service.UserService;
48 import org.onap.portalapp.portal.transport.ProfileDetail;
49 import org.onap.portalapp.portal.utils.EcompPortalUtils;
50 import org.onap.portalapp.util.EPUserUtils;
51 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
52 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
53 import org.onap.portalsdk.core.util.SystemProperties;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.context.annotation.EnableAspectJAutoProxy;
56 import org.springframework.web.bind.annotation.RequestBody;
57 import org.springframework.web.bind.annotation.RequestMapping;
58 import org.springframework.web.bind.annotation.GetMapping;
59 import org.springframework.web.bind.annotation.PutMapping;
60 import org.springframework.web.bind.annotation.RequestMethod;
61 import org.springframework.web.bind.annotation.RestController;
62
63 @RestController
64 @org.springframework.context.annotation.Configuration
65 @EnableAspectJAutoProxy
66 @EPAuditLog
67 public class UserController extends EPRestrictedBaseController {
68
69         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UserController.class);
70
71         @Autowired
72         private UserService userService;
73
74         private static final String HIDDEN_DEFAULT_PASSWORD = "*****";
75
76         /**
77          * RESTful service method to get ONAP Logged in User details.
78          * 
79          * @param request
80          *            HttpServletRequest
81          * 
82          * @return PortalRestResponse of EPUser
83          */
84         @GetMapping(value = { "/portalApi/loggedinUser" }, produces = "application/json")
85         public PortalRestResponse<ProfileDetail> getLoggedinUser(HttpServletRequest request) {
86                 PortalRestResponse<ProfileDetail> portalRestResponse = null;
87                 try {
88                         EPUser user = EPUserUtils.getUserSession(request);
89                         ProfileDetail profileDetail = new ProfileDetail(user.getFirstName(), user.getLastName(),
90                                         user.getMiddleInitial(), user.getEmail(), user.getLoginId(),  HIDDEN_DEFAULT_PASSWORD);
91                         portalRestResponse = new PortalRestResponse<ProfileDetail>(PortalRestStatusEnum.OK, "success",
92                                         profileDetail);
93                         EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/loggedinUser", "result =", profileDetail);
94                 } catch (Exception e) {
95                         portalRestResponse = new PortalRestResponse<ProfileDetail>(PortalRestStatusEnum.ERROR, e.getMessage(),
96                                         null);
97                         logger.error(EELFLoggerDelegate.errorLogger, "getLoggedinUser failed", e);
98                 }
99                 return portalRestResponse;
100         }
101
102         /**
103          * RESTful service method to update ONAP Logged in User in DB.
104          * 
105          * @param request
106          *            HttpServletRequest
107          * @param profileDetail
108          *            Body with user information
109          * @return PortalRestResponse of String
110          */
111         @PutMapping(value = {
112                         "/portalApi/modifyLoggedinUser" }, produces = "application/json")
113         public PortalRestResponse<String> modifyLoggedinUser(HttpServletRequest request,
114                         @RequestBody ProfileDetail profileDetail) {
115                 PortalRestResponse<String> portalRestResponse = null;
116                 try {
117                         String errorMsg = "";
118                         if (profileDetail.getFirstName().equals("") || profileDetail.getLastName().equals("")
119                                         || profileDetail.getEmail().equals("") || profileDetail.getLoginId().equals("")
120                                         || profileDetail.getLoginPassword().equals("")) {
121                                 errorMsg = "Required field(s) is missing";
122                                 portalRestResponse = new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, errorMsg, null);
123                                 logger.error(EELFLoggerDelegate.errorLogger, "modifyLoggedinUser failed", errorMsg);
124                         } else {
125                                 EPUser user = EPUserUtils.getUserSession(request);
126                                 user.setFirstName(profileDetail.getFirstName());
127                                 user.setLastName(profileDetail.getLastName());
128                                 user.setEmail(profileDetail.getEmail());
129                                 user.setMiddleInitial(profileDetail.getMiddleName());
130                                 user.setLoginId(profileDetail.getLoginId());
131                                 if (!HIDDEN_DEFAULT_PASSWORD.equals(profileDetail.getLoginPassword())){
132                                         user.setLoginPwd(CipherUtil.encryptPKC(profileDetail.getLoginPassword()));
133                                 }
134                                 userService.saveUser(user);
135                                 // Update user info in the session
136                                 request.getSession().setAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME),
137                                                 user);
138                                 portalRestResponse = new PortalRestResponse<String>(PortalRestStatusEnum.OK, "success", null);
139                                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/modifyLoggedinUser", "result =", user);
140                         }
141                 } catch (Exception e) {
142                         portalRestResponse = new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.toString(), null);
143                         logger.error(EELFLoggerDelegate.errorLogger, "modifyLoggedinUser failed", e);
144                 }
145                 return portalRestResponse;
146         }
147 }