1ec71d78435ad951e22a9b488731320e4b15771a
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / service / EPLoginServiceImpl.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalapp.portal.service;
39
40 import java.util.Date;
41 import java.util.HashMap;
42 import java.util.HashSet;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.Set;
46
47 import org.apache.commons.lang.StringUtils;
48 import org.onap.portalapp.command.EPLoginBean;
49 import org.onap.portalapp.portal.domain.EPUser;
50 import org.onap.portalapp.portal.logging.aop.EPMetricsLog;
51 import org.onap.portalapp.portal.logging.format.EPAppMessagesEnum;
52 import org.onap.portalapp.portal.logging.logic.EPLogUtil;
53 import org.onap.portalapp.util.EPUserUtils;
54 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
55 import org.onap.portalsdk.core.menu.MenuBuilder;
56 import org.onap.portalsdk.core.onboarding.exception.CipherUtilException;
57 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
58 import org.onap.portalsdk.core.service.DataAccessService;
59 import org.onap.portalsdk.core.service.support.FusionService;
60 import org.onap.portalsdk.core.util.SystemProperties;
61 import org.onap.portalsdk.core.web.support.AppUtils;
62 import org.springframework.beans.factory.annotation.Autowired;
63 import org.springframework.context.annotation.EnableAspectJAutoProxy;
64 import org.springframework.stereotype.Service;
65 import org.springframework.transaction.annotation.Transactional;
66
67 @Service("eploginService")
68 @Transactional
69 @org.springframework.context.annotation.Configuration
70 @EnableAspectJAutoProxy
71 @EPMetricsLog
72 public class EPLoginServiceImpl extends FusionService implements EPLoginService {
73         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPLoginServiceImpl.class);
74
75         @Autowired
76         private DataAccessService dataAccessService;
77
78         /*
79          * (non-Javadoc)
80          * @see org.onap.portalapp.portal.service.EPLoginService#findUser(org.openecomp.portalapp.command.EPLoginBean, java.lang.String, java.util.HashMap)
81          */
82         @SuppressWarnings("rawtypes")
83         public EPLoginBean findUser(EPLoginBean bean, String menuPropertiesFilename, HashMap additionalParams)
84                         throws Exception {
85                 return findUser(bean, menuPropertiesFilename, additionalParams, true);
86         }
87
88         /*
89          * (non-Javadoc)
90          * @see org.onap.portalapp.portal.service.EPLoginService#findUser(org.onap.portalapp.command.EPLoginBean, java.lang.String, java.util.HashMap, boolean)
91          */
92         @SuppressWarnings("rawtypes")
93         public EPLoginBean findUser(EPLoginBean bean, String menuPropertiesFilename_ignored, HashMap additionalParams,
94                         boolean matchPassword) throws Exception {
95                 EPUser user = null;
96                 EPUser userCopy = null;
97
98                 if (bean.getOrgUserId() != null) {
99                         user = (EPUser) findUser(bean);
100                 } else {
101                         if (matchPassword)
102                                 user = (EPUser) findUser(bean.getLoginId(), bean.getLoginPwd());
103                         else
104                                 user = (EPUser) findUserWithoutPwd(bean.getLoginId());
105                 }
106
107                 // run this command to fetch more information from the lazily loaded
108                 // object
109
110                 // This is funny - commenting out the following method call
111                 // 1. What are we doing with the returned values of the following two
112                 // methods? Nothing.
113                 // 2. Use a guest user scenario - user object will be null - clealry,
114                 // NPE.
115                 // 3. A check of if(user !=null) is made AFTER these bogus calls :) - If
116                 // these calls WERE doing anything significat (which they are not),
117                 // shouln't they have been moved inside that if check?
118
119                 // user.getEPUserApps();
120
121                 // Comments
122                 // 1. This method is clearly doing more than 'getting roles' - Not a
123                 // good name -
124                 // 2. Also, there is no null check - guest user scenarios will break the
125                 // code with NPE - added the check - Do not want to remove the call
126                 // altogether - not sure how it will effect things.
127
128                 if (user != null) {
129                         user.getEPRoles();
130
131                         // raise an error if the portal application is locked and the user
132                         // does not
133                         // have system administrator privileges
134                         if (AppUtils.isApplicationLocked()
135                                         && !EPUserUtils.hasRole(user, SystemProperties.getProperty(SystemProperties.SYS_ADMIN_ROLE_ID))) {
136                                 bean.setLoginErrorMessage(SystemProperties.MESSAGE_KEY_LOGIN_ERROR_APPLICATION_LOCKED);
137                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeUserAdminPrivilegesInfo, user.getLoginId());
138                         }
139
140                         // raise an error if the user is inactive
141                         if (!user.getActive()) {
142                                 bean.setLoginErrorMessage(SystemProperties.MESSAGE_KEY_LOGIN_ERROR_USER_INACTIVE);
143                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeUserInactiveWarning, user.getLoginId());
144                         }
145
146                         // only login the user if no errors have occurred
147                         if (bean.getLoginErrorMessage() == null) {
148
149                                 // this will be a snapshot of the user's information as
150                                 // retrieved from the database
151                                 userCopy = (EPUser) user.clone();
152
153                                 // update the last logged in date for the user
154                                 user.setLastLoginDate(new Date());
155                                 getDataAccessService().saveDomainObject(user, additionalParams);
156
157                                 // create the application menu based on the user's privileges
158                                 MenuBuilder menuBuilder = new MenuBuilder();
159                                 Set appMenu = menuBuilder.getMenu(
160                                                 SystemProperties.getProperty(SystemProperties.APPLICATION_MENU_SET_NAME), dataAccessService);
161                                 bean.setMenu(appMenu != null ? appMenu : new HashSet());
162                                 Set businessDirectMenu = menuBuilder.getMenu(
163                                                 SystemProperties.getProperty(SystemProperties.BUSINESS_DIRECT_MENU_SET_NAME),
164                                                 dataAccessService);
165                                 bean.setBusinessDirectMenu(businessDirectMenu != null ? businessDirectMenu : new HashSet());
166
167                                 bean.setUser(userCopy);
168                         }
169
170                 } else {
171                         EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeUserMissingError, bean.getOrgUserId());
172                 }
173
174                 return bean;
175         }
176
177         /**
178          * Searches the fn_user table for a row that matches the specified login_id
179          * and login_pwd values.
180          * 
181          * @param loginId
182          * @param password
183          * @return EPUser object; null on error or if no match.
184          */
185         @SuppressWarnings({ "rawtypes", "unchecked" })
186         private EPUser findUser(String loginId, String password) {
187                 Map<String, String> params = new HashMap<>();
188                 params.put("login_id", loginId);
189                 List<EPUser> list = null;
190                 try {
191                         list = dataAccessService.executeNamedQuery("getEPUserByLoginId", params, new HashMap());
192                         for (EPUser user : list) {
193                                 try {
194                                         if (StringUtils.isNotBlank(user.getLoginPwd())) {
195                                                 final String dbDecryptedPwd = CipherUtil.decryptPKC(user.getLoginPwd());
196                                                 if (dbDecryptedPwd.equals(password)) {
197                                                         return user;
198                                                 }
199                                         }
200                                 } catch (CipherUtilException e) {
201                                         logger.error(EELFLoggerDelegate.errorLogger, "findUser() failed", e);
202                                 }
203                         }
204
205                 } catch (Exception e) {
206                         EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError, e);
207                         logger.error(EELFLoggerDelegate.errorLogger, "findUser failed on " + loginId, e);
208                 }
209                 return null;
210         }
211
212         @SuppressWarnings("rawtypes")
213         public EPUser findUserWithoutPwd(String loginId) {
214                 Map<String, String> params = new HashMap<>();
215                 params.put("login_id", loginId);
216                 List list = null;
217                 try {
218                         list = dataAccessService.executeNamedQuery("getEPUserByLoginId", params, new HashMap());
219                 } catch (Exception e) {
220                         EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError, e);
221                         logger.error(EELFLoggerDelegate.errorLogger, "findUserWithoutPwd failed on " + loginId, e);
222                 }
223                 return (list == null || list.isEmpty()) ? null : (EPUser) list.get(0);
224         }
225
226         /**
227          * Searches the fn_user table for a row that matches the value of the bean's
228          * Organization User ID property.
229          * 
230          * @param bean
231          * @return EPUser object; null on error or if no match.
232          */
233         @SuppressWarnings("rawtypes")
234         private EPUser findUser(EPLoginBean bean) {
235                 Map<String, String> params = new HashMap<>();
236                 params.put("org_user_id", bean.getOrgUserId());
237                 List list = null;
238                 try {
239                         list = dataAccessService.executeNamedQuery("getEPUserByOrgUserId", params, new HashMap());
240                 } catch (Exception e) {
241                         EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError, e);
242                         logger.error(EELFLoggerDelegate.errorLogger, "findUser(EPLoginBean) failed", e);
243                 }
244                 return (list == null || list.isEmpty()) ? null : (EPUser) list.get(0);
245         }
246
247         public DataAccessService getDataAccessService() {
248                 return dataAccessService;
249         }
250
251         public void setDataAccessService(DataAccessService dataAccessService) {
252                 this.dataAccessService = dataAccessService;
253         }
254
255 }