aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2015-08-20 15:43:19 +0200
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2015-08-24 10:09:52 +0200
commitf763e13f83513125826841f33f09b0ba377c87bc (patch)
treebeb99d1e822121fff6b8352bd40fbf201c5e9000
parentc41709236fb221313de5e65377b761f03217a830 (diff)
downloadsonarqube-f763e13f83513125826841f33f09b0ba377c87bc.tar.gz
sonarqube-f763e13f83513125826841f33f09b0ba377c87bc.zip
Create PermissionValueValidator
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/permission/PermissionTemplateUpdater.java10
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/permission/PermissionValueValidator.java45
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/permission/ws/Parameters.java7
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionRequest.java15
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/permission/PermissionTemplateUpdaterTest.java2
-rw-r--r--sonar-db/src/main/java/org/sonar/core/permission/ComponentPermissions.java3
-rw-r--r--sonar-db/src/main/java/org/sonar/core/permission/GlobalPermissions.java2
7 files changed, 58 insertions, 26 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionTemplateUpdater.java b/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionTemplateUpdater.java
index 4e4385c45f6..8c18e90f4e0 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionTemplateUpdater.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionTemplateUpdater.java
@@ -21,7 +21,6 @@
package org.sonar.server.permission;
import org.sonar.api.security.DefaultGroups;
-import org.sonar.core.permission.ComponentPermissions;
import org.sonar.db.DbClient;
import org.sonar.db.permission.PermissionTemplateDao;
import org.sonar.db.permission.PermissionTemplateDto;
@@ -32,6 +31,7 @@ import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.user.UserSession;
import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
+import static org.sonar.server.permission.PermissionValueValidator.validateProjectPermission;
abstract class PermissionTemplateUpdater {
@@ -54,7 +54,7 @@ abstract class PermissionTemplateUpdater {
void executeUpdate() {
checkGlobalAdminUser(userSession);
Long templateId = getTemplateId(templateKey);
- validatePermission(permission);
+ validateProjectPermission(permission);
doExecute(templateId, permission);
}
@@ -79,12 +79,6 @@ abstract class PermissionTemplateUpdater {
return groupDto.getId();
}
- private void validatePermission(String permission) {
- if (permission == null || !ComponentPermissions.ALL.contains(permission)) {
- throw new BadRequestException("Invalid permission: " + permission);
- }
- }
-
private Long getTemplateId(String key) {
PermissionTemplateDto permissionTemplateDto = permissionTemplateDao.selectTemplateByKey(key);
if (permissionTemplateDto == null) {
diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionValueValidator.java b/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionValueValidator.java
new file mode 100644
index 00000000000..923c6c447f9
--- /dev/null
+++ b/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionValueValidator.java
@@ -0,0 +1,45 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonar.server.permission;
+
+import org.sonar.core.permission.ComponentPermissions;
+import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.server.exceptions.BadRequestException;
+
+public class PermissionValueValidator {
+ private PermissionValueValidator() {
+ // static methods only
+ }
+
+ public static void validateProjectPermission(String permission) {
+ if (!ComponentPermissions.ALL.contains(permission)) {
+ throw new BadRequestException(String.format("The 'permission' parameter for project permissions must be one of %s. '%s' was passed.", ComponentPermissions.ALL_ON_ONE_LINE,
+ permission));
+ }
+ }
+
+ public static void validateGlobalPermission(String permission) {
+ if (!GlobalPermissions.ALL.contains(permission)) {
+ throw new BadRequestException(String.format("The 'permission' parameter for global permissions must be one of %s. '%s' was passed.", GlobalPermissions.ALL_ON_ONE_LINE,
+ permission));
+ }
+ }
+}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/Parameters.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/Parameters.java
index 5d9fef10ffb..dbaf8c6262d 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/Parameters.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/Parameters.java
@@ -20,7 +20,6 @@
package org.sonar.server.permission.ws;
-import com.google.common.base.Joiner;
import org.sonar.api.server.ws.WebService;
import org.sonar.core.permission.ComponentPermissions;
import org.sonar.core.permission.GlobalPermissions;
@@ -33,15 +32,13 @@ class Parameters {
static final String PARAM_PROJECT_UUID = "projectId";
static final String PARAM_PROJECT_KEY = "projectKey";
static final String PARAM_USER_LOGIN = "login";
- static final String PROJECT_PERMISSIONS_ONE_LINE = Joiner.on(", ").join(ComponentPermissions.ALL);
- static final String GLOBAL_PERMISSIONS_ONE_LINE = Joiner.on(", ").join(GlobalPermissions.ALL);
private static final String PERMISSION_PARAM_DESCRIPTION = String.format("Permission" +
"<ul>" +
"<li>Possible values for global permissions: %s</li>" +
"<li>Possible values for project permissions %s</li>" +
"</ul>",
- GLOBAL_PERMISSIONS_ONE_LINE,
- PROJECT_PERMISSIONS_ONE_LINE);
+ GlobalPermissions.ALL_ON_ONE_LINE,
+ ComponentPermissions.ALL_ON_ONE_LINE);
private Parameters() {
// static methods only
diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionRequest.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionRequest.java
index 96c479befa1..d043ea3a942 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionRequest.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionRequest.java
@@ -22,22 +22,20 @@ package org.sonar.server.permission.ws;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.WebService.SelectionMode;
-import org.sonar.core.permission.ComponentPermissions;
import org.sonar.core.permission.GlobalPermissions;
-import org.sonar.server.exceptions.BadRequestException;
import static org.sonar.api.server.ws.WebService.Param.PAGE;
import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
import static org.sonar.api.server.ws.WebService.Param.SELECTED;
import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
-import static org.sonar.server.permission.ws.Parameters.GLOBAL_PERMISSIONS_ONE_LINE;
import static org.sonar.server.permission.ws.Parameters.PARAM_GROUP_ID;
import static org.sonar.server.permission.ws.Parameters.PARAM_GROUP_NAME;
import static org.sonar.server.permission.ws.Parameters.PARAM_PERMISSION;
import static org.sonar.server.permission.ws.Parameters.PARAM_PROJECT_KEY;
import static org.sonar.server.permission.ws.Parameters.PARAM_PROJECT_UUID;
import static org.sonar.server.permission.ws.Parameters.PARAM_USER_LOGIN;
-import static org.sonar.server.permission.ws.Parameters.PROJECT_PERMISSIONS_ONE_LINE;
+import static org.sonar.server.permission.PermissionValueValidator.validateGlobalPermission;
+import static org.sonar.server.permission.PermissionValueValidator.validateProjectPermission;
import static org.sonar.server.ws.WsUtils.checkRequest;
class PermissionRequest {
@@ -174,14 +172,9 @@ class PermissionRequest {
private void checkPermissionParameter() {
if (hasProject) {
- if (!ComponentPermissions.ALL.contains(permission)) {
- throw new BadRequestException(String.format("The '%s' parameter for project permissions must be one of %s. '%s' was passed.", PARAM_PERMISSION,
- PROJECT_PERMISSIONS_ONE_LINE, permission));
- }
+ validateProjectPermission(permission);
} else if (!GlobalPermissions.ALL.contains(permission)) {
- throw new BadRequestException(String.format("The '%s' parameter for global permissions must be one of %s. '%s' was passed.", PARAM_PERMISSION, GLOBAL_PERMISSIONS_ONE_LINE,
- permission
- ));
+ validateGlobalPermission(permission);
}
}
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/PermissionTemplateUpdaterTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/PermissionTemplateUpdaterTest.java
index 87c87da3dc9..30dddeb55df 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/permission/PermissionTemplateUpdaterTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/permission/PermissionTemplateUpdaterTest.java
@@ -114,7 +114,7 @@ public class PermissionTemplateUpdaterTest {
when(dbClient.permissionTemplateDao()).thenReturn(permissionTemplateDao);
PermissionTemplateUpdater updater =
- new PermissionTemplateUpdater(dbClient, userSessionRule, "my_template", "invalid", "user") {
+ new PermissionTemplateUpdater(dbClient, userSessionRule, "my_template", "invalid_permission", "user") {
@Override
void doExecute(Long templateId, String permission) {
}
diff --git a/sonar-db/src/main/java/org/sonar/core/permission/ComponentPermissions.java b/sonar-db/src/main/java/org/sonar/core/permission/ComponentPermissions.java
index 4d2522cae47..01e61968ae1 100644
--- a/sonar-db/src/main/java/org/sonar/core/permission/ComponentPermissions.java
+++ b/sonar-db/src/main/java/org/sonar/core/permission/ComponentPermissions.java
@@ -20,6 +20,7 @@
package org.sonar.core.permission;
+import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import java.util.List;
import org.sonar.api.web.UserRole;
@@ -34,5 +35,5 @@ public final class ComponentPermissions {
* All the component permissions values, ordered from {@link UserRole#USER} to {@link UserRole#CODEVIEWER}.
*/
public static final List<String> ALL = ImmutableList.of(UserRole.USER, UserRole.ADMIN, UserRole.ISSUE_ADMIN, UserRole.CODEVIEWER);
-
+ public static final String ALL_ON_ONE_LINE = Joiner.on(", ").join(ComponentPermissions.ALL);
}
diff --git a/sonar-db/src/main/java/org/sonar/core/permission/GlobalPermissions.java b/sonar-db/src/main/java/org/sonar/core/permission/GlobalPermissions.java
index 9f671e16794..8a01a732f12 100644
--- a/sonar-db/src/main/java/org/sonar/core/permission/GlobalPermissions.java
+++ b/sonar-db/src/main/java/org/sonar/core/permission/GlobalPermissions.java
@@ -20,6 +20,7 @@
package org.sonar.core.permission;
+import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import java.util.List;
@@ -40,6 +41,7 @@ public final class GlobalPermissions {
* All the global permissions values, ordered from {@link #SYSTEM_ADMIN} to {@link #PROVISIONING}.
*/
public static final List<String> ALL = ImmutableList.of(SYSTEM_ADMIN, QUALITY_PROFILE_ADMIN, DASHBOARD_SHARING, SCAN_EXECUTION, PREVIEW_EXECUTION, PROVISIONING);
+ public static final String ALL_ON_ONE_LINE = Joiner.on(", ").join(GlobalPermissions.ALL);
private GlobalPermissions() {
// only static methods