2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 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.openecomp.portalapp.portal.service;
40 import java.io.BufferedReader;
41 import java.io.IOException;
42 import java.io.InputStreamReader;
43 import java.net.HttpURLConnection;
45 import java.util.ArrayList;
46 import java.util.List;
48 import org.json.JSONArray;
49 import org.json.JSONObject;
50 import org.openecomp.portalapp.portal.domain.EPUser;
51 import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;
52 import org.openecomp.portalsdk.core.FusionObject.Utilities;
53 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
54 import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;
55 import org.openecomp.portalsdk.core.service.DataAccessService;
56 import org.openecomp.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;
61 @Service("userService")
63 public class UserServiceImpl implements UserService {
65 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UserServiceImpl.class);
68 private DataAccessService dataAccessService;
70 public DataAccessService getDataAccessService() {
71 return dataAccessService;
74 public void setDataAccessService(DataAccessService dataAccessService) {
75 this.dataAccessService = dataAccessService;
78 @SuppressWarnings("unchecked")
80 public List<EPUser> getUserByUserId(String userId) {
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;
88 String url = EPCommonSystemProperties.getProperty(EPCommonSystemProperties.AUTH_USER_SERVER);
89 URL obj = new URL(url);
91 con = (HttpURLConnection) obj.openConnection();
93 // optional default is GET
94 con.setRequestMethod("GET");
95 con.setConnectTimeout(3000);
96 con.setReadTimeout(8000);
98 StringBuffer response = new StringBuffer();
100 in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
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
108 for (int i = 0; i < jsonUsers.length(); i++) {
109 JSONObject eachObject = jsonUsers.getJSONObject(i);
110 EPUser eachUser = new EPUser();
111 eachUser.setOrgUserId(eachObject.get("id").toString());
112 eachUser.setFirstName(eachObject.get("givenName").toString());
113 eachUser.setLastName(eachObject.get("familyName").toString());
114 eachUser.setEmail(eachObject.get("email").toString());
118 for (int i = 0; i < users.size(); i++) {
119 if (Utilities.nvl(userId).length() > 0) {
120 if (!userId.equalsIgnoreCase(users.get(i).getOrgUserId())) {
124 filterdUsers.add(users.get(i));
127 } catch (Exception e) {
128 logger.error(EELFLoggerDelegate.errorLogger, "getUserByUserId failed", e);
133 } catch (IOException e) {
134 logger.error(EELFLoggerDelegate.errorLogger, "getUserByUserId failed to close", e);
142 List<EPUser> list = null;
143 StringBuffer criteria = new StringBuffer();
144 criteria.append(" where org_user_id = '").append(userId).append("'");
145 list = getDataAccessService().getList(EPUser.class, criteria.toString(), null, null);
146 return (list == null || list.size() == 0) ? null : list;
152 @SuppressWarnings("unchecked")
154 public List<EPUser> getUserByFirstLastName(String firstName, String lastName) {
156 if (!SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM).trim().equalsIgnoreCase("OIDC")) {
158 List<EPUser> list = null;
159 StringBuffer criteria = new StringBuffer();
160 if (firstName != null)
161 criteria.append(" where first_name = '").append(firstName).append("'");
162 if (lastName != null)
163 criteria.append(" where last_name = '").append(lastName).append("'");
164 list = getDataAccessService().getList(EPUser.class, criteria.toString(), null, null);
165 return (list == null || list.size() == 0) ? null : list;
169 List<EPUser> users = new ArrayList<EPUser>();
170 List<EPUser> filterdUsers = new ArrayList<EPUser>();
171 BufferedReader in = null;
172 HttpURLConnection con = null;
174 String url = EPCommonSystemProperties.getProperty(EPCommonSystemProperties.AUTH_USER_SERVER);
175 URL obj = new URL(url);
177 con = (HttpURLConnection) obj.openConnection();
179 // optional default is GET
180 con.setRequestMethod("GET");
181 con.setConnectTimeout(3000);
182 con.setReadTimeout(8000);
184 StringBuffer response = new StringBuffer();
186 in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
188 while ((inputLine = in.readLine()) != null)
189 response.append(inputLine);
190 JSONObject jObject = new JSONObject(response.toString()); // json
191 JSONArray jsonUsers = jObject.getJSONArray("response"); // get
194 for (int i = 0; i < jsonUsers.length(); i++) {
195 JSONObject eachObject = jsonUsers.getJSONObject(i);
196 EPUser eachUser = new EPUser();
197 eachUser.setOrgUserId(eachObject.get("id").toString());// getString("id"));
198 eachUser.setFirstName(eachObject.get("givenName").toString());
199 eachUser.setLastName(eachObject.get("familyName").toString());
200 eachUser.setEmail(eachObject.get("email").toString());
204 for (int i = 0; i < users.size(); i++) {
206 if (Utilities.nvl(firstName).length() > 0) {
207 if (!firstName.equalsIgnoreCase(users.get(i).getFirstName())) {
211 if (Utilities.nvl(lastName).length() > 0) {
212 if (!lastName.equalsIgnoreCase(users.get(i).getLastName())) {
217 filterdUsers.add(users.get(i));
221 } catch (Exception e) {
222 logger.error(EELFLoggerDelegate.errorLogger, "getUserByFirstLastName failed", e);
227 } catch (IOException e) {
228 logger.error(EELFLoggerDelegate.errorLogger, "getUserByFirstLastName failed to close", e);
237 @SuppressWarnings("unchecked")
238 public String saveNewUser(EPUser newUser, String checkDuplicate) throws Exception {
242 List<EPUser> list = null;
243 StringBuffer criteria = new StringBuffer();
244 criteria.append(" where org_user_id = '").append(newUser.getLoginId()).append("'");
245 list = getDataAccessService().getList(EPUser.class, criteria.toString(), null, null);
246 if (list == null || list.size() == 0) {
247 newUser.setActive(true);
248 newUser.setOrgUserId(newUser.getLoginId());
249 newUser.setLoginPwd(CipherUtil.encrypt(newUser.getLoginPwd()));
250 getDataAccessService().saveDomainObject(newUser, null);
252 if (checkDuplicate.equals("Yes")) {
253 // userId already exist in database
254 return "Record already exist";
257 EPUser oldUser = (EPUser) list.get(0);
258 oldUser.setFirstName(newUser.getFirstName());
259 oldUser.setLastName(newUser.getLastName());
260 oldUser.setMiddleInitial(newUser.getMiddleInitial());
261 if (!oldUser.getLoginPwd().equals(newUser.getLoginPwd()))
262 oldUser.setLoginPwd(CipherUtil.encrypt(newUser.getLoginPwd()));
264 oldUser.setLoginPwd(newUser.getLoginPwd());
265 getDataAccessService().saveDomainObject(oldUser, null);
271 } catch (Exception e) {
272 logger.error(EELFLoggerDelegate.errorLogger, "saveNewUser failed", e);
273 throw new Exception(e);
279 public void saveUser(EPUser user) throws Exception {
280 getDataAccessService().saveDomainObject(user, null);