32b28c7dab98e6714e84cd1952de58597c8a3391
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / controller / PortalAdminController.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 java.util.List;
41
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
44
45 import org.onap.portalapp.controller.EPRestrictedBaseController;
46 import org.onap.portalapp.portal.domain.EPRole;
47 import org.onap.portalapp.portal.domain.EPUser;
48 import org.onap.portalapp.portal.domain.EcompAuditLog;
49 import org.onap.portalapp.portal.logging.aop.EPAuditLog;
50 import org.onap.portalapp.portal.logging.aop.EPEELFLoggerAdvice;
51 import org.onap.portalapp.portal.logging.logic.EPLogUtil;
52 import org.onap.portalapp.portal.service.AdminRolesService;
53 import org.onap.portalapp.portal.service.PortalAdminService;
54 import org.onap.portalapp.portal.transport.FieldsValidator;
55 import org.onap.portalapp.portal.transport.PortalAdmin;
56 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
57 import org.onap.portalapp.portal.utils.EcompPortalUtils;
58 import org.onap.portalapp.util.EPUserUtils;
59 import org.onap.portalapp.validation.DataValidator;
60 import org.onap.portalapp.validation.SecureString;
61 import org.onap.portalsdk.core.domain.AuditLog;
62 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
63 import org.onap.portalsdk.core.service.AuditService;
64 import org.onap.portalsdk.core.util.SystemProperties;
65 import org.slf4j.MDC;
66 import org.springframework.beans.factory.annotation.Autowired;
67 import org.springframework.context.annotation.Configuration;
68 import org.springframework.context.annotation.EnableAspectJAutoProxy;
69 import org.springframework.web.bind.annotation.PathVariable;
70 import org.springframework.web.bind.annotation.RequestBody;
71 import org.springframework.web.bind.annotation.RequestMapping;
72 import org.springframework.web.bind.annotation.RequestMethod;
73 import org.springframework.web.bind.annotation.RestController;
74
75 @RestController
76 @Configuration
77 @EnableAspectJAutoProxy
78 @EPAuditLog
79 public class PortalAdminController extends EPRestrictedBaseController {
80         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PortalAdminController.class);
81         private static final DataValidator DATA_VALIDATOR = new DataValidator();
82
83         private PortalAdminService portalAdminService;
84         private AdminRolesService adminRolesService;
85         private AuditService auditService;
86
87         @Autowired
88         public PortalAdminController(PortalAdminService portalAdminService,
89                 AdminRolesService adminRolesService, AuditService auditService){
90                 this.portalAdminService = portalAdminService;
91                 this.adminRolesService = adminRolesService;
92                 this.auditService = auditService;
93         }
94
95         @RequestMapping(value = { "/portalApi/portalAdmins" }, method = RequestMethod.GET, produces = "application/json")
96         public List<PortalAdmin> getPortalAdmins(HttpServletRequest request, HttpServletResponse response) {
97                 EPUser user = EPUserUtils.getUserSession(request);
98                 List<PortalAdmin> portalAdmins = null;
99                 if (user == null) {
100                         logger.debug(EELFLoggerDelegate.debugLogger, "PortalAdminController.getPortalAdmins, null user");
101                         EcompPortalUtils.setBadPermissions(user, response, "getPortalAdmins");
102                 } else if (!adminRolesService.isSuperAdmin(user)) {
103                         logger.debug(EELFLoggerDelegate.debugLogger, "PortalAdminController.getPortalAdmins, bad permissions");
104                         EcompPortalUtils.setBadPermissions(user, response, "createPortalAdmin");
105                 } else {
106                         // return the list of portal admins
107                         portalAdmins = portalAdminService.getPortalAdmins();
108                         logger.debug(EELFLoggerDelegate.debugLogger, "portalAdmins: called getPortalAdmins()");
109                         EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/getPortalAdmins", "result =", portalAdmins);
110                 }
111
112                 return portalAdmins;
113         }
114
115         /**
116          * RESTful service method to create a new portal admin. Requirement: you
117          * must be the ONAP portal super admin user.
118          * @param request 
119          * @param userId 
120          * @param response 
121          * @return FieldsValidator 
122          */
123         @RequestMapping(value = { "/portalApi/portalAdmin" }, method = RequestMethod.POST)
124         public FieldsValidator createPortalAdmin(HttpServletRequest request, @RequestBody String userId,
125                         HttpServletResponse response) {
126                 EPUser user = EPUserUtils.getUserSession(request);
127                 FieldsValidator fieldsValidator = null;
128                 if(!DATA_VALIDATOR.isValid(new SecureString(userId))){
129                         logger.debug(EELFLoggerDelegate.debugLogger, "PortalAdminController.createPortalAdmin not valid userId");
130                         EcompPortalUtils.setBadPermissions(user, response, "createPortalAdmin");
131                 }else if (user == null) {
132                         logger.debug(EELFLoggerDelegate.debugLogger, "PortalAdminController.createPortalAdmin, null user");
133                         EcompPortalUtils.setBadPermissions(user, response, "createPortalAdmin");
134                 } else if (!adminRolesService.isSuperAdmin(user)) {
135                         logger.debug(EELFLoggerDelegate.debugLogger, "PortalAdminController.createPortalAdmin bad permissions");
136                         EcompPortalUtils.setBadPermissions(user, response, "createPortalAdmin");
137                 } else {
138                         fieldsValidator = portalAdminService.createPortalAdmin(userId);
139                         int statusCode = fieldsValidator.httpStatusCode.intValue();
140                         response.setStatus(statusCode);
141                         if (statusCode == 200) {
142                                 AuditLog auditLog = new AuditLog();
143                                 auditLog.setUserId(user.getId());
144                                 auditLog.setActivityCode(EcompAuditLog.CD_ACTIVITY_ADD_PORTAL_ADMIN);
145                                 auditLog.setAffectedRecordId(userId);
146                                 try {
147                                         auditService.logActivity(auditLog, null);
148                                 } catch (Exception e) {
149                                         logger.error(EELFLoggerDelegate.errorLogger, "createPortalAdmin: failed for save audit log", e);
150                                 }
151                                 MDC.put(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC());
152                                 MDC.put(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC());
153                                 EcompPortalUtils.calculateDateTimeDifferenceForLog(
154                                                 MDC.get(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP),
155                                                 MDC.get(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP));
156                                 logger.info(EELFLoggerDelegate.auditLogger,
157                                                 EPLogUtil.formatAuditLogMessage("PortalAdminController.createPortalAdmin",
158                                                                 EcompAuditLog.CD_ACTIVITY_ADD_PORTAL_ADMIN, user.getOrgUserId(), userId,
159                                                                 "A new Portal Admin has been added"));
160                                 MDC.remove(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP);
161                                 MDC.remove(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP);
162                                 MDC.remove(SystemProperties.MDC_TIMER);
163                         }
164                 }
165                 EcompPortalUtils.logAndSerializeObject(logger, "/portalAdmin", "POST result =", response.getStatus());
166
167                 return fieldsValidator;
168         }
169
170         @RequestMapping(value = { "/portalApi/portalAdmin/{userInfo}" }, method = RequestMethod.DELETE)
171         public FieldsValidator deletePortalAdmin(HttpServletRequest request, @PathVariable("userInfo") String userInfo,
172                         HttpServletResponse response) {
173
174                 if(!DATA_VALIDATOR.isValid(new SecureString(userInfo))){
175                         logger.debug(EELFLoggerDelegate.debugLogger, "PortalAdminController.deletePortalAdmin not valid userId");
176                         return null;
177                 }
178
179                 int userIdIdx = userInfo.indexOf("-");
180                 Long userId = null;
181                 String sbcid = null;
182                 FieldsValidator fieldsValidator = null;
183                 try {
184                         if (userIdIdx == -1) {
185                                 logger.error(EELFLoggerDelegate.errorLogger, "deletePortalAdmin missing userId");
186                                 return fieldsValidator;
187                         } else {
188                                 String userIdStr = userInfo.substring(0, userIdIdx);
189                                 userId = Long.valueOf(userIdStr);
190                                 sbcid = userInfo.substring(userIdIdx + 1, userInfo.length());
191                         }
192                 } catch (Exception e) {
193                         logger.error(EELFLoggerDelegate.errorLogger, "deletePortalAdmin error while parsing the userInfo", e);
194                 }
195                 EPUser user = EPUserUtils.getUserSession(request);
196                 if (!adminRolesService.isSuperAdmin(user)) {
197                         EcompPortalUtils.setBadPermissions(user, response, "deletePortalAdmin");
198                 } else {
199                         fieldsValidator = portalAdminService.deletePortalAdmin(userId);
200                         int statusCode = fieldsValidator.httpStatusCode.intValue();
201                         response.setStatus(statusCode);
202                         if (statusCode == 200) {
203                                 AuditLog auditLog = new AuditLog();
204                                 auditLog.setUserId(user.getId());
205                                 auditLog.setActivityCode(EcompAuditLog.CD_ACTIVITY_DELETE_PORTAL_ADMIN);
206                                 auditLog.setAffectedRecordId(sbcid);
207                                 auditService.logActivity(auditLog, null);
208                                 
209                                 MDC.put(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC());
210                                 MDC.put(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC());
211                                 EcompPortalUtils.calculateDateTimeDifferenceForLog(
212                                                 MDC.get(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP),
213                                                 MDC.get(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP));
214                                 logger.info(EELFLoggerDelegate.auditLogger,
215                                                 EPLogUtil.formatAuditLogMessage("PortalAdminController.deletePortalAdmin",
216                                                                 EcompAuditLog.CD_ACTIVITY_DELETE_PORTAL_ADMIN, user.getOrgUserId(), sbcid,
217                                                                 "A Portal Admin has been deleted"));
218                                 MDC.remove(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP);
219                                 MDC.remove(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP);
220                                 MDC.remove(SystemProperties.MDC_TIMER);
221                         }
222                 }
223                 EcompPortalUtils.logAndSerializeObject(logger, "/portalAdmin", "DELETE result =", response.getStatus());
224
225                 return fieldsValidator;
226         }
227
228         @RequestMapping(value = {
229                         "/portalApi/adminAppsRoles/{appId}" }, method = RequestMethod.GET, produces = "application/json")
230         public List<EPRole> getRolesByApp(HttpServletRequest request, @PathVariable("appId") Long appId,
231                         HttpServletResponse response) {
232                 EPUser user = EPUserUtils.getUserSession(request);
233                 List<EPRole> rolesByApp = null;
234
235                 try {
236                         if (user == null) {
237                                 EcompPortalUtils.setBadPermissions(user, response, "getUserApps");
238                         } else {
239                                 rolesByApp = adminRolesService.getRolesByApp(user, appId);
240                         }
241                 } catch (Exception e) {
242                         logger.error(EELFLoggerDelegate.errorLogger,    "getRolesByApp failed", e);
243                 }
244
245                 return rolesByApp;
246         }
247
248 }