2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.portal.controller;
40 import java.util.List;
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
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;
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;
77 @EnableAspectJAutoProxy
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();
83 private PortalAdminService portalAdminService;
84 private AdminRolesService adminRolesService;
85 private AuditService auditService;
88 public PortalAdminController(PortalAdminService portalAdminService,
89 AdminRolesService adminRolesService, AuditService auditService){
90 this.portalAdminService = portalAdminService;
91 this.adminRolesService = adminRolesService;
92 this.auditService = auditService;
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;
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");
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);
116 * RESTful service method to create a new portal admin. Requirement: you
117 * must be the ONAP portal super admin user.
121 * @return FieldsValidator
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");
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);
147 auditService.logActivity(auditLog, null);
148 } catch (Exception e) {
149 logger.error(EELFLoggerDelegate.errorLogger, "createPortalAdmin: failed for save audit log", e);
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);
165 EcompPortalUtils.logAndSerializeObject(logger, "/portalAdmin", "POST result =", response.getStatus());
167 return fieldsValidator;
170 @RequestMapping(value = { "/portalApi/portalAdmin/{userInfo}" }, method = RequestMethod.DELETE)
171 public FieldsValidator deletePortalAdmin(HttpServletRequest request, @PathVariable("userInfo") String userInfo,
172 HttpServletResponse response) {
174 if(!DATA_VALIDATOR.isValid(new SecureString(userInfo))){
175 logger.debug(EELFLoggerDelegate.debugLogger, "PortalAdminController.deletePortalAdmin not valid userId");
179 int userIdIdx = userInfo.indexOf("-");
182 FieldsValidator fieldsValidator = null;
184 if (userIdIdx == -1) {
185 logger.error(EELFLoggerDelegate.errorLogger, "deletePortalAdmin missing userId");
186 return fieldsValidator;
188 String userIdStr = userInfo.substring(0, userIdIdx);
189 userId = Long.valueOf(userIdStr);
190 sbcid = userInfo.substring(userIdIdx + 1, userInfo.length());
192 } catch (Exception e) {
193 logger.error(EELFLoggerDelegate.errorLogger, "deletePortalAdmin error while parsing the userInfo", e);
195 EPUser user = EPUserUtils.getUserSession(request);
196 if (!adminRolesService.isSuperAdmin(user)) {
197 EcompPortalUtils.setBadPermissions(user, response, "deletePortalAdmin");
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);
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);
223 EcompPortalUtils.logAndSerializeObject(logger, "/portalAdmin", "DELETE result =", response.getStatus());
225 return fieldsValidator;
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;
237 EcompPortalUtils.setBadPermissions(user, response, "getUserApps");
239 rolesByApp = adminRolesService.getRolesByApp(user, appId);
241 } catch (Exception e) {
242 logger.error(EELFLoggerDelegate.errorLogger, "getRolesByApp failed", e);