Skip to content

Commit

Permalink
SONAR-9140 drop deprecated selectAuthorizedRootProjectsUuids
Browse files Browse the repository at this point in the history
  • Loading branch information
sns-seb committed Apr 27, 2017
1 parent 8e4afbb commit 98f444c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 90 deletions.
Expand Up @@ -20,8 +20,6 @@
package org.sonar.db.permission; package org.sonar.db.permission;


import java.util.Collection; import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.sonar.db.Dao; import org.sonar.db.Dao;
Expand Down Expand Up @@ -133,20 +131,6 @@ public Set<Long> keepAuthorizedProjectIds(DbSession dbSession, Collection<Long>
}); });
} }


/**
* @deprecated it loads too many results and there's no functional need.
*/
@Deprecated
public Collection<String> selectAuthorizedRootProjectsUuids(DbSession dbSession, @Nullable Integer userId, String role) {
String sql;
Map<String, Object> params = new HashMap<>(2);
sql = "selectAuthorizedRootProjectsUuids";
params.put(USER_ID_PARAM, userId);
params.put("role", role);

return dbSession.selectList(sql, params);
}

/** /**
* Keep only authorized user that have the given permission on a given project. * Keep only authorized user that have the given permission on a given project.
* Please Note that if the permission is 'Anyone' is NOT taking into account by thie method. * Please Note that if the permission is 'Anyone' is NOT taking into account by thie method.
Expand Down
Expand Up @@ -191,34 +191,6 @@
</foreach> </foreach>
</select> </select>


<select id="selectAuthorizedRootProjectsUuids" parameterType="map" resultType="string">
<choose>
<when test="userId != null">
SELECT p.uuid as root_project_uuid
FROM group_roles gr
INNER JOIN projects p on p.id = gr.resource_id AND p.module_uuid IS NULL
where
gr.role=#{role,jdbcType=VARCHAR}
and (gr.group_id is null or exists (select 1 from groups_users gu where gu.user_id = #{userId,jdbcType=INTEGER} and gu.group_id = gr.group_id))
UNION
SELECT p.uuid as root_project_uuid
FROM user_roles ur
INNER JOIN projects p on p.id = ur.resource_id AND p.module_uuid IS NULL
where
ur.role=#{role,jdbcType=VARCHAR}
and ur.user_id = #{userId,jdbcType=INTEGER}
</when>
<otherwise>
SELECT p.uuid as root_project_uuid
FROM group_roles gr
INNER JOIN projects p on p.id = gr.resource_id AND p.module_uuid IS NULL
where
gr.role=#{role,jdbcType=VARCHAR}
and gr.group_id is null
</otherwise>
</choose>
</select>

<select id="keepAuthorizedUsersForRoleAndProject" parameterType="map" resultType="int"> <select id="keepAuthorizedUsersForRoleAndProject" parameterType="map" resultType="int">
SELECT gu.user_id SELECT gu.user_id
FROM groups_users gu FROM groups_users gu
Expand Down
Expand Up @@ -298,34 +298,6 @@ public void anonymous_should_be_authorized() {
assertThat(componentIds).isEmpty(); assertThat(componentIds).isEmpty();
} }


@Test
public void should_return_root_project_uuids_for_user() {
db.prepareDbUnit(getClass(), "should_return_root_project_keys_for_user.xml");

Collection<String> rootProjectUuids = underTest.selectAuthorizedRootProjectsUuids(dbSession, USER, "user");

assertThat(rootProjectUuids).containsOnly("ABCD");
}

@Test
public void should_return_root_project_uuids_for_group() {
// but user is not in an authorized group
db.prepareDbUnit(getClass(), "should_return_root_project_keys_for_group.xml");

Collection<String> rootProjectUuids = underTest.selectAuthorizedRootProjectsUuids(dbSession, USER, "user");

assertThat(rootProjectUuids).containsOnly("ABCD");
}

@Test
public void should_return_root_project_uuids_for_anonymous() {
db.prepareDbUnit(getClass(), "should_return_root_project_keys_for_anonymous.xml");

Collection<String> rootProjectUuids = underTest.selectAuthorizedRootProjectsUuids(dbSession, null, "user");

assertThat(rootProjectUuids).containsOnly("ABCD");
}

@Test @Test
public void keep_authorized_users_for_role_and_project_for_user() { public void keep_authorized_users_for_role_and_project_for_user() {
db.prepareDbUnit(getClass(), "keep_authorized_users_for_role_and_project_for_user.xml"); db.prepareDbUnit(getClass(), "keep_authorized_users_for_role_and_project_for_user.xml");
Expand Down
Expand Up @@ -19,11 +19,9 @@
*/ */
package org.sonar.server.favorite.ws; package org.sonar.server.favorite.ws;


import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.function.Predicate;
import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService; import org.sonar.api.server.ws.WebService;
Expand Down Expand Up @@ -88,9 +86,7 @@ private static SearchRequest toWsRequest(Request request) {
private SearchResults toSearchResults(SearchRequest request) { private SearchResults toSearchResults(SearchRequest request) {
userSession.checkLoggedIn(); userSession.checkLoggedIn();
try (DbSession dbSession = dbClient.openSession(false)) { try (DbSession dbSession = dbClient.openSession(false)) {
List<ComponentDto> authorizedFavorites = favoriteFinder.list().stream() List<ComponentDto> authorizedFavorites = getAuthorizedFavorites(dbSession);
.filter(isAuthorized(dbSession))
.collect(MoreCollectors.toList());
Paging paging = Paging.forPageIndex(request.getPage()).withPageSize(request.getPageSize()).andTotal(authorizedFavorites.size()); Paging paging = Paging.forPageIndex(request.getPage()).withPageSize(request.getPageSize()).andTotal(authorizedFavorites.size());
List<ComponentDto> displayedFavorites = authorizedFavorites.stream() List<ComponentDto> displayedFavorites = authorizedFavorites.stream()
.skip(paging.offset()) .skip(paging.offset())
Expand All @@ -101,21 +97,24 @@ private SearchResults toSearchResults(SearchRequest request) {
} }
} }


private Predicate<ComponentDto> isAuthorized(DbSession dbSession) { private List<ComponentDto> getAuthorizedFavorites(DbSession dbSession) {
Collection<String> rootProjectsUuids = dbClient.authorizationDao().selectAuthorizedRootProjectsUuids(dbSession, userSession.getUserId(), UserRole.USER); List<ComponentDto> componentDtos = favoriteFinder.list();
Set<String> authorizedProjectUuids = rootProjectsUuids Set<Long> favoriteComponentIds = componentDtos.stream()
.stream() .map(ComponentDto::getId)
.collect(MoreCollectors.toSet(rootProjectsUuids.size())); .collect(MoreCollectors.toSet(componentDtos.size()));
return dto -> authorizedProjectUuids.contains(dto.projectUuid()); Set<Long> authorizedFavoriteComponentIds = dbClient.authorizationDao().keepAuthorizedProjectIds(dbSession, favoriteComponentIds, userSession.getUserId(), UserRole.USER);
return componentDtos.stream()
.filter(dto -> authorizedFavoriteComponentIds.contains(dto.getId()))
.collect(MoreCollectors.toList());
} }


private Map<String, OrganizationDto> getOrganizationsOfComponents(DbSession dbSession, List<ComponentDto> displayedFavorites) { private Map<String, OrganizationDto> getOrganizationsOfComponents(DbSession dbSession, List<ComponentDto> displayedFavorites) {
Set<String> organizationUuids = displayedFavorites.stream() Set<String> organizationUuids = displayedFavorites.stream()
.map(ComponentDto::getOrganizationUuid) .map(ComponentDto::getOrganizationUuid)
.collect(MoreCollectors.toSet()); .collect(MoreCollectors.toSet());
return dbClient.organizationDao().selectByUuids(dbSession, organizationUuids) return dbClient.organizationDao().selectByUuids(dbSession, organizationUuids)
.stream() .stream()
.collect(MoreCollectors.uniqueIndex(OrganizationDto::getUuid)); .collect(MoreCollectors.uniqueIndex(OrganizationDto::getUuid));
} }


private static class SearchResults { private static class SearchResults {
Expand Down
Expand Up @@ -21,7 +21,6 @@


import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
Expand Down Expand Up @@ -150,14 +149,14 @@ private boolean isDispatcherAuthorized(PropertyDto prop, String dispatcher) {
} }


private Map<Long, ComponentDto> searchProjects(DbSession dbSession, List<PropertyDto> properties) { private Map<Long, ComponentDto> searchProjects(DbSession dbSession, List<PropertyDto> properties) {
Set<String> authorizedComponentUuids = new HashSet<>(dbClient.authorizationDao().selectAuthorizedRootProjectsUuids(dbSession, userSession.getUserId(), UserRole.USER));
Set<Long> componentIds = properties.stream() Set<Long> componentIds = properties.stream()
.map(PropertyDto::getResourceId) .map(PropertyDto::getResourceId)
.filter(Objects::nonNull) .filter(Objects::nonNull)
.collect(MoreCollectors.toSet(properties.size())); .collect(MoreCollectors.toSet(properties.size()));
Set<Long> authorizedProjectIds = dbClient.authorizationDao().keepAuthorizedProjectIds(dbSession, componentIds, userSession.getUserId(), UserRole.USER);
return dbClient.componentDao().selectByIds(dbSession, componentIds) return dbClient.componentDao().selectByIds(dbSession, componentIds)
.stream() .stream()
.filter(c -> authorizedComponentUuids.contains(c.uuid())) .filter(c -> authorizedProjectIds.contains(c.getId()))
.collect(MoreCollectors.uniqueIndex(ComponentDto::getId)); .collect(MoreCollectors.uniqueIndex(ComponentDto::getId));
} }


Expand Down

0 comments on commit 98f444c

Please sign in to comment.