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.util.ArrayList;
41 import java.util.List;
43 import org.apache.cxf.transport.http.HTTPException;
44 import org.openecomp.portalapp.portal.domain.EPApp;
45 import org.openecomp.portalapp.portal.domain.EPRole;
46 import org.openecomp.portalapp.portal.domain.EPUser;
47 import org.openecomp.portalapp.portal.domain.EPUserApp;
48 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
49 import org.openecomp.portalapp.portal.transport.RemoteUserWithRoles;
50 import org.openecomp.portalapp.portal.transport.UserApplicationRoles;
51 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
52 import org.openecomp.portalsdk.core.service.DataAccessService;
53 import org.springframework.beans.factory.annotation.Autowired;
54 import org.springframework.context.annotation.EnableAspectJAutoProxy;
55 import org.springframework.stereotype.Service;
56 import org.springframework.transaction.annotation.Transactional;
58 import com.fasterxml.jackson.databind.DeserializationFeature;
59 import com.fasterxml.jackson.databind.ObjectMapper;
61 @Service("userRolesService")
63 @org.springframework.context.annotation.Configuration
64 @EnableAspectJAutoProxy
66 public class UserRolesServiceImpl extends UserRolesCommonServiceImpl implements UserRolesService {
68 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UserRolesServiceImpl.class);
71 private DataAccessService dataAccessService;
73 private ApplicationsRestClientService applicationsRestClientService;
75 private EPUser getUserFromRemoteApp(String orgUserId, EPApp app,
76 ApplicationsRestClientService applicationsRestClientService) throws HTTPException {
77 EPUser user = applicationsRestClientService.get(EPUser.class, app.getId(),
78 String.format("/user/%s", orgUserId));
82 private static void createNewUserOnRemoteApp(String orgUserId, EPApp app,
83 ApplicationsRestClientService applicationsRestClientService, SearchService searchService,
84 ObjectMapper mapper) throws Exception {
85 EPUser client = searchService.searchUserByUserId(orgUserId);
87 String msg = "cannot create user " + orgUserId + ", because he/she cannot be found in phonebook.";
88 logger.error(EELFLoggerDelegate.errorLogger, msg);
89 throw new Exception(msg);
91 client.setLoginId(orgUserId);
92 client.setActive(true);
93 // The remote doesn't care about other apps, and this has caused
94 // serialization problems - infinite recursion.
95 client.getEPUserApps().clear();
96 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
97 String userAsString = mapper.writeValueAsString(client);
98 logger.debug(EELFLoggerDelegate.debugLogger,
99 "about to post new client to remote application, users json = " + userAsString);
100 applicationsRestClientService.post(EPUser.class, app.getId(), userAsString, String.format("/user", orgUserId));
104 public List<UserApplicationRoles> getUsersFromAppEndpoint(Long appId) throws HTTPException {
105 RemoteUserWithRoles[] remoteUsers = applicationsRestClientService.get(RemoteUserWithRoles[].class, appId,
107 ArrayList<UserApplicationRoles> userApplicationRoles = new ArrayList<UserApplicationRoles>();
108 for (RemoteUserWithRoles remoteUser : remoteUsers) {
109 UserApplicationRoles userWithRemoteAppRoles = convertToUserApplicationRoles(appId, remoteUser);
110 if (userWithRemoteAppRoles.getRoles() != null && userWithRemoteAppRoles.getRoles().size() > 0) {
111 userApplicationRoles.add(userWithRemoteAppRoles);
113 logger.debug(EELFLoggerDelegate.debugLogger, "User " + userWithRemoteAppRoles.getOrgUserId()
114 + " doesn't have any roles assigned to any app.");
119 return userApplicationRoles;
122 public static void persistExternalRoleInEcompDb(EPRole externalAppRole, Long appId, EPRoleService roleService) {
123 externalAppRole.setAppId(appId);
124 externalAppRole.setAppRoleId(externalAppRole.getId());
125 externalAppRole.setId(null); // We will persist a new role, with ecomp
126 // role id which will be different than
127 // external app role id.
129 roleService.saveRole(externalAppRole);
130 logger.debug(EELFLoggerDelegate.debugLogger,
131 String.format("ECOMP persists role from app:%d, app roleId: %d, roleName: %s", appId,
132 externalAppRole.getAppRoleId(), externalAppRole.getName()));
136 public List<EPUserApp> getCachedAppRolesForUser(Long appId, Long userId) {
137 // Find the records for this user-app combo, if any
138 String filter = " where user_id = " + Long.toString(userId) + " and app_id = " + Long.toString(appId);
139 @SuppressWarnings("unchecked")
140 List<EPUserApp> roleList = dataAccessService.getList(EPUserApp.class, filter, null, null);
141 logger.debug(EELFLoggerDelegate.debugLogger, "getCachedAppRolesForUser: list size is {}", roleList.size());