private boolean isMember(DbSession dbSession, OrganizationDto organization, int userId) { return dbClient.organizationMemberDao().select(dbSession, organization.getUuid(), userId).isPresent(); }
@Test public void delete_by_organization() { underTest.insert(dbSession, create("O1", 512)); underTest.insert(dbSession, create("O1", 513)); underTest.insert(dbSession, create("O2", 512)); underTest.deleteByOrganizationUuid(dbSession, "O1"); assertThat(underTest.select(dbSession, "O1", 512)).isNotPresent(); assertThat(underTest.select(dbSession, "O1", 513)).isNotPresent(); assertThat(underTest.select(dbSession, "O2", 512)).isPresent(); }
@Test public void delete_by_user_id() { underTest.insert(dbSession, create("O1", 512)); underTest.insert(dbSession, create("O1", 513)); underTest.insert(dbSession, create("O2", 512)); underTest.deleteByUserId(dbSession, 512); db.commit(); assertThat(db.select("select organization_uuid as \"organizationUuid\", user_id as \"userId\" from organization_members")) .extracting((row) -> row.get("organizationUuid"), (row) -> row.get("userId")) .containsOnly(tuple("O1", 513L)); }
@Test public void select() { underTest.insert(dbSession, create("O1", 512)); Optional<OrganizationMemberDto> result = underTest.select(dbSession, "O1", 512); assertThat(result).isPresent(); assertThat(result.get()).extracting(OrganizationMemberDto::getOrganizationUuid, OrganizationMemberDto::getUserId).containsExactly("O1", 512); assertThat(underTest.select(dbSession, "O1", 256)).isNotPresent(); assertThat(underTest.select(dbSession, "O2", 512)).isNotPresent(); }
private void deleteOrganization(DbSession dbSession, OrganizationDto organization) { Collection<String> uuids = dbClient.organizationMemberDao().selectUserUuidsByOrganizationUuid(dbSession, organization.getUuid()); dbClient.organizationMemberDao().deleteByOrganizationUuid(dbSession, organization.getUuid()); dbClient.organizationDao().deleteByUuid(dbSession, organization.getUuid()); dbClient.userDao().cleanHomepage(dbSession, organization); dbClient.webhookDao().selectByOrganizationUuid(dbSession, organization.getUuid()) .forEach(wh -> dbClient.webhookDeliveryDao().deleteByWebhook(dbSession, wh)); dbClient.webhookDao().deleteByOrganization(dbSession, organization); List<UserDto> users = dbClient.userDao().selectByUuids(dbSession, uuids); userIndexer.commitAndIndex(dbSession, users); }
@Test public void select_organization_uuids_by_user_id() { OrganizationDto organizationDto1 = db.organizations().insert(); OrganizationDto organizationDto2 = db.organizations().insert(); OrganizationDto organizationDto3 = db.organizations().insert(); underTest.insert(dbSession, create(organizationDto1.getUuid(), 512)); underTest.insert(dbSession, create(organizationDto2.getUuid(), 512)); assertThat(underTest.selectOrganizationUuidsByUser(dbSession, 512)).containsOnly(organizationDto1.getUuid(), organizationDto2.getUuid()) .doesNotContain(organizationDto3.getUuid()); assertThat(underTest.selectOrganizationUuidsByUser(dbSession, 123)).isEmpty(); }
private Set<String> loadUserOrganizations(@Nullable UserDto assignee) { if (assignee == null) { return Collections.emptySet(); } try (DbSession dbSession = dbClient.openSession(false)) { return dbClient.organizationMemberDao().selectOrganizationUuidsByUser(dbSession, assignee.getId()); } }
private void associateMembersOfDefaultOrganizationToGroup(DbSession dbSession, String defaultOrganizationUuid, GroupDto membersGroup) { List<Integer> organizationMembers = dbClient.organizationMemberDao().selectUserIdsByOrganizationUuid(dbSession, defaultOrganizationUuid); organizationMembers.forEach(member -> dbClient.userGroupDao().insert(dbSession, new UserGroupDto().setGroupId(membersGroup.getId()).setUserId(member))); }
private void removeMember(DbSession dbSession, OrganizationDto organization, UserDto user) { ensureLastAdminIsNotRemoved(dbSession, organization, user); int userId = user.getId(); String organizationUuid = organization.getUuid(); dbClient.userPermissionDao().deleteOrganizationMemberPermissions(dbSession, organizationUuid, userId); dbClient.permissionTemplateDao().deleteUserPermissionsByOrganization(dbSession, organizationUuid, userId); dbClient.qProfileEditUsersDao().deleteByOrganizationAndUser(dbSession, organization, user); dbClient.userGroupDao().deleteByOrganizationAndUser(dbSession, organizationUuid, userId); dbClient.propertiesDao().deleteByOrganizationAndUser(dbSession, organizationUuid, userId); dbClient.propertiesDao().deleteByOrganizationAndMatchingLogin(dbSession, organizationUuid, user.getLogin(), singletonList(DEFAULT_ISSUE_ASSIGNEE)); dbClient.organizationMemberDao().delete(dbSession, organizationUuid, userId); userIndexer.commitAndIndex(dbSession, user); }
@Override public void indexOnStartup(Set<IndexType> uninitializedIndexTypes) { try (DbSession dbSession = dbClient.openSession(false)) { ListMultimap<String, String> organizationUuidsByUserUuid = ArrayListMultimap.create(); dbClient.organizationMemberDao().selectAllForUserIndexing(dbSession, organizationUuidsByUserUuid::put); BulkIndexer bulkIndexer = newBulkIndexer(Size.LARGE, IndexingListener.FAIL_ON_ERROR); bulkIndexer.start(); dbClient.userDao().scrollAll(dbSession, // only index requests, no deletion requests. // Deactivated users are not deleted but updated. u -> bulkIndexer.add(newIndexRequest(u, organizationUuidsByUserUuid))); bulkIndexer.stop(); } }
/** * @return the number of items that have been successfully indexed */ @Override public IndexingResult index(DbSession dbSession, Collection<EsQueueDto> items) { if (items.isEmpty()) { return new IndexingResult(); } Set<String> uuids = items .stream() .map(EsQueueDto::getDocId) .collect(toHashSet(items.size())); ListMultimap<String, String> organizationUuidsByUserUuid = ArrayListMultimap.create(); dbClient.organizationMemberDao().selectForUserIndexing(dbSession, uuids, organizationUuidsByUserUuid::put); BulkIndexer bulkIndexer = newBulkIndexer(Size.REGULAR, new OneToOneResilientIndexingListener(dbClient, dbSession, items)); bulkIndexer.start(); dbClient.userDao().scrollByUuids(dbSession, uuids, // only index requests, no deletion requests. // Deactivated users are not deleted but updated. u -> { uuids.remove(u.getUuid()); bulkIndexer.add(newIndexRequest(u, organizationUuidsByUserUuid)); }); // the remaining uuids reference rows that don't exist in db. They must // be deleted from index. uuids.forEach(uuid -> bulkIndexer.addDeletion(INDEX_TYPE_USER, uuid)); return bulkIndexer.stop(); }
@Override public void handle(Request request, Response response) throws Exception { userSession.checkLoggedIn().checkIsSystemAdministrator(); String login = request.mandatoryParam(PARAM_LOGIN); checkRequest(!login.equals(userSession.getLogin()), "Self-deactivation is not possible"); try (DbSession dbSession = dbClient.openSession(false)) { UserDto user = dbClient.userDao().selectByLogin(dbSession, login); checkFound(user, "User '%s' doesn't exist", login); ensureNotLastAdministrator(dbSession, user); Integer userId = user.getId(); dbClient.userTokenDao().deleteByUser(dbSession, user); dbClient.propertiesDao().deleteByKeyAndValue(dbSession, DEFAULT_ISSUE_ASSIGNEE, user.getLogin()); dbClient.propertiesDao().deleteByQuery(dbSession, PropertyQuery.builder().setUserId(userId).build()); dbClient.userGroupDao().deleteByUserId(dbSession, userId); dbClient.userPermissionDao().deleteByUserId(dbSession, userId); dbClient.permissionTemplateDao().deleteUserPermissionsByUserId(dbSession, userId); dbClient.qProfileEditUsersDao().deleteByUser(dbSession, user); dbClient.organizationMemberDao().deleteByUserId(dbSession, userId); dbClient.userPropertiesDao().deleteByUser(dbSession, user); dbClient.userDao().deactivateUser(dbSession, user); userIndexer.commitAndIndex(dbSession, user); } writeResponse(response, login); }
@Test public void select_user_uuids_by_organization_uuid() { OrganizationDto organization = db.organizations().insert(); OrganizationDto anotherOrganization = db.organizations().insert(); UserDto user = db.users().insertUser(); UserDto anotherUser = db.users().insertUser(); UserDto userInAnotherOrganization = db.users().insertUser(); db.organizations().addMember(organization, user); db.organizations().addMember(organization, anotherUser); db.organizations().addMember(anotherOrganization, userInAnotherOrganization); List<String> result = underTest.selectUserUuidsByOrganizationUuid(dbSession, organization.getUuid()); assertThat(result).containsOnly(user.getUuid(), anotherUser.getUuid()); }
private void insertOrganizationMember(DbSession dbSession, OrganizationDto organizationDto, int userId) { dbClient.organizationMemberDao().insert(dbSession, new OrganizationMemberDto() .setOrganizationUuid(organizationDto.getUuid()) .setUserId(userId)); } }
private void deleteOrganization(DbSession dbSession, OrganizationDto organization) { Collection<String> uuids = dbClient.organizationMemberDao().selectUserUuidsByOrganizationUuid(dbSession, organization.getUuid()); dbClient.organizationMemberDao().deleteByOrganizationUuid(dbSession, organization.getUuid()); dbClient.organizationDao().deleteByUuid(dbSession, organization.getUuid()); dbClient.userDao().cleanHomepage(dbSession, organization); dbClient.webhookDao().selectByOrganizationUuid(dbSession, organization.getUuid()) .forEach(wh -> dbClient.webhookDeliveryDao().deleteByWebhook(dbSession, wh)); dbClient.webhookDao().deleteByOrganization(dbSession, organization); List<UserDto> users = dbClient.userDao().selectByUuids(dbSession, uuids); userIndexer.commitAndIndex(dbSession, users); }
private void loadOrganizations(DbSession dbSession, SearchResponseData result) { Collection<ComponentDto> components = result.getComponents(); dbClient.organizationDao().selectByUuids( dbSession, components.stream().map(ComponentDto::getOrganizationUuid).collect(MoreCollectors.toSet())) .forEach(result::addOrganization); if (userSession.isLoggedIn()) { result.setUserOrganizationUuids(dbClient.organizationMemberDao().selectOrganizationUuidsByUser(dbSession, userSession.getUserId())); } }
@Test public void select_user_ids() { OrganizationDto organization = db.organizations().insert(); OrganizationDto anotherOrganization = db.organizations().insert(); UserDto user = db.users().insertUser(); UserDto anotherUser = db.users().insertUser(); UserDto userInAnotherOrganization = db.users().insertUser(); db.organizations().addMember(organization, user); db.organizations().addMember(organization, anotherUser); db.organizations().addMember(anotherOrganization, userInAnotherOrganization); List<Integer> result = underTest.selectUserIdsByOrganizationUuid(dbSession, organization.getUuid()); assertThat(result).containsOnly(user.getId(), anotherUser.getId()); }
@Test public void hasMembership_keeps_membership_in_cache() { OrganizationDto organization = db.organizations().insert(); UserDto user = db.users().insertUser(); db.organizations().addMember(organization, user); ServerUserSession session = newUserSession(user); assertThat(session.hasMembership(organization)).isTrue(); // membership updated but not cache db.getDbClient().organizationMemberDao().delete(db.getSession(), organization.getUuid(), user.getId()); db.commit(); assertThat(session.hasMembership(organization)).isTrue(); }
@Test public void select_all_for_indexing() { OrganizationDto org1 = db.organizations().insert(o -> o.setUuid("ORG_1")); OrganizationDto org2 = db.organizations().insert(o -> o.setUuid("ORG_2")); UserDto user1 = db.users().insertUser(); UserDto user2 = db.users().insertUser(); db.organizations().addMember(org1, user1); db.organizations().addMember(org1, user2); db.organizations().addMember(org2, user1); List<Tuple> result = new ArrayList<>(); underTest.selectAllForUserIndexing(dbSession, (login, org) -> result.add(tuple(login, org))); assertThat(result).containsOnly(tuple(user1.getUuid(), "ORG_1"), tuple(user1.getUuid(), "ORG_2"), tuple(user2.getUuid(), "ORG_1")); }