c0d14f603261e521c6235d67db3455133f57b100
[portal.git] / ecomp-portal-BE-os / src / main / java / org / onap / portalapp / portal / service / UserRolesServiceImpl.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 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.onap.portalapp.portal.service;
39
40 import java.util.List;
41
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;
54
55 import com.fasterxml.jackson.databind.DeserializationFeature;
56 import com.fasterxml.jackson.databind.ObjectMapper;
57
58 @Service("userRolesService")
59 @Transactional
60 @org.springframework.context.annotation.Configuration
61 @EnableAspectJAutoProxy
62 @EPMetricsLog
63 public class UserRolesServiceImpl extends UserRolesCommonServiceImpl implements UserRolesService {
64
65         private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UserRolesServiceImpl.class);
66
67         @Autowired
68         private DataAccessService dataAccessService;
69         
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));
74                 return user;
75         }
76
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);
81                 if (client == null) {
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);
85                 }
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));
96         }
97
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 ecomp
102                                                                                 // role id which will be different than
103                                                                                 // external app role id.
104
105                 roleService.saveRole(externalAppRole);
106                 logger.debug(EELFLoggerDelegate.debugLogger,
107                                 String.format("ECOMP persists role from app:%d, app roleId: %d, roleName: %s", appId,
108                                                 externalAppRole.getAppRoleId(), externalAppRole.getName()));
109         }
110
111         @Override
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());
118                 return roleList;
119         }
120
121         
122
123 }