1 package org.openecomp.portalsdk.core.service;
4 import java.util.HashMap;
5 import java.util.HashSet;
6 import java.util.Iterator;
9 import java.util.SortedSet;
10 import java.util.TreeSet;
12 import org.openecomp.portalsdk.core.command.LoginBean;
13 import org.openecomp.portalsdk.core.domain.Role;
14 import org.openecomp.portalsdk.core.domain.RoleFunction;
15 import org.openecomp.portalsdk.core.domain.User;
16 import org.openecomp.portalsdk.core.domain.UserApp;
17 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
18 import org.openecomp.portalsdk.core.menu.MenuBuilder;
19 import org.openecomp.portalsdk.core.service.support.FusionService;
20 import org.openecomp.portalsdk.core.util.SystemProperties;
21 import org.openecomp.portalsdk.core.web.support.AppUtils;
22 import org.openecomp.portalsdk.core.web.support.UserUtils;
23 import org.springframework.beans.factory.annotation.Autowired;
25 import com.fasterxml.jackson.databind.ObjectMapper;
27 public class LoginServiceCentralizedImpl extends FusionService implements LoginService {
29 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(LoginServiceCentralizedImpl.class);
32 AppService appService;
35 private DataAccessService dataAccessService;
38 RestApiRequestBuilder restApiRequestBuilder;
40 @SuppressWarnings("unused")
41 private MenuBuilder menuBuilder;
44 public LoginBean findUser(LoginBean bean, String menuPropertiesFilename, HashMap additionalParams)
46 return findUser(bean, menuPropertiesFilename, additionalParams, true);
49 @SuppressWarnings("rawtypes")
50 public LoginBean findUser(LoginBean bean, String menuPropertiesFilename, HashMap additionalParams,
51 boolean matchPassword) throws Exception {
55 if (bean.getUserid() != null && bean.getUserid() != null) {
56 user = (User) findUser(bean);
59 user = (User) findUser(bean.getLoginId(), bean.getLoginPwd());
61 user = (User) findUserWithoutPwd(bean.getLoginId());
66 if (AppUtils.isApplicationLocked()
67 && !UserUtils.hasRole(user, SystemProperties.getProperty(SystemProperties.SYS_ADMIN_ROLE_ID))) {
68 bean.setLoginErrorMessage(SystemProperties.MESSAGE_KEY_LOGIN_ERROR_APPLICATION_LOCKED);
71 // raise an error if the user is inactive
72 if (!user.getActive()) {
73 bean.setLoginErrorMessage(SystemProperties.MESSAGE_KEY_LOGIN_ERROR_USER_INACTIVE);
76 if (!userHasActiveRoles(user)) {
77 bean.setLoginErrorMessage(SystemProperties.MESSAGE_KEY_LOGIN_ERROR_USER_INACTIVE);
79 // only login the user if no errors have occurred
80 if (bean.getLoginErrorMessage() == null) {
82 // this will be a snapshot of the user's information as
83 // retrieved from the database
84 userCopy = (User) user.clone();
86 User appuser = getUser(userCopy);
88 appuser.setLastLoginDate(new Date());
90 // update the last logged in date for the user
91 // user.setLastLoginDate(new Date());
92 getDataAccessService().saveDomainObject(appuser, additionalParams);
94 // update the audit log of the user
95 // Check for the client device type and set log attributes
98 // save the above changes to the User and their audit trail
100 // create the application menu based on the user's privileges
102 Set appMenu = getMenuBuilder().getMenu(
103 SystemProperties.getProperty(SystemProperties.APPLICATION_MENU_SET_NAME), dataAccessService);
104 bean.setMenu(appMenu != null ? appMenu : new HashSet());
105 System.out.println(appMenu);
106 Set businessDirectMenu = getMenuBuilder().getMenu(
107 SystemProperties.getProperty(SystemProperties.BUSINESS_DIRECT_MENU_SET_NAME),
109 bean.setBusinessDirectMenu(businessDirectMenu != null ? businessDirectMenu : new HashSet());
111 bean.setUser(userCopy);
118 private boolean userHasActiveRoles(User user) {
119 boolean hasActiveRole = false;
120 Iterator roles = user.getRoles().iterator();
121 while (roles.hasNext()) {
122 Role role = (Role) roles.next();
123 if (role.getActive()) {
124 hasActiveRole = true;
128 return hasActiveRole;
131 @SuppressWarnings("null")
132 public User findUser(LoginBean bean) throws Exception {
136 ObjectMapper mapper = new ObjectMapper();
137 HashSet<RoleFunction> rolefun = null;
139 String repsonse = restApiRequestBuilder.getViaREST("/getUser/" + bean.getUserid(), true, bean.getUserid());
141 user = mapper.readValue(repsonse, User.class);
143 @SuppressWarnings("unchecked")
144 Set<UserApp> setAppsObj = user.getUserApps();
146 Iterator<UserApp> it = setAppsObj.iterator();
147 while (it.hasNext()) {
148 Object next = it.next();
150 UserApp nextApp = mapper.convertValue(next, UserApp.class);
151 rolefun = new HashSet<>();
152 Role role = nextApp.getRole();
154 Set<RoleFunction> roleFunctionList = role.getRoleFunctions();
155 Set<RoleFunction> roleFunctionListNew = new HashSet<>();
156 Iterator<RoleFunction> itetaror = roleFunctionList.iterator();
157 while (itetaror.hasNext()) {
158 Object nextValue = itetaror.next();
159 RoleFunction roleFunction = mapper.convertValue(nextValue, RoleFunction.class);
160 roleFunctionListNew.add(roleFunction);
163 role.setRoleFunctions(roleFunctionListNew);
164 nextApp.setRole(role);
165 nextApp.getRole().getRoleFunctions();
166 SortedSet<UserApp> UserAppSet = new TreeSet<>();
167 UserAppSet.add(nextApp);
168 user.setUserApps(UserAppSet);
174 public User findUser(String loginId, String password) {
178 StringBuffer criteria = new StringBuffer();
179 criteria.append(" where login_id = '").append(loginId).append("'").append(" and login_pwd = '").append(password)
182 list = getDataAccessService().getList(User.class, criteria.toString(), null, null);
183 return (list == null || list.size() == 0) ? null : (User) list.get(0);
186 private User findUserWithoutPwd(String loginId) {
188 StringBuffer criteria = new StringBuffer();
189 criteria.append(" where login_id = '").append(loginId).append("'");
190 list = getDataAccessService().getList(User.class, criteria.toString(), null, null);
191 return (list == null || list.size() == 0) ? null : (User) list.get(0);
194 public DataAccessService getDataAccessService() {
195 return dataAccessService;
198 public void setDataAccessService(DataAccessService dataAccessService) {
199 this.dataAccessService = dataAccessService;
202 public MenuBuilder getMenuBuilder() {
203 return new MenuBuilder();
206 public void setMenuBuilder(MenuBuilder menuBuilder) {
207 this.menuBuilder = menuBuilder;
210 public User getUser(User user) {
213 StringBuffer criteria = new StringBuffer();
214 criteria.append(" where login_id = '").append(user.getLoginId()).append("'");
216 list = getDataAccessService().getList(User.class, criteria.toString(), null, null);
217 return (list == null || list.size() == 0) ? null : (User) list.get(0);