Update license; improve coverage; add docs dir
[portal.git] / ecomp-portal-BE-os / src / main / java / org / openecomp / portalapp / portal / service / UserRolesServiceImpl.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.openecomp.portalapp.portal.service;
39
40 import java.util.ArrayList;
41 import java.util.List;
42
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;
57
58 import com.fasterxml.jackson.databind.DeserializationFeature;
59 import com.fasterxml.jackson.databind.ObjectMapper;
60
61 @Service("userRolesService")
62 @Transactional
63 @org.springframework.context.annotation.Configuration
64 @EnableAspectJAutoProxy
65 @EPMetricsLog
66 public class UserRolesServiceImpl extends UserRolesCommonServiceImpl implements UserRolesService {
67
68         private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UserRolesServiceImpl.class);
69
70         @Autowired
71         private DataAccessService dataAccessService;
72         @Autowired
73         private ApplicationsRestClientService applicationsRestClientService;
74
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));
79                 return user;
80         }
81
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);
86                 if (client == null) {
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);
90                 }
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));
101         }
102
103         @Override
104         public List<UserApplicationRoles> getUsersFromAppEndpoint(Long appId) throws HTTPException {
105                 RemoteUserWithRoles[] remoteUsers = applicationsRestClientService.get(RemoteUserWithRoles[].class, appId,
106                                 "/users");
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);
112                         } else {
113                                 logger.debug(EELFLoggerDelegate.debugLogger, "User " + userWithRemoteAppRoles.getOrgUserId()
114                                                 + " doesn't have any roles assigned to any app.");
115
116                         }
117                 }
118
119                 return userApplicationRoles;
120         }
121
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.
128
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()));
133         }
134
135         @Override
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());
142                 return roleList;
143         }
144
145 }