]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8261 Support organizations in web service api/permissions/groups
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 18 Oct 2016 11:57:08 +0000 (13:57 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Thu, 20 Oct 2016 13:10:12 +0000 (15:10 +0200)
16 files changed:
server/sonar-server/src/main/java/org/sonar/server/authentication/UserIdentityAuthenticator.java
server/sonar-server/src/main/java/org/sonar/server/permission/ws/GroupsAction.java
server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/TemplateGroupsAction.java
server/sonar-server/src/test/java/org/sonar/server/permission/UserPermissionChangerTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/GroupsActionTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/ApplyTemplateActionTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/BulkApplyTemplateActionTest.java
sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionDao.java
sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionMapper.java
sonar-db/src/main/java/org/sonar/db/user/GroupDao.java
sonar-db/src/main/java/org/sonar/db/user/GroupMapper.java
sonar-db/src/main/resources/org/sonar/db/permission/GroupPermissionMapper.xml
sonar-db/src/main/resources/org/sonar/db/user/GroupMapper.xml
sonar-db/src/test/java/org/sonar/db/permission/GroupPermissionDaoTest.java
sonar-db/src/test/java/org/sonar/db/permission/UserPermissionDaoTest.java
sonar-db/src/test/java/org/sonar/db/user/GroupDaoTest.java

index 1653c01626eb98c6a82f24830ef85f44bfdc7ad3..53b9dcd309c8bb47c7791b39c33a28eae7e3ceaf 100644 (file)
@@ -37,6 +37,7 @@ import org.sonar.db.DbSession;
 import org.sonar.db.user.GroupDto;
 import org.sonar.db.user.UserDto;
 import org.sonar.db.user.UserGroupDto;
+import org.sonar.server.organization.DefaultOrganization;
 import org.sonar.server.organization.DefaultOrganizationProvider;
 import org.sonar.server.user.ExternalIdentity;
 import org.sonar.server.user.NewUser;
@@ -124,7 +125,8 @@ public class UserIdentityAuthenticator {
       Collection<String> groupsToRemove = Sets.difference(userGroups, identityGroups);
       Collection<String> allGroups = new ArrayList<>(groupsToAdd);
       allGroups.addAll(groupsToRemove);
-      Map<String, GroupDto> groupsByName = from(dbClient.groupDao().selectByNames(dbSession, allGroups)).uniqueIndex(GroupDtoToName.INSTANCE);
+      DefaultOrganization defaultOrganization = defaultOrganizationProvider.get();
+      Map<String, GroupDto> groupsByName = from(dbClient.groupDao().selectByNames(dbSession, defaultOrganization.getUuid(), allGroups)).uniqueIndex(GroupDtoToName.INSTANCE);
 
       addGroups(dbSession, userDto, groupsToAdd, groupsByName);
       removeGroups(dbSession, userDto, groupsToRemove, groupsByName);
index 49a8715ec5055ee00b437a78e54e2f9a30deb882..0f1ea915a4340435d322a8934bfd8047028fa475 100644 (file)
@@ -25,15 +25,16 @@ import com.google.common.collect.TreeMultimap;
 import com.google.common.io.Resources;
 import java.util.List;
 import java.util.Optional;
-import java.util.stream.Collectors;
 import org.sonar.api.security.DefaultGroups;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.api.server.ws.WebService.Param;
 import org.sonar.api.utils.Paging;
+import org.sonar.core.util.stream.Collectors;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
+import org.sonar.db.organization.OrganizationDto;
 import org.sonar.db.permission.GroupPermissionDto;
 import org.sonar.db.permission.PermissionQuery;
 import org.sonar.db.user.GroupDto;
@@ -47,9 +48,11 @@ import static org.sonar.db.permission.PermissionQuery.DEFAULT_PAGE_SIZE;
 import static org.sonar.db.permission.PermissionQuery.RESULTS_MAX_SIZE;
 import static org.sonar.db.permission.PermissionQuery.SEARCH_QUERY_MIN_LENGTH;
 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdmin;
+import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createOrganizationParameter;
 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createPermissionParameter;
 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createProjectParameters;
 import static org.sonar.server.ws.WsUtils.writeProtobuf;
+import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ORGANIZATION_KEY;
 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
 
 public class GroupsAction implements PermissionsWsAction {
@@ -78,7 +81,7 @@ public class GroupsAction implements PermissionsWsAction {
       .setResponseExample(Resources.getResource(getClass(), "groups-example.json"))
       .setHandler(this);
 
-    // TODO add orgKey
+    createOrganizationParameter(action);
     createPermissionParameter(action).setRequired(false);
     createProjectParameters(action);
   }
@@ -86,14 +89,15 @@ public class GroupsAction implements PermissionsWsAction {
   @Override
   public void handle(Request request, Response response) throws Exception {
     try (DbSession dbSession = dbClient.openSession(false)) {
+      OrganizationDto org = support.findOrganization(dbSession, request.param(PARAM_ORGANIZATION_KEY));
       Optional<ProjectId> projectId = support.findProject(dbSession, request);
-      checkProjectAdmin(userSession, projectId);
+      checkProjectAdmin(userSession, org.getUuid(), projectId);
 
       PermissionQuery query = buildPermissionQuery(request, projectId);
       // TODO validatePermission(groupsRequest.getPermission(), wsProjectRef);
-      List<GroupDto> groups = findGroups(dbSession, query);
-      int total = dbClient.groupPermissionDao().countGroupsByPermissionQuery(dbSession, query);
-      List<GroupPermissionDto> groupsWithPermission = findGroupPermissions(dbSession, groups, projectId);
+      List<GroupDto> groups = findGroups(dbSession, org, query);
+      int total = dbClient.groupPermissionDao().countGroupsByQuery(dbSession, org.getUuid(), query);
+      List<GroupPermissionDto> groupsWithPermission = findGroupPermissions(dbSession, org, groups, projectId);
       Paging paging = Paging.forPageIndex(request.mandatoryParamAsInt(Param.PAGE)).withPageSize(query.getPageSize()).andTotal(total);
       WsGroupsResponse groupsResponse = buildResponse(groups, groupsWithPermission, paging);
       writeProtobuf(groupsResponse, request, response);
@@ -141,22 +145,20 @@ public class GroupsAction implements PermissionsWsAction {
     return response.build();
   }
 
-  private List<GroupDto> findGroups(DbSession dbSession, PermissionQuery dbQuery) {
-    // TODO support organizations
-    List<String> orderedNames = dbClient.groupPermissionDao().selectGroupNamesByPermissionQuery(dbSession, dbQuery);
-    List<GroupDto> groups = dbClient.groupDao().selectByNames(dbSession, orderedNames);
+  private List<GroupDto> findGroups(DbSession dbSession, OrganizationDto org, PermissionQuery dbQuery) {
+    List<String> orderedNames = dbClient.groupPermissionDao().selectGroupNamesByQuery(dbSession, org.getUuid(), dbQuery);
+    List<GroupDto> groups = dbClient.groupDao().selectByNames(dbSession, org.getUuid(), orderedNames);
     if (orderedNames.contains(DefaultGroups.ANYONE)) {
-      groups.add(0, new GroupDto().setId(0L).setName(DefaultGroups.ANYONE));
+      groups.add(0, new GroupDto().setId(0L).setName(DefaultGroups.ANYONE).setOrganizationUuid(org.getUuid()));
     }
     return Ordering.explicit(orderedNames).onResultOf(GroupDto::getName).immutableSortedCopy(groups);
   }
 
-  private List<GroupPermissionDto> findGroupPermissions(DbSession dbSession, List<GroupDto> groups, Optional<ProjectId> project) {
+  private List<GroupPermissionDto> findGroupPermissions(DbSession dbSession, OrganizationDto org, List<GroupDto> groups, Optional<ProjectId> project) {
     if (groups.isEmpty()) {
       return emptyList();
     }
-    // TODO use groupId
-    List<String> names = groups.stream().map(GroupDto::getName).collect(Collectors.toList());
-    return dbClient.groupPermissionDao().selectGroupPermissionsByGroupNamesAndProject(dbSession, names, project.isPresent() ? project.get().getId() : null);
+    List<Long> ids = groups.stream().map(GroupDto::getId).collect(Collectors.toList(groups.size()));
+    return dbClient.groupPermissionDao().selectByGroupIds(dbSession, org.getUuid(), ids, project.isPresent() ? project.get().getId() : null);
   }
 }
index 3c2519050b1913ef0a29c73575ef56b684b1f870..77c4c25b228cb1e56f48574e8464ec6fc24972b2 100644 (file)
@@ -143,7 +143,7 @@ public class TemplateGroupsAction implements PermissionsWsAction {
 
   private List<GroupDto> findGroups(DbSession dbSession, PermissionQuery dbQuery, PermissionTemplateDto template) {
     List<String> orderedNames = dbClient.permissionTemplateDao().selectGroupNamesByQueryAndTemplate(dbSession, dbQuery, template.getId());
-    List<GroupDto> groups = dbClient.groupDao().selectByNames(dbSession, orderedNames);
+    List<GroupDto> groups = dbClient.groupDao().selectByNames(dbSession, template.getOrganizationUuid(), orderedNames);
     if (orderedNames.contains(DefaultGroups.ANYONE)) {
       groups.add(0, new GroupDto().setId(0L).setName(DefaultGroups.ANYONE));
     }
index 19fb94398fb0d62624c13002d160ec6816b4c7e9..441d16a1b7fb3ee2467babd64b49fc1e50ec100f 100644 (file)
@@ -27,10 +27,11 @@ import org.sonar.api.utils.System2;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.organization.OrganizationTesting;
 import org.sonar.db.user.GroupDto;
 import org.sonar.db.user.UserDto;
 import org.sonar.server.exceptions.BadRequestException;
+import org.sonar.server.organization.DefaultOrganizationProvider;
+import org.sonar.server.organization.TestDefaultOrganizationProvider;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
@@ -50,7 +51,8 @@ public class UserPermissionChangerTest {
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
 
-  private UserPermissionChanger underTest = new UserPermissionChanger(db.getDbClient());
+  private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
+  private UserPermissionChanger underTest = new UserPermissionChanger(db.getDbClient(), defaultOrganizationProvider);
   private OrganizationDto org1;
   private OrganizationDto org2;
   private UserDto user1;
@@ -59,8 +61,8 @@ public class UserPermissionChangerTest {
 
   @Before
   public void setUp() throws Exception {
-    org1 = OrganizationTesting.insert(db, newOrganizationDto());
-    org2 = OrganizationTesting.insert(db, newOrganizationDto());
+    org1 = db.organizations().insert(newOrganizationDto());
+    org2 = db.organizations().insert(newOrganizationDto());
     user1 = db.users().insertUser();
     user2 = db.users().insertUser();
     project = db.components().insertProject();
index 400a9c44d3e0b15b89366a29bc3e7fff447da5fa..aa03a2523c212292f7668324263edca0afa9d828 100644 (file)
@@ -74,7 +74,7 @@ public class GroupsActionTest extends BasePermissionWsTest<GroupsAction> {
 
   @Test
   public void search_for_groups_with_one_permission() throws Exception {
-    loginAsAdmin();
+    loginAsAdminOnDefaultOrganization();
     newRequest()
       .setParam(PARAM_PERMISSION, SCAN_EXECUTION)
       .execute()
@@ -111,7 +111,7 @@ public class GroupsActionTest extends BasePermissionWsTest<GroupsAction> {
 
   @Test
   public void search_with_selection() throws Exception {
-    loginAsAdmin();
+    loginAsAdminOnDefaultOrganization();
     String result = newRequest()
       .setParam(PARAM_PERMISSION, SCAN_EXECUTION)
       .execute()
@@ -122,7 +122,7 @@ public class GroupsActionTest extends BasePermissionWsTest<GroupsAction> {
 
   @Test
   public void search_groups_with_pagination() throws Exception {
-    loginAsAdmin();
+    loginAsAdminOnDefaultOrganization();
     String result = newRequest()
       .setParam(PARAM_PERMISSION, SCAN_EXECUTION)
       .setParam(PAGE_SIZE, "1")
@@ -137,7 +137,7 @@ public class GroupsActionTest extends BasePermissionWsTest<GroupsAction> {
 
   @Test
   public void search_groups_with_query() throws Exception {
-    loginAsAdmin();
+    loginAsAdminOnDefaultOrganization();
     String result = newRequest()
       .setParam(PARAM_PERMISSION, SCAN_EXECUTION)
       .setParam(TEXT_QUERY, "group-")
@@ -182,7 +182,7 @@ public class GroupsActionTest extends BasePermissionWsTest<GroupsAction> {
     GroupDto groupWithoutPermission = db.users().insertGroup(db.getDefaultOrganization(), "group-without-permission");
     GroupDto anotherGroup = db.users().insertGroup(db.getDefaultOrganization(), "another-group");
 
-    loginAsAdmin();
+    loginAsAdminOnDefaultOrganization();
     String result = newRequest()
       .setParam(PARAM_PERMISSION, ISSUE_ADMIN)
       .setParam(PARAM_PROJECT_ID, "project-uuid")
@@ -203,7 +203,7 @@ public class GroupsActionTest extends BasePermissionWsTest<GroupsAction> {
 
     GroupDto groupWithoutPermission = db.users().insertGroup(db.getDefaultOrganization(), "group-without-permission");
 
-    loginAsAdmin();
+    loginAsAdminOnDefaultOrganization();
     String result = newRequest()
       .setParam(PARAM_PERMISSION, ISSUE_ADMIN)
       .setParam(PARAM_PROJECT_ID, project.uuid())
@@ -220,7 +220,7 @@ public class GroupsActionTest extends BasePermissionWsTest<GroupsAction> {
     GroupDto group = db.users().insertGroup(db.getDefaultOrganization(), "group-with-permission");
     db.users().insertProjectPermissionOnGroup(group, ISSUE_ADMIN, project);
 
-    loginAsAdmin();
+    loginAsAdminOnDefaultOrganization();
     String result = newRequest()
       .setParam(PARAM_PROJECT_ID, project.uuid())
       .setParam(TEXT_QUERY, "nyo")
@@ -236,7 +236,7 @@ public class GroupsActionTest extends BasePermissionWsTest<GroupsAction> {
     GroupDto group = db.users().insertGroup(db.getDefaultOrganization(), "project-group-name");
     db.users().insertProjectPermissionOnGroup(group, ISSUE_ADMIN, view);
 
-    loginAsAdmin();
+    loginAsAdminOnDefaultOrganization();
     String result = newRequest()
       .setParam(PARAM_PERMISSION, ISSUE_ADMIN)
       .setParam(PARAM_PROJECT_ID, "view-uuid")
@@ -275,7 +275,7 @@ public class GroupsActionTest extends BasePermissionWsTest<GroupsAction> {
 
     expectedException.expect(BadRequestException.class);
 
-    loginAsAdmin();
+    loginAsAdminOnDefaultOrganization();
     newRequest()
       .setParam(PARAM_PERMISSION, SCAN_EXECUTION)
       .setParam(PARAM_PROJECT_ID, "project-uuid")
@@ -286,8 +286,4 @@ public class GroupsActionTest extends BasePermissionWsTest<GroupsAction> {
   private WsTester.TestRequest newRequest() {
     return wsTester.newPostRequest(CONTROLLER, "groups");
   }
-
-  private void loginAsAdmin() {
-    userSession.login("login").setGlobalPermissions(SYSTEM_ADMIN);
-  }
 }
index 9a216e68b7afb9c7756cb2c19a717507aefdf3ca..aa115e7e94b4d63d3f7ba713b627dc260d1e237c 100644 (file)
@@ -239,7 +239,7 @@ public class ApplyTemplateActionTest extends BasePermissionWsTest<ApplyTemplateA
 
   private List<String> selectProjectPermissionGroups(ComponentDto project, String permission) {
     PermissionQuery query = PermissionQuery.builder().setPermission(permission).setComponentUuid(project.uuid()).build();
-    return db.getDbClient().groupPermissionDao().selectGroupNamesByPermissionQuery(db.getSession(), query);
+    return db.getDbClient().groupPermissionDao().selectGroupNamesByQuery(db.getSession(), db.getDefaultOrganization().getUuid(), query);
   }
 
   private List<String> selectProjectPermissionUsers(ComponentDto project, String permission) {
index cec65a28e1b58e003ef992e103abb41855d794e9..d4210ba743a5a44374faee07c386a4029e3e3783 100644 (file)
@@ -208,7 +208,7 @@ public class BulkApplyTemplateActionTest extends BasePermissionWsTest<BulkApplyT
 
   private List<String> selectProjectPermissionGroups(ComponentDto project, String permission) {
     PermissionQuery query = PermissionQuery.builder().setPermission(permission).setComponentUuid(project.uuid()).build();
-    return db.getDbClient().groupPermissionDao().selectGroupNamesByPermissionQuery(db.getSession(), query);
+    return db.getDbClient().groupPermissionDao().selectGroupNamesByQuery(db.getSession(), db.getDefaultOrganization().getUuid(), query);
   }
 
   private List<String> selectProjectPermissionUsers(ComponentDto project, String permission) {
index 18bff43b55e9136ab80c839752ee371a5799ce40..2fb928549abdfb1052ce42d5944cb39869f1a9ba 100644 (file)
@@ -52,27 +52,17 @@ public class GroupPermissionDao implements Dao {
 
   /**
    * @return group names, sorted in alphabetical order
-   * @deprecated not compatible with organizations.
    */
-  @Deprecated
-  public List<String> selectGroupNamesByPermissionQuery(DbSession dbSession, PermissionQuery query) {
-    return mapper(dbSession).selectGroupNamesByPermissionQuery(query, new RowBounds(query.getPageOffset(), query.getPageSize()));
+  public List<String> selectGroupNamesByQuery(DbSession dbSession, String organizationUuid, PermissionQuery query) {
+    return mapper(dbSession).selectGroupNamesByQuery(organizationUuid, query, new RowBounds(query.getPageOffset(), query.getPageSize()));
   }
 
-  /**
-   * @deprecated not compatible with organizations.
-   */
-  @Deprecated
-  public int countGroupsByPermissionQuery(DbSession dbSession, PermissionQuery query) {
-    return mapper(dbSession).countGroupsByPermissionQuery(query);
+  public int countGroupsByQuery(DbSession dbSession, String organizationUuid, PermissionQuery query) {
+    return mapper(dbSession).countGroupsByQuery(organizationUuid, query);
   }
 
-  /**
-   * @deprecated group name parameter is not enough to identify a group. It is not compatible with organizations.
-   */
-  @Deprecated
-  public List<GroupPermissionDto> selectGroupPermissionsByGroupNamesAndProject(DbSession dbSession, List<String> groupNames, @Nullable Long projectId) {
-    return executeLargeInputs(groupNames, groups -> mapper(dbSession).selectGroupPermissionByGroupNames(groups, projectId));
+  public List<GroupPermissionDto> selectByGroupIds(DbSession dbSession, String organizationUuid, List<Long> groupIds, @Nullable Long projectId) {
+    return executeLargeInputs(groupIds, groups -> mapper(dbSession).selectByGroupIds(organizationUuid, groups, projectId));
   }
 
   /**
@@ -99,7 +89,6 @@ public class GroupPermissionDao implements Dao {
     return mapper(session).selectGlobalPermissionsOfGroup(organizationUuid, groupId);
   }
 
-
   /**
    * Selects the permissions granted to group and project. An empty list is returned if the
    * group or project do not exist.
index 28c9af27a761c466323b86673450d4d6f2e6decf..7bd77e82349368014140eabe02d606137322972f 100644 (file)
@@ -34,23 +34,11 @@ public interface GroupPermissionMapper {
   @Deprecated
   int countGroups(Map<String, Object> parameters);
 
-  /**
-   * @deprecated does not support organizations
-   */
-  @Deprecated
-  List<String> selectGroupNamesByPermissionQuery(@Param("query") PermissionQuery query, RowBounds rowBounds);
+  List<String> selectGroupNamesByQuery(@Param("organizationUuid") String organizationUuid, @Param("query") PermissionQuery query, RowBounds rowBounds);
 
-  /**
-   * @deprecated does not support organizations
-   */
-  @Deprecated
-  int countGroupsByPermissionQuery(@Param("query") PermissionQuery query);
+  int countGroupsByQuery(@Param("organizationUuid") String organizationUuid, @Param("query") PermissionQuery query);
 
-  /**
-   * @deprecated does not support organizations
-   */
-  @Deprecated
-  List<GroupPermissionDto> selectGroupPermissionByGroupNames(@Param("groupNames") List<String> groupNames, @Nullable @Param("projectId") Long projectId);
+  List<GroupPermissionDto> selectByGroupIds(@Param("organizationUuid") String organizationUuid, @Param("groupIds") List<Long> groupIds, @Nullable @Param("projectId") Long projectId);
 
   void groupsCountByProjectIdAndPermission(Map<String, Object> parameters, ResultHandler resultHandler);
 
index 524597f270b9d79c5fa22d96fe0bd5c860009402..9a0d2824e08b46640e0ca31b5002d3f1ea15d395 100644 (file)
@@ -63,12 +63,8 @@ public class GroupDao implements Dao {
     return Optional.ofNullable(mapper(dbSession).selectByName(organizationUuid, name));
   }
 
-  /**
-   * @deprecated organization should be added as a parameter
-   */
-  @Deprecated
-  public List<GroupDto> selectByNames(DbSession session, Collection<String> names) {
-    return executeLargeInputs(names, mapper(session)::selectByNames);
+  public List<GroupDto> selectByNames(DbSession dbSession, String organizationUuid, Collection<String> names) {
+    return executeLargeInputs(names, pageOfNames -> mapper(dbSession).selectByNames(organizationUuid, pageOfNames));
   }
 
   @CheckForNull
@@ -76,6 +72,10 @@ public class GroupDao implements Dao {
     return mapper(dbSession).selectById(groupId);
   }
 
+  public List<GroupDto> selectByIds(DbSession dbSession, List<Long> ids) {
+    return executeLargeInputs(ids, mapper(dbSession)::selectByIds);
+  }
+
   public void deleteById(DbSession dbSession, long groupId) {
     mapper(dbSession).deleteById(groupId);
   }
index ddf3097edb155b0a35b54ab738108ff6230ca5cb..3d258876b9bf4be9ef15167ff73550c0ec0470fc 100644 (file)
@@ -35,7 +35,7 @@ public interface GroupMapper {
 
   List<GroupDto> selectByUserLogin(String userLogin);
 
-  List<GroupDto> selectByNames(@Param("names") List<String> names);
+  List<GroupDto> selectByNames(@Param("organizationUuid") String organizationUuid, @Param("names") List<String> names);
 
   void insert(GroupDto groupDto);
 
@@ -59,4 +59,6 @@ public interface GroupMapper {
   int updateNonRootUsersOfGroup(@Param("groupId") long groupId,
     @Param("defaultOrganizationUuid") String defaultOrganizationUuid,
     @Param("now") long now);
+
+  List<GroupDto> selectByIds(@Param("ids") List<Long> ids);
 }
index fc3c7b8d2ea053464c984549f14448379e6c6ee0..56f22ea19541c1029887f0225a7d94478134f79b 100644 (file)
     ) groups
   </select>
 
-  <select id="selectGroupNamesByPermissionQuery" parameterType="map" resultType="string">
-    select distinct gr.name, lower(gr.name), gr.group_id
+  <select id="selectGroupNamesByQuery" parameterType="map" resultType="string">
+    select distinct sub.name, lower(sub.name), sub.groupId
     <include refid="groupsByQuery" />
-    order by lower(gr.name), gr.name, gr.group_id
+    order by lower(sub.name), sub.name, sub.groupId
   </select>
 
-  <select id="countGroupsByPermissionQuery" parameterType="map" resultType="int">
+  <select id="countGroupsByQuery" parameterType="map" resultType="int">
     select count(1)
     from (
-      select distinct gr.group_id
+      select distinct sub.groupId
       <include refid="groupsByQuery" />) g
   </select>
 
   <sql id="groupsByQuery">
     from (
-    select g.id as group_id, g.name as name, gr.role as permission, gr.resource_id as resource_id, gr.id as id
-    from groups g
+      select g.id as groupId, g.name as name, gr.role as permission, gr.resource_id as componentId, gr.id as id
+      from groups g
       left join group_roles gr on g.id = gr.group_id
-    UNION ALL
-    select 0 as group_id, 'Anyone' as name, gr.role as permission, gr.resource_id as resource_id, gr.id as id
+      where
+      g.organization_uuid = #{organizationUuid,jdbcType=VARCHAR}
+
+    union all
+
+    select 0 as groupId, 'Anyone' as name, gr.role as permission, gr.resource_id as componentId, gr.id as id
     from group_roles gr
     <if test="query.withAtLeastOnePermission()">
-      where gr.group_id is null
+      where
+      gr.organization_uuid = #{organizationUuid,jdbcType=VARCHAR} and
+      gr.group_id is null
     </if>
-    ) gr
-    left join projects p on gr.resource_id = p.id
+
+    ) sub
+    left join projects p on sub.componentId = p.id
     <where>
       <if test="query.searchQueryToSql != null">
-        and lower(gr.name) like #{query.searchQueryToSql,jdbcType=VARCHAR} ESCAPE '/'
+        and lower(sub.name) like #{query.searchQueryToSql,jdbcType=VARCHAR} ESCAPE '/'
       </if>
       <!-- filter rows with group permissions -->
       <if test="query.withAtLeastOnePermission()">
-        and gr.permission is not null
+        and sub.permission is not null
         <if test="query.componentUuid==null">
-          and gr.resource_id is null
+          and sub.componentId is null
         </if>
         <if test="query.componentUuid!=null">
-          and p.uuid=#{query.componentUuid,jdbcType=VARCHAR}
+          and p.uuid = #{query.componentUuid,jdbcType=VARCHAR}
         </if>
         <if test="query.permission!=null">
-          and gr.permission=#{query.permission,jdbcType=VARCHAR}
+          and sub.permission = #{query.permission,jdbcType=VARCHAR}
         </if>
       </if>
     </where>
   </sql>
 
-  <select id="selectGroupPermissionByGroupNames" parameterType="map" resultType="GroupPermission">
-    SELECT sub.id, sub.groupId, sub.resourceId, sub.role
-    FROM
+  <select id="selectByGroupIds" parameterType="map" resultType="GroupPermission">
+    select sub.groupId as groupId, sub.componentId as resourceId, sub.permission as role, sub.organizationUuid as organizationUuid
+    from
       (
-      SELECT gr.id AS id, gr.group_id AS groupId, gr.resource_id AS resourceId, gr.role AS role, g.name AS name
-      FROM group_roles gr
-      INNER JOIN groups g ON g.id=gr.group_id
-      UNION ALL
-      SELECT gr.id as id, 0 AS group_id, gr.resource_id AS resource_id, gr.role AS role, 'Anyone' AS name
-      FROM group_roles gr
-      WHERE gr.group_id is null
+      select gr.group_id as groupId, gr.resource_id as componentId, gr.role as permission, g.name as name, gr.organization_uuid as organizationUuid
+      from group_roles gr
+      inner join groups g ON g.id = gr.group_id
+      where gr.organization_uuid = #{organizationUuid,jdbcType=VARCHAR} and
+      gr.group_id is not null
+
+      union all
+
+      select 0 as groupId, gr.resource_id as componentId, gr.role as permission, 'Anyone' as name, gr.organization_uuid as organizationUuid
+      from group_roles gr
+      where
+      gr.group_id is null and
+      gr.organization_uuid = #{organizationUuid,jdbcType=VARCHAR}
       ) sub
     where
-      sub.name IN
-      <foreach collection="groupNames" open="(" close=")" item="name" separator=",">
-        #{name}
+      sub.groupId in
+      <foreach collection="groupIds" open="(" close=")" item="groupId" separator=",">
+        #{groupId,jdbcType=BIGINT}
       </foreach>
-      <if test="projectId!=null">
-        AND sub.resourceId=#{projectId,jdbcType=BIGINT}
+      <if test="projectId != null">
+        and sub.componentId=#{projectId,jdbcType=BIGINT}
       </if>
       <if test="projectId==null">
-        AND sub.resourceId IS NULL
+        and sub.componentId is null
       </if>
   </select>
 
index 2898980a517122d451e0cb04efc9853abf826b70..45cfbc85797d9ac8e8c2d408d65e10af40eb8803 100644 (file)
     where g.organization_uuid = #{organizationUuid,jdbcType=VARCHAR} and g.name = #{name,jdbcType=VARCHAR}
   </select>
 
+  <select id="selectByIds" parameterType="long" resultType="Group">
+    select
+    <include refid="groupColumns"/>
+    from groups g
+    where g.id in
+    <foreach item="id" index="index" collection="ids" open="(" separator="," close=")">
+      #{id,jdbcType=BIGINT}
+    </foreach>
+  </select>
+
   <select id="selectByKey" parameterType="string" resultType="Group">
     SELECT
     <include refid="groupColumns"/>
   </select>
 
   <select id="selectByNames" parameterType="map" resultType="Group">
-    SELECT
+    select
     <include refid="groupColumns"/>
-    FROM groups g
-    <where>
-      g.name IN
+    from groups g
+    where
+      g.organization_uuid = #{organizationUuid,jdbcType=VARCHAR} and
+      g.name in
       <foreach item="name" index="index" collection="names" open="(" separator="," close=")">
-        #{name}
+        #{name,jdbcType=VARCHAR}
       </foreach>
-    </where>
   </select>
 
   <insert id="insert" parameterType="Group" keyColumn="id" useGeneratedKeys="true" keyProperty="id">
index 535fd5bb5d2e99bb23e73d53d4c6e8275e2a0086..35bc6077431e7b2d79c8df4ed0c1544abf7a9ada 100644 (file)
@@ -23,6 +23,7 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 import java.util.stream.IntStream;
+import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.sonar.api.utils.System2;
@@ -47,17 +48,27 @@ import static org.sonar.db.component.ComponentTesting.newProjectDto;
 import static org.sonar.db.user.GroupTesting.newGroupDto;
 
 public class GroupPermissionDaoTest {
+
+  private static final long ANYONE_ID = 0L;
+  private static final long MISSING_ID = -1L;
+
   @Rule
   public DbTester db = DbTester.create(System2.INSTANCE);
 
   private DbSession dbSession = db.getSession();
   private GroupPermissionDao underTest = new GroupPermissionDao();
+  private String defaultOrganizationUuid;
+
+  @Before
+  public void setUp() throws Exception {
+    defaultOrganizationUuid = db.getDefaultOrganization().getUuid();
+  }
 
   @Test
   public void group_count_by_permission_and_component_id() {
-    GroupDto group1 = db.users().insertGroup(newGroupDto());
-    GroupDto group2 = db.users().insertGroup(newGroupDto());
-    GroupDto group3 = db.users().insertGroup(newGroupDto());
+    GroupDto group1 = db.users().insertGroup();
+    GroupDto group2 = db.users().insertGroup();
+    GroupDto group3 = db.users().insertGroup();
     ComponentDto project1 = db.components().insertProject();
     ComponentDto project2 = db.components().insertProject();
     ComponentDto project3 = db.components().insertProject();
@@ -82,52 +93,41 @@ public class GroupPermissionDaoTest {
   }
 
   @Test
-  public void select_groups_by_query() {
-    GroupDto group1 = db.users().insertGroup(newGroupDto());
-    GroupDto group2 = db.users().insertGroup(newGroupDto());
-    GroupDto group3 = db.users().insertGroup(newGroupDto());
+  public void selectGroupNamesByQuery_is_ordered_by_group_names() {
+    GroupDto group2 = addGroupOnDefaultOrganization("Group-2");
+    GroupDto group3 = addGroupOnDefaultOrganization("Group-3");
+    GroupDto group1 = addGroupOnDefaultOrganization("Group-1");
     db.users().insertPermissionOnAnyone(SCAN_EXECUTION);
 
-    List<String> groupNames = underTest.selectGroupNamesByPermissionQuery(dbSession, PermissionQuery.builder().build());
-    assertThat(groupNames).containsOnly("Anyone", group1.getName(), group2.getName(), group3.getName());
+    assertThat(underTest.selectGroupNamesByQuery(dbSession,
+      defaultOrganizationUuid, PermissionQuery.builder().build())).containsExactly(ANYONE, group1.getName(), group2.getName(), group3.getName());
   }
 
   @Test
-  public void select_groups_by_query_is_ordered_by_group_names() {
-    db.users().insertGroup(newGroupDto().setName("Group-2"));
-    db.users().insertGroup(newGroupDto().setName("Group-3"));
-    db.users().insertGroup(newGroupDto().setName("Group-1"));
-    db.users().insertPermissionOnAnyone(SCAN_EXECUTION);
-
-    assertThat(underTest.selectGroupNamesByPermissionQuery(dbSession,
-      PermissionQuery.builder().build())).containsExactly("Anyone", "Group-1", "Group-2", "Group-3");
-  }
-
-  @Test
-  public void count_groups_by_query() {
-    GroupDto group1 = db.users().insertGroup(newGroupDto().setName("Group-1"));
-    GroupDto group2 = db.users().insertGroup(newGroupDto().setName("Group-2"));
-    GroupDto group3 = db.users().insertGroup(newGroupDto().setName("Group-3"));
+  public void countGroupsByQuery() {
+    GroupDto group1 = addGroupOnDefaultOrganization("Group-1");
+    addGroupOnDefaultOrganization("Group-2");
+    addGroupOnDefaultOrganization("Group-3");
     db.users().insertPermissionOnAnyone(SCAN_EXECUTION);
     db.users().insertPermissionOnGroup(group1, PROVISIONING);
 
-    assertThat(underTest.countGroupsByPermissionQuery(dbSession,
-      PermissionQuery.builder().build())).isEqualTo(4);
-    assertThat(underTest.countGroupsByPermissionQuery(dbSession,
-      PermissionQuery.builder().setPermission(PROVISIONING).build())).isEqualTo(1);
-    assertThat(underTest.countGroupsByPermissionQuery(dbSession,
-      PermissionQuery.builder().withAtLeastOnePermission().build())).isEqualTo(2);
-    assertThat(underTest.countGroupsByPermissionQuery(dbSession,
-      PermissionQuery.builder().setSearchQuery("Group-").build())).isEqualTo(3);
-    assertThat(underTest.countGroupsByPermissionQuery(dbSession,
-      PermissionQuery.builder().setSearchQuery("Any").build())).isEqualTo(1);
+    assertThat(underTest.countGroupsByQuery(dbSession,
+      defaultOrganizationUuid, PermissionQuery.builder().build())).isEqualTo(4);
+    assertThat(underTest.countGroupsByQuery(dbSession,
+      defaultOrganizationUuid, PermissionQuery.builder().setPermission(PROVISIONING).build())).isEqualTo(1);
+    assertThat(underTest.countGroupsByQuery(dbSession,
+      defaultOrganizationUuid, PermissionQuery.builder().withAtLeastOnePermission().build())).isEqualTo(2);
+    assertThat(underTest.countGroupsByQuery(dbSession,
+      defaultOrganizationUuid, PermissionQuery.builder().setSearchQuery("Group-").build())).isEqualTo(3);
+    assertThat(underTest.countGroupsByQuery(dbSession,
+      defaultOrganizationUuid, PermissionQuery.builder().setSearchQuery("Any").build())).isEqualTo(1);
   }
 
   @Test
-  public void select_groups_by_query_with_global_permission() {
-    GroupDto group1 = db.users().insertGroup(newGroupDto().setName("Group-1"));
-    GroupDto group2 = db.users().insertGroup(newGroupDto().setName("Group-2"));
-    GroupDto group3 = db.users().insertGroup(newGroupDto().setName("Group-3"));
+  public void selectGroupNamesByQuery_with_global_permission() {
+    GroupDto group1 = addGroupOnDefaultOrganization("Group-1");
+    GroupDto group2 = addGroupOnDefaultOrganization("Group-2");
+    GroupDto group3 = addGroupOnDefaultOrganization("Group-3");
 
     ComponentDto project = db.components().insertComponent(newProjectDto());
 
@@ -137,24 +137,24 @@ public class GroupPermissionDaoTest {
     db.users().insertPermissionOnGroup(group3, SYSTEM_ADMIN);
     db.users().insertProjectPermissionOnGroup(group2, UserRole.ADMIN, project);
 
-    assertThat(underTest.selectGroupNamesByPermissionQuery(dbSession,
-      PermissionQuery.builder().setPermission(SCAN_EXECUTION).build())).containsExactly(ANYONE, "Group-1");
+    assertThat(underTest.selectGroupNamesByQuery(dbSession,
+      defaultOrganizationUuid, PermissionQuery.builder().setPermission(SCAN_EXECUTION).build())).containsExactly(ANYONE, group1.getName());
 
-    assertThat(underTest.selectGroupNamesByPermissionQuery(dbSession,
-      PermissionQuery.builder().setPermission(SYSTEM_ADMIN).build())).containsExactly("Group-3");
+    assertThat(underTest.selectGroupNamesByQuery(dbSession,
+      defaultOrganizationUuid, PermissionQuery.builder().setPermission(SYSTEM_ADMIN).build())).containsExactly(group3.getName());
 
-    assertThat(underTest.selectGroupNamesByPermissionQuery(dbSession,
-      PermissionQuery.builder().setPermission(PROVISIONING).build())).containsExactly(ANYONE);
+    assertThat(underTest.selectGroupNamesByQuery(dbSession,
+      defaultOrganizationUuid, PermissionQuery.builder().setPermission(PROVISIONING).build())).containsExactly(ANYONE);
   }
 
   @Test
   public void select_groups_by_query_with_project_permissions() {
-    GroupDto group1 = db.users().insertGroup(newGroupDto());
-    GroupDto group2 = db.users().insertGroup(newGroupDto());
-    GroupDto group3 = db.users().insertGroup(newGroupDto());
+    GroupDto group1 = db.users().insertGroup();
+    GroupDto group2 = db.users().insertGroup();
+    GroupDto group3 = db.users().insertGroup();
 
-    ComponentDto project = db.components().insertComponent(newProjectDto());
-    ComponentDto anotherProject = db.components().insertComponent(newProjectDto());
+    ComponentDto project = db.components().insertProject();
+    ComponentDto anotherProject = db.components().insertProject();
 
     db.users().insertProjectPermissionOnGroup(group1, SCAN_EXECUTION, project);
     db.users().insertProjectPermissionOnGroup(group1, PROVISIONING, project);
@@ -166,113 +166,114 @@ public class GroupPermissionDaoTest {
     db.users().insertPermissionOnGroup(group2, SCAN_EXECUTION);
 
     PermissionQuery.Builder builderOnComponent = PermissionQuery.builder().setComponentUuid(project.uuid());
-    assertThat(underTest.selectGroupNamesByPermissionQuery(dbSession,
-      builderOnComponent.withAtLeastOnePermission().build())).containsOnlyOnce(group1.getName());
-    assertThat(underTest.selectGroupNamesByPermissionQuery(dbSession,
-      builderOnComponent.setPermission(SCAN_EXECUTION).build())).containsOnlyOnce(group1.getName());
-    assertThat(underTest.selectGroupNamesByPermissionQuery(dbSession,
-      builderOnComponent.setPermission(USER).build())).containsOnlyOnce(ANYONE);
+    assertThat(underTest.selectGroupNamesByQuery(dbSession,
+      defaultOrganizationUuid, builderOnComponent.withAtLeastOnePermission().build())).containsOnlyOnce(group1.getName());
+    assertThat(underTest.selectGroupNamesByQuery(dbSession,
+      defaultOrganizationUuid, builderOnComponent.setPermission(SCAN_EXECUTION).build())).containsOnlyOnce(group1.getName());
+    assertThat(underTest.selectGroupNamesByQuery(dbSession,
+      defaultOrganizationUuid, builderOnComponent.setPermission(USER).build())).containsOnlyOnce(ANYONE);
   }
 
   @Test
-  public void select_groups_by_query_paginated() {
-    IntStream.rangeClosed(0, 9).forEach(i -> db.users().insertGroup(newGroupDto().setName(i + "-name")));
+  public void selectGroupNamesByQuery_is_paginated() {
+    IntStream.rangeClosed(0, 9).forEach(i -> addGroupOnDefaultOrganization(i + "-name"));
 
-    assertThat(underTest.selectGroupNamesByPermissionQuery(dbSession,
-      PermissionQuery.builder().setPageIndex(2).setPageSize(3).build())).containsExactly("3-name", "4-name", "5-name");
+    List<String> groupNames = underTest.selectGroupNamesByQuery(dbSession,
+      defaultOrganizationUuid, PermissionQuery.builder().setPageIndex(2).setPageSize(3).build());
+    assertThat(groupNames).containsExactly("3-name", "4-name", "5-name");
   }
 
   @Test
-  public void select_groups_by_query_with_search_query() {
-    GroupDto group = db.users().insertGroup(newGroupDto().setName("group-anyone"));
-    db.users().insertGroup(newGroupDto().setName("unknown"));
+  public void selectGroupNamesByQuery_with_search_query() {
+    GroupDto group = addGroupOnDefaultOrganization("group-anyone");
+    addGroupOnDefaultOrganization("unknown");
     db.users().insertPermissionOnGroup(group, SCAN_EXECUTION);
 
-    assertThat(underTest.selectGroupNamesByPermissionQuery(dbSession,
-      PermissionQuery.builder().setSearchQuery("any").build())).containsOnlyOnce(ANYONE, "group-anyone");
+    assertThat(underTest.selectGroupNamesByQuery(dbSession,
+      defaultOrganizationUuid, PermissionQuery.builder().setSearchQuery("any").build())).containsOnlyOnce(ANYONE, group.getName());
   }
 
   @Test
-  public void select_groups_by_query_does_not_return_anyone_when_group_roles_is_empty() {
-    GroupDto group = db.users().insertGroup(newGroupDto());
+  public void selectGroupNamesByQuery_does_not_return_anyone_when_group_roles_is_empty() {
+    GroupDto group = db.users().insertGroup();
 
-    assertThat(underTest.selectGroupNamesByPermissionQuery(dbSession,
-      PermissionQuery.builder().build()))
+    assertThat(underTest.selectGroupNamesByQuery(dbSession,
+      defaultOrganizationUuid, PermissionQuery.builder().build()))
         .doesNotContain(ANYONE)
         .containsExactly(group.getName());
   }
 
   @Test
-  public void select_group_permissions_by_group_names_on_global_permissions() {
-    GroupDto group1 = db.users().insertGroup(newGroupDto().setName("Group-1"));
+  public void selectByGroupIds_on_global_permissions() {
+    GroupDto group1 = addGroupOnDefaultOrganization("Group-1");
     db.users().insertPermissionOnGroup(group1, SCAN_EXECUTION);
 
-    GroupDto group2 = db.users().insertGroup(newGroupDto().setName("Group-2"));
+    GroupDto group2 = addGroupOnDefaultOrganization("Group-2");
     ComponentDto project = db.components().insertComponent(newProjectDto());
     db.users().insertProjectPermissionOnGroup(group2, UserRole.ADMIN, project);
 
-    GroupDto group3 = db.users().insertGroup(newGroupDto().setName("Group-3"));
+    GroupDto group3 = addGroupOnDefaultOrganization("Group-3");
     db.users().insertPermissionOnGroup(group3, SYSTEM_ADMIN);
 
     // Anyone
     db.users().insertPermissionOnAnyone(SCAN_EXECUTION);
     db.users().insertPermissionOnAnyone(PROVISIONING);
 
-    assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Group-1"), null))
+    assertThat(underTest.selectByGroupIds(dbSession, defaultOrganizationUuid, asList(group1.getId()), null))
       .extracting(GroupPermissionDto::getGroupId, GroupPermissionDto::getRole, GroupPermissionDto::getResourceId)
       .containsOnly(tuple(group1.getId(), SCAN_EXECUTION, null));
 
-    assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Group-2"), null)).isEmpty();
+    assertThat(underTest.selectByGroupIds(dbSession, defaultOrganizationUuid, asList(group2.getId()), null)).isEmpty();
 
-    assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Group-3"), null))
+    assertThat(underTest.selectByGroupIds(dbSession, defaultOrganizationUuid, asList(group3.getId()), null))
       .extracting(GroupPermissionDto::getGroupId, GroupPermissionDto::getRole, GroupPermissionDto::getResourceId)
       .containsOnly(tuple(group3.getId(), SYSTEM_ADMIN, null));
 
-    assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Anyone"), null))
+    assertThat(underTest.selectByGroupIds(dbSession, defaultOrganizationUuid, asList(ANYONE_ID), null))
       .extracting(GroupPermissionDto::getGroupId, GroupPermissionDto::getRole, GroupPermissionDto::getResourceId)
       .containsOnly(
         tuple(0L, SCAN_EXECUTION, null),
         tuple(0L, PROVISIONING, null));
 
-    assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Group-1", "Group-2", "Anyone"), null)).hasSize(3);
-    assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Unknown"), null)).isEmpty();
-    assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, Collections.emptyList(), null)).isEmpty();
+    assertThat(underTest.selectByGroupIds(dbSession, defaultOrganizationUuid, asList(group1.getId(), group2.getId(), ANYONE_ID), null)).hasSize(3);
+    assertThat(underTest.selectByGroupIds(dbSession, defaultOrganizationUuid, asList(MISSING_ID), null)).isEmpty();
+    assertThat(underTest.selectByGroupIds(dbSession, defaultOrganizationUuid, Collections.emptyList(), null)).isEmpty();
   }
 
   @Test
-  public void select_group_permissions_by_group_names_on_project_permissions() {
-    GroupDto group1 = db.users().insertGroup(newGroupDto().setName("Group-1"));
+  public void selectByGroupIds_on_projects() {
+    GroupDto group1 = addGroupOnDefaultOrganization("Group-1");
     db.users().insertPermissionOnGroup(group1, PROVISIONING);
 
-    GroupDto group2 = db.users().insertGroup(newGroupDto().setName("Group-2"));
+    GroupDto group2 = addGroupOnDefaultOrganization("Group-2");
     ComponentDto project = db.components().insertComponent(newProjectDto());
     db.users().insertProjectPermissionOnGroup(group2, USER, project);
 
-    GroupDto group3 = db.users().insertGroup(newGroupDto().setName("Group-3"));
+    GroupDto group3 = addGroupOnDefaultOrganization("Group-3");
     db.users().insertProjectPermissionOnGroup(group3, USER, project);
 
     // Anyone group
     db.users().insertPermissionOnAnyone(SCAN_EXECUTION);
     db.users().insertProjectPermissionOnAnyone(PROVISIONING, project);
 
-    assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Group-1"), project.getId())).isEmpty();
+    assertThat(underTest.selectByGroupIds(dbSession, defaultOrganizationUuid, asList(group1.getId()), project.getId())).isEmpty();
 
-    assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Group-2"), project.getId()))
+    assertThat(underTest.selectByGroupIds(dbSession, defaultOrganizationUuid, asList(group2.getId()), project.getId()))
       .extracting(GroupPermissionDto::getGroupId, GroupPermissionDto::getRole, GroupPermissionDto::getResourceId)
       .containsOnly(tuple(group2.getId(), USER, project.getId()));
 
-    assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Group-3"), project.getId()))
+    assertThat(underTest.selectByGroupIds(dbSession, defaultOrganizationUuid, asList(group3.getId()), project.getId()))
       .extracting(GroupPermissionDto::getGroupId, GroupPermissionDto::getRole, GroupPermissionDto::getResourceId)
       .containsOnly(tuple(group3.getId(), USER, project.getId()));
 
-    assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Anyone"), project.getId()))
+    assertThat(underTest.selectByGroupIds(dbSession, defaultOrganizationUuid, asList(ANYONE_ID), project.getId()))
       .extracting(GroupPermissionDto::getGroupId, GroupPermissionDto::getRole, GroupPermissionDto::getResourceId)
       .containsOnly(tuple(0L, PROVISIONING, project.getId()));
 
-    assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Group-1", "Group-2", "Anyone"), project.getId())).hasSize(2);
-    assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Unknown"), project.getId())).isEmpty();
-    assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Group-1"), 123L)).isEmpty();
-    assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, Collections.emptyList(), project.getId())).isEmpty();
+    assertThat(underTest.selectByGroupIds(dbSession, defaultOrganizationUuid, asList(group1.getId(), group2.getId(), ANYONE_ID), project.getId())).hasSize(2);
+    assertThat(underTest.selectByGroupIds(dbSession, defaultOrganizationUuid, asList(MISSING_ID), project.getId())).isEmpty();
+    assertThat(underTest.selectByGroupIds(dbSession, defaultOrganizationUuid, asList(group1.getId()), 123L)).isEmpty();
+    assertThat(underTest.selectByGroupIds(dbSession, defaultOrganizationUuid, Collections.emptyList(), project.getId())).isEmpty();
   }
 
   @Test
@@ -435,4 +436,8 @@ public class GroupPermissionDaoTest {
   private void assertThatNoPermission(String permission) {
     assertThat(db.countSql("select count(id) from group_roles where role='" + permission + "'")).isEqualTo(0);
   }
+
+  private GroupDto addGroupOnDefaultOrganization(String name) {
+    return db.users().insertGroup(db.getDefaultOrganization(), name);
+  }
 }
index 4210dd4b5a0d4c1ffe4b1a158d717bb575cbf2b9..44ca88443906161764c0ab806927a905c01bffe4 100644 (file)
@@ -33,7 +33,6 @@ import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.organization.OrganizationTesting;
 import org.sonar.db.user.UserDto;
 
 import static java.util.Arrays.asList;
@@ -295,7 +294,7 @@ public class UserPermissionDaoTest {
 
   @Test
   public void selectGlobalPermissionsOfUser() {
-    OrganizationDto org = OrganizationTesting.insert(dbTester, newOrganizationDto());
+    OrganizationDto org = dbTester.organizations().insert(newOrganizationDto());
     addGlobalPermissionOnDefaultOrganization("perm1", user1);
     addGlobalPermissionOnDefaultOrganization("perm2", user2);
     addGlobalPermission(org, "perm3", user1);
@@ -310,7 +309,7 @@ public class UserPermissionDaoTest {
 
   @Test
   public void selectProjectPermissionsOfUser() {
-    OrganizationDto org = OrganizationTesting.insert(dbTester, newOrganizationDto());
+    OrganizationDto org = dbTester.organizations().insert(newOrganizationDto());
     ComponentDto project3 = dbTester.components().insertProject();
     addGlobalPermission(org, "perm1", user1);
     addProjectPermission(org, "perm2", user1, project1);
index 3a8db595d9f7f0b0d708306fc54f14c455360a64..99ee0be311529d2400b32919593ca7146d9dc7fe 100644 (file)
@@ -33,7 +33,6 @@ import org.sonar.db.organization.OrganizationDto;
 
 import static java.util.Arrays.asList;
 import static java.util.Arrays.stream;
-import static java.util.Collections.singletonList;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
@@ -43,6 +42,7 @@ import static org.sonar.db.user.GroupTesting.newGroupDto;
 public class GroupDaoTest {
 
   private static final long NOW = 1_500_000L;
+  private static final long MISSING_ID = -1L;
   private static final OrganizationDto AN_ORGANIZATION = new OrganizationDto()
     .setKey("an-org")
     .setName("An Org")
@@ -54,9 +54,8 @@ public class GroupDaoTest {
 
   @Rule
   public DbTester db = DbTester.create(system2);
-
-  private final DbSession dbSession = db.getSession();
-  private GroupDao underTest = new GroupDao(system2);
+  private DbSession dbSession = db.getSession();
+  private GroupDao underTest = db.getDbClient().groupDao();
 
   // not static as group id is changed in each test
   private final GroupDto aGroup = new GroupDto()
@@ -101,15 +100,32 @@ public class GroupDaoTest {
 
   @Test
   public void selectByNames() {
-    underTest.insert(dbSession, newGroupDto().setName("group1"));
-    underTest.insert(dbSession, newGroupDto().setName("group2"));
-    underTest.insert(dbSession, newGroupDto().setName("group3"));
+    GroupDto group1InOrg1 = underTest.insert(dbSession, newGroupDto().setName("group1").setOrganizationUuid("org1"));
+    GroupDto group2InOrg1 = underTest.insert(dbSession, newGroupDto().setName("group2").setOrganizationUuid("org1"));
+    GroupDto group1InOrg2 = underTest.insert(dbSession, newGroupDto().setName("group1").setOrganizationUuid("org2"));
+    GroupDto group3InOrg2 = underTest.insert(dbSession, newGroupDto().setName("group3").setOrganizationUuid("org2"));
     dbSession.commit();
 
-    assertThat(underTest.selectByNames(dbSession, asList("group1", "group2", "group3"))).hasSize(3);
-    assertThat(underTest.selectByNames(dbSession, singletonList("group1"))).hasSize(1);
-    assertThat(underTest.selectByNames(dbSession, asList("group1", "unknown"))).hasSize(1);
-    assertThat(underTest.selectByNames(dbSession, Collections.emptyList())).isEmpty();
+    assertThat(underTest.selectByNames(dbSession, "org1", asList("group1", "group2", "group3", "missingGroup"))).extracting(GroupDto::getId)
+      .containsOnly(group1InOrg1.getId(), group2InOrg1.getId());
+
+    assertThat(underTest.selectByNames(dbSession, "org1", Collections.emptyList())).isEmpty();
+    assertThat(underTest.selectByNames(dbSession, "missingOrg", asList("group1"))).isEmpty();
+  }
+
+  @Test
+  public void selectByIds() {
+    GroupDto group1 = db.users().insertGroup();
+    GroupDto group2 = db.users().insertGroup();
+    GroupDto group3 = db.users().insertGroup();
+
+    assertThat(underTest.selectByIds(dbSession, asList(group1.getId(), group2.getId())))
+      .extracting(GroupDto::getId).containsOnly(group1.getId(), group2.getId());
+
+    assertThat(underTest.selectByIds(dbSession, asList(group1.getId(), MISSING_ID)))
+      .extracting(GroupDto::getId).containsOnly(group1.getId());
+
+    assertThat(underTest.selectByIds(dbSession, Collections.emptyList())).isEmpty();
   }
 
   @Test
@@ -371,7 +387,7 @@ public class GroupDaoTest {
     };
     UserDto[] adminPerGroupPermissionUsers = {
       db.users().makeRoot(db.users().insertUser()),
-      db.users().insertUser()  // incorrectly not root
+      db.users().insertUser() // incorrectly not root
     };
     OrganizationDto otherOrganization = db.organizations().insert();
     GroupDto nonAdminGroup = db.users().insertGroup(otherOrganization);