1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
/*-
* ============LICENSE_START=======================================================
* SDC
* ================================================================================
* Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.openecomp.sdc.be.filters;
import org.onap.sdc.security.AuthenticationCookie;
import org.onap.sdc.security.IUsersThreadLocalHolder;
import org.onap.sdc.security.PortalClient;
import org.onap.sdc.security.RestrictionAccessFilterException;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.be.user.UserBusinessLogic;
import org.openecomp.sdc.common.api.Constants;
import org.openecomp.sdc.common.datastructure.UserContext;
import org.openecomp.sdc.common.log.wrappers.Logger;
import org.openecomp.sdc.common.util.ThreadLocalsHolder;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class ThreadLocalUtils implements IUsersThreadLocalHolder {
@Autowired
PortalClient portalClient;
@Autowired
UserBusinessLogic userBusinessLogic;
private static final Logger log = Logger.getLogger(ThreadLocalUtils.class);
@Override
public void setUserContext(AuthenticationCookie authenticationCookie) {
UserContext userContext;
userContext = new UserContext(authenticationCookie.getUserID(), authenticationCookie.getRoles(), authenticationCookie.getFirstName(), authenticationCookie.getLastName());
ThreadLocalsHolder.setUserContext(userContext);
}
protected void setUserContext(HttpServletRequest httpRequest) {
String user_id = httpRequest.getHeader(Constants.USER_ID_HEADER);
if (user_id != null) {
String userRolesFromPortal = null;
Set<String> roles = null;
try {
userRolesFromPortal = portalClient.fetchUserRolesFromPortal(user_id);
roles = new HashSet<>(Arrays.asList(userRolesFromPortal));
} catch (RestrictionAccessFilterException e) {
log.debug("Failed to fetch user ID - {} from portal", user_id);
log.debug(e.getMessage());
}
UserContext userContext = new UserContext(user_id, roles, null, null);
ThreadLocalsHolder.setUserContext(userContext);
} else log.debug("user_id value in req header is null, userContext will not be initialized");
}
protected void setUserContextFromDB(HttpServletRequest httpRequest) {
String user_id = httpRequest.getHeader(Constants.USER_ID_HEADER);
//there are some internal request that have no user_id header e.g. healthcheck
if (user_id != null) {
updateUserContext(user_id);
} else log.debug("user_id value in req header is null, userContext will not be initialized");
}
protected void setUserContextFromDB(AuthenticationCookie authenticationCookie) {
String user_id = authenticationCookie.getUserID();
updateUserContext(user_id);
}
private void updateUserContext(String user_id) {
User user = userBusinessLogic.getUser(user_id, false);
Set<String> roles = new HashSet<>(Arrays.asList(user.getRole()));
UserContext userContext = new UserContext(user_id, roles, user.getFirstName(), user.getLastName());
ThreadLocalsHolder.setUserContext(userContext);
}
}
|