*/
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);
}
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;
@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();
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);