]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9140 support public components in RoleDao
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 14 Apr 2017 15:19:41 +0000 (17:19 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 27 Apr 2017 12:25:54 +0000 (14:25 +0200)
server/sonar-db-dao/src/main/java/org/sonar/db/user/RoleDao.java
server/sonar-db-dao/src/test/java/org/sonar/db/user/RoleDaoTest.java

index 66e14f432a1fa3bc4e267fbd10154d4287e98e65..e0197856a7cc8ae7064d0b3e849d3fc710499528 100644 (file)
  */
 package org.sonar.db.user;
 
+import com.google.common.collect.ImmutableSet;
 import java.util.List;
+import java.util.Set;
+import org.sonar.api.web.UserRole;
 import org.sonar.db.Dao;
 import org.sonar.db.DbSession;
 
+import static com.google.common.base.Preconditions.checkArgument;
+import static org.sonar.api.web.UserRole.CODEVIEWER;
+import static org.sonar.api.web.UserRole.USER;
+
 public class RoleDao implements Dao {
+  private static final Set<String> UNSUPPORTED_PROJECT_PERMISSIONS = ImmutableSet.of(USER, CODEVIEWER);
 
   /**
    * All the projects on which the user has {@code permission}, directly or through
    * groups.
+   *
+   * @throws IllegalArgumentException this method does not support permissions {@link UserRole#USER user} nor
+   *         {@link UserRole#CODEVIEWER codeviewer} because it does not support public root components.
    */
   public List<Long> selectComponentIdsByPermissionAndUserId(DbSession dbSession, String permission, int userId) {
+    checkArgument(
+      !UNSUPPORTED_PROJECT_PERMISSIONS.contains(permission),
+      "Permissions %s are not supported by selectComponentIdsByPermissionAndUserId", UNSUPPORTED_PROJECT_PERMISSIONS);
     return mapper(dbSession).selectComponentIdsByPermissionAndUserId(permission, userId);
   }
 
index 3ec27a22c61b535b9b968b0d772f14a9fe993568..81b0e19af65f29af48aeb0eaccd9b78efe031d4f 100644 (file)
 package org.sonar.db.user;
 
 import java.util.List;
+import java.util.Random;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 import org.sonar.api.utils.System2;
 import org.sonar.api.web.UserRole;
 import org.sonar.db.DbSession;
@@ -36,6 +38,8 @@ public class RoleDaoTest {
 
   @Rule
   public DbTester db = DbTester.create(System2.INSTANCE);
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
 
   private DbSession dbSession = db.getSession();
   private RoleDao underTest = db.getDbClient().roleDao();
@@ -53,6 +57,25 @@ public class RoleDaoTest {
     project2 = db.components().insertProject();
   }
 
+  @Test
+  public void selectComponentIdsByPermissionAndUserId_throws_IAR_if_permission_USER_is_specified() {
+    expectUnsupportedUserAndCodeViewerPermission();
+
+    underTest.selectComponentIdsByPermissionAndUserId(dbSession, UserRole.USER, new Random().nextInt(55));
+  }
+
+  @Test
+  public void selectComponentIdsByPermissionAndUserId_throws_IAR_if_permission_CODEVIEWER_is_specified() {
+    expectUnsupportedUserAndCodeViewerPermission();
+
+    underTest.selectComponentIdsByPermissionAndUserId(dbSession, UserRole.CODEVIEWER, new Random().nextInt(55));
+  }
+
+  private void expectUnsupportedUserAndCodeViewerPermission() {
+    expectedException.expect(IllegalArgumentException.class);
+    expectedException.expectMessage("Permissions [user, codeviewer] are not supported by selectComponentIdsByPermissionAndUserId");
+  }
+
   @Test
   public void selectComponentIdsByPermissionAndUserId() {
     db.users().insertProjectPermissionOnUser(user1, UserRole.ADMIN, project1);