nexus site path corrected
[portal.git] / ecomp-portal-BE / src / main / java / org / openecomp / portalapp / portal / service / EPLoginServiceImpl.java
1 /*-
2  * ================================================================================
3  * eCOMP Portal
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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  * ================================================================================
19  */
20 package org.openecomp.portalapp.portal.service;
21
22 import java.util.Date;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Set;
27
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;
46
47 @Service("eploginService")
48 @Transactional
49 @org.springframework.context.annotation.Configuration
50 @EnableAspectJAutoProxy
51 @EPMetricsLog
52 public class EPLoginServiceImpl extends FusionService implements EPLoginService {
53     EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPLoginServiceImpl.class);
54
55     @SuppressWarnings("unused")
56         private MenuBuilder  menuBuilder;
57  
58     @Autowired
59         private DataAccessService  dataAccessService;
60
61
62     @SuppressWarnings("rawtypes")
63         public EPLoginBean findUser(EPLoginBean bean, String menuPropertiesFilename, HashMap additionalParams ) throws Exception {
64         return findUser(bean, menuPropertiesFilename, additionalParams, true);
65     }
66
67         @SuppressWarnings("rawtypes")
68         public EPLoginBean findUser(EPLoginBean bean, String menuPropertiesFilename, HashMap additionalParams, boolean matchPassword) throws Exception {
69                 EPUser user = null;
70                 EPUser userCopy = null;
71
72                 if (bean.getOrgUserId() != null) {
73                         user = (EPUser) findUser(bean);
74                 } else {
75                         if (matchPassword)
76                                 user = (EPUser) findUser(bean.getLoginId(), bean.getLoginPwd());
77                         else
78                                 user = (EPUser) findUserWithoutPwd(bean.getLoginId());
79                 }
80
81                 if (user != null) {
82
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());
87                         }
88
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());
93                         }
94
95 /*
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()) {
103                                         break;
104                                 }
105                         }
106  */
107
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);
111                         }
112
113                         // only login the user if no errors have occurred
114                         if (bean.getLoginErrorMessage() == null) {
115
116                                 // this will be a snapshot of the user's information as retrieved from the database
117                                 userCopy = (EPUser) user.clone();
118
119                                 // update the last logged in date for the user
120                                 user.setLastLoginDate(new Date());
121                                 getDataAccessService().saveDomainObject(user, additionalParams);
122
123                                 // update the audit log of the user
124                                 // Check for the client device type and set log attributes appropriately
125
126                                 // save the above changes to the User and their audit trail
127
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());
133
134                                 bean.setUser(userCopy);
135                         }
136
137                 } else {
138                         EPLogUtil.logEcompError(EPAppMessagesEnum.BeUserMissingError, bean.getOrgUserId());
139                 }
140                         
141
142                 return bean;
143         }
144
145         private boolean userHasActiveRoles(Set<EPUserApp> userApps) {
146                 for (EPUserApp userApp : userApps) {
147                         if (userApp.getRole().getActive()) {
148                                 return true;
149                         }
150                 }
151                 return false;
152         }
153
154         @SuppressWarnings("rawtypes")
155         public EPLoginBean findUserWithoutPassword(EPLoginBean bean, String menuPropertiesFilename, HashMap additionalParams ) throws Exception {
156         return findUser(bean, menuPropertiesFilename, additionalParams, false);
157     }
158         
159     public EPUser findUser(String loginId, String password) {
160       List<?>      list     = null;
161
162       StringBuffer criteria = new StringBuffer();
163       criteria.append(" where login_id = '").append(loginId).append("'")
164               .append(" and login_pwd = '").append(password).append("'");
165       
166       try {
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);
172       }
173
174       return (list == null || list.size() == 0) ? null : (EPUser) list.get(0);
175     }
176     
177     @Override
178     public EPUser findUserWithoutPwd(String loginId) {
179         List<?>      list     = null;
180
181         StringBuffer criteria = new StringBuffer();
182         criteria.append(" where login_id = '").append(loginId).append("'");
183         
184         try {
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);
191         }
192
193         return (list == null || list.size() == 0) ? null : (EPUser)list.get(0);
194       }
195
196
197     public EPUser findUser(EPLoginBean bean) {
198       List<?>          list = null;
199
200       StringBuffer criteria = new StringBuffer();
201       criteria.append(" where org_user_id = '").append(bean.getOrgUserId()).append("'");
202       
203       try { 
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);
209       }
210
211       return (list == null || list.size() == 0) ? null : (EPUser)list.get(0);
212     }
213
214
215     public MenuBuilder getMenuBuilder() {
216         return new MenuBuilder();
217     }
218
219
220     public void setMenuBuilder(MenuBuilder menuBuilder) {
221         this.menuBuilder = menuBuilder;
222     }
223
224     
225     public DataAccessService getDataAccessService() {
226                 return dataAccessService;
227         }
228
229
230         public void setDataAccessService(DataAccessService dataAccessService) {
231                 this.dataAccessService = dataAccessService;
232         }
233
234
235 }