@Test public void select_by_query() { // global insertProperty("global.one", "one", null, null); insertProperty("global.two", "two", null, null); // struts insertProperty("struts.one", "one", 10L, null); // commons insertProperty("commonslang.one", "one", 11L, null); // user insertProperty("user.one", "one", null, 100); insertProperty("user.two", "two", 10L, 100); // other insertProperty("other.one", "one", 12L, null); List<PropertyDto> results = underTest.selectByQuery(PropertyQuery.builder().setKey("user.two").setComponentId(10L).setUserId(100).build(), db.getSession()); assertThat(results).hasSize(1); assertThat(results.get(0).getValue()).isEqualTo("two"); results = underTest.selectByQuery(PropertyQuery.builder().setKey("user.one").setUserId(100).build(), db.getSession()); assertThat(results).hasSize(1); assertThat(results.get(0).getValue()).isEqualTo("one"); }
private void appendComponent(JsonWriter json, ComponentDto component, UserSession userSession, DbSession session) { List<PropertyDto> propertyDtos = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder() .setKey("favourite") .setComponentId(component.getId()) .setUserId(userSession.getUserId()) .build(), session); boolean isFavourite = propertyDtos.size() == 1;
@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.userDao().deactivateUser(dbSession, user); userIndexer.commitAndIndex(dbSession, user); } writeResponse(response, login); }
/** * Add a notification to a user. */ public void add(DbSession dbSession, String channel, String dispatcher, UserDto user, @Nullable ComponentDto project) { String key = String.join(".", PROP_NOTIFICATION_PREFIX, dispatcher, channel); Long projectId = project == null ? null : project.getId(); List<PropertyDto> existingNotification = dbClient.propertiesDao().selectByQuery( PropertyQuery.builder() .setKey(key) .setComponentId(projectId) .setUserId(user.getId()) .build(), dbSession).stream() .filter(notificationScope(project)) .collect(MoreCollectors.toList()); checkArgument(existingNotification.isEmpty() || !PROP_NOTIFICATION_VALUE.equals(existingNotification.get(0).getValue()), "Notification already added"); dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto() .setKey(key) .setUserId(user.getId()) .setValue(PROP_NOTIFICATION_VALUE) .setResourceId(projectId)); }
/** * Remove a notification from a user. */ public void remove(DbSession dbSession, String channel, String dispatcher, UserDto user, @Nullable ComponentDto project) { String key = String.join(".", PROP_NOTIFICATION_PREFIX, dispatcher, channel); Long projectId = project == null ? null : project.getId(); List<PropertyDto> existingNotification = dbClient.propertiesDao().selectByQuery( PropertyQuery.builder() .setKey(key) .setComponentId(projectId) .setUserId(user.getId()) .build(), dbSession).stream() .filter(notificationScope(project)) .collect(MoreCollectors.toList()); checkArgument(!existingNotification.isEmpty() && PROP_NOTIFICATION_VALUE.equals(existingNotification.get(0).getValue()), "Notification doesn't exist"); dbClient.propertiesDao().delete(dbSession, new PropertyDto() .setKey(key) .setUserId(user.getId()) .setValue(PROP_NOTIFICATION_VALUE) .setResourceId(projectId)); }
private UnaryOperator<ListResponse.Builder> addNotifications(DbSession dbSession, UserDto user) { return response -> { List<PropertyDto> properties = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder().setUserId(user.getId()).build(), dbSession); Map<Long, ComponentDto> componentsById = searchProjects(dbSession, properties); Map<String, OrganizationDto> organizationsByUuid = getOrganizations(dbSession, componentsById.values()); Predicate<PropertyDto> isNotification = prop -> prop.getKey().startsWith("notification."); Predicate<PropertyDto> isComponentInDb = prop -> prop.getResourceId() == null || componentsById.containsKey(prop.getResourceId()); Notification.Builder notification = Notification.newBuilder(); properties.stream() .filter(isNotification) .filter(channelAndDispatcherAuthorized()) .filter(isComponentInDb) .map(toWsNotification(notification, organizationsByUuid, componentsById)) .sorted(comparing(Notification::getProject, nullsFirst(naturalOrder())) .thenComparing(comparing(Notification::getChannel)) .thenComparing(comparing(Notification::getType))) .forEach(response::addNotifications); return response; }; }
/** * @return the list of favorite components of the authenticated user. Empty list if the user is not authenticated */ public List<ComponentDto> list() { if (!userSession.isLoggedIn()) { return emptyList(); } try (DbSession dbSession = dbClient.openSession(false)) { PropertyQuery dbQuery = PropertyQuery.builder() .setKey(PROP_FAVORITE_KEY) .setUserId(userSession.getUserId()) .build(); Set<Long> componentIds = dbClient.propertiesDao().selectByQuery(dbQuery, dbSession).stream().map(PropertyDto::getResourceId).collect(Collectors.toSet()); return dbClient.componentDao().selectByIds(dbSession, componentIds).stream() .sorted(Comparator.comparing(ComponentDto::name)) .collect(toList()); } } }
private Set<String> loadFavoriteProjectUuids(DbSession dbSession) { if (!userSession.isLoggedIn()) { return Collections.emptySet(); } List<PropertyDto> props = dbClient.propertiesDao().selectByQuery( PropertyQuery.builder() .setUserId(userSession.getUserId()) .setKey("favourite") .build(), dbSession); List<Long> favoriteDbIds = props.stream() .map(PropertyDto::getResourceId) .collect(MoreCollectors.toList(props.size())); return dbClient.componentDao().selectByIds(dbSession, favoriteDbIds).stream() .filter(ComponentDto::isEnabled) .filter(f -> f.qualifier().equals(Qualifiers.PROJECT)) .map(ComponentDto::uuid) .collect(MoreCollectors.toSet()); }
/** * Set favorite to the logged in user. If no user, no action is done */ public void add(DbSession dbSession, ComponentDto componentDto, @Nullable Integer userId) { if (userId == null) { return; } List<PropertyDto> existingFavoriteOnComponent = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder() .setKey(PROP_FAVORITE_KEY) .setUserId(userId) .setComponentId(componentDto.getId()) .build(), dbSession); checkRequest(existingFavoriteOnComponent.isEmpty(), "Component '%s' is already a favorite", componentDto.getDbKey()); dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto() .setKey(PROP_FAVORITE_KEY) .setResourceId(componentDto.getId()) .setUserId(userId)); }
public static Builder builder() { return new Builder(); }
private boolean isFavourite(DbSession session, ComponentDto component) { PropertyQuery propertyQuery = PropertyQuery.builder() .setUserId(userSession.getUserId()) .setKey("favourite") .setComponentId(component.getId()) .build(); List<PropertyDto> componentFavourites = dbClient.propertiesDao().selectByQuery(propertyQuery, session); return componentFavourites.size() == 1; }
private void appendComponent(JsonWriter json, ComponentDto component, UserSession userSession, DbSession session) { List<PropertyDto> propertyDtos = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder() .setKey("favourite") .setComponentId(component.getId()) .setUserId(userSession.getUserId()) .build(), session); boolean isFavourite = propertyDtos.size() == 1;
@Test public void update_default_assignee_when_updating_login() { createDefaultGroup(); UserDto oldUser = db.users().insertUser(); ComponentDto project1 = db.components().insertPrivateProject(); ComponentDto project2 = db.components().insertPrivateProject(); ComponentDto anotherProject = db.components().insertPrivateProject(); db.properties().insertProperties( new PropertyDto().setKey(DEFAULT_ISSUE_ASSIGNEE).setValue(oldUser.getLogin()), new PropertyDto().setKey(DEFAULT_ISSUE_ASSIGNEE).setValue(oldUser.getLogin()).setResourceId(project1.getId()), new PropertyDto().setKey(DEFAULT_ISSUE_ASSIGNEE).setValue(oldUser.getLogin()).setResourceId(project2.getId()), new PropertyDto().setKey(DEFAULT_ISSUE_ASSIGNEE).setValue("another login").setResourceId(anotherProject.getId())); userIndexer.indexOnStartup(null); underTest.updateAndCommit(session, oldUser, new UpdateUser() .setLogin("new_login"), u -> { }); assertThat(db.getDbClient().propertiesDao().selectByQuery(PropertyQuery.builder().setKey(DEFAULT_ISSUE_ASSIGNEE).build(), db.getSession())) .extracting(PropertyDto::getValue, PropertyDto::getResourceId) .containsOnly( tuple("new_login", null), tuple("new_login", project1.getId()), tuple("new_login", project2.getId()), tuple("another login", anotherProject.getId())); }
@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); }
private UnaryOperator<ListResponse.Builder> addNotifications(DbSession dbSession, UserDto user) { return response -> { List<PropertyDto> properties = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder().setUserId(user.getId()).build(), dbSession); Map<Long, ComponentDto> componentsById = searchProjects(dbSession, properties); Map<String, OrganizationDto> organizationsByUuid = getOrganizations(dbSession, componentsById.values()); Predicate<PropertyDto> isNotification = prop -> prop.getKey().startsWith("notification."); Predicate<PropertyDto> isComponentInDb = prop -> prop.getResourceId() == null || componentsById.containsKey(prop.getResourceId()); Notification.Builder notification = Notification.newBuilder(); properties.stream() .filter(isNotification) .filter(channelAndDispatcherAuthorized()) .filter(isComponentInDb) .map(toWsNotification(notification, organizationsByUuid, componentsById)) .sorted(comparing(Notification::getProject, nullsFirst(naturalOrder())) .thenComparing(comparing(Notification::getChannel)) .thenComparing(comparing(Notification::getType))) .forEach(response::addNotifications); return response; }; }
/** * Remove a notification from a user. */ public void remove(DbSession dbSession, String channel, String dispatcher, UserDto user, @Nullable ComponentDto project) { String key = String.join(".", PROP_NOTIFICATION_PREFIX, dispatcher, channel); Long projectId = project == null ? null : project.getId(); List<PropertyDto> existingNotification = dbClient.propertiesDao().selectByQuery( PropertyQuery.builder() .setKey(key) .setComponentId(projectId) .setUserId(user.getId()) .build(), dbSession).stream() .filter(notificationScope(project)) .collect(MoreCollectors.toList()); checkArgument(!existingNotification.isEmpty() && PROP_NOTIFICATION_VALUE.equals(existingNotification.get(0).getValue()), "Notification doesn't exist"); dbClient.propertiesDao().delete(dbSession, new PropertyDto() .setKey(key) .setUserId(user.getId()) .setValue(PROP_NOTIFICATION_VALUE) .setResourceId(projectId)); }
/** * Add a notification to a user. */ public void add(DbSession dbSession, String channel, String dispatcher, UserDto user, @Nullable ComponentDto project) { String key = String.join(".", PROP_NOTIFICATION_PREFIX, dispatcher, channel); Long projectId = project == null ? null : project.getId(); List<PropertyDto> existingNotification = dbClient.propertiesDao().selectByQuery( PropertyQuery.builder() .setKey(key) .setComponentId(projectId) .setUserId(user.getId()) .build(), dbSession).stream() .filter(notificationScope(project)) .collect(MoreCollectors.toList()); checkArgument(existingNotification.isEmpty() || !PROP_NOTIFICATION_VALUE.equals(existingNotification.get(0).getValue()), "Notification already added"); dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto() .setKey(key) .setUserId(user.getId()) .setValue(PROP_NOTIFICATION_VALUE) .setResourceId(projectId)); }
/** * @return the list of favorite components of the authenticated user. Empty list if the user is not authenticated */ public List<ComponentDto> list() { if (!userSession.isLoggedIn()) { return emptyList(); } try (DbSession dbSession = dbClient.openSession(false)) { PropertyQuery dbQuery = PropertyQuery.builder() .setKey(PROP_FAVORITE_KEY) .setUserId(userSession.getUserId()) .build(); Set<Long> componentIds = dbClient.propertiesDao().selectByQuery(dbQuery, dbSession).stream().map(PropertyDto::getResourceId).collect(Collectors.toSet()); return dbClient.componentDao().selectByIds(dbSession, componentIds).stream() .sorted(Comparator.comparing(ComponentDto::name)) .collect(toList()); } } }
@Test public void delete_by_organization_and_matching_login() { OrganizationDto organization = db.organizations().insert(); OrganizationDto anotherOrganization = db.organizations().insert(); ComponentDto project = db.components().insertPrivateProject(organization); ComponentDto anotherProject = db.components().insertPrivateProject(anotherOrganization); UserDto user = db.users().insertUser(); UserDto anotherUser = db.users().insertUser(); insertProperty("KEY_11", user.getLogin(), project.getId(), null); insertProperty("KEY_12", user.getLogin(), project.getId(), null); insertProperty("KEY_11", anotherUser.getLogin(), project.getId(), null); insertProperty("KEY_11", user.getLogin(), anotherProject.getId(), null); underTest.deleteByOrganizationAndMatchingLogin(session, organization.getUuid(), user.getLogin(), newArrayList("KEY_11", "KEY_12")); assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder().setComponentId(project.getId()).build(), session)) .hasSize(1) .extracting(PropertyDto::getValue).containsOnly(anotherUser.getLogin()); assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder().setComponentId(anotherProject.getId()).build(), session)).extracting(PropertyDto::getValue) .hasSize(1).containsOnly(user.getLogin()); }
@Test public void delete_by_organization_and_user() { OrganizationDto organization = db.organizations().insert(); OrganizationDto anotherOrganization = db.organizations().insert(); ComponentDto project = db.components().insertPrivateProject(organization); ComponentDto anotherProject = db.components().insertPrivateProject(anotherOrganization); UserDto user = db.users().insertUser(); UserDto anotherUser = db.users().insertUser(); insertProperty("KEY_11", "VALUE", project.getId(), user.getId()); insertProperty("KEY_12", "VALUE", project.getId(), user.getId()); insertProperty("KEY_11", "VALUE", project.getId(), anotherUser.getId()); insertProperty("KEY_11", "VALUE", anotherProject.getId(), user.getId()); underTest.deleteByOrganizationAndUser(session, organization.getUuid(), user.getId()); assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder().setComponentId(project.getId()).build(), session)) .hasSize(1) .extracting(PropertyDto::getUserId).containsOnly(anotherUser.getId()); assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder().setComponentId(anotherProject.getId()).build(), session)).extracting(PropertyDto::getUserId) .hasSize(1).containsOnly(user.getId()); }