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.util.ArrayList;
23 import java.util.List;
25 import org.apache.cxf.transport.http.HTTPException;
26 import org.openecomp.portalapp.portal.domain.EPApp;
27 import org.openecomp.portalapp.portal.domain.EPRole;
28 import org.openecomp.portalapp.portal.domain.EPUser;
29 import org.openecomp.portalapp.portal.domain.EPUserApp;
30 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
31 import org.openecomp.portalapp.portal.transport.RemoteUserWithRoles;
32 import org.openecomp.portalapp.portal.transport.UserApplicationRoles;
33 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
34 import org.openecomp.portalsdk.core.service.DataAccessService;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.context.annotation.EnableAspectJAutoProxy;
37 import org.springframework.stereotype.Service;
38 import org.springframework.transaction.annotation.Transactional;
40 import com.fasterxml.jackson.databind.DeserializationFeature;
41 import com.fasterxml.jackson.databind.ObjectMapper;
43 @Service("userRolesService")
45 @org.springframework.context.annotation.Configuration
46 @EnableAspectJAutoProxy
48 public class UserRolesServiceImpl extends UserRolesCommonServiceImpl implements UserRolesService {
50 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UserRolesServiceImpl.class);
53 private DataAccessService dataAccessService;
55 private ApplicationsRestClientService applicationsRestClientService;
57 private EPUser getUserFromRemoteApp(String orgUserId, EPApp app,
58 ApplicationsRestClientService applicationsRestClientService) throws HTTPException {
59 EPUser user = applicationsRestClientService.get(EPUser.class, app.getId(),
60 String.format("/user/%s", orgUserId));
64 private static void createNewUserOnRemoteApp(String orgUserId, EPApp app,
65 ApplicationsRestClientService applicationsRestClientService, SearchService searchService,
66 ObjectMapper mapper) throws Exception {
67 EPUser client = searchService.searchUserByUserId(orgUserId);
69 String msg = "cannot create user " + orgUserId + ", because he/she cannot be found in phonebook.";
70 logger.error(EELFLoggerDelegate.errorLogger, msg);
71 throw new Exception(msg);
73 client.setLoginId(orgUserId);
74 client.setActive(true);
75 // The remote doesn't care about other apps, and this has caused
76 // serialization problems - infinite recursion.
77 client.getEPUserApps().clear();
78 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
79 String userAsString = mapper.writeValueAsString(client);
80 logger.debug(EELFLoggerDelegate.debugLogger,
81 "about to post new client to remote application, users json = " + userAsString);
82 applicationsRestClientService.post(EPUser.class, app.getId(), userAsString, String.format("/user", orgUserId));
86 public List<UserApplicationRoles> getUsersFromAppEndpoint(Long appId) throws HTTPException {
87 RemoteUserWithRoles[] remoteUsers = applicationsRestClientService.get(RemoteUserWithRoles[].class, appId,
89 ArrayList<UserApplicationRoles> userApplicationRoles = new ArrayList<UserApplicationRoles>();
90 for (RemoteUserWithRoles remoteUser : remoteUsers) {
91 UserApplicationRoles userWithRemoteAppRoles = convertToUserApplicationRoles(appId, remoteUser);
92 if (userWithRemoteAppRoles.getRoles() != null && userWithRemoteAppRoles.getRoles().size() > 0) {
93 userApplicationRoles.add(userWithRemoteAppRoles);
95 logger.debug(EELFLoggerDelegate.debugLogger, "User " + userWithRemoteAppRoles.getOrgUserId()
96 + " doesn't have any roles assigned to any app.");
101 return userApplicationRoles;
104 public static void persistExternalRoleInEcompDb(EPRole externalAppRole, Long appId, EPRoleService roleService) {
105 externalAppRole.setAppId(appId);
106 externalAppRole.setAppRoleId(externalAppRole.getId());
107 externalAppRole.setId(null); // We will persist a new role, with ecomp
108 // role id which will be different than
109 // external app role id.
111 roleService.saveRole(externalAppRole);
112 logger.debug(EELFLoggerDelegate.debugLogger,
113 String.format("ECOMP persists role from app:%d, app roleId: %d, roleName: %s", appId,
114 externalAppRole.getAppRoleId(), externalAppRole.getName()));
118 public List<EPUserApp> getCachedAppRolesForUser(Long appId, Long userId) {
119 // Find the records for this user-app combo, if any
120 String filter = " where user_id = " + Long.toString(userId) + " and app_id = " + Long.toString(appId);
121 @SuppressWarnings("unchecked")
122 List<EPUserApp> roleList = dataAccessService.getList(EPUserApp.class, filter, null, null);
123 logger.debug(EELFLoggerDelegate.debugLogger, "getCachedAppRolesForUser: list size is {}", roleList.size());