2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 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============================================
38 package org.onap.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.hibernate.criterion.Criterion;
49 import org.hibernate.criterion.Restrictions;
50 import org.json.JSONArray;
51 import org.json.JSONObject;
52 import org.onap.portalapp.portal.domain.EPUser;
53 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
54 import org.onap.portalsdk.core.domain.FusionObject.Utilities;
55 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
56 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
57 import org.onap.portalsdk.core.service.DataAccessService;
58 import org.onap.portalsdk.core.util.SystemProperties;
59 import org.springframework.beans.factory.annotation.Autowired;
60 import org.springframework.stereotype.Service;
61 import org.springframework.transaction.annotation.Transactional;
63 @Service("userService")
65 public class UserServiceImpl implements UserService {
67 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UserServiceImpl.class);
70 private DataAccessService dataAccessService;
72 public DataAccessService getDataAccessService() {
73 return dataAccessService;
76 public void setDataAccessService(DataAccessService dataAccessService) {
77 this.dataAccessService = dataAccessService;
80 @SuppressWarnings("unchecked")
82 public List<EPUser> getUserByUserId(String userId) {
84 if (SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM).trim().equalsIgnoreCase("OIDC")) {
85 List<EPUser> users = new ArrayList<EPUser>();
86 List<EPUser> filterdUsers = new ArrayList<EPUser>();
87 BufferedReader in = null;
88 HttpURLConnection con = null;
90 String url = EPCommonSystemProperties.getProperty(EPCommonSystemProperties.AUTH_USER_SERVER);
91 URL obj = new URL(url);
93 con = (HttpURLConnection) obj.openConnection();
95 // optional default is GET
96 con.setRequestMethod("GET");
97 con.setConnectTimeout(3000);
98 con.setReadTimeout(8000);
100 StringBuffer response = new StringBuffer();
102 in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
104 while ((inputLine = in.readLine()) != null)
105 response.append(inputLine);
106 JSONObject jObject = new JSONObject(response.toString()); // json
107 JSONArray jsonUsers = jObject.getJSONArray("response"); // get
110 for (int i = 0; i < jsonUsers.length(); i++) {
111 JSONObject eachObject = jsonUsers.getJSONObject(i);
112 EPUser eachUser = new EPUser();
113 eachUser.setOrgUserId(eachObject.get("id").toString());
114 eachUser.setFirstName(eachObject.get("givenName").toString());
115 eachUser.setLastName(eachObject.get("familyName").toString());
116 eachUser.setEmail(eachObject.get("email").toString());
120 for (int i = 0; i < users.size(); i++) {
121 if (Utilities.nvl(userId).length() > 0) {
122 if (!userId.equalsIgnoreCase(users.get(i).getOrgUserId())) {
126 filterdUsers.add(users.get(i));
129 } catch (Exception e) {
130 logger.error(EELFLoggerDelegate.errorLogger, "getUserByUserId failed", e);
135 } catch (IOException e) {
136 logger.error(EELFLoggerDelegate.errorLogger, "getUserByUserId failed to close", e);
144 List<Criterion> restrictionsList = new ArrayList<Criterion>();
145 Criterion orgUserIdCriterion = Restrictions.eq("orgUserId",userId);
146 restrictionsList.add(orgUserIdCriterion);
148 List<EPUser> list = (List<EPUser>) dataAccessService.getList(EPUser.class, null, restrictionsList, null);
150 return (list == null || list.size() == 0) ? null : list;
156 @SuppressWarnings("unchecked")
158 public List<EPUser> getUserByFirstLastName(String firstName, String lastName) {
160 if (!SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM).trim().equalsIgnoreCase("OIDC")) {
161 List<Criterion> restrictionsList = new ArrayList<Criterion>();
162 Criterion firstNameCriterion = Restrictions.eq("firstName", firstName);
163 Criterion lastNameCriterion = Restrictions.eq("lastName", lastName);
164 restrictionsList.add(Restrictions.or(firstNameCriterion, lastNameCriterion));
166 List<EPUser> list = (List<EPUser>) dataAccessService.getList(EPUser.class, null, restrictionsList, null);
167 return (list == null || list.size() == 0) ? null : list;
171 List<EPUser> users = new ArrayList<EPUser>();
172 List<EPUser> filterdUsers = new ArrayList<EPUser>();
173 BufferedReader in = null;
174 HttpURLConnection con = null;
176 String url = EPCommonSystemProperties.getProperty(EPCommonSystemProperties.AUTH_USER_SERVER);
177 URL obj = new URL(url);
179 con = (HttpURLConnection) obj.openConnection();
181 // optional default is GET
182 con.setRequestMethod("GET");
183 con.setConnectTimeout(3000);
184 con.setReadTimeout(8000);
186 StringBuffer response = new StringBuffer();
188 in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
190 while ((inputLine = in.readLine()) != null)
191 response.append(inputLine);
192 JSONObject jObject = new JSONObject(response.toString()); // json
193 JSONArray jsonUsers = jObject.getJSONArray("response"); // get
196 for (int i = 0; i < jsonUsers.length(); i++) {
197 JSONObject eachObject = jsonUsers.getJSONObject(i);
198 EPUser eachUser = new EPUser();
199 eachUser.setOrgUserId(eachObject.get("id").toString());// getString("id"));
200 eachUser.setFirstName(eachObject.get("givenName").toString());
201 eachUser.setLastName(eachObject.get("familyName").toString());
202 eachUser.setEmail(eachObject.get("email").toString());
206 for (int i = 0; i < users.size(); i++) {
208 if (Utilities.nvl(firstName).length() > 0) {
209 if (!firstName.equalsIgnoreCase(users.get(i).getFirstName())) {
213 if (Utilities.nvl(lastName).length() > 0) {
214 if (!lastName.equalsIgnoreCase(users.get(i).getLastName())) {
219 filterdUsers.add(users.get(i));
223 } catch (Exception e) {
224 logger.error(EELFLoggerDelegate.errorLogger, "getUserByFirstLastName failed", e);
229 } catch (IOException e) {
230 logger.error(EELFLoggerDelegate.errorLogger, "getUserByFirstLastName failed to close", e);
239 @SuppressWarnings("unchecked")
240 public String saveNewUser(EPUser newUser, String checkDuplicate) throws Exception {
244 List<Criterion> restrictionsList = new ArrayList<Criterion>();
245 Criterion orgUserIdCriterion = Restrictions.eq("orgUserId",newUser.getLoginId());
246 restrictionsList.add(orgUserIdCriterion);
247 List<EPUser> list = (List<EPUser>) dataAccessService.getList(EPUser.class, null, restrictionsList, 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);
255 if (checkDuplicate.equals("Yes")) {
256 // userId already exist in database
257 return "Record already exist";
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()));
267 oldUser.setLoginPwd(newUser.getLoginPwd());
268 getDataAccessService().saveDomainObject(oldUser, null);
274 } catch (Exception e) {
275 logger.error(EELFLoggerDelegate.errorLogger, "saveNewUser failed", e);
276 throw new Exception(e);
282 public void saveUser(EPUser user) throws Exception {
283 getDataAccessService().saveDomainObject(user, null);