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.portal.controller;
40 import java.security.Principal;
41 import java.util.List;
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
44 import org.onap.portal.domain.db.fn.FnRole;
45 import org.onap.portal.domain.db.fn.FnUser;
46 import org.onap.portal.domain.dto.ecomp.EcompAuditLog;
47 import org.onap.portal.domain.dto.transport.FieldsValidator;
48 import org.onap.portal.domain.dto.transport.PortalAdmin;
49 import org.onap.portal.logging.aop.EPEELFLoggerAdvice;
50 import org.onap.portal.logging.logic.EPLogUtil;
51 import org.onap.portal.service.AdminRolesService;
52 import org.onap.portal.service.PortalAdminService;
53 import org.onap.portal.service.user.FnUserService;
54 import org.onap.portal.utils.EPCommonSystemProperties;
55 import org.onap.portal.utils.EcompPortalUtils;
56 import org.onap.portal.validation.DataValidator;
57 import org.onap.portal.validation.SecureString;
58 import org.onap.portalsdk.core.domain.AuditLog;
59 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
60 import org.onap.portalsdk.core.service.AuditService;
61 import org.onap.portalsdk.core.service.AuditServiceImpl;
62 import org.onap.portalsdk.core.util.SystemProperties;
64 import org.springframework.beans.factory.annotation.Autowired;
65 import org.springframework.context.annotation.Configuration;
66 import org.springframework.context.annotation.EnableAspectJAutoProxy;
67 import org.springframework.web.bind.annotation.PathVariable;
68 import org.springframework.web.bind.annotation.RequestBody;
69 import org.springframework.web.bind.annotation.RequestMapping;
70 import org.springframework.web.bind.annotation.RequestMethod;
71 import org.springframework.web.bind.annotation.RestController;
75 @EnableAspectJAutoProxy
76 public class PortalAdminController {
78 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PortalAdminController.class);
79 private static final DataValidator DATA_VALIDATOR = new DataValidator();
81 private PortalAdminService portalAdminService;
82 private final FnUserService fnUserService;
83 private AdminRolesService adminRolesService;
84 private AuditServiceImpl auditService = new AuditServiceImpl();
87 public PortalAdminController(PortalAdminService portalAdminService,
88 FnUserService fnUserService, AdminRolesService adminRolesService) {
89 this.portalAdminService = portalAdminService;
90 this.fnUserService = fnUserService;
91 this.adminRolesService = adminRolesService;
94 @RequestMapping(value = {"/portalApi/portalAdmins"}, method = RequestMethod.GET, produces = "application/json")
95 public List<PortalAdmin> getPortalAdmins(Principal principal, HttpServletRequest request,
96 HttpServletResponse response) {
97 FnUser user = fnUserService.loadUserByUsername(principal.getName());
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.getLoginId())) {
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);
115 @RequestMapping(value = {"/portalApi/portalAdmin"}, method = RequestMethod.POST)
116 public FieldsValidator createPortalAdmin(Principal principal, HttpServletRequest request,
117 @RequestBody String userId,
118 HttpServletResponse response) {
119 FnUser user = fnUserService.loadUserByUsername(principal.getName());
120 FieldsValidator fieldsValidator = null;
121 if (!DATA_VALIDATOR.isValid(new SecureString(userId))) {
122 logger.debug(EELFLoggerDelegate.debugLogger, "PortalAdminController.createPortalAdmin not valid userId");
123 EcompPortalUtils.setBadPermissions(user, response, "createPortalAdmin");
124 } else if (user == null) {
125 logger.debug(EELFLoggerDelegate.debugLogger, "PortalAdminController.createPortalAdmin, null user");
126 EcompPortalUtils.setBadPermissions(user, response, "createPortalAdmin");
127 } else if (!adminRolesService.isSuperAdmin(user.getLoginId())) {
128 logger.debug(EELFLoggerDelegate.debugLogger, "PortalAdminController.createPortalAdmin bad permissions");
129 EcompPortalUtils.setBadPermissions(user, response, "createPortalAdmin");
131 fieldsValidator = portalAdminService.createPortalAdmin(userId);
132 int statusCode = fieldsValidator.getHttpStatusCode().intValue();
133 response.setStatus(statusCode);
134 if (statusCode == 200) {
135 AuditLog auditLog = new AuditLog();
136 auditLog.setUserId(user.getId());
137 auditLog.setActivityCode(EcompAuditLog.CD_ACTIVITY_ADD_PORTAL_ADMIN);
138 auditLog.setAffectedRecordId(userId);
140 auditService.logActivity(auditLog, null);
141 } catch (Exception e) {
142 logger.error(EELFLoggerDelegate.errorLogger, "createPortalAdmin: failed for save audit log", e);
144 MDC.put(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC());
145 MDC.put(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC());
146 EcompPortalUtils.calculateDateTimeDifferenceForLog(
147 MDC.get(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP),
148 MDC.get(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP));
149 logger.info(EELFLoggerDelegate.auditLogger,
150 EPLogUtil.formatAuditLogMessage("PortalAdminController.createPortalAdmin",
151 EcompAuditLog.CD_ACTIVITY_ADD_PORTAL_ADMIN, user.getOrgUserId(), userId,
152 "A new Portal Admin has been added"));
153 MDC.remove(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP);
154 MDC.remove(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP);
155 MDC.remove(SystemProperties.MDC_TIMER);
158 EcompPortalUtils.logAndSerializeObject(logger, "/portalAdmin", "POST result =", response.getStatus());
160 return fieldsValidator;
163 @RequestMapping(value = {"/portalApi/portalAdmin/{userInfo}"}, method = RequestMethod.DELETE)
164 public FieldsValidator deletePortalAdmin(Principal principal, HttpServletRequest request,
165 @PathVariable("userInfo") String userInfo,
166 HttpServletResponse response) {
167 if (!DATA_VALIDATOR.isValid(new SecureString(userInfo))) {
168 logger.debug(EELFLoggerDelegate.debugLogger, "PortalAdminController.deletePortalAdmin not valid userId");
171 int userIdIdx = userInfo.indexOf("-");
174 FieldsValidator fieldsValidator = null;
176 if (userIdIdx == -1) {
177 logger.error(EELFLoggerDelegate.errorLogger, "deletePortalAdmin missing userId");
178 return fieldsValidator;
180 String userIdStr = userInfo.substring(0, userIdIdx);
181 userId = Long.valueOf(userIdStr);
182 sbcid = userInfo.substring(userIdIdx + 1, userInfo.length());
184 } catch (Exception e) {
185 logger.error(EELFLoggerDelegate.errorLogger, "deletePortalAdmin error while parsing the userInfo", e);
187 FnUser user = fnUserService.loadUserByUsername(principal.getName());
188 if (!adminRolesService.isSuperAdmin(user.getLoginId())) {
189 EcompPortalUtils.setBadPermissions(user, response, "deletePortalAdmin");
191 fieldsValidator = portalAdminService.deletePortalAdmin(userId);
192 int statusCode = fieldsValidator.getHttpStatusCode().intValue();
193 response.setStatus(statusCode);
194 if (statusCode == 200) {
195 AuditLog auditLog = new AuditLog();
196 auditLog.setUserId(user.getId());
197 auditLog.setActivityCode(EcompAuditLog.CD_ACTIVITY_DELETE_PORTAL_ADMIN);
198 auditLog.setAffectedRecordId(sbcid);
199 auditService.logActivity(auditLog, null);
200 MDC.put(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC());
201 MDC.put(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC());
202 EcompPortalUtils.calculateDateTimeDifferenceForLog(
203 MDC.get(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP),
204 MDC.get(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP));
205 logger.info(EELFLoggerDelegate.auditLogger,
206 EPLogUtil.formatAuditLogMessage("PortalAdminController.deletePortalAdmin",
207 EcompAuditLog.CD_ACTIVITY_DELETE_PORTAL_ADMIN, user.getOrgUserId(), sbcid,
208 "A Portal Admin has been deleted"));
209 MDC.remove(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP);
210 MDC.remove(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP);
211 MDC.remove(SystemProperties.MDC_TIMER);
214 EcompPortalUtils.logAndSerializeObject(logger, "/portalAdmin", "DELETE result =", response.getStatus());
216 return fieldsValidator;
219 @RequestMapping(value = {
220 "/portalApi/adminAppsRoles/{appId}"}, method = RequestMethod.GET, produces = "application/json")
221 public List<FnRole> getRolesByApp(Principal principal, HttpServletRequest request,
222 @PathVariable("appId") Long appId, HttpServletResponse response) {
223 FnUser user = fnUserService.loadUserByUsername(principal.getName());
224 List<FnRole> rolesByApp = null;
227 EcompPortalUtils.setBadPermissions(user, response, "getUserApps");
229 rolesByApp = adminRolesService.getRolesByApp(appId);
231 } catch (Exception e) {
232 logger.error(EELFLoggerDelegate.errorLogger, "getRolesByApp failed", e);