From eadae3d0fb363e01285c7ac9a0942b0c33413852 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Fri, 14 Apr 2017 17:19:41 +0200 Subject: [PATCH] SONAR-9140 support public components in RoleDao --- .../main/java/org/sonar/db/user/RoleDao.java | 14 +++++++++++ .../java/org/sonar/db/user/RoleDaoTest.java | 23 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/RoleDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/RoleDao.java index 66e14f432a1..e0197856a7c 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/user/RoleDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/RoleDao.java @@ -19,17 +19,31 @@ */ 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 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 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); } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/user/RoleDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/user/RoleDaoTest.java index 3ec27a22c61..81b0e19af65 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/user/RoleDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/user/RoleDaoTest.java @@ -20,9 +20,11 @@ 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); -- 2.39.5