login and Certman AAF Integration changes
[portal.git] / ecomp-portal-BE-os / src / main / java / org / onap / portalapp / portal / service / UserServiceImpl.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017-2018 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  * 
37  */
38 package org.onap.portalapp.portal.service;
39
40 import java.io.BufferedReader;
41 import java.io.IOException;
42 import java.io.InputStreamReader;
43 import java.net.HttpURLConnection;
44 import java.net.URL;
45 import java.util.ArrayList;
46 import java.util.List;
47
48 import org.json.JSONArray;
49 import org.json.JSONObject;
50 import org.onap.portalapp.portal.domain.EPUser;
51 import org.onap.portalapp.portal.utils.EPSystemProperties;
52 import org.onap.portalsdk.core.domain.FusionObject.Utilities;
53 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
54 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
55 import org.onap.portalsdk.core.service.DataAccessService;
56 import org.onap.portalsdk.core.util.SystemProperties;
57 import org.springframework.beans.factory.annotation.Autowired;
58 import org.springframework.stereotype.Service;
59 import org.springframework.transaction.annotation.Transactional;
60
61 @Service("userService")
62 @Transactional
63 public class UserServiceImpl implements UserService {
64
65         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UserServiceImpl.class);
66
67         @Autowired
68         private DataAccessService dataAccessService;
69
70         public DataAccessService getDataAccessService() {
71                 return dataAccessService;
72         }
73
74         public void setDataAccessService(DataAccessService dataAccessService) {
75                 this.dataAccessService = dataAccessService;
76         }
77
78         @SuppressWarnings("rawtypes")
79         @Override
80         public List getUserByUserId(String userId) {
81
82                 if (SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM).trim().equalsIgnoreCase("OIDC")) {
83                         List<EPUser> users = new ArrayList<EPUser>();
84                         List<EPUser> filterdUsers = new ArrayList<EPUser>();
85                         BufferedReader in = null;
86                         HttpURLConnection con = null;
87                         try {
88                                 String url = EPSystemProperties.getProperty(EPSystemProperties.AUTH_USER_SERVER);
89                                 URL obj = new URL(url);
90
91                                 con = (HttpURLConnection) obj.openConnection();
92
93                                 // optional default is GET
94                                 con.setRequestMethod("GET");
95                                 con.setConnectTimeout(3000);
96                                 con.setReadTimeout(8000);
97
98                                 StringBuffer response = new StringBuffer();
99
100                                 in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
101                                 String inputLine;
102                                 while ((inputLine = in.readLine()) != null)
103                                         response.append(inputLine);
104                                 JSONObject jObject = new JSONObject(response.toString()); // json
105                                 JSONArray jsonUsers = jObject.getJSONArray("response"); // get data object
106                                 for (int i = 0; i < jsonUsers.length(); i++) {
107                                         JSONObject eachObject = jsonUsers.getJSONObject(i);
108                                         EPUser eachUser = new EPUser();
109                                         eachUser.setOrgUserId(eachObject.get("id").toString());// getString("id"));
110                                         eachUser.setFirstName(eachObject.get("givenName").toString());
111                                         eachUser.setLastName(eachObject.get("familyName").toString());
112                                         eachUser.setEmail(eachObject.get("email").toString());
113                                         users.add(eachUser);
114                                 }
115
116                                 for (int i = 0; i < users.size(); i++) {
117
118                                         if (Utilities.nvl(userId).length() > 0) {
119                                                 if (!userId.equalsIgnoreCase(users.get(i).getOrgUserId())) {
120                                                         continue;
121                                                 }
122                                         }
123                                         filterdUsers.add(users.get(i));
124
125                                 }
126
127                         } catch (Exception e) {
128                                 logger.error(EELFLoggerDelegate.errorLogger, "getUserByUserId failed", e);
129                         } finally {
130                                 try {
131                                         if(in!=null) {
132                                         in.close();
133                                         }
134                                         con.disconnect();
135                                 } catch (IOException e) {
136                                         logger.error(EELFLoggerDelegate.errorLogger, "getUserByUserId 2 failed", e);
137                                 }
138                         }
139
140                         return filterdUsers;
141
142                 } else {
143
144                         List list = null;
145                         StringBuffer criteria = new StringBuffer();
146                         criteria.append(" where org_user_id = '").append(userId).append("'");
147                         list = getDataAccessService().getList(EPUser.class, criteria.toString(), null, null);
148                         return (list == null || list.size() == 0) ? null : list;
149
150                 }
151
152         }
153
154         @SuppressWarnings("rawtypes")
155         @Override
156         public List getUserByFirstLastName(String firstName, String lastName) {
157
158                 if (!SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM).trim().equalsIgnoreCase("OIDC")) {
159
160                         List list = null;
161                         StringBuffer criteria = new StringBuffer();
162                         if (firstName != null && lastName != null)
163                                 criteria.append(" where first_name = '").append(firstName).append("' and last_name = '")
164                                                 .append(lastName).append("'");
165                         else if (firstName != null)
166                                 criteria.append(" where first_name = '").append(firstName).append("'");
167                         else if (lastName != null)
168                                 criteria.append(" where last_name = '").append(lastName).append("'");
169                         list = getDataAccessService().getList(EPUser.class, criteria.toString(), null, null);
170                         return (list == null || list.size() == 0) ? null : list;
171
172                 } else {
173
174                         List<EPUser> users = new ArrayList<EPUser>();
175                         List<EPUser> filterdUsers = new ArrayList<EPUser>();
176                         BufferedReader in = null;
177                         HttpURLConnection con = null;
178                         try {
179                                 String url = EPSystemProperties.getProperty(EPSystemProperties.AUTH_USER_SERVER);
180                                 URL obj = new URL(url);
181
182                                 con = (HttpURLConnection) obj.openConnection();
183
184                                 // optional default is GET
185                                 con.setRequestMethod("GET");
186                                 con.setConnectTimeout(3000);
187                                 con.setReadTimeout(8000);
188
189                                 StringBuffer response = new StringBuffer();
190
191                                 in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
192                                 String inputLine;
193                                 while ((inputLine = in.readLine()) != null) {
194                                         response.append(inputLine);
195                                 }
196                                 JSONObject jObject = new JSONObject(response.toString()); // json
197                                 JSONArray jsonUsers = jObject.getJSONArray("response"); // get data object
198                                 for (int i = 0; i < jsonUsers.length(); i++) {
199                                         JSONObject eachObject = jsonUsers.getJSONObject(i);
200                                         EPUser eachUser = new EPUser();
201                                         eachUser.setOrgUserId(eachObject.get("id").toString());// getString("id"));
202                                         eachUser.setFirstName(eachObject.get("givenName").toString());
203                                         eachUser.setLastName(eachObject.get("familyName").toString());
204                                         eachUser.setEmail(eachObject.get("email").toString());
205                                         users.add(eachUser);
206                                 }
207
208                                 for (int i = 0; i < users.size(); i++) {
209
210                                         if (Utilities.nvl(firstName).length() > 0) {
211                                                 if (!firstName.equalsIgnoreCase(users.get(i).getFirstName())) {
212                                                         continue;
213                                                 }
214                                         }
215                                         if (Utilities.nvl(lastName).length() > 0) {
216                                                 if (!lastName.equalsIgnoreCase(users.get(i).getLastName())) {
217                                                         continue;
218                                                 }
219                                         }
220
221                                         filterdUsers.add(users.get(i));
222
223                                 }
224
225                         } catch (Exception e) {
226                                 logger.error(EELFLoggerDelegate.errorLogger, "getUserByFirstLastName failed", e);
227                         } finally {
228                                 try {
229                                         if(in!=null) {
230                                         in.close();
231                                         con.disconnect();
232                                         }
233                                 } catch (IOException e) {
234                                         logger.error(EELFLoggerDelegate.errorLogger, "getUserByFirstLastName 2 failed", e);
235                                 }
236                         }
237
238                         return filterdUsers;
239                 }
240
241         }
242
243         public String saveNewUser(EPUser newUser, String checkDuplicate) throws Exception {
244                 try {
245                         List list = null;
246                         StringBuffer criteria = new StringBuffer();
247                         criteria.append(" where org_user_id = '").append(newUser.getLoginId()).append("'");
248                         list = getDataAccessService().getList(EPUser.class, criteria.toString(), null, null);
249                         if (list == null || list.size() == 0) {
250                                 newUser.setActive(true);
251                                 newUser.setOrgUserId(newUser.getLoginId());
252                                 newUser.setLoginPwd(CipherUtil.encryptPKC(newUser.getLoginPwd()));
253                                 getDataAccessService().saveDomainObject(newUser, null);
254                         } else {
255                                 if (checkDuplicate.equals("Yes")) {
256                                         // userId already exist in database
257                                         return "Record already exist";
258                                 } else {
259
260                                         EPUser oldUser = (EPUser) list.get(0);
261                                         oldUser.setFirstName(newUser.getFirstName());
262                                         oldUser.setLastName(newUser.getLastName());
263                                         oldUser.setMiddleInitial(newUser.getMiddleInitial());
264                                         if (!oldUser.getLoginPwd().equals(newUser.getLoginPwd()))
265                                                 oldUser.setLoginPwd(CipherUtil.encryptPKC(newUser.getLoginPwd()));
266                                         else
267                                                 oldUser.setLoginPwd(newUser.getLoginPwd());
268                                         getDataAccessService().saveDomainObject(oldUser, null);
269
270                                 }
271
272                         }
273
274                 } catch (Exception e) {
275                         logger.error(EELFLoggerDelegate.errorLogger, "saveNewUser failed", e);
276                         throw new Exception(e);
277                 }
278                 return "success";
279         };
280
281 }