2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
7 * Modification Copyright © 2020 IBM.
8 * ===================================================================
10 * Unless otherwise specified, all software contained herein is licensed
11 * under the Apache License, Version 2.0 (the "License");
12 * you may not use this software except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * Unless otherwise specified, all documentation contained herein is licensed
24 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25 * you may not use this documentation except in compliance with the License.
26 * You may obtain a copy of the License at
28 * https://creativecommons.org/licenses/by/4.0/
30 * Unless required by applicable law or agreed to in writing, documentation
31 * distributed under the License is distributed on an "AS IS" BASIS,
32 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33 * See the License for the specific language governing permissions and
34 * limitations under the License.
36 * ============LICENSE_END============================================
40 package org.onap.portalapp.portal.controller;
42 import java.util.List;
44 import javax.servlet.http.HttpServletRequest;
45 import javax.servlet.http.HttpServletResponse;
47 import org.onap.portalapp.controller.EPRestrictedBaseController;
48 import org.onap.portalapp.portal.domain.EPRole;
49 import org.onap.portalapp.portal.domain.EPUser;
50 import org.onap.portalapp.portal.domain.EcompAuditLog;
51 import org.onap.portalapp.portal.logging.aop.EPAuditLog;
52 import org.onap.portalapp.portal.logging.aop.EPEELFLoggerAdvice;
53 import org.onap.portalapp.portal.logging.logic.EPLogUtil;
54 import org.onap.portalapp.portal.service.AdminRolesService;
55 import org.onap.portalapp.portal.service.PortalAdminService;
56 import org.onap.portalapp.portal.transport.FieldsValidator;
57 import org.onap.portalapp.portal.transport.PortalAdmin;
58 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
59 import org.onap.portalapp.portal.utils.EcompPortalUtils;
60 import org.onap.portalapp.util.EPUserUtils;
61 import org.onap.portalapp.validation.DataValidator;
62 import org.onap.portalapp.validation.SecureString;
63 import org.onap.portalsdk.core.domain.AuditLog;
64 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
65 import org.onap.portalsdk.core.service.AuditService;
66 import org.onap.portalsdk.core.util.SystemProperties;
68 import org.springframework.beans.factory.annotation.Autowired;
69 import org.springframework.context.annotation.Configuration;
70 import org.springframework.context.annotation.EnableAspectJAutoProxy;
71 import org.springframework.web.bind.annotation.PathVariable;
72 import org.springframework.web.bind.annotation.RequestBody;
73 import org.springframework.web.bind.annotation.GetMapping;
74 import org.springframework.web.bind.annotation.PostMapping;
75 import org.springframework.web.bind.annotation.DeleteMapping;
76 import org.springframework.web.bind.annotation.RestController;
80 @EnableAspectJAutoProxy
82 public class PortalAdminController extends EPRestrictedBaseController {
83 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PortalAdminController.class);
84 private static final DataValidator DATA_VALIDATOR = new DataValidator();
86 private PortalAdminService portalAdminService;
87 private AdminRolesService adminRolesService;
88 private AuditService auditService;
91 public PortalAdminController(PortalAdminService portalAdminService,
92 AdminRolesService adminRolesService, AuditService auditService){
93 this.portalAdminService = portalAdminService;
94 this.adminRolesService = adminRolesService;
95 this.auditService = auditService;
98 @GetMapping(value = { "/portalApi/portalAdmins" }, produces = "application/json")
99 public List<PortalAdmin> getPortalAdmins(HttpServletRequest request, HttpServletResponse response) {
100 EPUser user = EPUserUtils.getUserSession(request);
101 List<PortalAdmin> portalAdmins = null;
103 logger.debug(EELFLoggerDelegate.debugLogger, "PortalAdminController.getPortalAdmins, null user");
104 EcompPortalUtils.setBadPermissions(user, response, "getPortalAdmins");
105 } else if (!adminRolesService.isSuperAdmin(user)) {
106 logger.debug(EELFLoggerDelegate.debugLogger, "PortalAdminController.getPortalAdmins, bad permissions");
107 EcompPortalUtils.setBadPermissions(user, response, "createPortalAdmin");
109 // return the list of portal admins
110 portalAdmins = portalAdminService.getPortalAdmins();
111 logger.debug(EELFLoggerDelegate.debugLogger, "portalAdmins: called getPortalAdmins()");
112 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/getPortalAdmins", "result =", portalAdmins);
119 * RESTful service method to create a new portal admin. Requirement: you
120 * must be the ONAP portal super admin user.
124 * @return FieldsValidator
126 @PostMapping(value = { "/portalApi/portalAdmin" })
127 public FieldsValidator createPortalAdmin(HttpServletRequest request, @RequestBody String userId,
128 HttpServletResponse response) {
129 EPUser user = EPUserUtils.getUserSession(request);
130 FieldsValidator fieldsValidator = null;
131 if(!DATA_VALIDATOR.isValid(new SecureString(userId))){
132 logger.debug(EELFLoggerDelegate.debugLogger, "PortalAdminController.createPortalAdmin not valid userId");
133 EcompPortalUtils.setBadPermissions(user, response, "createPortalAdmin");
134 }else if (user == null) {
135 logger.debug(EELFLoggerDelegate.debugLogger, "PortalAdminController.createPortalAdmin, null user");
136 EcompPortalUtils.setBadPermissions(user, response, "createPortalAdmin");
137 } else if (!adminRolesService.isSuperAdmin(user)) {
138 logger.debug(EELFLoggerDelegate.debugLogger, "PortalAdminController.createPortalAdmin bad permissions");
139 EcompPortalUtils.setBadPermissions(user, response, "createPortalAdmin");
141 fieldsValidator = portalAdminService.createPortalAdmin(userId);
142 int statusCode = fieldsValidator.httpStatusCode.intValue();
143 response.setStatus(statusCode);
144 if (statusCode == 200) {
145 AuditLog auditLog = new AuditLog();
146 auditLog.setUserId(user.getId());
147 auditLog.setActivityCode(EcompAuditLog.CD_ACTIVITY_ADD_PORTAL_ADMIN);
148 auditLog.setAffectedRecordId(userId);
150 auditService.logActivity(auditLog, null);
151 } catch (Exception e) {
152 logger.error(EELFLoggerDelegate.errorLogger, "createPortalAdmin: failed for save audit log", e);
154 String auditMessageInfo = EPLogUtil.formatAuditLogMessage("PortalAdminController.createPortalAdmin",
155 EcompAuditLog.CD_ACTIVITY_ADD_PORTAL_ADMIN, user.getOrgUserId(), userId,
156 "A new Portal Admin has been added");
158 EPLogUtil.logAuditMessage(logger, auditMessageInfo);
161 EcompPortalUtils.logAndSerializeObject(logger, "/portalAdmin", "POST result =", response.getStatus());
163 return fieldsValidator;
166 @DeleteMapping(value = { "/portalApi/portalAdmin/{userInfo}" })
167 public FieldsValidator deletePortalAdmin(HttpServletRequest request, @PathVariable("userInfo") String userInfo,
168 HttpServletResponse response) {
170 if(!DATA_VALIDATOR.isValid(new SecureString(userInfo))){
171 logger.debug(EELFLoggerDelegate.debugLogger, "PortalAdminController.deletePortalAdmin not valid userId");
175 int userIdIdx = userInfo.indexOf("-");
178 FieldsValidator fieldsValidator = null;
180 if (userIdIdx == -1) {
181 logger.error(EELFLoggerDelegate.errorLogger, "deletePortalAdmin missing userId");
182 return fieldsValidator;
184 String userIdStr = userInfo.substring(0, userIdIdx);
185 userId = Long.valueOf(userIdStr);
186 sbcid = userInfo.substring(userIdIdx + 1, userInfo.length());
188 } catch (Exception e) {
189 logger.error(EELFLoggerDelegate.errorLogger, "deletePortalAdmin error while parsing the userInfo", e);
191 EPUser user = EPUserUtils.getUserSession(request);
192 if (!adminRolesService.isSuperAdmin(user)) {
193 EcompPortalUtils.setBadPermissions(user, response, "deletePortalAdmin");
195 fieldsValidator = portalAdminService.deletePortalAdmin(userId);
196 int statusCode = fieldsValidator.httpStatusCode.intValue();
197 response.setStatus(statusCode);
198 if (statusCode == 200) {
199 AuditLog auditLog = new AuditLog();
200 auditLog.setUserId(user.getId());
201 auditLog.setActivityCode(EcompAuditLog.CD_ACTIVITY_DELETE_PORTAL_ADMIN);
202 auditLog.setAffectedRecordId(sbcid);
203 auditService.logActivity(auditLog, null);
204 String auditMessageInfo = EPLogUtil.formatAuditLogMessage("PortalAdminController.deletePortalAdmin",
205 EcompAuditLog.CD_ACTIVITY_DELETE_PORTAL_ADMIN, user.getOrgUserId(), sbcid,
206 "A Portal Admin has been deleted");
208 EPLogUtil.logAuditMessage(logger, auditMessageInfo);
212 EcompPortalUtils.logAndSerializeObject(logger, "/portalAdmin", "DELETE result =", response.getStatus());
214 return fieldsValidator;
217 @GetMapping(value = {
218 "/portalApi/adminAppsRoles/{appId}" }, produces = "application/json")
219 public List<EPRole> getRolesByApp(HttpServletRequest request, @PathVariable("appId") Long appId,
220 HttpServletResponse response) {
221 EPUser user = EPUserUtils.getUserSession(request);
222 List<EPRole> rolesByApp = null;
226 EcompPortalUtils.setBadPermissions(user, response, "getUserApps");
228 rolesByApp = adminRolesService.getRolesByApp(user, appId);
230 } catch (Exception e) {
231 logger.error(EELFLoggerDelegate.errorLogger, "getRolesByApp failed", e);