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.util.List;
42 import org.apache.cxf.transport.http.HTTPException;
43 import org.onap.portalapp.portal.domain.EPApp;
44 import org.onap.portalapp.portal.domain.EPRole;
45 import org.onap.portalapp.portal.domain.EPUser;
46 import org.onap.portalapp.portal.domain.EPUserApp;
47 import org.onap.portalapp.portal.logging.aop.EPMetricsLog;
48 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
49 import org.onap.portalsdk.core.service.DataAccessService;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.context.annotation.EnableAspectJAutoProxy;
52 import org.springframework.stereotype.Service;
53 import org.springframework.transaction.annotation.Transactional;
55 import com.fasterxml.jackson.databind.DeserializationFeature;
56 import com.fasterxml.jackson.databind.ObjectMapper;
58 @Service("userRolesService")
60 @org.springframework.context.annotation.Configuration
61 @EnableAspectJAutoProxy
63 public class UserRolesServiceImpl extends UserRolesCommonServiceImpl implements UserRolesService {
65 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UserRolesServiceImpl.class);
68 private DataAccessService dataAccessService;
70 private EPUser getUserFromRemoteApp(String orgUserId, EPApp app,
71 ApplicationsRestClientService applicationsRestClientService) throws HTTPException {
72 EPUser user = applicationsRestClientService.get(EPUser.class, app.getId(),
73 String.format("/user/%s", orgUserId));
77 private static void createNewUserOnRemoteApp(String orgUserId, EPApp app,
78 ApplicationsRestClientService applicationsRestClientService, SearchService searchService,
79 ObjectMapper mapper) throws Exception {
80 EPUser client = searchService.searchUserByUserId(orgUserId);
82 String msg = "cannot create user " + orgUserId + ", because he/she cannot be found in phonebook.";
83 logger.error(EELFLoggerDelegate.errorLogger, msg);
84 throw new Exception(msg);
86 client.setLoginId(orgUserId);
87 client.setActive(true);
88 // The remote doesn't care about other apps, and this has caused
89 // serialization problems - infinite recursion.
90 client.getEPUserApps().clear();
91 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
92 String userAsString = mapper.writeValueAsString(client);
93 logger.debug(EELFLoggerDelegate.debugLogger,
94 "about to post new client to remote application, users json = " + userAsString);
95 applicationsRestClientService.post(EPUser.class, app.getId(), userAsString, String.format("/user", orgUserId));
98 public static void persistExternalRoleInEcompDb(EPRole externalAppRole, Long appId, EPRoleService roleService) {
99 externalAppRole.setAppId(appId);
100 externalAppRole.setAppRoleId(externalAppRole.getId());
101 externalAppRole.setId(null); // We will persist a new role, with onap
102 // role id which will be different than
103 // external app role id.
105 roleService.saveRole(externalAppRole);
106 logger.debug(EELFLoggerDelegate.debugLogger,
107 String.format("ONAP persists role from app:%d, app roleId: %d, roleName: %s", appId,
108 externalAppRole.getAppRoleId(), externalAppRole.getName()));
112 public List<EPUserApp> getCachedAppRolesForUser(Long appId, Long userId) {
113 // Find the records for this user-app combo, if any
114 String filter = " where user_id = " + Long.toString(userId) + " and app_id = " + Long.toString(appId);
115 @SuppressWarnings("unchecked")
116 List<EPUserApp> roleList = dataAccessService.getList(EPUserApp.class, filter, null, null);
117 logger.debug(EELFLoggerDelegate.debugLogger, "getCachedAppRolesForUser: list size is {}", roleList.size());