package org.sonar.server.permission;
-import org.apache.commons.lang.StringUtils;
-import org.picocontainer.annotations.Nullable;
+import java.util.List;
+import java.util.Map;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.util.RubyUtils;
-import java.util.List;
-import java.util.Map;
+import static com.google.common.base.CharMatcher.WHITESPACE;
public class ApplyPermissionTemplateQuery {
private static final String TEMPLATE_KEY = "template_key";
private static final String COMPONENTS_KEY = "components";
- private final String templateKey;
- private List<String> selectedComponents;
+ private final String templateUuid;
+ private List<String> componentKeys;
- private ApplyPermissionTemplateQuery(@Nullable String templateKey) {
- this.templateKey = templateKey;
+ private ApplyPermissionTemplateQuery(String templateUuid, List<String> componentKeys) {
+ this.templateUuid = templateUuid;
+ this.componentKeys = componentKeys;
+ validate();
}
- public static ApplyPermissionTemplateQuery buildFromParams(Map<String, Object> params) {
- ApplyPermissionTemplateQuery query = new ApplyPermissionTemplateQuery((String)params.get(TEMPLATE_KEY));
- query.setSelectedComponents(RubyUtils.toStrings(params.get(COMPONENTS_KEY)));
- return query;
+ public static ApplyPermissionTemplateQuery createFromMap(Map<String, Object> params) {
+ String templateUuid = (String) params.get(TEMPLATE_KEY);
+ List<String> componentKeys = RubyUtils.toStrings(params.get(COMPONENTS_KEY));
+ return new ApplyPermissionTemplateQuery(templateUuid, componentKeys);
}
- public String getTemplateKey() {
- return templateKey;
+ public static ApplyPermissionTemplateQuery create(String templateUuid, List<String> componentKeys) {
+ return new ApplyPermissionTemplateQuery(templateUuid, componentKeys);
}
- public List<String> getSelectedComponents() {
- return selectedComponents;
+ public String getTemplateUuid() {
+ return templateUuid;
}
- public void validate() {
- if(StringUtils.isBlank(templateKey)) {
+ public List<String> getComponentKeys() {
+ return componentKeys;
+ }
+
+ private void validate() {
+ if (templateUuid == null || WHITESPACE.trimFrom(templateUuid).isEmpty()) {
throw new BadRequestException("Permission template is mandatory");
}
- if(selectedComponents == null || selectedComponents.isEmpty()) {
- throw new BadRequestException("Please provide at least one entry to which the permission template should be applied");
+ if (componentKeys == null || componentKeys.isEmpty()) {
+ throw new BadRequestException("No project provided. Please provide at least one project.");
}
}
-
- private void setSelectedComponents(List<String> selectedComponents) {
- this.selectedComponents = selectedComponents;
- }
}
import org.sonar.server.issue.index.IssueAuthorizationIndexer;
import org.sonar.server.user.UserSession;
+import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
+import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdminUserByComponentKey;
+
/**
* Used by ruby code <pre>Internal.permissions</pre>
*/
indexProjectPermissions();
}
+ /**
+ * @deprecated since 5.2 – to be deleted once Permission Template page does not rely on Ruby
+ */
+ @Deprecated
public void applyPermissionTemplate(Map<String, Object> params) {
- userSession.checkLoggedIn();
- ApplyPermissionTemplateQuery query = ApplyPermissionTemplateQuery.buildFromParams(params);
+ ApplyPermissionTemplateQuery query = ApplyPermissionTemplateQuery.createFromMap(params);
applyPermissionTemplate(query);
}
- void applyPermissionTemplate(ApplyPermissionTemplateQuery query) {
- query.validate();
-
- boolean projectsChanged = false;
- DbSession session = dbClient.openSession(false);
+ public void applyPermissionTemplate(ApplyPermissionTemplateQuery query) {
+ DbSession dbSession = dbClient.openSession(false);
try {
- // If only one project is selected, check user has admin permission on it, otherwise we are in the case of a bulk change and only
- // system
- // admin has permission to do it
- if (query.getSelectedComponents().size() == 1) {
- checkProjectAdminPermission(query.getSelectedComponents().get(0));
+ if (query.getComponentKeys().size() == 1) {
+ checkProjectAdminUserByComponentKey(userSession, query.getComponentKeys().get(0));
} else {
- checkProjectAdminPermission(null);
- userSession.checkGlobalPermission(GlobalPermissions.SYSTEM_ADMIN);
+ checkGlobalAdminUser(userSession);
}
- for (String componentKey : query.getSelectedComponents()) {
- ComponentDto component = componentFinder.getByKey(session, componentKey);
- permissionRepository.applyPermissionTemplate(session, query.getTemplateKey(), component.getId());
- projectsChanged = true;
+ for (String componentKey : query.getComponentKeys()) {
+ ComponentDto component = componentFinder.getByKey(dbSession, componentKey);
+ permissionRepository.applyPermissionTemplate(dbSession, query.getTemplateUuid(), component.getId());
}
- session.commit();
+ dbSession.commit();
} finally {
- session.close();
- }
- if (projectsChanged) {
- indexProjectPermissions();
+ dbClient.closeSession(dbSession);
}
+
+ indexProjectPermissions();
}
private void applyChange(Operation operation, PermissionChange change, DbSession session) {
import static org.sonar.server.permission.ws.Parameters.createGroupIdParameter;
import static org.sonar.server.permission.ws.Parameters.createGroupNameParameter;
import static org.sonar.server.permission.ws.Parameters.createPermissionParameter;
-import static org.sonar.server.permission.ws.Parameters.createProjectKeyParameter;
-import static org.sonar.server.permission.ws.Parameters.createProjectUuidParameter;
+import static org.sonar.server.permission.ws.Parameters.createProjectParameter;
public class AddGroupAction implements PermissionsWsAction {
createPermissionParameter(action);
createGroupNameParameter(action);
createGroupIdParameter(action);
- createProjectUuidParameter(action);
- createProjectKeyParameter(action);
+ createProjectParameter(action);
}
@Override
import static org.sonar.server.permission.ws.PermissionRequestValidator.validateNotAnyoneAndAdminPermission;
import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPermission;
import static org.sonar.server.permission.ws.Parameters.PARAM_PERMISSION;
-import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_ID;
+import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_ID_EXPLICIT;
import static org.sonar.server.permission.ws.Parameters.createGroupIdParameter;
import static org.sonar.server.permission.ws.Parameters.createGroupNameParameter;
import static org.sonar.server.permission.ws.Parameters.createProjectPermissionParameter;
-import static org.sonar.server.permission.ws.Parameters.createTemplateKeyParameterToAddRemoveUserOrGroup;
+import static org.sonar.server.permission.ws.Parameters.createExplicitTemplateId;
public class AddGroupToTemplateAction implements PermissionsWsAction {
private final DbClient dbClient;
"It requires administration permissions to access.")
.setHandler(this);
- createTemplateKeyParameterToAddRemoveUserOrGroup(action);
+ createExplicitTemplateId(action);
createProjectPermissionParameter(action);
createGroupIdParameter(action);
createGroupNameParameter(action);
public void handle(Request wsRequest, Response wsResponse) throws Exception {
checkGlobalAdminUser(userSession);
- String templateKey = wsRequest.mandatoryParam(PARAM_LONG_TEMPLATE_ID);
+ String templateKey = wsRequest.mandatoryParam(PARAM_TEMPLATE_ID_EXPLICIT);
String permission = wsRequest.mandatoryParam(PARAM_PERMISSION);
WsGroupRef group = WsGroupRef.fromRequest(wsRequest);
import org.sonar.server.permission.ws.PermissionRequest.Builder;
import static org.sonar.server.permission.ws.Parameters.createPermissionParameter;
-import static org.sonar.server.permission.ws.Parameters.createProjectKeyParameter;
-import static org.sonar.server.permission.ws.Parameters.createProjectUuidParameter;
+import static org.sonar.server.permission.ws.Parameters.createProjectParameter;
import static org.sonar.server.permission.ws.Parameters.createUserLoginParameter;
public class AddUserAction implements PermissionsWsAction {
createPermissionParameter(action);
createUserLoginParameter(action);
- createProjectUuidParameter(action);
- createProjectKeyParameter(action);
+ createProjectParameter(action);
}
@Override
import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPermission;
import static org.sonar.server.permission.ws.Parameters.PARAM_PERMISSION;
-import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_ID;
+import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_ID_EXPLICIT;
import static org.sonar.server.permission.ws.Parameters.PARAM_USER_LOGIN;
import static org.sonar.server.permission.ws.Parameters.createProjectPermissionParameter;
-import static org.sonar.server.permission.ws.Parameters.createTemplateKeyParameterToAddRemoveUserOrGroup;
+import static org.sonar.server.permission.ws.Parameters.createExplicitTemplateId;
import static org.sonar.server.permission.ws.Parameters.createUserLoginParameter;
public class AddUserToTemplateAction implements PermissionsWsAction {
"It requires administration permissions to access.")
.setHandler(this);
- createTemplateKeyParameterToAddRemoveUserOrGroup(action);
+ createExplicitTemplateId(action);
createProjectPermissionParameter(action);
createUserLoginParameter(action);
}
public void handle(Request wsRequest, Response wsResponse) throws Exception {
checkGlobalAdminUser(userSession);
- String templateKey = wsRequest.mandatoryParam(PARAM_LONG_TEMPLATE_ID);
+ String templateKey = wsRequest.mandatoryParam(PARAM_TEMPLATE_ID_EXPLICIT);
String permission = wsRequest.mandatoryParam(PARAM_PERMISSION);
final String userLogin = wsRequest.mandatoryParam(PARAM_USER_LOGIN);
import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdminUserByComponentDto;
import static org.sonar.server.permission.PermissionQueryParser.toMembership;
import static org.sonar.server.permission.ws.Parameters.createPermissionParameter;
-import static org.sonar.server.permission.ws.Parameters.createProjectKeyParameter;
-import static org.sonar.server.permission.ws.Parameters.createProjectUuidParameter;
+import static org.sonar.server.permission.ws.Parameters.createProjectParameter;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
public class GroupsAction implements PermissionsWsAction {
.setHandler(this);
createPermissionParameter(action);
- createProjectUuidParameter(action);
- createProjectKeyParameter(action);
+ createProjectParameter(action);
}
@Override
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.core.permission.ProjectPermissions;
+import static java.lang.String.format;
+
class Parameters {
static final String PARAM_PERMISSION = "permission";
static final String PARAM_GROUP_NAME = "groupName";
static final String PARAM_GROUP_ID = "groupId";
- static final String PARAM_PROJECT_UUID = "projectId";
+ static final String PARAM_PROJECT_ID = "projectId";
static final String PARAM_PROJECT_KEY = "projectKey";
static final String PARAM_USER_LOGIN = "login";
- static final String PARAM_LONG_TEMPLATE_ID = "templateId";
+ static final String PARAM_TEMPLATE_ID_EXPLICIT = "templateId";
static final String PARAM_TEMPLATE_ID = "id";
static final String PARAM_TEMPLATE_NAME = "name";
static final String PARAM_TEMPLATE_DESCRIPTION = "description";
static final String PARAM_TEMPLATE_PATTERN = "projectKeyPattern";
- private static final String PERMISSION_PARAM_DESCRIPTION = String.format("Permission" +
+ private static final String PERMISSION_PARAM_DESCRIPTION = format("Permission" +
"<ul>" +
"<li>Possible values for global permissions: %s</li>" +
"<li>Possible values for project permissions %s</li>" +
"</ul>",
GlobalPermissions.ALL_ON_ONE_LINE,
ProjectPermissions.ALL_ON_ONE_LINE);
- private static final String PROJECT_PERMISSION_PARAM_DESCRIPTION = String.format("Permission" +
+ private static final String PROJECT_PERMISSION_PARAM_DESCRIPTION = format("Permission" +
"<ul>" +
"<li>Possible values for project permissions %s</li>" +
"</ul>",
.setExampleValue("42");
}
- static void createProjectUuidParameter(NewAction action) {
- action.createParam(PARAM_PROJECT_UUID)
+ static void createProjectParameter(NewAction action) {
+ createProjectIdParameter(action);
+ createProjectKeyParameter(action);
+ }
+
+ private static void createProjectIdParameter(NewAction action) {
+ action.createParam(PARAM_PROJECT_ID)
.setDescription("Project id")
.setExampleValue("ce4c03d6-430f-40a9-b777-ad877c00aa4d");
}
- static void createProjectKeyParameter(NewAction action) {
+ private static void createProjectKeyParameter(NewAction action) {
action.createParam(PARAM_PROJECT_KEY)
.setDescription("Project key")
.setExampleValue("org.apache.hbas:hbase");
.setExampleValue("g.hopper");
}
- static void createTemplateKeyParameterToAddRemoveUserOrGroup(NewAction action) {
- action.createParam(PARAM_LONG_TEMPLATE_ID)
+ static void createExplicitTemplateId(NewAction action) {
+ action.createParam(PARAM_TEMPLATE_ID_EXPLICIT)
.setRequired(true)
- .setDescription("Template key")
- .setExampleValue("developer_template_20150820_170218");
+ .setDescription("Template id")
+ .setExampleValue("617f3501-3d32-41bd-a7ac-18315d2e3c7a");
}
static void createTemplateProjectKeyPatternParameter(NewAction action) {
import com.google.common.base.Optional;
import javax.annotation.CheckForNull;
+import org.sonar.api.server.ws.Request;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
import static org.sonar.api.security.DefaultGroups.ANYONE;
import static org.sonar.api.security.DefaultGroups.isAnyone;
import static org.sonar.server.ws.WsUtils.checkFound;
+import static org.sonar.server.ws.WsUtils.checkRequest;
public class PermissionDependenciesFinder {
private final DbClient dbClient;
return Optional.of(componentFinder.getProjectByUuidOrKey(dbSession, wsProjectRef.uuid(), wsProjectRef.key()));
}
+ public ComponentDto getProject(DbSession dbSession, Request wsRequest) {
+ Optional<WsProjectRef> projectRef = WsProjectRef.optionalFromRequest(wsRequest);
+ checkRequest(projectRef.isPresent(), "The project id or the project key must be provided.");
+
+ return componentFinder.getProjectByUuidOrKey(dbSession, projectRef.get().uuid(), projectRef.get().key());
+ }
+
String getGroupName(DbSession dbSession, PermissionRequest request) {
GroupDto group = getGroup(dbSession, request.group());
}
/**
- *
+ *
* @return null if it's the anyone group
*/
@CheckForNull
}
private void setProject(Request request) {
- this.project = WsProjectRef.fromRequest(request);
+ this.project = WsProjectRef.optionalFromRequest(request);
}
private void checkPermissionParameter() {
import org.sonar.core.permission.ProjectPermissions;
import org.sonar.server.exceptions.BadRequestException;
-import static com.google.common.base.CharMatcher.WHITESPACE;
import static com.google.common.base.Strings.isNullOrEmpty;
import static java.lang.String.format;
+import static org.apache.commons.lang.StringUtils.isBlank;
import static org.sonar.api.security.DefaultGroups.isAnyone;
import static org.sonar.server.permission.ws.Parameters.PARAM_PERMISSION;
import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_PATTERN;
}
public static void validateTemplateNameFormat(String name) {
- String nameWithoutWhitespaces = WHITESPACE.trimFrom(name);
- checkRequest(!nameWithoutWhitespaces.isEmpty(), MSG_TEMPLATE_NAME_NOT_BLANK);
+ checkRequest(!isBlank(name), MSG_TEMPLATE_NAME_NOT_BLANK);
}
public static void validateProjectPattern(@Nullable String projectPattern) {
import static org.sonar.server.permission.ws.Parameters.createGroupIdParameter;
import static org.sonar.server.permission.ws.Parameters.createGroupNameParameter;
import static org.sonar.server.permission.ws.Parameters.createPermissionParameter;
-import static org.sonar.server.permission.ws.Parameters.createProjectKeyParameter;
-import static org.sonar.server.permission.ws.Parameters.createProjectUuidParameter;
+import static org.sonar.server.permission.ws.Parameters.createProjectParameter;
public class RemoveGroupAction implements PermissionsWsAction {
createPermissionParameter(action);
createGroupNameParameter(action);
createGroupIdParameter(action);
- createProjectUuidParameter(action);
- createProjectKeyParameter(action);
+ createProjectParameter(action);
}
@Override
import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPermission;
import static org.sonar.server.permission.ws.Parameters.PARAM_PERMISSION;
-import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_ID;
+import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_ID_EXPLICIT;
import static org.sonar.server.permission.ws.Parameters.createGroupIdParameter;
import static org.sonar.server.permission.ws.Parameters.createGroupNameParameter;
import static org.sonar.server.permission.ws.Parameters.createProjectPermissionParameter;
-import static org.sonar.server.permission.ws.Parameters.createTemplateKeyParameterToAddRemoveUserOrGroup;
+import static org.sonar.server.permission.ws.Parameters.createExplicitTemplateId;
public class RemoveGroupFromTemplateAction implements PermissionsWsAction {
private final DbClient dbClient;
"It requires administration permissions to access.")
.setHandler(this);
- createTemplateKeyParameterToAddRemoveUserOrGroup(action);
+ createExplicitTemplateId(action);
createProjectPermissionParameter(action);
createGroupIdParameter(action);
createGroupNameParameter(action);
public void handle(Request wsRequest, Response wsResponse) throws Exception {
checkGlobalAdminUser(userSession);
- String templateKey = wsRequest.mandatoryParam(PARAM_LONG_TEMPLATE_ID);
+ String templateKey = wsRequest.mandatoryParam(PARAM_TEMPLATE_ID_EXPLICIT);
String permission = wsRequest.mandatoryParam(PARAM_PERMISSION);
WsGroupRef group = WsGroupRef.fromRequest(wsRequest);
import org.sonar.server.permission.ws.PermissionRequest.Builder;
import static org.sonar.server.permission.ws.Parameters.createPermissionParameter;
-import static org.sonar.server.permission.ws.Parameters.createProjectKeyParameter;
-import static org.sonar.server.permission.ws.Parameters.createProjectUuidParameter;
+import static org.sonar.server.permission.ws.Parameters.createProjectParameter;
import static org.sonar.server.permission.ws.Parameters.createUserLoginParameter;
public class RemoveUserAction implements PermissionsWsAction {
createPermissionParameter(action);
createUserLoginParameter(action);
- createProjectUuidParameter(action);
- createProjectKeyParameter(action);
+ createProjectParameter(action);
}
@Override
import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPermission;
import static org.sonar.server.permission.ws.Parameters.PARAM_PERMISSION;
-import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_ID;
+import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_ID_EXPLICIT;
import static org.sonar.server.permission.ws.Parameters.PARAM_USER_LOGIN;
import static org.sonar.server.permission.ws.Parameters.createProjectPermissionParameter;
-import static org.sonar.server.permission.ws.Parameters.createTemplateKeyParameterToAddRemoveUserOrGroup;
+import static org.sonar.server.permission.ws.Parameters.createExplicitTemplateId;
import static org.sonar.server.permission.ws.Parameters.createUserLoginParameter;
public class RemoveUserFromTemplateAction implements PermissionsWsAction {
"It requires administration permissions to access.")
.setHandler(this);
- createTemplateKeyParameterToAddRemoveUserOrGroup(action);
+ createExplicitTemplateId(action);
createProjectPermissionParameter(action);
createUserLoginParameter(action);
}
public void handle(Request wsRequest, Response wsResponse) throws Exception {
checkGlobalAdminUser(userSession);
- String templateKey = wsRequest.mandatoryParam(PARAM_LONG_TEMPLATE_ID);
+ String templateKey = wsRequest.mandatoryParam(PARAM_TEMPLATE_ID_EXPLICIT);
String permission = wsRequest.mandatoryParam(PARAM_PERMISSION);
String userLogin = wsRequest.mandatoryParam(PARAM_USER_LOGIN);
import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdminUserByComponentKey;
import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdminUserByComponentUuid;
-import static org.sonar.server.permission.ws.Parameters.createProjectKeyParameter;
-import static org.sonar.server.permission.ws.Parameters.createProjectUuidParameter;
+import static org.sonar.server.permission.ws.Parameters.createProjectParameter;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
public class SearchProjectPermissionsAction implements PermissionsWsAction {
.addSearchQuery("sonarq", "project names", "project keys")
.setHandler(this);
- createProjectUuidParameter(action);
- createProjectKeyParameter(action);
+ createProjectParameter(action);
}
@Override
}
private void checkRequestAndPermissions(Request wsRequest) {
- Optional<WsProjectRef> project = WsProjectRef.fromRequest(wsRequest);
+ Optional<WsProjectRef> project = WsProjectRef.optionalFromRequest(wsRequest);
boolean hasProject = project.isPresent();
boolean hasProjectUuid = hasProject && project.get().uuid() != null;
boolean hasProjectKey = hasProject && project.get().key() != null;
private List<ComponentDto> searchRootComponents(DbSession dbSession, Request wsRequest, Paging paging) {
String query = wsRequest.param(TEXT_QUERY);
- Optional<WsProjectRef> project = WsProjectRef.fromRequest(wsRequest);
+ Optional<WsProjectRef> project = WsProjectRef.optionalFromRequest(wsRequest);
if (project.isPresent()) {
return singletonList(componentFinder.getProjectByUuidOrKey(dbSession, project.get().uuid(), project.get().key()));
import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdminUserByComponentDto;
import static org.sonar.server.permission.PermissionQueryParser.toMembership;
import static org.sonar.server.permission.ws.Parameters.createPermissionParameter;
-import static org.sonar.server.permission.ws.Parameters.createProjectKeyParameter;
-import static org.sonar.server.permission.ws.Parameters.createProjectUuidParameter;
+import static org.sonar.server.permission.ws.Parameters.createProjectParameter;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
public class UsersAction implements PermissionsWsAction {
.setHandler(this);
createPermissionParameter(action);
- createProjectUuidParameter(action);
- createProjectKeyParameter(action);
+ createProjectParameter(action);
}
@Override
import com.google.common.base.Optional;
import javax.annotation.CheckForNull;
-import javax.annotation.Nullable;
import org.sonar.api.server.ws.Request;
+import static org.sonar.server.permission.ws.Parameters.PARAM_PROJECT_ID;
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.ws.WsUtils.checkRequest;
/**
private final String uuid;
private final String key;
- private WsProjectRef(@Nullable String uuid, @Nullable String key) {
+ private WsProjectRef(Request wsRequest) {
+ String uuid = wsRequest.param(PARAM_PROJECT_ID);
+ String key = wsRequest.param(PARAM_PROJECT_KEY);
checkRequest(uuid != null ^ key != null, "Project id or project key can be provided, not both.");
this.uuid = uuid;
this.key = key;
}
- static Optional<WsProjectRef> fromRequest(Request wsRequest) {
+ static Optional<WsProjectRef> optionalFromRequest(Request wsRequest) {
if (hasNoProjectParam(wsRequest)) {
return Optional.absent();
}
- return Optional.of(new WsProjectRef(
- wsRequest.param(PARAM_PROJECT_UUID),
- wsRequest.param(PARAM_PROJECT_KEY))
- );
+ return Optional.of(new WsProjectRef(wsRequest));
+ }
+
+ static WsProjectRef fromRequest(Request wsRequest) {
+ return new WsProjectRef(wsRequest);
}
@CheckForNull
}
private static boolean hasNoProjectParam(Request wsRequest) {
- return !wsRequest.hasParam(PARAM_PROJECT_UUID) && !wsRequest.hasParam(PARAM_PROJECT_KEY);
+ return !wsRequest.hasParam(PARAM_PROJECT_ID) && !wsRequest.hasParam(PARAM_PROJECT_KEY);
}
}
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
+import java.util.Collections;
+import java.util.Map;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.server.exceptions.BadRequestException;
-import java.util.Collections;
-import java.util.Map;
-
import static org.assertj.core.api.Assertions.assertThat;
public class ApplyPermissionTemplateQueryTest {
params.put("template_key", "my_template_key");
params.put("components", Lists.newArrayList("1", "2", "3"));
- ApplyPermissionTemplateQuery query = ApplyPermissionTemplateQuery.buildFromParams(params);
+ ApplyPermissionTemplateQuery query = ApplyPermissionTemplateQuery.createFromMap(params);
- assertThat(query.getTemplateKey()).isEqualTo("my_template_key");
- assertThat(query.getSelectedComponents()).containsOnly("1", "2", "3");
+ assertThat(query.getTemplateUuid()).isEqualTo("my_template_key");
+ assertThat(query.getComponentKeys()).containsOnly("1", "2", "3");
}
@Test
params.put("template_key", "");
params.put("components", Lists.newArrayList("1", "2", "3"));
- ApplyPermissionTemplateQuery query = ApplyPermissionTemplateQuery.buildFromParams(params);
- query.validate();
+ ApplyPermissionTemplateQuery.createFromMap(params);
}
@Test
public void should_invalidate_query_with_no_components() {
throwable.expect(BadRequestException.class);
- throwable.expectMessage("Please provide at least one entry to which the permission template should be applied");
+ throwable.expectMessage("No project provided. Please provide at least one project.");
Map<String, Object> params = Maps.newHashMap();
params.put("template_key", "my_template_key");
params.put("components", Collections.EMPTY_LIST);
- ApplyPermissionTemplateQuery query = ApplyPermissionTemplateQuery.buildFromParams(params);
- query.validate();
+ ApplyPermissionTemplateQuery.createFromMap(params);
}
}
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_PROJECT_ID;
import static org.sonar.server.permission.ws.PermissionsWs.ENDPOINT;
@Category(DbTests.class)
newRequest()
.setParam(PARAM_GROUP_NAME, "sonar-administrators")
- .setParam(PARAM_PROJECT_UUID, "project-uuid")
+ .setParam(PARAM_PROJECT_ID, "project-uuid")
.setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
.execute();
newRequest()
.setParam(PARAM_GROUP_NAME, "sonar-administrators")
- .setParam(PARAM_PROJECT_UUID, "unknown-project-uuid")
+ .setParam(PARAM_PROJECT_ID, "unknown-project-uuid")
.setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
.execute();
}
newRequest()
.setParam(PARAM_GROUP_NAME, "sonar-administrators")
- .setParam(PARAM_PROJECT_UUID, "file-uuid")
+ .setParam(PARAM_PROJECT_ID, "file-uuid")
.setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
.execute();
}
newRequest()
.setParam(PARAM_GROUP_NAME, "sonar-administrators")
.setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
- .setParam(PARAM_PROJECT_UUID, "project-uuid")
+ .setParam(PARAM_PROJECT_ID, "project-uuid")
.setParam(PARAM_PROJECT_KEY, "project-key")
.execute();
}
import static org.sonar.db.user.GroupMembershipQuery.IN;
import static org.sonar.db.user.GroupTesting.newGroupDto;
import static org.sonar.server.permission.ws.Parameters.PARAM_GROUP_ID;
-import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_ID;
+import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_ID_EXPLICIT;
import static org.sonar.server.permission.ws.Parameters.PARAM_PERMISSION;
@Category(DbTests.class)
@Test
public void add_with_group_id() {
ws.newRequest()
- .setParam(PARAM_LONG_TEMPLATE_ID, permissionTemplate.getKee())
+ .setParam(PARAM_TEMPLATE_ID_EXPLICIT, permissionTemplate.getKee())
.setParam(PARAM_PERMISSION, CODEVIEWER)
.setParam(PARAM_GROUP_ID, String.valueOf(group.getId()))
.execute();
request.setParam(Parameters.PARAM_GROUP_NAME, groupName);
}
if (templateKey != null) {
- request.setParam(PARAM_LONG_TEMPLATE_ID, templateKey);
+ request.setParam(PARAM_TEMPLATE_ID_EXPLICIT, templateKey);
}
if (permission != null) {
request.setParam(Parameters.PARAM_PERMISSION, permission);
import static org.sonar.server.permission.ws.AddUserAction.ACTION;
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_PROJECT_ID;
import static org.sonar.server.permission.ws.Parameters.PARAM_USER_LOGIN;
import static org.sonar.server.permission.ws.PermissionsWs.ENDPOINT;
ws.newPostRequest(ENDPOINT, ACTION)
.setParam(PARAM_USER_LOGIN, "ray.bradbury")
- .setParam(PARAM_PROJECT_UUID, "project-uuid")
+ .setParam(PARAM_PROJECT_ID, "project-uuid")
.setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
.execute();
ws.newPostRequest(ENDPOINT, ACTION)
.setParam(PARAM_USER_LOGIN, "ray.bradbury")
- .setParam(PARAM_PROJECT_UUID, "unknown-project-uuid")
+ .setParam(PARAM_PROJECT_ID, "unknown-project-uuid")
.setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
.execute();
}
ws.newPostRequest(ENDPOINT, ACTION)
.setParam(PARAM_USER_LOGIN, "ray.bradbury")
- .setParam(PARAM_PROJECT_UUID, "file-uuid")
+ .setParam(PARAM_PROJECT_ID, "file-uuid")
.setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
.execute();
}
ws.newPostRequest(ENDPOINT, ACTION)
.setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
.setParam(PARAM_USER_LOGIN, "ray.bradbury")
- .setParam(PARAM_PROJECT_UUID, "project-uuid")
+ .setParam(PARAM_PROJECT_ID, "project-uuid")
.setParam(PARAM_PROJECT_KEY, "project-key")
.execute();
}
request.setParam(Parameters.PARAM_USER_LOGIN, userLogin);
}
if (templateKey != null) {
- request.setParam(Parameters.PARAM_LONG_TEMPLATE_ID, templateKey);
+ request.setParam(Parameters.PARAM_TEMPLATE_ID_EXPLICIT, templateKey);
}
if (permission != null) {
request.setParam(Parameters.PARAM_PERMISSION, permission);
import static org.sonar.db.component.ComponentTesting.newProjectDto;
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_PROJECT_ID;
import static org.sonar.test.JsonAssert.assertJson;
@Category(DbTests.class)
String result = ws.newRequest()
.setParam(PARAM_PERMISSION, ISSUE_ADMIN)
- .setParam(PARAM_PROJECT_UUID, "project-uuid")
+ .setParam(PARAM_PROJECT_ID, "project-uuid")
.execute().getInput();
assertThat(result).contains("project-group-name")
@Test
public void fail_if_project_uuid_and_project_key_are_provided() {
expectedException.expect(BadRequestException.class);
- expectedException.expectMessage("Project id or project key can be provided, not both.");
dbClient.componentDao().insert(dbSession, newProjectDto("project-uuid").setKey("project-key"));
ws.newRequest()
.setParam(PARAM_PERMISSION, SCAN_EXECUTION)
- .setParam(PARAM_PROJECT_UUID, "project-uuid")
+ .setParam(PARAM_PROJECT_ID, "project-uuid")
.setParam(PARAM_PROJECT_KEY, "project-key")
.execute();
}
+++ /dev/null
-/*
- * 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.ws;
-
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.sonar.server.exceptions.BadRequestException;
-
-import static org.sonar.server.permission.ws.PermissionRequestValidator.MSG_TEMPLATE_NAME_NOT_BLANK;
-
-public class PermissionRequestValidatorTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
- @Test
- public void validate_template_name() {
- PermissionRequestValidator.validateTemplateNameFormat(" text \r\n");
- expectedException.expect(BadRequestException.class);
- expectedException.expectMessage(MSG_TEMPLATE_NAME_NOT_BLANK);
-
- PermissionRequestValidator.validateTemplateNameFormat(" \r\n");
- }
-}
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_PROJECT_ID;
import static org.sonar.server.permission.ws.RemoveGroupAction.ACTION;
@Category(DbTests.class)
newRequest()
.setParam(PARAM_GROUP_NAME, "sonar-administrators")
- .setParam(PARAM_PROJECT_UUID, "project-uuid")
+ .setParam(PARAM_PROJECT_ID, "project-uuid")
.setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
.execute();
newRequest()
.setParam(PARAM_GROUP_NAME, "sonar-administrators")
- .setParam(PARAM_PROJECT_UUID, "unknown-project-uuid")
+ .setParam(PARAM_PROJECT_ID, "unknown-project-uuid")
.setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
.execute();
}
newRequest()
.setParam(PARAM_GROUP_NAME, "sonar-administrators")
- .setParam(PARAM_PROJECT_UUID, "file-uuid")
+ .setParam(PARAM_PROJECT_ID, "file-uuid")
.setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
.execute();
}
newRequest()
.setParam(PARAM_GROUP_NAME, "sonar-administrators")
.setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
- .setParam(PARAM_PROJECT_UUID, "project-uuid")
+ .setParam(PARAM_PROJECT_ID, "project-uuid")
.setParam(PARAM_PROJECT_KEY, "project-key")
.execute();
}
import static org.sonar.db.user.GroupTesting.newGroupDto;
import static org.sonar.server.permission.ws.Parameters.PARAM_GROUP_ID;
import static org.sonar.server.permission.ws.Parameters.PARAM_PERMISSION;
-import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_ID;
+import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_ID_EXPLICIT;
@Category(DbTests.class)
public class RemoveGroupFromTemplateActionTest {
commit();
ws.newRequest()
- .setParam(PARAM_LONG_TEMPLATE_ID, permissionTemplate.getKee())
+ .setParam(PARAM_TEMPLATE_ID_EXPLICIT, permissionTemplate.getKee())
.setParam(PARAM_PERMISSION, PERMISSION)
.setParam(PARAM_GROUP_ID, String.valueOf(group.getId()))
.execute();
request.setParam(Parameters.PARAM_GROUP_NAME, groupName);
}
if (templateKey != null) {
- request.setParam(PARAM_LONG_TEMPLATE_ID, templateKey);
+ request.setParam(PARAM_TEMPLATE_ID_EXPLICIT, templateKey);
}
if (permission != null) {
request.setParam(Parameters.PARAM_PERMISSION, permission);
import static org.sonar.db.component.ComponentTesting.newProjectDto;
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_PROJECT_ID;
import static org.sonar.server.permission.ws.Parameters.PARAM_USER_LOGIN;
import static org.sonar.server.permission.ws.PermissionsWs.ENDPOINT;
import static org.sonar.server.permission.ws.RemoveUserAction.ACTION;
ws.newPostRequest(PermissionsWs.ENDPOINT, ACTION)
.setParam(PARAM_USER_LOGIN, "ray.bradbury")
- .setParam(PARAM_PROJECT_UUID, "project-uuid")
+ .setParam(PARAM_PROJECT_ID, "project-uuid")
.setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
.execute();
ws.newPostRequest(PermissionsWs.ENDPOINT, ACTION)
.setParam(PARAM_USER_LOGIN, "ray.bradbury")
- .setParam(PARAM_PROJECT_UUID, "unknown-project-uuid")
+ .setParam(PARAM_PROJECT_ID, "unknown-project-uuid")
.setParam(PARAM_PERMISSION, UserRole.ISSUE_ADMIN)
.execute();
}
ws.newPostRequest(PermissionsWs.ENDPOINT, ACTION)
.setParam(PARAM_USER_LOGIN, "ray.bradbury")
- .setParam(PARAM_PROJECT_UUID, "file-uuid")
+ .setParam(PARAM_PROJECT_ID, "file-uuid")
.setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
.execute();
}
ws.newPostRequest(ENDPOINT, ACTION)
.setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
.setParam(PARAM_USER_LOGIN, "ray.bradbury")
- .setParam(PARAM_PROJECT_UUID, "project-uuid")
+ .setParam(PARAM_PROJECT_ID, "project-uuid")
.setParam(PARAM_PROJECT_KEY, "project-key")
.execute();
}
request.setParam(Parameters.PARAM_USER_LOGIN, userLogin);
}
if (templateKey != null) {
- request.setParam(Parameters.PARAM_LONG_TEMPLATE_ID, templateKey);
+ request.setParam(Parameters.PARAM_TEMPLATE_ID_EXPLICIT, templateKey);
}
if (permission != null) {
request.setParam(Parameters.PARAM_PERMISSION, permission);
import static org.sonar.db.component.ComponentTesting.newView;
import static org.sonar.db.user.GroupTesting.newGroupDto;
import static org.sonar.db.user.UserTesting.newUserDto;
-import static org.sonar.server.permission.ws.Parameters.PARAM_PROJECT_UUID;
+import static org.sonar.server.permission.ws.Parameters.PARAM_PROJECT_ID;
import static org.sonar.test.JsonAssert.assertJson;
public class SearchProjectPermissionsActionTest {
commit();
String result = ws.newRequest()
- .setParam(PARAM_PROJECT_UUID, "project-uuid")
+ .setParam(PARAM_PROJECT_ID, "project-uuid")
.execute().getInput();
assertThat(result).contains("project-uuid");
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.user.GroupDto;
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.ForbiddenException;
return dbClient.permissionTemplateDao().insert(dbSession, template);
}
+ private GroupDto insertGroup(GroupDto group) {
+ return dbClient.groupDao().insert(db.getSession(), group);
+ }
+
private void commit() {
dbSession.commit();
}
import static org.sonar.db.user.UserTesting.newUserDto;
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_PROJECT_ID;
import static org.sonar.test.JsonAssert.assertJson;
@Category(DbTests.class)
String result = ws.newRequest()
.setParam(PARAM_PERMISSION, ISSUE_ADMIN)
- .setParam(PARAM_PROJECT_UUID, "project-uuid")
+ .setParam(PARAM_PROJECT_ID, "project-uuid")
.execute().getInput();
assertThat(result).contains("project-user-login")
ws.newRequest()
.setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
- .setParam(PARAM_PROJECT_UUID, "project-uuid")
+ .setParam(PARAM_PROJECT_ID, "project-uuid")
.setParam(PARAM_PROJECT_KEY, "project-key")
.execute();
}
}
}
- public void applyPermissionTemplate(DbSession session, String templateKey, Long resourceId) {
- PermissionTemplateDto permissionTemplate = dbClient.permissionTemplateDao().selectPermissionTemplateWithPermissions(session, templateKey);
+ public void applyPermissionTemplate(DbSession session, String templateUuid, long resourceId) {
+ PermissionTemplateDto permissionTemplate = dbClient.permissionTemplateDao().selectPermissionTemplateWithPermissions(session, templateUuid);
updateProjectAuthorizationDate(session, resourceId);
dbClient.roleDao().removeAllPermissions(session, resourceId);
List<PermissionTemplateUserDto> usersPermissions = permissionTemplate.getUsersPermissions();
+ //TODO should return an empty list if there's no user permissions
if (usersPermissions != null) {
for (PermissionTemplateUserDto userPermission : usersPermissions) {
insertUserPermission(resourceId, userPermission.getUserId(), userPermission.getPermission(), false, session);
}
}
List<PermissionTemplateGroupDto> groupsPermissions = permissionTemplate.getGroupsPermissions();
+ //TODO should return an empty list if there's no group permission
if (groupsPermissions != null) {
for (PermissionTemplateGroupDto groupPermission : groupsPermissions) {
Long groupId = groupPermission.getGroupId() == null ? null : groupPermission.getGroupId();
}
}
- public void grantDefaultRoles(DbSession session, Long componentId, String qualifier) {
+ public void grantDefaultRoles(DbSession session, long componentId, String qualifier) {
ResourceDto resource = dbClient.resourceDao().selectResource(componentId, session);
String applicablePermissionTemplateKey = getApplicablePermissionTemplateKey(session, resource.getKey(), qualifier);
applyPermissionTemplate(session, applicablePermissionTemplateKey, componentId);
return kee;
}
+ /**
+ * @since 5.2 the kee column is a proper uuid. Before that it was build on the name + timestamp
+ */
+ public String getUuid() {
+ return kee;
+ }
+
public PermissionTemplateDto setKee(String kee) {
this.kee = kee;
return this;
}
+ /**
+ * @since 5.2 the kee column is a proper uuid. Before it was build on the name + timestamp
+ */
+ public PermissionTemplateDto setUuid(String uuid) {
+ this.kee = uuid;
+ return this;
+ }
+
@CheckForNull
public String getDescription() {
return description;