2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ================================================================================
20 package org.openecomp.portalapp.portal.controller;
22 import java.util.List;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.context.annotation.EnableAspectJAutoProxy;
30 import org.springframework.web.bind.annotation.PathVariable;
31 import org.springframework.web.bind.annotation.RequestBody;
32 import org.springframework.web.bind.annotation.RequestMapping;
33 import org.springframework.web.bind.annotation.RequestMethod;
34 import org.springframework.web.bind.annotation.RestController;
36 import org.openecomp.portalsdk.core.domain.AuditLog;
37 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
38 import org.openecomp.portalsdk.core.service.AuditService;
39 import org.openecomp.portalsdk.core.util.SystemProperties;
40 import org.openecomp.portalapp.controller.EPRestrictedBaseController;
41 import org.openecomp.portalapp.portal.domain.EPRole;
42 import org.openecomp.portalapp.portal.domain.EPUser;
43 import org.openecomp.portalapp.portal.domain.EcompAuditLog;
44 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
45 import org.openecomp.portalapp.portal.logging.aop.EPEELFLoggerAdvice;
46 import org.openecomp.portalapp.portal.logging.logic.EPLogUtil;
47 import org.openecomp.portalapp.portal.service.AdminRolesService;
48 import org.openecomp.portalapp.portal.service.PortalAdminService;
49 import org.openecomp.portalapp.portal.transport.FieldsValidator;
50 import org.openecomp.portalapp.portal.transport.PortalAdmin;
51 import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;
52 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
53 import org.openecomp.portalapp.util.EPUserUtils;
56 @org.springframework.context.annotation.Configuration
57 @EnableAspectJAutoProxy
59 public class PortalAdminController extends EPRestrictedBaseController {
61 PortalAdminService portalAdminService;
63 AdminRolesService adminRolesService;
65 AuditService auditService;
67 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PortalAdminController.class);
69 @RequestMapping(value = { "/portalApi/portalAdmins" }, method = RequestMethod.GET, produces = "application/json")
70 public List<PortalAdmin> getPortalAdmins(HttpServletRequest request, HttpServletResponse response) {
71 EPUser user = EPUserUtils.getUserSession(request);
72 List<PortalAdmin> portalAdmins = null;
74 logger.debug(EELFLoggerDelegate.debugLogger, "PortalAdminController.getPortalAdmins, null user");
75 EcompPortalUtils.setBadPermissions(user, response, "getPortalAdmins");
76 } else if (!adminRolesService.isSuperAdmin(user)) {
77 logger.debug(EELFLoggerDelegate.debugLogger, "PortalAdminController.getPortalAdmins, bad permissions");
78 EcompPortalUtils.setBadPermissions(user, response, "createPortalAdmin");
80 // return the list of portal admins
81 portalAdmins = portalAdminService.getPortalAdmins();
82 logger.debug(EELFLoggerDelegate.debugLogger, "portalAdmins: called getPortalAdmins()");
83 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/getPortalAdmins", "result =", portalAdmins);
90 * RESTful service method to create a new portal admin. Requirement: you
91 * must be the Ecomp portal super admin user.
94 @RequestMapping(value = { "/portalApi/portalAdmin" }, method = RequestMethod.POST)
95 public FieldsValidator createPortalAdmin(HttpServletRequest request, @RequestBody String userId,
96 HttpServletResponse response) {
97 EPUser user = EPUserUtils.getUserSession(request);
98 FieldsValidator fieldsValidator = null;
100 logger.debug(EELFLoggerDelegate.debugLogger, "PortalAdminController.createPortalAdmin, null user");
101 EcompPortalUtils.setBadPermissions(user, response, "createPortalAdmin");
102 } else if (!adminRolesService.isSuperAdmin(user)) {
103 logger.debug(EELFLoggerDelegate.debugLogger, "PortalAdminController.createPortalAdmin bad permissions");
104 EcompPortalUtils.setBadPermissions(user, response, "createPortalAdmin");
106 fieldsValidator = portalAdminService.createPortalAdmin(userId);
107 int statusCode = fieldsValidator.httpStatusCode.intValue();
108 response.setStatus(statusCode);
109 if (statusCode == 200) {
110 AuditLog auditLog = new AuditLog();
111 auditLog.setUserId(user.getId());
112 auditLog.setActivityCode(EcompAuditLog.CD_ACTIVITY_ADD_PORTAL_ADMIN);
113 auditLog.setAffectedRecordId(userId);
115 auditService.logActivity(auditLog, null);
116 } catch (Exception e) {
117 logger.error(EELFLoggerDelegate.errorLogger, "createPortalAdmin: failed for save audit log", e);
119 MDC.put(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC());
120 MDC.put(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC());
121 EcompPortalUtils.calculateDateTimeDifferenceForLog(
122 MDC.get(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP),
123 MDC.get(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP));
124 logger.info(EELFLoggerDelegate.auditLogger,
125 EPLogUtil.formatAuditLogMessage("PortalAdminController.createPortalAdmin",
126 EcompAuditLog.CD_ACTIVITY_ADD_PORTAL_ADMIN, user.getOrgUserId(), userId,
127 "A new Portal Admin has been added"));
128 MDC.remove(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP);
129 MDC.remove(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP);
130 MDC.remove(SystemProperties.MDC_TIMER);
133 EcompPortalUtils.logAndSerializeObject(logger, "/portalAdmin", "POST result =", response.getStatus());
135 return fieldsValidator;
138 @RequestMapping(value = { "/portalApi/portalAdmin/{userInfo}" }, method = RequestMethod.DELETE)
139 public FieldsValidator deletePortalAdmin(HttpServletRequest request, @PathVariable("userInfo") String userInfo,
140 HttpServletResponse response) {
141 int userIdIdx = userInfo.indexOf("-");
144 FieldsValidator fieldsValidator = null;
146 if (userIdIdx == -1) {
147 logger.error(EELFLoggerDelegate.errorLogger, "deletePortalAdmin missing userId");
148 return fieldsValidator;
150 String userIdStr = userInfo.substring(0, userIdIdx);
151 userId = Long.valueOf(userIdStr);
152 sbcid = userInfo.substring(userIdIdx + 1, userInfo.length());
154 } catch (Exception e) {
155 logger.error(EELFLoggerDelegate.errorLogger, "deletePortalAdmin error while parsing the userInfo", e);
157 EPUser user = EPUserUtils.getUserSession(request);
158 if (!adminRolesService.isSuperAdmin(user)) {
159 EcompPortalUtils.setBadPermissions(user, response, "deletePortalAdmin");
161 fieldsValidator = portalAdminService.deletePortalAdmin(userId);
162 int statusCode = fieldsValidator.httpStatusCode.intValue();
163 response.setStatus(statusCode);
164 if (statusCode == 200) {
165 AuditLog auditLog = new AuditLog();
166 auditLog.setUserId(user.getId());
167 auditLog.setActivityCode(EcompAuditLog.CD_ACTIVITY_DELETE_PORTAL_ADMIN);
168 auditLog.setAffectedRecordId(sbcid);
169 auditService.logActivity(auditLog, null);
171 MDC.put(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC());
172 MDC.put(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC());
173 EcompPortalUtils.calculateDateTimeDifferenceForLog(
174 MDC.get(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP),
175 MDC.get(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP));
176 logger.info(EELFLoggerDelegate.auditLogger,
177 EPLogUtil.formatAuditLogMessage("PortalAdminController.deletePortalAdmin",
178 EcompAuditLog.CD_ACTIVITY_DELETE_PORTAL_ADMIN, user.getOrgUserId(), sbcid,
179 "A Portal Admin has been deleted"));
180 MDC.remove(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP);
181 MDC.remove(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP);
182 MDC.remove(SystemProperties.MDC_TIMER);
185 EcompPortalUtils.logAndSerializeObject(logger, "/portalAdmin", "DELETE result =", response.getStatus());
187 return fieldsValidator;
190 @RequestMapping(value = {
191 "/portalApi/adminAppsRoles/{appId}" }, method = RequestMethod.GET, produces = "application/json")
192 public List<EPRole> getRolesByApp(HttpServletRequest request, @PathVariable("appId") Long appId,
193 HttpServletResponse response) {
194 EPUser user = EPUserUtils.getUserSession(request);
195 List<EPRole> rolesByApp = null;
199 EcompPortalUtils.setBadPermissions(user, response, "getUserApps");
201 rolesByApp = adminRolesService.getRolesByApp(user, appId);
203 } catch (Exception e) {
204 logger.error(EELFLoggerDelegate.errorLogger,
205 "Exception occurred while performing getRolesByApp operation, Details: "
206 + EcompPortalUtils.getStackTrace(e));