@Test public void testSetUserRole() throws Exception { UserRole userRole = UserRole.newInstance(TESTAPP, Role.ADMIN).withUserID(USER).build(); Experiment experiment = Experiment.withID(Experiment.ID.newInstance()).withApplicationName(TESTAPP).build(); Experiment experiment1 = Experiment.withID(Experiment.ID.newInstance()).withApplicationName(TESTAPP).build(); List<Experiment> experimentList = new ArrayList<>(); when(experiments.getExperiments(TESTAPP)).thenReturn(experimentList); Map<String, String> status = createTestStatus(userRole); Map map = defaultAuthorization.setUserRole(userRole, EventLog.SYSTEM_USER); assertEquals(status, map); experimentList.add(experiment); experimentList.add(experiment1); status.remove("reason"); status.put("roleAssignmentStatus", "SUCCESS"); when(experiments.getExperiments(TESTAPP)).thenReturn(experimentList); UserRoleList userRoleList = Mockito.mock(UserRoleList.class); when(authorizationRepository.getUserRoleList(userRole.getUserID())).thenReturn(userRoleList); when(authorizationRepository.getUserInfo(userRole.getUserID())).thenReturn(EventLog.SYSTEM_USER); when(userRoleList.getRoleList()).thenReturn(Collections.<UserRole>emptyList()); map = defaultAuthorization.setUserRole(userRole, EventLog.SYSTEM_USER); assertEquals(status, map); experimentList.add(experiment); experimentList.add(experiment1); when(experiments.getExperiments(TESTAPP)).thenReturn(experimentList); status.put("reason", "RepositoryException"); status.put("roleAssignmentStatus", "FAILED"); doThrow(RepositoryException.class).when(authorizationRepository) .setUserRole(userRole); map = defaultAuthorization.setUserRole(userRole, EventLog.SYSTEM_USER); assertEquals(status, map); }
@Override public Map setUserRole(UserRole userRole, UserInfo admin) { Map<String, String> status = new HashMap<>(); status.put("applicationName", userRole.getApplicationName().toString()); status.put("userID", userRole.getUserID().toString()); status.put("role", userRole.getRole().toString()); List<Experiment> experimentList = experiments.getExperiments(userRole.getApplicationName()); if (!experimentList.isEmpty()) { List<UserRole> userRoleList = authorizationRepository.getUserRoleList(userRole.getUserID()).getRoleList(); Role oldRole = null; for (UserRole role : userRoleList) { if (role.getApplicationName().equals(userRole.getApplicationName())) { oldRole = role.getRole(); break; } } try { authorizationRepository.setUserRole(userRole); status.put("roleAssignmentStatus", "SUCCESS"); UserInfo user = getUserInfo(userRole.getUserID()); eventLog.postEvent(new AuthorizationChangeEvent(admin, userRole.getApplicationName(), user, oldRole == null || "superadmin".equalsIgnoreCase(oldRole.toString()) ? null : oldRole.toString(), userRole.getRole().toString())); } catch (RepositoryException e) { LOGGER.info("RepoitoryException for setting user Role in DefaultAuthorization ", e); status.put("roleAssignmentStatus", "FAILED"); status.put("reason", "RepositoryException"); } } else { status.put("roleAssignmentStatus", "FAILED"); status.put("reason", "No application named " + userRole.getApplicationName()); } return status; }
DefaultAuthorization implements Authorization { @Override public Map setUserRole(UserRole userRole, UserInfo admin) { Map<String, String> status = new HashMap<>(); status.put("applicationName", userRole.getApplicationName().toString()); status.put("userID", userRole.getUserID().toString()); status.put("role", userRole.getRole().toString()); List<Experiment> experimentList = experiments.getExperiments(userRole.getApplicationName()); if (!experimentList.isEmpty()) { List<UserRole> userRoleList = authorizationRepository.getUserRoleList(userRole.getUserID()).getRoleList(); Role oldRole = null; for (UserRole role : userRoleList) { if (role.getApplicationName().equals(userRole.getApplicationName())) { oldRole = role.getRole(); break; } } try { authorizationRepository.setUserRole(userRole); status.put("roleAssignmentStatus", "SUCCESS"); UserInfo user = getUserInfo(userRole.getUserID()); eventLog.postEvent(new AuthorizationChangeEvent(admin, userRole.getApplicationName(), user, oldRole == null || "superadmin".equalsIgnoreCase(oldRole.toString()) ? null : oldRole.toString(), userRole.getRole().toString())); } catch (RepositoryException e) { LOGGER.info("RepoitoryException for setting user Role in DefaultAuthorization ", e); status.put("roleAssignmentStatus", "FAILED"); status.put("reason", "RepositoryException"); } } else { status.put("roleAssignmentStatus", "FAILED"); status.put("reason", "No application named " + userRole.getApplicationName()); } return status; } }
DefaultAuthorization implements Authorization { @Override public Map setUserRole(UserRole userRole, UserInfo admin) { Map<String, String> status = new HashMap<>(); status.put("applicationName", userRole.getApplicationName().toString()); status.put("userID", userRole.getUserID().toString()); status.put("role", userRole.getRole().toString()); List<Experiment> experimentList = experiments.getExperiments(userRole.getApplicationName()); if (!experimentList.isEmpty()) { List<UserRole> userRoleList = authorizationRepository.getUserRoleList(userRole.getUserID()).getRoleList(); Role oldRole = null; for (UserRole role : userRoleList) { if (role.getApplicationName().equals(userRole.getApplicationName())) { oldRole = role.getRole(); break; } } try { authorizationRepository.setUserRole(userRole); status.put("roleAssignmentStatus", "SUCCESS"); UserInfo user = getUserInfo(userRole.getUserID()); eventLog.postEvent(new AuthorizationChangeEvent(admin, userRole.getApplicationName(), user, oldRole == null || "superadmin".equalsIgnoreCase(oldRole.toString()) ? null : oldRole.toString(), userRole.getRole().toString())); } catch (RepositoryException e) { LOGGER.info("RepoitoryException for setting user Role in DefaultAuthorization ", e); status.put("roleAssignmentStatus", "FAILED"); status.put("reason", "RepositoryException"); } } else { status.put("roleAssignmentStatus", "FAILED"); status.put("reason", "No application named " + userRole.getApplicationName()); } return status; } @Inject  DefaultAuthorization(final AuthorizationRepository authorizationRepository, final Experiments experiments,
                                final EventLog eventLog); }
DefaultAuthorization implements Authorization { @Override public Map setUserRole(UserRole userRole, UserInfo admin) { Map<String, String> status = new HashMap<>(); status.put("applicationName", userRole.getApplicationName().toString()); status.put("userID", userRole.getUserID().toString()); status.put("role", userRole.getRole().toString()); List<Experiment> experimentList = experiments.getExperiments(userRole.getApplicationName()); if (!experimentList.isEmpty()) { List<UserRole> userRoleList = authorizationRepository.getUserRoleList(userRole.getUserID()).getRoleList(); Role oldRole = null; for (UserRole role : userRoleList) { if (role.getApplicationName().equals(userRole.getApplicationName())) { oldRole = role.getRole(); break; } } try { authorizationRepository.setUserRole(userRole); status.put("roleAssignmentStatus", "SUCCESS"); UserInfo user = getUserInfo(userRole.getUserID()); eventLog.postEvent(new AuthorizationChangeEvent(admin, userRole.getApplicationName(), user, oldRole == null || "superadmin".equalsIgnoreCase(oldRole.toString()) ? null : oldRole.toString(), userRole.getRole().toString())); } catch (RepositoryException e) { LOGGER.info("RepoitoryException for setting user Role in DefaultAuthorization ", e); status.put("roleAssignmentStatus", "FAILED"); status.put("reason", "RepositoryException"); } } else { status.put("roleAssignmentStatus", "FAILED"); status.put("reason", "No application named " + userRole.getApplicationName()); } return status; } @Inject  DefaultAuthorization(final AuthorizationRepository authorizationRepository, final Experiments experiments,
                                final EventLog eventLog); @Override List<Permission> getPermissionsFromRole(Role role); @Override UserPermissionsList getUserPermissionsList(UserInfo.Username userID); @Override UserRoleList getApplicationUsers(Application.Name applicationName); @Override UserPermissions getUserPermissions(UserInfo.Username userID, Application.Name applicationName); @Override void deleteUserRole(UserInfo.Username userID, Application.Name applicationName, UserInfo admin); @Override void checkUserPermissions(UserInfo.Username userID, Application.Name applicationName, Permission permission); @Override UserInfo.Username getUser(String authHeader); @Override Map setUserRole(UserRole userRole, UserInfo admin); @Override UserRoleList getUserRoleList(UserInfo.Username userID); @Override void checkSuperAdmin(UserInfo.Username userID); @Override UserInfo getUserInfo(UserInfo.Username userID); @Override void assignUserToSuperAdminRole(final UserInfo candidateUserInfo, final UserInfo assigningUserInfo); @Override void removeUserFromSuperAdminRole(final UserInfo candidateUserInfo, final UserInfo assigningUserInfo); @Override List<UserRole> getSuperAdminRoleList(); }
DefaultAuthorization implements Authorization { @Override public Map setUserRole(UserRole userRole, UserInfo admin) { Map<String, String> status = new HashMap<>(); status.put("applicationName", userRole.getApplicationName().toString()); status.put("userID", userRole.getUserID().toString()); status.put("role", userRole.getRole().toString()); List<Experiment> experimentList = experiments.getExperiments(userRole.getApplicationName()); if (!experimentList.isEmpty()) { List<UserRole> userRoleList = authorizationRepository.getUserRoleList(userRole.getUserID()).getRoleList(); Role oldRole = null; for (UserRole role : userRoleList) { if (role.getApplicationName().equals(userRole.getApplicationName())) { oldRole = role.getRole(); break; } } try { authorizationRepository.setUserRole(userRole); status.put("roleAssignmentStatus", "SUCCESS"); UserInfo user = getUserInfo(userRole.getUserID()); eventLog.postEvent(new AuthorizationChangeEvent(admin, userRole.getApplicationName(), user, oldRole == null || "superadmin".equalsIgnoreCase(oldRole.toString()) ? null : oldRole.toString(), userRole.getRole().toString())); } catch (RepositoryException e) { LOGGER.info("RepoitoryException for setting user Role in DefaultAuthorization ", e); status.put("roleAssignmentStatus", "FAILED"); status.put("reason", "RepositoryException"); } } else { status.put("roleAssignmentStatus", "FAILED"); status.put("reason", "No application named " + userRole.getApplicationName()); } return status; } @Inject  DefaultAuthorization(final AuthorizationRepository authorizationRepository, final Experiments experiments,
                                final EventLog eventLog); @Override List<Permission> getPermissionsFromRole(Role role); @Override UserPermissionsList getUserPermissionsList(UserInfo.Username userID); @Override UserRoleList getApplicationUsers(Application.Name applicationName); @Override UserPermissions getUserPermissions(UserInfo.Username userID, Application.Name applicationName); @Override void deleteUserRole(UserInfo.Username userID, Application.Name applicationName, UserInfo admin); @Override void checkUserPermissions(UserInfo.Username userID, Application.Name applicationName, Permission permission); @Override UserInfo.Username getUser(String authHeader); @Override Map setUserRole(UserRole userRole, UserInfo admin); @Override UserRoleList getUserRoleList(UserInfo.Username userID); @Override void checkSuperAdmin(UserInfo.Username userID); @Override UserInfo getUserInfo(UserInfo.Username userID); @Override void assignUserToSuperAdminRole(final UserInfo candidateUserInfo, final UserInfo assigningUserInfo); @Override void removeUserFromSuperAdminRole(final UserInfo candidateUserInfo, final UserInfo assigningUserInfo); @Override List<UserRole> getSuperAdminRoleList();  }