@Test public void shouldCreateDefaultOrganization() { IdentityProvider idp = new IdentityProvider(); idp.setId("test"); User adminUser = new User(); adminUser.setId("admin-id"); when(organizationService.createDefault()).thenReturn(Maybe.just(new Organization())); when(identityProviderService.create(eq(ReferenceType.ORGANIZATION), eq(Organization.DEFAULT), any(NewIdentityProvider.class), isNull())).thenReturn(Single.just(idp)); when(organizationService.update(eq(Organization.DEFAULT), any(PatchOrganization.class), isNull())).thenReturn(Single.just(new Organization())); when(userService.create(argThat(user -> !user.isInternal() && user.getUsername().equals("admin") && user.getSource().equals(idp.getId()) && user.getReferenceType() == ReferenceType.ORGANIZATION && user.getReferenceId().equals(Organization.DEFAULT)))).thenReturn(Single.just(adminUser)); when(domainService.findById("admin")).thenReturn(Maybe.empty()); doNothing().when(membershipHelper).setOrganizationPrimaryOwnerRole(argThat(user -> user.getId().equals(adminUser.getId()))); assertTrue(cut.upgrade()); }
@Override public boolean upgrade() { try { Organization organization = organizationService.createDefault().blockingGet(); if (organization != null) { logger.info("Default organization successfully created"); Domain adminDomain = domainService.findById(ADMIN_DOMAIN).blockingGet(); if (adminDomain != null) { PatchOrganization patchOrganization = new PatchOrganization(); patchOrganization.setIdentities(adminDomain.getIdentities() != null ? new ArrayList<>(adminDomain.getIdentities()) : null); organizationService.update(organization.getId(), patchOrganization,null).blockingGet(); Role organizationOwnerRole = roleService.findDefaultRole(Organization.DEFAULT, DefaultRole.ORGANIZATION_OWNER, ReferenceType.ORGANIZATION).blockingGet(); Page<User> userPage; int page = 0; do { userPage = userService.findAll(ReferenceType.ORGANIZATION, Organization.DEFAULT, page, PAGE_SIZE).blockingGet(); userPage.getData().forEach(user -> membershipHelper.setRole(user, organizationOwnerRole)); page++; } while (userPage.getData().size() == PAGE_SIZE); domainService.delete(ADMIN_DOMAIN).blockingGet(); } else { IdentityProvider inlineProvider = createInlineProvider(); User adminUser = createAdminUser(inlineProvider); membershipHelper.setOrganizationPrimaryOwnerRole(adminUser); } } else { logger.info("One or more organizations already exist. Check if default organization is up to date"); organization = organizationService.findById(Organization.DEFAULT).blockingGet(); if (organization != null) { final List<String> identities = organization.getIdentities(); IdentityProvider inlineIdp = identityProviderService.findAll(ReferenceType.ORGANIZATION, Organization.DEFAULT) .flattenAsFlowable(identityProviders -> identityProviders) .filter(identityProvider -> identityProvider.getType().equals("inline-am-idp") && !identityProvider.isExternal() && identities.contains(identityProvider.getId())) .firstElement().blockingGet(); if (inlineIdp != null) { if (inlineIdp.getConfiguration().contains(",\"username\":\"" + ADMIN_USERNAME + "\",") && inlineIdp.getRoleMapper().isEmpty()) { User adminUser = userService.findByUsernameAndSource(ReferenceType.ORGANIZATION, Organization.DEFAULT, ADMIN_USERNAME, inlineIdp.getId()).blockingGet(); if (adminUser == null) { adminUser = createAdminUser(inlineIdp); membershipHelper.setOrganizationPrimaryOwnerRole(adminUser); } } } } } } catch (Exception e) { logger.error("An error occurred trying to initialize default organization", e); return false; } return true; }
DefaultOrganizationUpgrader implements Upgrader, Ordered { @Override public boolean upgrade() { try { Organization organization = organizationService.createDefault().blockingGet(); if (organization != null) { logger.info("Default organization successfully created"); Domain adminDomain = domainService.findById(ADMIN_DOMAIN).blockingGet(); if (adminDomain != null) { PatchOrganization patchOrganization = new PatchOrganization(); patchOrganization.setIdentities(adminDomain.getIdentities() != null ? new ArrayList<>(adminDomain.getIdentities()) : null); organizationService.update(organization.getId(), patchOrganization,null).blockingGet(); Role organizationOwnerRole = roleService.findDefaultRole(Organization.DEFAULT, DefaultRole.ORGANIZATION_OWNER, ReferenceType.ORGANIZATION).blockingGet(); Page<User> userPage; int page = 0; do { userPage = userService.findAll(ReferenceType.ORGANIZATION, Organization.DEFAULT, page, PAGE_SIZE).blockingGet(); userPage.getData().forEach(user -> membershipHelper.setRole(user, organizationOwnerRole)); page++; } while (userPage.getData().size() == PAGE_SIZE); domainService.delete(ADMIN_DOMAIN).blockingGet(); } else { IdentityProvider inlineProvider = createInlineProvider(); User adminUser = createAdminUser(inlineProvider); membershipHelper.setOrganizationPrimaryOwnerRole(adminUser); } } else { logger.info("One or more organizations already exist. Check if default organization is up to date"); organization = organizationService.findById(Organization.DEFAULT).blockingGet(); if (organization != null) { final List<String> identities = organization.getIdentities(); IdentityProvider inlineIdp = identityProviderService.findAll(ReferenceType.ORGANIZATION, Organization.DEFAULT) .flattenAsFlowable(identityProviders -> identityProviders) .filter(identityProvider -> identityProvider.getType().equals("inline-am-idp") && !identityProvider.isExternal() && identities.contains(identityProvider.getId())) .firstElement().blockingGet(); if (inlineIdp != null) { if (inlineIdp.getConfiguration().contains(",\"username\":\"" + ADMIN_USERNAME + "\",") && inlineIdp.getRoleMapper().isEmpty()) { User adminUser = userService.findByUsernameAndSource(ReferenceType.ORGANIZATION, Organization.DEFAULT, ADMIN_USERNAME, inlineIdp.getId()).blockingGet(); if (adminUser == null) { adminUser = createAdminUser(inlineIdp); membershipHelper.setOrganizationPrimaryOwnerRole(adminUser); } } } } } } catch (Exception e) { logger.error("An error occurred trying to initialize default organization", e); return false; } return true; } }
DefaultOrganizationUpgrader implements Upgrader, Ordered { @Override public boolean upgrade() { try { Organization organization = organizationService.createDefault().blockingGet(); if (organization != null) { logger.info("Default organization successfully created"); Domain adminDomain = domainService.findById(ADMIN_DOMAIN).blockingGet(); if (adminDomain != null) { PatchOrganization patchOrganization = new PatchOrganization(); patchOrganization.setIdentities(adminDomain.getIdentities() != null ? new ArrayList<>(adminDomain.getIdentities()) : null); organizationService.update(organization.getId(), patchOrganization,null).blockingGet(); Role organizationOwnerRole = roleService.findDefaultRole(Organization.DEFAULT, DefaultRole.ORGANIZATION_OWNER, ReferenceType.ORGANIZATION).blockingGet(); Page<User> userPage; int page = 0; do { userPage = userService.findAll(ReferenceType.ORGANIZATION, Organization.DEFAULT, page, PAGE_SIZE).blockingGet(); userPage.getData().forEach(user -> membershipHelper.setRole(user, organizationOwnerRole)); page++; } while (userPage.getData().size() == PAGE_SIZE); domainService.delete(ADMIN_DOMAIN).blockingGet(); } else { IdentityProvider inlineProvider = createInlineProvider(); User adminUser = createAdminUser(inlineProvider); membershipHelper.setOrganizationPrimaryOwnerRole(adminUser); } } else { logger.info("One or more organizations already exist. Check if default organization is up to date"); organization = organizationService.findById(Organization.DEFAULT).blockingGet(); if (organization != null) { final List<String> identities = organization.getIdentities(); IdentityProvider inlineIdp = identityProviderService.findAll(ReferenceType.ORGANIZATION, Organization.DEFAULT) .flattenAsFlowable(identityProviders -> identityProviders) .filter(identityProvider -> identityProvider.getType().equals("inline-am-idp") && !identityProvider.isExternal() && identities.contains(identityProvider.getId())) .firstElement().blockingGet(); if (inlineIdp != null) { if (inlineIdp.getConfiguration().contains(",\"username\":\"" + ADMIN_USERNAME + "\",") && inlineIdp.getRoleMapper().isEmpty()) { User adminUser = userService.findByUsernameAndSource(ReferenceType.ORGANIZATION, Organization.DEFAULT, ADMIN_USERNAME, inlineIdp.getId()).blockingGet(); if (adminUser == null) { adminUser = createAdminUser(inlineIdp); membershipHelper.setOrganizationPrimaryOwnerRole(adminUser); } } } } } } catch (Exception e) { logger.error("An error occurred trying to initialize default organization", e); return false; } return true; } DefaultOrganizationUpgrader(OrganizationService organizationService,
                                       IdentityProviderService identityProviderService,
                                       UserService userService,
                                       MembershipHelper membershipHelper,
                                       RoleService roleService,
                                       DomainService domainService); }
DefaultOrganizationUpgrader implements Upgrader, Ordered { @Override public boolean upgrade() { try { Organization organization = organizationService.createDefault().blockingGet(); if (organization != null) { logger.info("Default organization successfully created"); Domain adminDomain = domainService.findById(ADMIN_DOMAIN).blockingGet(); if (adminDomain != null) { PatchOrganization patchOrganization = new PatchOrganization(); patchOrganization.setIdentities(adminDomain.getIdentities() != null ? new ArrayList<>(adminDomain.getIdentities()) : null); organizationService.update(organization.getId(), patchOrganization,null).blockingGet(); Role organizationOwnerRole = roleService.findDefaultRole(Organization.DEFAULT, DefaultRole.ORGANIZATION_OWNER, ReferenceType.ORGANIZATION).blockingGet(); Page<User> userPage; int page = 0; do { userPage = userService.findAll(ReferenceType.ORGANIZATION, Organization.DEFAULT, page, PAGE_SIZE).blockingGet(); userPage.getData().forEach(user -> membershipHelper.setRole(user, organizationOwnerRole)); page++; } while (userPage.getData().size() == PAGE_SIZE); domainService.delete(ADMIN_DOMAIN).blockingGet(); } else { IdentityProvider inlineProvider = createInlineProvider(); User adminUser = createAdminUser(inlineProvider); membershipHelper.setOrganizationPrimaryOwnerRole(adminUser); } } else { logger.info("One or more organizations already exist. Check if default organization is up to date"); organization = organizationService.findById(Organization.DEFAULT).blockingGet(); if (organization != null) { final List<String> identities = organization.getIdentities(); IdentityProvider inlineIdp = identityProviderService.findAll(ReferenceType.ORGANIZATION, Organization.DEFAULT) .flattenAsFlowable(identityProviders -> identityProviders) .filter(identityProvider -> identityProvider.getType().equals("inline-am-idp") && !identityProvider.isExternal() && identities.contains(identityProvider.getId())) .firstElement().blockingGet(); if (inlineIdp != null) { if (inlineIdp.getConfiguration().contains(",\"username\":\"" + ADMIN_USERNAME + "\",") && inlineIdp.getRoleMapper().isEmpty()) { User adminUser = userService.findByUsernameAndSource(ReferenceType.ORGANIZATION, Organization.DEFAULT, ADMIN_USERNAME, inlineIdp.getId()).blockingGet(); if (adminUser == null) { adminUser = createAdminUser(inlineIdp); membershipHelper.setOrganizationPrimaryOwnerRole(adminUser); } } } } } } catch (Exception e) { logger.error("An error occurred trying to initialize default organization", e); return false; } return true; } DefaultOrganizationUpgrader(OrganizationService organizationService,
                                       IdentityProviderService identityProviderService,
                                       UserService userService,
                                       MembershipHelper membershipHelper,
                                       RoleService roleService,
                                       DomainService domainService); @Override boolean upgrade(); @Override int getOrder(); }
DefaultOrganizationUpgrader implements Upgrader, Ordered { @Override public boolean upgrade() { try { Organization organization = organizationService.createDefault().blockingGet(); if (organization != null) { logger.info("Default organization successfully created"); Domain adminDomain = domainService.findById(ADMIN_DOMAIN).blockingGet(); if (adminDomain != null) { PatchOrganization patchOrganization = new PatchOrganization(); patchOrganization.setIdentities(adminDomain.getIdentities() != null ? new ArrayList<>(adminDomain.getIdentities()) : null); organizationService.update(organization.getId(), patchOrganization,null).blockingGet(); Role organizationOwnerRole = roleService.findDefaultRole(Organization.DEFAULT, DefaultRole.ORGANIZATION_OWNER, ReferenceType.ORGANIZATION).blockingGet(); Page<User> userPage; int page = 0; do { userPage = userService.findAll(ReferenceType.ORGANIZATION, Organization.DEFAULT, page, PAGE_SIZE).blockingGet(); userPage.getData().forEach(user -> membershipHelper.setRole(user, organizationOwnerRole)); page++; } while (userPage.getData().size() == PAGE_SIZE); domainService.delete(ADMIN_DOMAIN).blockingGet(); } else { IdentityProvider inlineProvider = createInlineProvider(); User adminUser = createAdminUser(inlineProvider); membershipHelper.setOrganizationPrimaryOwnerRole(adminUser); } } else { logger.info("One or more organizations already exist. Check if default organization is up to date"); organization = organizationService.findById(Organization.DEFAULT).blockingGet(); if (organization != null) { final List<String> identities = organization.getIdentities(); IdentityProvider inlineIdp = identityProviderService.findAll(ReferenceType.ORGANIZATION, Organization.DEFAULT) .flattenAsFlowable(identityProviders -> identityProviders) .filter(identityProvider -> identityProvider.getType().equals("inline-am-idp") && !identityProvider.isExternal() && identities.contains(identityProvider.getId())) .firstElement().blockingGet(); if (inlineIdp != null) { if (inlineIdp.getConfiguration().contains(",\"username\":\"" + ADMIN_USERNAME + "\",") && inlineIdp.getRoleMapper().isEmpty()) { User adminUser = userService.findByUsernameAndSource(ReferenceType.ORGANIZATION, Organization.DEFAULT, ADMIN_USERNAME, inlineIdp.getId()).blockingGet(); if (adminUser == null) { adminUser = createAdminUser(inlineIdp); membershipHelper.setOrganizationPrimaryOwnerRole(adminUser); } } } } } } catch (Exception e) { logger.error("An error occurred trying to initialize default organization", e); return false; } return true; } DefaultOrganizationUpgrader(OrganizationService organizationService,
                                       IdentityProviderService identityProviderService,
                                       UserService userService,
                                       MembershipHelper membershipHelper,
                                       RoleService roleService,
                                       DomainService domainService); @Override boolean upgrade(); @Override int getOrder(); static String ADMIN_USERNAME; static String DEFAULT_INLINE_IDP_CONFIG; }