270369c50b2bba5ff4ec6454bdffad22b21bc17c
[portal/sdk.git] /
1 package org.openecomp.portalsdk.core.service;
2
3 import java.util.Date;
4 import java.util.HashMap;
5 import java.util.HashSet;
6 import java.util.Iterator;
7 import java.util.List;
8 import java.util.Set;
9 import java.util.SortedSet;
10 import java.util.TreeSet;
11
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;
24 import org.springframework.transaction.annotation.Transactional;
25
26 import com.fasterxml.jackson.databind.ObjectMapper;
27
28
29 @Transactional
30 public class LoginServiceCentralizedImpl extends FusionService implements LoginService {
31
32         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(LoginServiceCentralizedImpl.class);
33
34         @Autowired
35         AppService appService;
36
37         @Autowired
38         private DataAccessService dataAccessService;
39         
40         @Autowired
41         RestApiRequestBuilder restApiRequestBuilder;
42
43         @SuppressWarnings("unused")
44         private MenuBuilder menuBuilder;
45
46         @Override
47         public LoginBean findUser(LoginBean bean, String menuPropertiesFilename, HashMap additionalParams)
48                         throws Exception {
49                 return findUser(bean, menuPropertiesFilename, additionalParams, true);
50         }
51
52         @SuppressWarnings("rawtypes")
53         public LoginBean findUser(LoginBean bean, String menuPropertiesFilename, HashMap additionalParams,
54                         boolean matchPassword) throws Exception {
55                 User user = null;
56                 User userCopy = null;
57
58                 if (bean.getUserid() != null && bean.getUserid() != null) {
59                         user = (User) findUser(bean);
60                 } else {
61                         if (matchPassword)
62                                 user = (User) findUser(bean.getLoginId(), bean.getLoginPwd());
63                         else
64                                 user = (User) findUserWithoutPwd(bean.getLoginId());
65                 }
66
67                 if (user != null) {
68
69                         if (AppUtils.isApplicationLocked()
70                                         && !UserUtils.hasRole(user, SystemProperties.getProperty(SystemProperties.SYS_ADMIN_ROLE_ID))) {
71                                 bean.setLoginErrorMessage(SystemProperties.MESSAGE_KEY_LOGIN_ERROR_APPLICATION_LOCKED);
72                         }
73
74                         // raise an error if the user is inactive
75                         if (!user.getActive()) {
76                                 bean.setLoginErrorMessage(SystemProperties.MESSAGE_KEY_LOGIN_ERROR_USER_INACTIVE);
77                         }
78
79                         if (!userHasActiveRoles(user)) {
80                                 bean.setLoginErrorMessage(SystemProperties.MESSAGE_KEY_LOGIN_ERROR_USER_INACTIVE);
81                         }
82                         // only login the user if no errors have occurred
83                         if (bean.getLoginErrorMessage() == null) {
84
85                                 // this will be a snapshot of the user's information as
86                                 // retrieved from the database
87                                 userCopy = (User) user.clone();
88
89                                 User appuser = getUser(userCopy);
90
91                                 appuser.setLastLoginDate(new Date());
92
93                                 // update the last logged in date for the user
94                                 // user.setLastLoginDate(new Date());
95                                 getDataAccessService().saveDomainObject(appuser, additionalParams);
96
97                                 // update the audit log of the user
98                                 // Check for the client device type and set log attributes
99                                 // appropriately
100
101                                 // save the above changes to the User and their audit trail
102
103                                 // create the application menu based on the user's privileges
104
105                                 Set appMenu = getMenuBuilder().getMenu(
106                                                 SystemProperties.getProperty(SystemProperties.APPLICATION_MENU_SET_NAME), dataAccessService);
107                                 bean.setMenu(appMenu != null ? appMenu : new HashSet());
108                                 Set businessDirectMenu = getMenuBuilder().getMenu(
109                                                 SystemProperties.getProperty(SystemProperties.BUSINESS_DIRECT_MENU_SET_NAME),
110                                                 dataAccessService);
111                                 bean.setBusinessDirectMenu(businessDirectMenu != null ? businessDirectMenu : new HashSet());
112
113                                 bean.setUser(userCopy);
114                         }
115                 }
116
117                 return bean;
118         }
119
120         private boolean userHasActiveRoles(User user) {
121                 boolean hasActiveRole = false;
122                 Iterator roles = user.getRoles().iterator();
123                 while (roles.hasNext()) {
124                         Role role = (Role) roles.next();
125                         if (role.getActive()) {
126                                 hasActiveRole = true;
127                                 break;
128                         }
129                 }
130                 return hasActiveRole;
131         }
132
133         @SuppressWarnings("null")
134         public User findUser(LoginBean bean) throws Exception {
135
136                 User user = null;
137
138                 ObjectMapper mapper = new ObjectMapper();
139                 HashSet<RoleFunction> rolefun = null;
140
141                 String repsonse = restApiRequestBuilder.getViaREST("/user/" + bean.getUserid(), true, bean.getUserid());
142                 
143
144                 user = mapper.readValue(repsonse, User.class);
145                 
146                 Set<RoleFunction> roleFunctionListNew = new HashSet<>();
147                 Set<RoleFunction> roleFunctionList  = new HashSet<>();
148                 SortedSet<UserApp> UserAppSet = new TreeSet<>();
149
150                 @SuppressWarnings("unchecked")
151                 Set<UserApp> setAppsObj = user.getUserApps();
152
153                 Iterator<UserApp> it = setAppsObj.iterator();
154                 while (it.hasNext()) {
155                         Object next = it.next();
156
157                         UserApp nextApp = mapper.convertValue(next, UserApp.class);
158                         rolefun = new HashSet<>();
159                         Role role = nextApp.getRole();
160
161                          roleFunctionList = role.getRoleFunctions();
162                 
163                         Iterator<RoleFunction> itetaror = roleFunctionList.iterator();
164                         while (itetaror.hasNext()) {
165                                 Object nextValue = itetaror.next();
166                                 RoleFunction roleFunction = mapper.convertValue(nextValue, RoleFunction.class);
167                                 roleFunctionListNew.add(roleFunction);
168                         }
169
170                         role.setRoleFunctions(roleFunctionListNew);
171                         nextApp.setRole(role);
172                         nextApp.getRole().getRoleFunctions();
173                         
174                         UserAppSet.add(nextApp);
175                         user.setUserApps(UserAppSet);
176                 }
177
178                 return user;
179         }
180
181         public User findUser(String loginId, String password) {
182
183                 List list = null;
184
185                 StringBuffer criteria = new StringBuffer();
186                 criteria.append(" where login_id = '").append(loginId).append("'").append(" and login_pwd = '").append(password)
187                                 .append("'");
188
189                 list = getDataAccessService().getList(User.class, criteria.toString(), null, null);
190                 return (list == null || list.size() == 0) ? null : (User) list.get(0);
191         }
192
193         private User findUserWithoutPwd(String loginId) {
194                 List list = null;
195                 StringBuffer criteria = new StringBuffer();
196                 criteria.append(" where login_id = '").append(loginId).append("'");
197                 list = getDataAccessService().getList(User.class, criteria.toString(), null, null);
198                 return (list == null || list.size() == 0) ? null : (User) list.get(0);
199         }
200
201         public DataAccessService getDataAccessService() {
202                 return dataAccessService;
203         }
204
205         public void setDataAccessService(DataAccessService dataAccessService) {
206                 this.dataAccessService = dataAccessService;
207         }
208
209         public MenuBuilder getMenuBuilder() {
210                 return new MenuBuilder();
211         }
212
213         public void setMenuBuilder(MenuBuilder menuBuilder) {
214                 this.menuBuilder = menuBuilder;
215         }
216
217         public User getUser(User user) {
218                 List list = null;
219
220                 StringBuffer criteria = new StringBuffer();
221                 criteria.append(" where login_id = '").append(user.getLoginId()).append("'");
222
223                 list = getDataAccessService().getList(User.class, criteria.toString(), null, null);
224                 return (list == null || list.size() == 0) ? null : (User) list.get(0);
225
226         }
227
228 }