2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.portalsdk.external.authorization.service;
40 import java.util.Date;
41 import java.util.HashMap;
42 import java.util.HashSet;
43 import java.util.Iterator;
44 import java.util.List;
48 import javax.servlet.http.HttpServletRequest;
50 import org.onap.portalsdk.core.command.LoginBean;
51 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
52 import org.onap.portalsdk.core.menu.MenuBuilder;
53 import org.onap.portalsdk.core.service.DataAccessService;
54 import org.onap.portalsdk.core.service.LoginServiceCentralizedImpl;
55 import org.onap.portalsdk.core.util.SystemProperties;
56 import org.onap.portalsdk.core.web.support.AppUtils;
57 import org.onap.portalsdk.core.web.support.UserUtils;
58 import org.onap.portalsdk.core.domain.Role;
59 import org.onap.portalsdk.core.domain.User;
60 import org.springframework.beans.factory.annotation.Autowired;
61 import org.springframework.stereotype.Service;
63 @Service("loginExternalAuthService")
64 public class LoginExternalAuthServiceImpl implements LoginExternalAuthService {
66 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(LoginServiceCentralizedImpl.class);
69 private DataAccessService dataAccessService;
72 private UserApiService userApiService;
75 public LoginBean findUser(LoginBean bean, String menuPropertiesFilename,
76 @SuppressWarnings("rawtypes") Map additionalParams, HttpServletRequest request) throws Exception {
77 return findUser(bean, menuPropertiesFilename, additionalParams, true, request);
81 @SuppressWarnings("rawtypes")
82 public LoginBean findUser(LoginBean bean, String menuPropertiesFilename, Map additionalParams,
83 boolean matchPassword, HttpServletRequest request) throws Exception {
86 if (bean.getUserid() != null) {
87 user = findUser(bean, request);
90 user = findUser(bean.getLoginId(), bean.getLoginPwd());
92 user = findUserWithoutPwd(bean.getLoginId());
96 if (AppUtils.isApplicationLocked()
97 && !UserUtils.hasRole(user, SystemProperties.getProperty(SystemProperties.SYS_ADMIN_ROLE_ID))) {
98 bean.setLoginErrorMessage(SystemProperties.MESSAGE_KEY_LOGIN_ERROR_APPLICATION_LOCKED);
101 // raise an error if the user is inactive
102 if (!user.getActive()) {
103 bean.setLoginErrorMessage(SystemProperties.MESSAGE_KEY_LOGIN_ERROR_USER_INACTIVE);
106 if (!userHasActiveRoles(user)) {
107 bean.setLoginErrorMessage(SystemProperties.MESSAGE_KEY_LOGIN_ERROR_USER_INACTIVE);
109 // only login the user if no errors have occurred
110 if (bean.getLoginErrorMessage() == null) {
112 // this will be a snapshot of the user's information as
113 // retrieved from the database
114 User userCopy = null;
116 userCopy = (User) user.clone();
117 } catch (CloneNotSupportedException ex) {
119 logger.error(EELFLoggerDelegate.errorLogger, "findUser failed", ex);
122 User appuser = findUserWithoutPwd(user.getLoginId());
124 if (appuser == null && userHasRoleFunctions(user)) {
125 createUserIfNecessary(user);
127 appuser.setLastLoginDate(new Date());
129 // update the last logged in date for the user
130 dataAccessService.saveDomainObject(appuser, additionalParams);
132 // update the audit log of the user
133 // Check for the client device type and set log attributes
136 // save the above changes to the User and their audit trail
138 // create the application menu based on the user's privileges
140 Set appMenu = getMenuBuilder().getMenu(
141 SystemProperties.getProperty(SystemProperties.APPLICATION_MENU_SET_NAME), dataAccessService);
142 bean.setMenu(appMenu != null ? appMenu : new HashSet());
143 Set businessDirectMenu = getMenuBuilder().getMenu(
144 SystemProperties.getProperty(SystemProperties.BUSINESS_DIRECT_MENU_SET_NAME),
146 bean.setBusinessDirectMenu(businessDirectMenu != null ? businessDirectMenu : new HashSet());
148 bean.setUser(userCopy);
155 private void createUserIfNecessary(User user) {
156 logger.debug(EELFLoggerDelegate.debugLogger, "createUser: " + user.getOrgUserId());
157 User user1 = new User();
158 user1.setEmail(user.getEmail());
159 user1.setEmail(user.getEmail());
160 user1.setFirstName(user.getFirstName());
161 user1.setHrid(user.getHrid());
162 user1.setJobTitle(user.getJobTitle());
163 user1.setLastName(user.getLastName());
164 user1.setLoginId(user.getLoginId());
165 user1.setOrgManagerUserId(user.getOrgManagerUserId());
166 user1.setMiddleInitial(user.getMiddleInitial());
167 user1.setOrgCode(user.getOrgCode());
168 user1.setOrgId(user.getOrgId());
169 user1.setPhone(user.getPhone());
170 user1.setOrgUserId(user.getOrgUserId());
171 user1.setActive(user.getActive());
172 user1.setLastLoginDate(new Date());
175 dataAccessService.saveDomainObject(user1, null);
176 logger.debug(EELFLoggerDelegate.debugLogger, "createdUser Successfully: " + user.getOrgUserId());
177 } catch (Exception ex) {
178 logger.error(EELFLoggerDelegate.errorLogger, "createUserIfNecessary failed", ex);
183 private boolean userHasActiveRoles(User user) {
184 boolean hasActiveRole = false;
185 @SuppressWarnings("rawtypes")
186 Iterator roles = user.getRoles().iterator();
187 while (roles.hasNext()) {
188 Role role = (Role) roles.next();
189 if (role.getActive()) {
190 hasActiveRole = true;
194 return hasActiveRole;
197 private boolean userHasRoleFunctions(User user) {
198 boolean hasRoleFunctions = false;
199 @SuppressWarnings("rawtypes")
200 Iterator roles = user.getRoles().iterator();
201 while (roles.hasNext()) {
202 Role role = (Role) roles.next();
203 if (role.getActive() && role.getRoleFunctions() != null && !role.getRoleFunctions().isEmpty()) {
204 hasRoleFunctions = true;
208 return hasRoleFunctions;
211 private User findUser(LoginBean bean, HttpServletRequest request) throws Exception {
212 User user = userApiService.getUser(bean.getUserid(), request);
213 user.setId(getUserIdByOrgUserId(user.getOrgUserId()));
214 logger.debug(EELFLoggerDelegate.debugLogger, "findUser: Returning final user roles and permissions", user.toString());
218 private Long getUserIdByOrgUserId(String orgUserId) {
219 Map<String, String> params = new HashMap<>();
220 params.put("orgUserId", orgUserId);
221 @SuppressWarnings("rawtypes")
222 List list = dataAccessService.executeNamedQuery("getUserIdByorgUserId", params, null);
224 if (list != null && !list.isEmpty())
225 userId = (Long) list.get(0);
229 @SuppressWarnings("rawtypes")
230 private User findUser(String loginId, String password) {
231 Map<String, String> params = new HashMap<>();
232 params.put("login_id", loginId);
233 params.put("login_pwd", password);
234 List list = dataAccessService.executeNamedQuery("getUserByLoginIdLoginPwd", params, new HashMap());
235 return (list == null || list.isEmpty()) ? null : (User) list.get(0);
238 @SuppressWarnings("rawtypes")
240 public User findUserWithoutPwd(String loginId) {
241 Map<String, String> params = new HashMap<>();
242 params.put("org_user_id", loginId);
243 List list = dataAccessService.executeNamedQuery("getUserByOrgUserId", params, new HashMap());
244 return (list == null || list.isEmpty()) ? null : (User) list.get(0);
247 private MenuBuilder getMenuBuilder() {
248 return new MenuBuilder();