2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ================================================================================
20 package org.openecomp.portalapp.portal.service;
22 import java.io.BufferedReader;
23 import java.io.IOException;
24 import java.io.InputStreamReader;
25 import java.net.HttpURLConnection;
27 import java.util.ArrayList;
28 import java.util.List;
30 import org.json.JSONArray;
31 import org.json.JSONObject;
32 import org.openecomp.portalapp.portal.domain.EPUser;
33 import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;
34 import org.openecomp.portalsdk.core.FusionObject.Utilities;
35 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
36 import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;
37 import org.openecomp.portalsdk.core.service.DataAccessService;
38 import org.openecomp.portalsdk.core.util.SystemProperties;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.stereotype.Service;
41 import org.springframework.transaction.annotation.Transactional;
43 @Service("userService")
45 public class UserServiceImpl implements UserService {
47 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UserServiceImpl.class);
50 private DataAccessService dataAccessService;
52 public DataAccessService getDataAccessService() {
53 return dataAccessService;
56 public void setDataAccessService(DataAccessService dataAccessService) {
57 this.dataAccessService = dataAccessService;
60 @SuppressWarnings("unchecked")
62 public List<EPUser> getUserByUserId(String userId) {
64 if (SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM).trim().equalsIgnoreCase("OIDC")) {
65 List<EPUser> users = new ArrayList<EPUser>();
66 List<EPUser> filterdUsers = new ArrayList<EPUser>();
67 BufferedReader in = null;
68 HttpURLConnection con = null;
70 String url = EPCommonSystemProperties.getProperty(EPCommonSystemProperties.AUTH_USER_SERVER);
71 URL obj = new URL(url);
73 con = (HttpURLConnection) obj.openConnection();
75 // optional default is GET
76 con.setRequestMethod("GET");
77 con.setConnectTimeout(3000);
78 con.setReadTimeout(8000);
80 StringBuffer response = new StringBuffer();
82 in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
84 while ((inputLine = in.readLine()) != null)
85 response.append(inputLine);
86 JSONObject jObject = new JSONObject(response.toString()); // json
87 JSONArray jsonUsers = jObject.getJSONArray("response"); // get
90 for (int i = 0; i < jsonUsers.length(); i++) {
91 JSONObject eachObject = jsonUsers.getJSONObject(i);
92 EPUser eachUser = new EPUser();
93 eachUser.setOrgUserId(eachObject.get("id").toString());
94 eachUser.setFirstName(eachObject.get("givenName").toString());
95 eachUser.setLastName(eachObject.get("familyName").toString());
96 eachUser.setEmail(eachObject.get("email").toString());
100 for (int i = 0; i < users.size(); i++) {
101 if (Utilities.nvl(userId).length() > 0) {
102 if (!userId.equalsIgnoreCase(users.get(i).getOrgUserId())) {
106 filterdUsers.add(users.get(i));
109 } catch (Exception e) {
110 logger.error(EELFLoggerDelegate.errorLogger, "getUserByUserId failed", e);
115 } catch (IOException e) {
116 logger.error(EELFLoggerDelegate.errorLogger, "getUserByUserId failed to close", e);
124 List<EPUser> list = null;
125 StringBuffer criteria = new StringBuffer();
126 criteria.append(" where org_user_id = '").append(userId).append("'");
127 list = getDataAccessService().getList(EPUser.class, criteria.toString(), null, null);
128 return (list == null || list.size() == 0) ? null : list;
134 @SuppressWarnings("unchecked")
136 public List<EPUser> getUserByFirstLastName(String firstName, String lastName) {
138 if (!SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM).trim().equalsIgnoreCase("OIDC")) {
140 List<EPUser> list = null;
141 StringBuffer criteria = new StringBuffer();
142 if (firstName != null)
143 criteria.append(" where first_name = '").append(firstName).append("'");
144 if (lastName != null)
145 criteria.append(" where last_name = '").append(lastName).append("'");
146 list = getDataAccessService().getList(EPUser.class, criteria.toString(), null, null);
147 return (list == null || list.size() == 0) ? null : list;
151 List<EPUser> users = new ArrayList<EPUser>();
152 List<EPUser> filterdUsers = new ArrayList<EPUser>();
153 BufferedReader in = null;
154 HttpURLConnection con = null;
156 String url = EPCommonSystemProperties.getProperty(EPCommonSystemProperties.AUTH_USER_SERVER);
157 URL obj = new URL(url);
159 con = (HttpURLConnection) obj.openConnection();
161 // optional default is GET
162 con.setRequestMethod("GET");
163 con.setConnectTimeout(3000);
164 con.setReadTimeout(8000);
166 StringBuffer response = new StringBuffer();
168 in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
170 while ((inputLine = in.readLine()) != null)
171 response.append(inputLine);
172 JSONObject jObject = new JSONObject(response.toString()); // json
173 JSONArray jsonUsers = jObject.getJSONArray("response"); // get
176 for (int i = 0; i < jsonUsers.length(); i++) {
177 JSONObject eachObject = jsonUsers.getJSONObject(i);
178 EPUser eachUser = new EPUser();
179 eachUser.setOrgUserId(eachObject.get("id").toString());// getString("id"));
180 eachUser.setFirstName(eachObject.get("givenName").toString());
181 eachUser.setLastName(eachObject.get("familyName").toString());
182 eachUser.setEmail(eachObject.get("email").toString());
186 for (int i = 0; i < users.size(); i++) {
188 if (Utilities.nvl(firstName).length() > 0) {
189 if (!firstName.equalsIgnoreCase(users.get(i).getFirstName())) {
193 if (Utilities.nvl(lastName).length() > 0) {
194 if (!lastName.equalsIgnoreCase(users.get(i).getLastName())) {
199 filterdUsers.add(users.get(i));
203 } catch (Exception e) {
204 logger.error(EELFLoggerDelegate.errorLogger, "getUserByFirstLastName failed", e);
209 } catch (IOException e) {
210 logger.error(EELFLoggerDelegate.errorLogger, "getUserByFirstLastName failed to close", e);
219 @SuppressWarnings("unchecked")
220 public String saveNewUser(EPUser newUser, String checkDuplicate) throws Exception {
224 List<EPUser> list = null;
225 StringBuffer criteria = new StringBuffer();
226 criteria.append(" where org_user_id = '").append(newUser.getLoginId()).append("'");
227 list = getDataAccessService().getList(EPUser.class, criteria.toString(), null, null);
228 if (list == null || list.size() == 0) {
229 newUser.setActive(true);
230 newUser.setOrgUserId(newUser.getLoginId());
231 newUser.setLoginPwd(CipherUtil.encrypt(newUser.getLoginPwd()));
232 getDataAccessService().saveDomainObject(newUser, null);
234 if (checkDuplicate.equals("Yes")) {
235 // userId already exist in database
236 return "Record already exist";
239 EPUser oldUser = (EPUser) list.get(0);
240 oldUser.setFirstName(newUser.getFirstName());
241 oldUser.setLastName(newUser.getLastName());
242 oldUser.setMiddleInitial(newUser.getMiddleInitial());
243 if (!oldUser.getLoginPwd().equals(newUser.getLoginPwd()))
244 oldUser.setLoginPwd(CipherUtil.encrypt(newUser.getLoginPwd()));
246 oldUser.setLoginPwd(newUser.getLoginPwd());
247 getDataAccessService().saveDomainObject(oldUser, null);
253 } catch (Exception e) {
254 logger.error(EELFLoggerDelegate.errorLogger, "saveNewUser failed", e);
255 throw new Exception(e);
261 public void saveUser(EPUser user) throws Exception {
262 getDataAccessService().saveDomainObject(user, null);