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.service;
22 import java.util.HashMap;
23 import java.util.List;
26 import javax.annotation.PostConstruct;
27 import javax.servlet.http.HttpServletResponse;
29 import org.hibernate.Session;
30 import org.hibernate.SessionFactory;
31 import org.hibernate.Transaction;
32 import org.openecomp.portalapp.portal.domain.EPApp;
33 import org.openecomp.portalapp.portal.domain.EPUser;
34 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
35 import org.openecomp.portalapp.portal.transport.ExternalAccessUser;
36 import org.openecomp.portalapp.portal.transport.FieldsValidator;
37 import org.openecomp.portalapp.portal.transport.PortalAdmin;
38 import org.openecomp.portalapp.portal.transport.PortalAdminUserRole;
39 import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;
40 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
41 import org.openecomp.portalapp.portal.utils.PortalConstants;
42 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
43 import org.openecomp.portalsdk.core.service.DataAccessService;
44 import org.openecomp.portalsdk.core.util.SystemProperties;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.context.annotation.EnableAspectJAutoProxy;
47 import org.springframework.http.HttpEntity;
48 import org.springframework.http.HttpHeaders;
49 import org.springframework.http.HttpMethod;
50 import org.springframework.stereotype.Service;
51 import org.springframework.web.client.RestTemplate;
53 import com.fasterxml.jackson.databind.ObjectMapper;
55 @Service("portalAdminService")
56 @org.springframework.context.annotation.Configuration
57 @EnableAspectJAutoProxy
59 public class PortalAdminServiceImpl implements PortalAdminService {
61 private String SYS_ADMIN_ROLE_ID = "1";
62 private String ECOMP_APP_ID = "1";
64 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PortalAdminServiceImpl.class);
67 private SessionFactory sessionFactory;
69 private DataAccessService dataAccessService;
71 SearchService searchService;
73 private EPAppService epAppService;
75 RestTemplate template = new RestTemplate();
79 SYS_ADMIN_ROLE_ID = SystemProperties.getProperty(SystemProperties.SYS_ADMIN_ROLE_ID);
80 ECOMP_APP_ID = SystemProperties.getProperty(EPCommonSystemProperties.ECOMP_APP_ID);
83 public List<PortalAdmin> getPortalAdmins() {
85 Map<String, String> params = new HashMap<>();
86 params.put("adminRoleId", SYS_ADMIN_ROLE_ID);
87 @SuppressWarnings("unchecked")
88 List<PortalAdmin> portalAdmins = (List<PortalAdmin>) dataAccessService.executeNamedQuery("getPortalAdmins",
90 logger.debug(EELFLoggerDelegate.debugLogger, "getPortalAdmins was successful");
92 } catch (Exception e) {
93 logger.error(EELFLoggerDelegate.errorLogger,
94 "Exception occurred while performing getPortalAdmins operation, Details: "
95 + EcompPortalUtils.getStackTrace(e));
100 @SuppressWarnings("unchecked")
101 public FieldsValidator createPortalAdmin(String orgUserId) {
102 FieldsValidator fieldsValidator = new FieldsValidator();
103 logger.debug(EELFLoggerDelegate.debugLogger, "LR: createPortalAdmin: test 1");
104 boolean result = false;
106 boolean createNewUser = false;
107 List<EPUser> localUserList = dataAccessService.getList(EPUser.class, " where orgUserId='" + orgUserId + "'",
109 if (localUserList.size() > 0) {
110 user = localUserList.get(0);
112 createNewUser = true;
115 if (user != null && isLoggedInUserPortalAdmin(user.getId())) {
116 fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_CONFLICT);
117 logger.error(EELFLoggerDelegate.errorLogger,
118 "User '" + user.getOrgUserId() + "' already has PortalAdmin role assigned.");
119 } else if (user != null || createNewUser) {
120 Session localSession = null;
121 Transaction transaction = null;
123 localSession = sessionFactory.openSession();
125 transaction = localSession.beginTransaction();
127 user = this.searchService.searchUserByUserId(orgUserId);
129 // insert the user with active true in order to
131 user.setActive(true);
132 localSession.save(EPUser.class.getName(), user);
136 Long userid = user.getId();
137 PortalAdminUserRole userRole = new PortalAdminUserRole();
138 userRole.userId = userid;
139 userRole.roleId = Long.valueOf(SYS_ADMIN_ROLE_ID);
140 userRole.appId = Long.valueOf(ECOMP_APP_ID);
142 localSession.save(PortalAdminUserRole.class.getName(), userRole);
145 transaction.commit();
146 // Add role in the external central auth system
147 result = addPortalAdminInExternalCentralAuth(user.getOrgUserId(), PortalConstants.PORTAL_ADMIN_ROLE);
148 } catch (Exception e) {
149 EcompPortalUtils.rollbackTransaction(transaction, "createPortalAdmin rollback, exception = " + e);
150 logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
152 EcompPortalUtils.closeLocalSession(localSession, "createPortalAdmin");
155 logger.debug(EELFLoggerDelegate.debugLogger,
156 "LR: createPortalAdmin: no result. setting httpStatusCode to "
157 + HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
158 fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
159 logger.error(EELFLoggerDelegate.errorLogger, "PortalAdminServiceImpl.createPortalAdmin: bad request");
162 return fieldsValidator;
165 private boolean addPortalAdminInExternalCentralAuth(String loginId, String portalAdminRole){
166 boolean result = false;
169 if (EPCommonSystemProperties.containsProperty(
170 EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN)) {
171 name = loginId + SystemProperties
172 .getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN);
174 EPApp app = epAppService.getApp(PortalConstants.PORTAL_APP_ID);
175 String extRole = app.getNameSpace()+"."+portalAdminRole.replaceAll(" ", "_");
176 ObjectMapper addUserRoleMapper = new ObjectMapper();
177 ExternalAccessUser extUser = new ExternalAccessUser(name, extRole);
178 String userRole = addUserRoleMapper.writeValueAsString(extUser);
179 HttpHeaders headers = EcompPortalUtils.base64encodeKeyForAAFBasicAuth();
181 HttpEntity<String> addUserRole = new HttpEntity<>(userRole, headers);
183 SystemProperties.getProperty(
184 EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL)
186 HttpMethod.POST, addUserRole, String.class);
188 } catch (Exception e) {
189 // This happens only if role already exists in external central access system but not in local DB thats where we logging here
190 if (e.getMessage().equalsIgnoreCase("409 Conflict")) {
192 logger.debug(EELFLoggerDelegate.debugLogger, "Portal Admin role already exists", e.getMessage());
194 logger.error(EELFLoggerDelegate.errorLogger, "Failed to add Portal Admin role ", e);
201 public FieldsValidator deletePortalAdmin(Long userId) {
202 FieldsValidator fieldsValidator = new FieldsValidator();
203 logger.debug(EELFLoggerDelegate.debugLogger, "deletePortalAdmin: test 1");
204 boolean result = false;
205 Session localSession = null;
206 Transaction transaction = null;
209 localSession = sessionFactory.openSession();
210 transaction = localSession.beginTransaction();
211 dataAccessService.deleteDomainObjects(PortalAdminUserRole.class,
212 "user_id='" + userId + "' AND role_id='" + SYS_ADMIN_ROLE_ID + "'", null);
213 transaction.commit();
214 result = deletePortalAdminInExternalCentralAuth(userId, PortalConstants.PORTAL_ADMIN_ROLE);
215 } catch (Exception e) {
216 EcompPortalUtils.rollbackTransaction(transaction, "deletePortalAdmin rollback, exception = " + e);
217 logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
219 EcompPortalUtils.closeLocalSession(localSession, "deletePortalAdmin");
223 logger.debug(EELFLoggerDelegate.debugLogger, "deletePortalAdmin: no result. setting httpStatusCode to "
224 + HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
225 fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
227 return fieldsValidator;
231 @SuppressWarnings("unchecked")
232 private boolean deletePortalAdminInExternalCentralAuth(Long userId, String portalAdminRole){
233 boolean result = false;
236 List<EPUser> localUserList = dataAccessService.getList(EPUser.class, " where user_id = " + userId,
238 if (EPCommonSystemProperties.containsProperty(
239 EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN)) {
240 name = localUserList.get(0).getOrgUserId() + SystemProperties
241 .getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN);
243 EPApp app = epAppService.getApp(PortalConstants.PORTAL_APP_ID);
244 String extRole = app.getNameSpace()+"."+portalAdminRole.replaceAll(" ", "_");
245 HttpHeaders headers = EcompPortalUtils.base64encodeKeyForAAFBasicAuth();
246 HttpEntity<String> addUserRole = new HttpEntity<>(headers);
248 SystemProperties.getProperty(
249 EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL)
250 + "userRole/"+name+"/"+extRole,
251 HttpMethod.DELETE, addUserRole, String.class);
253 } catch (Exception e) {
254 if (e.getMessage().equalsIgnoreCase("404 Not Found")) {
255 logger.debug(EELFLoggerDelegate.debugLogger, "Portal Admin role already deleted or may not be found", e.getMessage());
257 logger.error(EELFLoggerDelegate.errorLogger, "Failed to add Portal Admin role ", e);
264 private void logQuery(String sql) {
265 logger.debug(EELFLoggerDelegate.debugLogger, "Executing query: " + sql);
268 private boolean isLoggedInUserPortalAdmin(Long userId) {
270 String sql = "SELECT u.user_id, u.first_name, u.last_name, u.login_id "
271 + " FROM fn_user u, fn_user_role ur " + " WHERE u.user_id = ur.user_id " + " AND ur.user_id="
272 + userId + " AND ur.role_id=" + SYS_ADMIN_ROLE_ID;
276 @SuppressWarnings("unchecked")
277 List<PortalAdmin> portalAdmins = dataAccessService.executeSQLQuery(sql, PortalAdmin.class, null);
278 logger.debug(EELFLoggerDelegate.debugLogger, portalAdmins.toString());
279 if (portalAdmins == null || portalAdmins.size() <= 0) {
284 } catch (Exception e) {
285 logger.error(EELFLoggerDelegate.errorLogger,
286 "Exception occurred while performing isLoggedInUserPortalAdmin operation, Details: "
287 + EcompPortalUtils.getStackTrace(e));