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.EPUser;
33 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
34 import org.openecomp.portalapp.portal.transport.FieldsValidator;
35 import org.openecomp.portalapp.portal.transport.PortalAdmin;
36 import org.openecomp.portalapp.portal.transport.PortalAdminUserRole;
37 import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;
38 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
39 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
40 import org.openecomp.portalsdk.core.service.DataAccessService;
41 import org.openecomp.portalsdk.core.util.SystemProperties;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.context.annotation.EnableAspectJAutoProxy;
44 import org.springframework.stereotype.Service;
46 @Service("portalAdminService")
47 @org.springframework.context.annotation.Configuration
48 @EnableAspectJAutoProxy
50 public class PortalAdminServiceImpl implements PortalAdminService {
52 private String SYS_ADMIN_ROLE_ID = "1";
53 private String ECOMP_APP_ID = "1";
55 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PortalAdminServiceImpl.class);
58 private SessionFactory sessionFactory;
60 private DataAccessService dataAccessService;
62 SearchService searchService;
66 SYS_ADMIN_ROLE_ID = SystemProperties.getProperty(SystemProperties.SYS_ADMIN_ROLE_ID);
67 ECOMP_APP_ID = SystemProperties.getProperty(EPCommonSystemProperties.ECOMP_APP_ID);
70 public List<PortalAdmin> getPortalAdmins() {
72 Map<String, String> params = new HashMap<>();
73 params.put("adminRoleId", SYS_ADMIN_ROLE_ID);
74 @SuppressWarnings("unchecked")
75 List<PortalAdmin> portalAdmins = (List<PortalAdmin>) dataAccessService.executeNamedQuery("getPortalAdmins",
77 logger.debug(EELFLoggerDelegate.debugLogger, "getPortalAdmins was successful");
79 } catch (Exception e) {
80 logger.error(EELFLoggerDelegate.errorLogger,
81 "Exception occurred while performing getPortalAdmins operation, Details: "
82 + EcompPortalUtils.getStackTrace(e));
87 @SuppressWarnings("unchecked")
88 public FieldsValidator createPortalAdmin(String orgUserId) {
89 FieldsValidator fieldsValidator = new FieldsValidator();
90 logger.debug(EELFLoggerDelegate.debugLogger, "LR: createPortalAdmin: test 1");
91 boolean result = false;
93 boolean createNewUser = false;
94 List<EPUser> localUserList = dataAccessService.getList(EPUser.class, " where orgUserId='" + orgUserId + "'",
96 if (localUserList.size() > 0) {
97 user = localUserList.get(0);
102 if (user != null && isLoggedInUserPortalAdmin(user.getId())) {
103 fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_CONFLICT);
104 logger.error(EELFLoggerDelegate.errorLogger,
105 "User '" + user.getOrgUserId() + "' already has PortalAdmin role assigned.");
106 } else if (user != null || createNewUser) {
107 Session localSession = null;
108 Transaction transaction = null;
110 localSession = sessionFactory.openSession();
112 transaction = localSession.beginTransaction();
114 user = this.searchService.searchUserByUserId(orgUserId);
116 // insert the user with active true in order to
118 user.setActive(true);
119 localSession.save(EPUser.class.getName(), user);
123 Long userid = user.getId();
124 PortalAdminUserRole userRole = new PortalAdminUserRole();
125 userRole.userId = userid;
126 userRole.roleId = Long.valueOf(SYS_ADMIN_ROLE_ID);
127 userRole.appId = Long.valueOf(ECOMP_APP_ID);
129 localSession.save(PortalAdminUserRole.class.getName(), userRole);
132 transaction.commit();
134 } catch (Exception e) {
135 EcompPortalUtils.rollbackTransaction(transaction, "createPortalAdmin rollback, exception = " + e);
136 logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
138 EcompPortalUtils.closeLocalSession(localSession, "createPortalAdmin");
141 logger.debug(EELFLoggerDelegate.debugLogger,
142 "LR: createPortalAdmin: no result. setting httpStatusCode to "
143 + HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
144 fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
145 logger.error(EELFLoggerDelegate.errorLogger, "PortalAdminServiceImpl.createPortalAdmin: bad request");
148 return fieldsValidator;
151 public FieldsValidator deletePortalAdmin(Long userId) {
152 FieldsValidator fieldsValidator = new FieldsValidator();
153 logger.debug(EELFLoggerDelegate.debugLogger, "deletePortalAdmin: test 1");
154 boolean result = false;
155 Session localSession = null;
156 Transaction transaction = null;
159 localSession = sessionFactory.openSession();
160 transaction = localSession.beginTransaction();
161 dataAccessService.deleteDomainObjects(PortalAdminUserRole.class,
162 "user_id='" + userId + "' AND role_id='" + SYS_ADMIN_ROLE_ID + "'", null);
163 transaction.commit();
165 } catch (Exception e) {
166 EcompPortalUtils.rollbackTransaction(transaction, "deletePortalAdmin rollback, exception = " + e);
167 logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
169 EcompPortalUtils.closeLocalSession(localSession, "deletePortalAdmin");
173 logger.debug(EELFLoggerDelegate.debugLogger, "deletePortalAdmin: no result. setting httpStatusCode to "
174 + HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
175 fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
177 return fieldsValidator;
180 private void logQuery(String sql) {
181 logger.debug(EELFLoggerDelegate.debugLogger, "Executing query: " + sql);
184 private boolean isLoggedInUserPortalAdmin(Long userId) {
186 String sql = "SELECT u.user_id, u.first_name, u.last_name, u.login_id "
187 + " FROM fn_user u, fn_user_role ur " + " WHERE u.user_id = ur.user_id " + " AND ur.user_id="
188 + userId + " AND ur.role_id=" + SYS_ADMIN_ROLE_ID;
192 @SuppressWarnings("unchecked")
193 List<PortalAdmin> portalAdmins = dataAccessService.executeSQLQuery(sql, PortalAdmin.class, null);
194 logger.debug(EELFLoggerDelegate.debugLogger, portalAdmins.toString());
195 if (portalAdmins == null || portalAdmins.size() <= 0) {
200 } catch (Exception e) {
201 logger.error(EELFLoggerDelegate.errorLogger,
202 "Exception occurred while performing isLoggedInUserPortalAdmin operation, Details: "
203 + EcompPortalUtils.getStackTrace(e));