|
30 | 30 | package org.hisp.dhis.webapi.controller.user; |
31 | 31 |
|
32 | 32 | import static org.hisp.dhis.fieldfiltering.FieldFilterParams.*; |
| 33 | +import static org.hisp.dhis.webapi.controller.security.ImpersonateUserController.hasAllowListedIp; |
33 | 34 | import static org.hisp.dhis.webapi.utils.ContextUtils.setNoStore; |
34 | 35 | import static org.springframework.http.CacheControl.noStore; |
35 | 36 | import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; |
|
54 | 55 | import org.hisp.dhis.dataapproval.DataApprovalLevel; |
55 | 56 | import org.hisp.dhis.dataapproval.DataApprovalLevelService; |
56 | 57 | import org.hisp.dhis.dataset.DataSetService; |
| 58 | +import org.hisp.dhis.external.conf.ConfigurationKey; |
| 59 | +import org.hisp.dhis.external.conf.DhisConfigurationProvider; |
57 | 60 | import org.hisp.dhis.feedback.ConflictException; |
58 | 61 | import org.hisp.dhis.fieldfiltering.FieldFilterService; |
59 | 62 | import org.hisp.dhis.fieldfiltering.FieldPreset; |
|
72 | 75 | import org.hisp.dhis.program.ProgramService; |
73 | 76 | import org.hisp.dhis.query.GetObjectParams; |
74 | 77 | import org.hisp.dhis.render.RenderService; |
| 78 | +import org.hisp.dhis.security.Authorities; |
75 | 79 | import org.hisp.dhis.security.PasswordManager; |
76 | 80 | import org.hisp.dhis.security.acl.Access; |
77 | 81 | import org.hisp.dhis.security.acl.AclService; |
|
118 | 122 | @RequestMapping("/api/me") |
119 | 123 | @RequiredArgsConstructor |
120 | 124 | public class MeController { |
| 125 | + @Nonnull private final ContextService contextService; |
| 126 | + @Nonnull private final DhisConfigurationProvider config; |
121 | 127 | @Nonnull private final UserService userService; |
122 | | - |
123 | 128 | @Nonnull private final UserControllerUtils userControllerUtils; |
124 | | - |
125 | | - @Nonnull protected ContextService contextService; |
126 | | - |
127 | 129 | @Nonnull private final RenderService renderService; |
128 | | - |
129 | 130 | @Nonnull private final FieldFilterService fieldFilterService; |
130 | | - |
131 | 131 | @Nonnull private final org.hisp.dhis.fieldfilter.FieldFilterService oldFieldFilterService; |
132 | | - |
133 | 132 | @Nonnull private final IdentifiableObjectManager manager; |
134 | | - |
135 | 133 | @Nonnull private final PasswordManager passwordManager; |
136 | | - |
137 | 134 | @Nonnull private final MessageService messageService; |
138 | | - |
139 | 135 | @Nonnull private final InterpretationService interpretationService; |
140 | | - |
141 | 136 | @Nonnull private final NodeService nodeService; |
142 | | - |
143 | 137 | @Nonnull private final PasswordValidationService passwordValidationService; |
144 | | - |
145 | 138 | @Nonnull private final ProgramService programService; |
146 | | - |
147 | 139 | @Nonnull private final DataSetService dataSetService; |
148 | | - |
149 | 140 | @Nonnull private final AclService aclService; |
150 | | - |
151 | 141 | @Nonnull private final DataApprovalLevelService approvalLevelService; |
152 | | - |
153 | 142 | @Nonnull private final FileResourceService fileResourceService; |
154 | | - |
155 | 143 | @Nonnull private ApiTokenService apiTokenService; |
156 | 144 |
|
157 | 145 | @GetMapping |
158 | 146 | @OpenApi.Response(MeDto.class) |
159 | 147 | @OpenApi.EntityType(MeDto.class) |
160 | 148 | public @ResponseBody ResponseEntity<JsonNode> getCurrentUser( |
161 | | - @CurrentUser(required = true) User user, GetObjectParams params) { |
| 149 | + @CurrentUser(required = true) User user, GetObjectParams params, HttpServletRequest request) { |
162 | 150 |
|
163 | 151 | List<String> fields = params.getFields(); |
164 | 152 | if (fields == null || fields.isEmpty()) fields = List.of("*"); |
@@ -188,26 +176,34 @@ public class MeController { |
188 | 176 | JsonMap<JsonMixed> s = |
189 | 177 | settingKeys.isEmpty() ? settings.toJson(false) : settings.toJson(true, settingKeys); |
190 | 178 | MeDto meDto = new MeDto(user, s, programs, dataSets, patTokens); |
191 | | - determineUserImpersonation(meDto); |
| 179 | + determineUserImpersonation(meDto, user.getAllAuthorities(), request); |
192 | 180 |
|
193 | 181 | ObjectNode jsonNodes = fieldFilterService.toObjectNodes(of(meDto, fields)).get(0); |
194 | 182 |
|
195 | 183 | return ResponseEntity.ok(jsonNodes); |
196 | 184 | } |
197 | 185 |
|
198 | | - private void determineUserImpersonation(MeDto meDto) { |
| 186 | + private void determineUserImpersonation( |
| 187 | + MeDto meDto, Set<String> allAuthorities, HttpServletRequest request) { |
199 | 188 | Authentication current = SecurityContextHolder.getContext().getAuthentication(); |
200 | 189 |
|
201 | | - Authentication original = null; |
202 | 190 | // iterate over granted authorities and find the 'switch user' authority |
203 | 191 | Collection<? extends GrantedAuthority> authorities = current.getAuthorities(); |
204 | 192 | for (GrantedAuthority auth : authorities) { |
205 | 193 | // check for switch user type of authority |
206 | | - if (auth instanceof SwitchUserGrantedAuthority) { |
207 | | - original = ((SwitchUserGrantedAuthority) auth).getSource(); |
208 | | - meDto.setImpersonation(original.getName()); |
| 194 | + if (auth instanceof SwitchUserGrantedAuthority userGrantedAuthority) { |
| 195 | + meDto.setImpersonation(userGrantedAuthority.getSource().getName()); |
209 | 196 | } |
210 | 197 | } |
| 198 | + |
| 199 | + String remoteAddr = request.getRemoteAddr(); |
| 200 | + boolean enabled = config.isEnabled(ConfigurationKey.SWITCH_USER_FEATURE_ENABLED); |
| 201 | + if (enabled |
| 202 | + && (allAuthorities.contains(Authorities.ALL.name()) |
| 203 | + || allAuthorities.contains(Authorities.F_IMPERSONATE_USER.name())) |
| 204 | + && hasAllowListedIp(remoteAddr, config)) { |
| 205 | + meDto.setCanImpersonate(true); |
| 206 | + } |
211 | 207 | } |
212 | 208 |
|
213 | 209 | private boolean fieldsContains(String key, List<String> fields) { |
|
0 commit comments