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.service.UserService;
53 import org.onap.portalapp.portal.service.UserServiceImpl;
54 import org.onap.portalapp.portal.domain.EPUser;
55 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
56 import org.onap.portalsdk.core.FusionObject.Utilities;
57 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
58 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
59 import org.onap.portalsdk.core.service.DataAccessService;
60 import org.onap.portalsdk.core.util.SystemProperties;
61 import org.springframework.beans.factory.annotation.Autowired;
62 import org.springframework.stereotype.Service;
63 import org.springframework.transaction.annotation.Transactional;
65 @Service("userService")
67 public class UserServiceImpl implements UserService {
69 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UserServiceImpl.class);
72 private DataAccessService dataAccessService;
74 public DataAccessService getDataAccessService() {
75 return dataAccessService;
78 public void setDataAccessService(DataAccessService dataAccessService) {
79 this.dataAccessService = dataAccessService;
82 @SuppressWarnings("unchecked")
84 public List<EPUser> getUserByUserId(String userId) {
86 if (SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM).trim().equalsIgnoreCase("OIDC")) {
87 List<EPUser> users = new ArrayList<EPUser>();
88 List<EPUser> filterdUsers = new ArrayList<EPUser>();
89 BufferedReader in = null;
90 HttpURLConnection con = null;
92 String url = EPCommonSystemProperties.getProperty(EPCommonSystemProperties.AUTH_USER_SERVER);
93 URL obj = new URL(url);
95 con = (HttpURLConnection) obj.openConnection();
97 // optional default is GET
98 con.setRequestMethod("GET");
99 con.setConnectTimeout(3000);
100 con.setReadTimeout(8000);
102 StringBuffer response = new StringBuffer();
104 in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
106 while ((inputLine = in.readLine()) != null)
107 response.append(inputLine);
108 JSONObject jObject = new JSONObject(response.toString()); // json
109 JSONArray jsonUsers = jObject.getJSONArray("response"); // get
112 for (int i = 0; i < jsonUsers.length(); i++) {
113 JSONObject eachObject = jsonUsers.getJSONObject(i);
114 EPUser eachUser = new EPUser();
115 eachUser.setOrgUserId(eachObject.get("id").toString());
116 eachUser.setFirstName(eachObject.get("givenName").toString());
117 eachUser.setLastName(eachObject.get("familyName").toString());
118 eachUser.setEmail(eachObject.get("email").toString());
122 for (int i = 0; i < users.size(); i++) {
123 if (Utilities.nvl(userId).length() > 0) {
124 if (!userId.equalsIgnoreCase(users.get(i).getOrgUserId())) {
128 filterdUsers.add(users.get(i));
131 } catch (Exception e) {
132 logger.error(EELFLoggerDelegate.errorLogger, "getUserByUserId failed", e);
137 } catch (IOException e) {
138 logger.error(EELFLoggerDelegate.errorLogger, "getUserByUserId failed to close", e);
146 List<Criterion> restrictionsList = new ArrayList<Criterion>();
147 Criterion orgUserIdCriterion = Restrictions.eq("orgUserId",userId);
148 restrictionsList.add(orgUserIdCriterion);
150 List<EPUser> list = (List<EPUser>) dataAccessService.getList(EPUser.class, null, restrictionsList, null);
152 return (list == null || list.size() == 0) ? null : list;
158 @SuppressWarnings("unchecked")
160 public List<EPUser> getUserByFirstLastName(String firstName, String lastName) {
162 if (!SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM).trim().equalsIgnoreCase("OIDC")) {
163 List<Criterion> restrictionsList = new ArrayList<Criterion>();
164 Criterion firstNameCriterion = Restrictions.eq("firstName", firstName);
165 Criterion lastNameCriterion = Restrictions.eq("lastName", lastName);
166 restrictionsList.add(Restrictions.or(firstNameCriterion, lastNameCriterion));
168 List<EPUser> list = (List<EPUser>) dataAccessService.getList(EPUser.class, null, restrictionsList, null);
169 return (list == null || list.size() == 0) ? null : list;
173 List<EPUser> users = new ArrayList<EPUser>();
174 List<EPUser> filterdUsers = new ArrayList<EPUser>();
175 BufferedReader in = null;
176 HttpURLConnection con = null;
178 String url = EPCommonSystemProperties.getProperty(EPCommonSystemProperties.AUTH_USER_SERVER);
179 URL obj = new URL(url);
181 con = (HttpURLConnection) obj.openConnection();
183 // optional default is GET
184 con.setRequestMethod("GET");
185 con.setConnectTimeout(3000);
186 con.setReadTimeout(8000);
188 StringBuffer response = new StringBuffer();
190 in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
192 while ((inputLine = in.readLine()) != null)
193 response.append(inputLine);
194 JSONObject jObject = new JSONObject(response.toString()); // json
195 JSONArray jsonUsers = jObject.getJSONArray("response"); // get
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());
208 for (int i = 0; i < users.size(); i++) {
210 if (Utilities.nvl(firstName).length() > 0) {
211 if (!firstName.equalsIgnoreCase(users.get(i).getFirstName())) {
215 if (Utilities.nvl(lastName).length() > 0) {
216 if (!lastName.equalsIgnoreCase(users.get(i).getLastName())) {
221 filterdUsers.add(users.get(i));
225 } catch (Exception e) {
226 logger.error(EELFLoggerDelegate.errorLogger, "getUserByFirstLastName failed", e);
231 } catch (IOException e) {
232 logger.error(EELFLoggerDelegate.errorLogger, "getUserByFirstLastName failed to close", e);
241 @SuppressWarnings("unchecked")
242 public String saveNewUser(EPUser newUser, String checkDuplicate) throws Exception {
246 List<Criterion> restrictionsList = new ArrayList<Criterion>();
247 Criterion orgUserIdCriterion = Restrictions.eq("orgUserId",newUser.getLoginId());
248 restrictionsList.add(orgUserIdCriterion);
249 List<EPUser> list = (List<EPUser>) dataAccessService.getList(EPUser.class, null, restrictionsList, null);
251 if (list == null || list.size() == 0) {
252 newUser.setActive(true);
253 newUser.setOrgUserId(newUser.getLoginId());
254 newUser.setLoginPwd(CipherUtil.encryptPKC(newUser.getLoginPwd()));
255 getDataAccessService().saveDomainObject(newUser, null);
257 if (checkDuplicate.equals("Yes")) {
258 // userId already exist in database
259 return "Record already exist";
262 EPUser oldUser = (EPUser) list.get(0);
263 oldUser.setFirstName(newUser.getFirstName());
264 oldUser.setLastName(newUser.getLastName());
265 oldUser.setMiddleInitial(newUser.getMiddleInitial());
266 if (!oldUser.getLoginPwd().equals(newUser.getLoginPwd()))
267 oldUser.setLoginPwd(CipherUtil.encryptPKC(newUser.getLoginPwd()));
269 oldUser.setLoginPwd(newUser.getLoginPwd());
270 getDataAccessService().saveDomainObject(oldUser, null);
276 } catch (Exception e) {
277 logger.error(EELFLoggerDelegate.errorLogger, "saveNewUser failed", e);
278 throw new Exception(e);
284 public void saveUser(EPUser user) throws Exception {
285 getDataAccessService().saveDomainObject(user, null);