|
6 | 6 | import static edu.harvard.hms.dbmi.avillach.auth.JAXRSConfiguration.fence_standard_access_rules;
|
7 | 7 | import static edu.harvard.hms.dbmi.avillach.auth.JAXRSConfiguration.fence_topmed_consent_group_concept_path;
|
8 | 8 |
|
9 |
| -import java.io.IOException; |
10 | 9 | import java.util.*;
|
11 | 10 | import java.util.regex.Matcher;
|
12 | 11 | import java.util.regex.Pattern;
|
13 |
| -import java.util.stream.Collectors; |
14 | 12 |
|
15 | 13 | import javax.annotation.PostConstruct;
|
16 | 14 | import javax.inject.Inject;
|
@@ -221,27 +219,71 @@ public Response getFENCEProfile(String callback_url, Map<String, String> authReq
|
221 | 219 | project_access_set.add(newRoleName);
|
222 | 220 | }
|
223 | 221 |
|
224 |
| - // given a set of all access role names, we can create a sinlge query to get all Role objects |
225 |
| - // that match the names in the set |
226 |
| - Set<String> roles = roleRepo.getRoleNamesByNames(project_access_set); |
| 222 | + /* |
| 223 | + Considerations for updating the user's roles: |
| 224 | + 1. If the user has a role that is not in the project_access_set, we need to remove it. |
| 225 | + a. Unless it is a manual role, PIC-SURE Top Admin, Admin, and Manual, in which case we do not need to do anything. |
| 226 | + 2. If the user does not have a role that is in the project_access_set, we need to add it. |
| 227 | + a. We can first check if the role exists in the database, and if it does, we can add it to the user. |
| 228 | + b. If the role does not exist in the database, we need to create it and add it to the user. |
| 229 | + 3. If the user has a role that is in the project_access_set, we do not need to do anything. |
| 230 | + */ |
| 231 | + |
| 232 | + // Step 1: Remove roles that are not in the project_access_set |
| 233 | + Set<Role> rolesToRemove = new HashSet<>(); |
| 234 | + // Also, track the roles that are assigned to the user and in the project_access_set |
| 235 | + Set<String> rolesAssigned = new HashSet<>(); |
| 236 | + for (Role role : current_user.getRoles()) { |
| 237 | + // .filter(userRole -> "PIC-SURE Top Admin".equals(userRole.getName()) || "Admin".equals(userRole.getName()) || userRole.getName().startsWith("MANUAL_")) |
| 238 | + if (!project_access_set.contains(role.getName()) |
| 239 | + && !role.getName().startsWith("MANUAL_") |
| 240 | + && !role.getName().equals(fence_open_access_role_name) |
| 241 | + && !role.getName().equals("PIC-SURE Top Admin") |
| 242 | + && !role.getName().equals("Admin")) { |
| 243 | + rolesToRemove.add(role); |
| 244 | + } |
| 245 | + |
| 246 | + if (project_access_set.contains(role.getName())) { |
| 247 | + rolesAssigned.add(role.getName()); |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + // Remove roles that are not in the project_access_set |
| 252 | + if (!CollectionUtils.isEmpty(rolesToRemove)) { |
| 253 | + logger.info("getFENCEProfile() removing roles: {}", rolesToRemove); |
| 254 | + current_user.getRoles().removeAll(rolesToRemove); |
| 255 | + } |
| 256 | + |
| 257 | + // Given the set of roles assigned and that set of roles that should be assigned, we can reduce the set of roles from the project_access_set |
| 258 | + // to only those that are not in the rolesAssigned set |
| 259 | + project_access_set.removeAll(rolesAssigned); |
| 260 | + |
| 261 | + // Given our reduced list of roles that should be assigned, we can determine which of those roles are not in the database |
| 262 | + // This also tells use which roles are in the database |
| 263 | + Set<String> rolesThatExist = roleRepo.getRoleNamesByNames(project_access_set); |
| 264 | + |
| 265 | + // Assign the roles that exist in the database to the user |
| 266 | + logger.info("getFENCEProfile() roles that exist in the database: {}", rolesThatExist); |
| 267 | + roleRepo.getRolesByNames(rolesThatExist).forEach(role -> current_user.getRoles().add(role)); |
227 | 268 |
|
228 | 269 | // Given a set of all access role names that exist in the database we can now determine which do not exist
|
229 | 270 | // and create them
|
230 |
| - project_access_set.removeAll(roles); |
231 |
| - logger.info("getFENCEProfile() roles that do not exist in the database: {}", project_access_set); |
| 271 | + project_access_set.removeAll(rolesThatExist); |
232 | 272 |
|
| 273 | + logger.info("getFENCEProfile() roles that do not exist in the database: {}", project_access_set); |
233 | 274 | // Given the set of all access role names that do not exist in the database we can now create them
|
234 | 275 | ArrayList<Role> newRoles = new ArrayList<>();
|
235 | 276 | for (String access_role_name : project_access_set) {
|
236 | 277 | newRoles.add(createAndUpsertRole(access_role_name, current_user));
|
237 | 278 | }
|
238 | 279 |
|
239 |
| - // add the new roles to the user |
240 |
| - current_user.getRoles().addAll(newRoles); |
241 |
| - |
242 | 280 | // Persist the new roles
|
| 281 | + logger.info("getFENCEProfile() persisting {} new roles", newRoles.size()); |
243 | 282 | roleRepo.persistAll(newRoles);
|
244 | 283 |
|
| 284 | + // Assign the new roles to the user |
| 285 | + current_user.getRoles().addAll(newRoles); |
| 286 | + |
245 | 287 | final String idp = extractIdp(current_user);
|
246 | 288 | if (current_user.getRoles() != null && (!current_user.getRoles().isEmpty() || openAccessIdpValues.contains(idp))) {
|
247 | 289 | Role openAccessRole = roleRepo.getUniqueResultByColumn("name", fence_open_access_role_name);
|
@@ -316,15 +358,15 @@ private User createUserFromFENCEProfile(JsonNode node) {
|
316 | 358 |
|
317 | 359 | User actual_user = userRepo.findOrCreate(new_user);
|
318 | 360 |
|
319 |
| - Set<Role> roles = new HashSet<>(); |
320 |
| - if (actual_user != null && !CollectionUtils.isEmpty(actual_user.getRoles())) { |
321 |
| - roles = actual_user.getRoles().stream() |
322 |
| - .filter(userRole -> "PIC-SURE Top Admin".equals(userRole.getName()) || "Admin".equals(userRole.getName()) || userRole.getName().startsWith("MANUAL_")) |
323 |
| - .collect(Collectors.toSet()); |
324 |
| - } |
| 361 | +// Set<Role> roles = new HashSet<>(); |
| 362 | +// if (actual_user != null && !CollectionUtils.isEmpty(actual_user.getRoles())) { |
| 363 | +// roles = actual_user.getRoles().stream() |
| 364 | +// .filter(userRole -> "PIC-SURE Top Admin".equals(userRole.getName()) || "Admin".equals(userRole.getName()) || userRole.getName().startsWith("MANUAL_")) |
| 365 | +// .collect(Collectors.toSet()); |
| 366 | +// } |
325 | 367 |
|
326 | 368 | // Clear current set of roles every time we create or retrieve a user but persist admin status
|
327 |
| - actual_user.setRoles(roles); |
| 369 | +// actual_user.setRoles(roles); |
328 | 370 |
|
329 | 371 | logger.debug("createUserFromFENCEProfile() cleared roles");
|
330 | 372 |
|
|
0 commit comments