42b0291c898304493a66601f3d7102920c3b3122
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / filters / ThreadLocalUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.be.filters;
21
22 import java.util.Arrays;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Optional;
26 import java.util.Set;
27 import javax.servlet.http.HttpServletRequest;
28 import org.onap.sdc.security.AuthenticationCookie;
29 import org.onap.sdc.security.IUsersThreadLocalHolder;
30 import org.onap.sdc.security.PortalClient;
31 import org.onap.sdc.security.RestrictionAccessFilterException;
32 import org.openecomp.sdc.be.model.User;
33 import org.openecomp.sdc.be.user.UserBusinessLogic;
34 import org.openecomp.sdc.common.api.Constants;
35 import org.openecomp.sdc.common.datastructure.UserContext;
36 import org.openecomp.sdc.common.log.wrappers.Logger;
37 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
38 import org.springframework.beans.factory.annotation.Autowired;
39
40 public class ThreadLocalUtils implements IUsersThreadLocalHolder {
41
42     private static final Logger log = Logger.getLogger(ThreadLocalUtils.class);
43     @Autowired
44     PortalClient portalClient;
45     @Autowired
46     UserBusinessLogic userBusinessLogic;
47
48     @Override
49     public void setUserContext(AuthenticationCookie authenticationCookie) {
50         UserContext userContext;
51         userContext = new UserContext(authenticationCookie.getUserID(), authenticationCookie.getRoles(), authenticationCookie.getFirstName(),
52             authenticationCookie.getLastName());
53         ThreadLocalsHolder.setUserContext(userContext);
54     }
55
56     protected void setUserContext(HttpServletRequest httpRequest) {
57         final String userId = httpRequest.getHeader(Constants.USER_ID_HEADER);
58         if (userId != null) {
59             Set<String> roles = null;
60             try {
61                 final Optional<String> userRolesFromPortalOptional = portalClient.fetchUserRolesFromPortal(userId);
62                 if (userRolesFromPortalOptional.isPresent()){
63                     roles = new HashSet<>(List.of(userRolesFromPortalOptional.get()));
64                 }
65             } catch (RestrictionAccessFilterException e) {
66                 log.debug("Failed to fetch user ID - {} from portal", userId);
67                 log.debug(e.getMessage());
68             }
69             final UserContext userContext = new UserContext(userId, roles, null, null);
70             ThreadLocalsHolder.setUserContext(userContext);
71         } else {
72             log.debug("user_id value in req header is null, userContext will not be initialized");
73         }
74     }
75
76     protected void setUserContextFromDB(HttpServletRequest httpRequest) {
77         String user_id = httpRequest.getHeader(Constants.USER_ID_HEADER);
78         //there are some internal request that have no user_id header e.g. healthcheck
79         if (user_id != null) {
80             updateUserContext(user_id);
81         } else {
82             log.debug("user_id value in req header is null, userContext will not be initialized");
83         }
84     }
85
86     private void updateUserContext(String user_id) {
87         User user = userBusinessLogic.getUser(user_id, false);
88         Set<String> roles = new HashSet<>(Arrays.asList(user.getRole()));
89         UserContext userContext = new UserContext(user.getUserId(), roles, user.getFirstName(), user.getLastName());
90         ThreadLocalsHolder.setUserContext(userContext);
91     }
92 }