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.Date;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.List;
28 import org.openecomp.portalapp.command.EPLoginBean;
29 import org.openecomp.portalapp.portal.domain.EPUser;
30 import org.openecomp.portalapp.portal.domain.EPUserApp;
31 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
32 import org.openecomp.portalapp.portal.logging.format.EPAppMessagesEnum;
33 import org.openecomp.portalapp.portal.logging.logic.EPLogUtil;
34 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
35 import org.openecomp.portalapp.util.EPUserUtils;
36 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
37 import org.openecomp.portalsdk.core.menu.MenuBuilder;
38 import org.openecomp.portalsdk.core.service.DataAccessService;
39 import org.openecomp.portalsdk.core.service.support.FusionService;
40 import org.openecomp.portalsdk.core.util.SystemProperties;
41 import org.openecomp.portalsdk.core.web.support.AppUtils;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.context.annotation.EnableAspectJAutoProxy;
44 import org.springframework.stereotype.Service;
45 import org.springframework.transaction.annotation.Transactional;
47 @Service("eploginService")
49 @org.springframework.context.annotation.Configuration
50 @EnableAspectJAutoProxy
52 public class EPLoginServiceImpl extends FusionService implements EPLoginService {
53 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPLoginServiceImpl.class);
55 @SuppressWarnings("unused")
56 private MenuBuilder menuBuilder;
59 private DataAccessService dataAccessService;
62 @SuppressWarnings("rawtypes")
63 public EPLoginBean findUser(EPLoginBean bean, String menuPropertiesFilename, HashMap additionalParams ) throws Exception {
64 return findUser(bean, menuPropertiesFilename, additionalParams, true);
67 @SuppressWarnings("rawtypes")
68 public EPLoginBean findUser(EPLoginBean bean, String menuPropertiesFilename, HashMap additionalParams, boolean matchPassword) throws Exception {
70 EPUser userCopy = null;
72 if (bean.getOrgUserId() != null) {
73 user = (EPUser) findUser(bean);
76 user = (EPUser) findUser(bean.getLoginId(), bean.getLoginPwd());
78 user = (EPUser) findUserWithoutPwd(bean.getLoginId());
83 // raise an error if the application is locked and the user does not have system administrator privileges
84 if (AppUtils.isApplicationLocked() && !EPUserUtils.hasRole(user, SystemProperties.getProperty(SystemProperties.SYS_ADMIN_ROLE_ID))) {
85 bean.setLoginErrorMessage(SystemProperties.MESSAGE_KEY_LOGIN_ERROR_APPLICATION_LOCKED);
86 EPLogUtil.logEcompError(EPAppMessagesEnum.BeUserAdminPrivilegesInfo, user.getLoginId());
89 // raise an error if the user is inactive
90 if (!user.getActive()) {
91 bean.setLoginErrorMessage(SystemProperties.MESSAGE_KEY_LOGIN_ERROR_USER_INACTIVE);
92 EPLogUtil.logEcompError(EPAppMessagesEnum.BeUserInactiveWarning, user.getLoginId());
96 * Original SDK from QUANTUM
97 boolean hasActiveRole = false;
98 Iterator roles = user.getRoles().iterator();
99 while (roles.hasNext()) {
100 Role role = (Role) roles.next();
101 hasActiveRole = true;
102 if (role.getActive()) {
108 // raise an error if no active roles exist for the user
109 if (!userHasActiveRoles(user.getEPUserApps())) {
110 bean.setLoginErrorMessage(SystemProperties.MESSAGE_KEY_LOGIN_ERROR_USER_INACTIVE);
113 // only login the user if no errors have occurred
114 if (bean.getLoginErrorMessage() == null) {
116 // this will be a snapshot of the user's information as retrieved from the database
117 userCopy = (EPUser) user.clone();
119 // update the last logged in date for the user
120 user.setLastLoginDate(new Date());
121 getDataAccessService().saveDomainObject(user, additionalParams);
123 // update the audit log of the user
124 // Check for the client device type and set log attributes appropriately
126 // save the above changes to the User and their audit trail
128 // create the application menu based on the user's privileges
129 Set appMenu = getMenuBuilder().getMenu(SystemProperties.getProperty(SystemProperties.APPLICATION_MENU_SET_NAME), dataAccessService);
130 bean.setMenu(appMenu != null ? appMenu : new HashSet());
131 Set businessDirectMenu = getMenuBuilder().getMenu(SystemProperties.getProperty(SystemProperties.BUSINESS_DIRECT_MENU_SET_NAME), dataAccessService);
132 bean.setBusinessDirectMenu(businessDirectMenu != null ? businessDirectMenu : new HashSet());
134 bean.setUser(userCopy);
138 EPLogUtil.logEcompError(EPAppMessagesEnum.BeUserMissingError, bean.getOrgUserId());
145 private boolean userHasActiveRoles(Set<EPUserApp> userApps) {
146 for (EPUserApp userApp : userApps) {
147 if (userApp.getRole().getActive()) {
154 @SuppressWarnings("rawtypes")
155 public EPLoginBean findUserWithoutPassword(EPLoginBean bean, String menuPropertiesFilename, HashMap additionalParams ) throws Exception {
156 return findUser(bean, menuPropertiesFilename, additionalParams, false);
159 public EPUser findUser(String loginId, String password) {
162 StringBuffer criteria = new StringBuffer();
163 criteria.append(" where login_id = '").append(loginId).append("'")
164 .append(" and login_pwd = '").append(password).append("'");
167 list = getDataAccessService().getList(EPUser.class, criteria.toString(), null, null);
168 } catch (Exception e) {
169 EPLogUtil.logEcompError(EPAppMessagesEnum.BeDaoSystemError);
170 String stackTrace = EcompPortalUtils.getStackTrace(e);
171 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while executing findUser operation. Details: " + stackTrace);
174 return (list == null || list.size() == 0) ? null : (EPUser) list.get(0);
178 public EPUser findUserWithoutPwd(String loginId) {
181 StringBuffer criteria = new StringBuffer();
182 criteria.append(" where login_id = '").append(loginId).append("'");
185 list = getDataAccessService().getList(EPUser.class, criteria.toString(), null, null);
186 } catch (Exception e) {
187 EPLogUtil.logEcompError(EPAppMessagesEnum.BeDaoSystemError);
188 String stackTrace = EcompPortalUtils.getStackTrace(e);
189 String message = "Exception occurred while performing findUser: '" + loginId + "'. Details: ";
190 logger.error(EELFLoggerDelegate.errorLogger, message + stackTrace);
193 return (list == null || list.size() == 0) ? null : (EPUser)list.get(0);
197 public EPUser findUser(EPLoginBean bean) {
200 StringBuffer criteria = new StringBuffer();
201 criteria.append(" where org_user_id = '").append(bean.getOrgUserId()).append("'");
204 list = getDataAccessService().getList(EPUser.class, criteria.toString(), null, null);
205 } catch (Exception e) {
206 EPLogUtil.logEcompError(EPAppMessagesEnum.BeDaoSystemError);
207 String stackTrace = EcompPortalUtils.getStackTrace(e);
208 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while executing findUser operation. Details: " + stackTrace);
211 return (list == null || list.size() == 0) ? null : (EPUser)list.get(0);
215 public MenuBuilder getMenuBuilder() {
216 return new MenuBuilder();
220 public void setMenuBuilder(MenuBuilder menuBuilder) {
221 this.menuBuilder = menuBuilder;
225 public DataAccessService getDataAccessService() {
226 return dataAccessService;
230 public void setDataAccessService(DataAccessService dataAccessService) {
231 this.dataAccessService = dataAccessService;