private final GroupIdOrAnyone groupId;
- public GroupPermissionChange(Operation operation, String permission, @Nullable ProjectRef projectRef,
+ public GroupPermissionChange(Operation operation, String permission, @Nullable ProjectId projectId,
GroupIdOrAnyone groupId) {
- super(operation, groupId.getOrganizationUuid(), permission, projectRef);
+ super(operation, groupId.getOrganizationUuid(), permission, projectId);
this.groupId = groupId;
}
private void checkAdminUsersExistOutsideTheRemovedGroup(DbSession dbSession, GroupPermissionChange change) {
if (GlobalPermissions.SYSTEM_ADMIN.equals(change.getPermission()) &&
- !change.getProjectRef().isPresent() &&
+ !change.getProjectId().isPresent() &&
// TODO support organizations
dbClient.roleDao().countUserPermissions(dbSession, change.getPermission(), change.getGroupIdOrAnyone().getId()) <= 0) {
throw new BadRequestException(String.format("Last group with '%s' permission. Permission cannot be removed.", GlobalPermissions.SYSTEM_ADMIN));
private final Operation operation;
private final String organizationUuid;
private final String permission;
- private final ProjectRef projectRef;
+ private final ProjectId projectId;
- public PermissionChange(Operation operation, String organizationUuid, String permission, @Nullable ProjectRef projectRef) {
+ public PermissionChange(Operation operation, String organizationUuid, String permission, @Nullable ProjectId projectId) {
this.operation = requireNonNull(operation);
this.organizationUuid = requireNonNull(organizationUuid);
this.permission = requireNonNull(permission);
- this.projectRef = projectRef;
- if (projectRef == null) {
+ this.projectId = projectId;
+ if (projectId == null) {
checkRequest(GlobalPermissions.ALL.contains(permission), "Invalid global permission '%s'. Valid values are %s", permission, GlobalPermissions.ALL);
} else {
checkRequest(ProjectPermissions.ALL.contains(permission), "Invalid project permission '%s'. Valid values are %s", permission, ProjectPermissions.ALL);
return permission;
}
- public Optional<ProjectRef> getProjectRef() {
- return Optional.ofNullable(projectRef);
+ public Optional<ProjectId> getProjectId() {
+ return Optional.ofNullable(projectId);
}
/**
- * Shortcut based on {@link #getProjectRef()}
+ * Shortcut based on {@link #getProjectId()}
*/
@CheckForNull
public String getProjectUuid() {
- return projectRef == null ? null : projectRef.getUuid();
+ return projectId == null ? null : projectId.getUuid();
}
/**
- * Shortcut based on {@link #getProjectRef()}
+ * Shortcut based on {@link #getProjectId()}
*/
@CheckForNull
public Long getNullableProjectId() {
- return projectRef == null ? null : projectRef.getId();
+ return projectId == null ? null : projectId.getId();
}
}
Set<Long> projectIds = new HashSet<>();
for (PermissionChange change : changes) {
boolean changed = doApply(dbSession, change);
- Optional<ProjectRef> projectId = change.getProjectRef();
+ Optional<ProjectId> projectId = change.getProjectId();
if (changed && projectId.isPresent()) {
projectIds.add(projectId.get().getId());
}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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 javax.annotation.concurrent.Immutable;
+import org.sonar.db.component.ComponentDto;
+
+import static java.util.Objects.requireNonNull;
+
+/**
+ * Reference to a project by its db id or uuid. The field "id" should
+ * be removed as soon as backend is fully based on uuids.
+ *
+ * @see org.sonar.server.permission.ws.WsProjectRef
+ */
+@Immutable
+public class ProjectId {
+
+ private final long id;
+ private final String uuid;
+
+ public ProjectId(long projectId, String projectUuid) {
+ this.id = projectId;
+ this.uuid = requireNonNull(projectUuid);
+ }
+
+ public ProjectId(ComponentDto dto) {
+ this(requireNonNull(dto.getId()), dto.uuid());
+ }
+
+ public long getId() {
+ return id;
+ }
+
+ public String getUuid() {
+ return uuid;
+ }
+}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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 javax.annotation.concurrent.Immutable;
-import org.sonar.db.component.ComponentDto;
-
-import static java.util.Objects.requireNonNull;
-
-/**
- * Reference to a project by its db id or uuid. Temporarily
- * as long as permissions do not use only uuids.
- */
-@Immutable
-public class ProjectRef {
-
- private final long id;
- private final String uuid;
-
- public ProjectRef(long projectId, String projectUuid) {
- this.id = projectId;
- this.uuid = requireNonNull(projectUuid);
- }
-
- public ProjectRef(ComponentDto dto) {
- this(requireNonNull(dto.getId()), dto.uuid());
- }
-
- public long getId() {
- return id;
- }
-
- public String getUuid() {
- return uuid;
- }
-}
private final UserId userId;
- public UserPermissionChange(Operation operation, String organizationUuid, String permission, @Nullable ProjectRef projectRef,
+ public UserPermissionChange(Operation operation, String organizationUuid, String permission, @Nullable ProjectId projectId,
UserId userId) {
- super(operation, organizationUuid, permission, projectRef);
+ super(operation, organizationUuid, permission, projectId);
this.userId = requireNonNull(userId);
}
private void checkOtherAdminUsersExist(DbSession session, PermissionChange change) {
if (GlobalPermissions.SYSTEM_ADMIN.equals(change.getPermission()) &&
- !change.getProjectRef().isPresent() &&
+ !change.getProjectId().isPresent() &&
dbClient.roleDao().countUserPermissions(session, change.getPermission(), null) <= 1) {
throw new BadRequestException(String.format("Last user with '%s' permission. Permission cannot be removed.", GlobalPermissions.SYSTEM_ADMIN));
}
import org.sonar.server.permission.GroupPermissionChange;
import org.sonar.server.permission.PermissionChange;
import org.sonar.server.permission.PermissionUpdater;
-import org.sonar.server.permission.ProjectRef;
+import org.sonar.server.permission.ProjectId;
import org.sonar.server.usergroups.ws.GroupIdOrAnyone;
import static java.util.Arrays.asList;
public void handle(Request request, Response response) throws Exception {
try (DbSession dbSession = dbClient.openSession(false)) {
GroupIdOrAnyone group = support.findGroup(dbSession, request);
- Optional<ProjectRef> projectId = support.findProject(dbSession, request);
+ Optional<ProjectId> projectId = support.findProject(dbSession, request);
PermissionChange change = new GroupPermissionChange(
PermissionChange.Operation.ADD,
import org.sonar.db.organization.OrganizationDto;
import org.sonar.server.permission.PermissionChange;
import org.sonar.server.permission.PermissionUpdater;
-import org.sonar.server.permission.ProjectRef;
+import org.sonar.server.permission.ProjectId;
import org.sonar.server.permission.UserId;
import org.sonar.server.permission.UserPermissionChange;
public void handle(Request request, Response response) throws Exception {
try (DbSession dbSession = dbClient.openSession(false)) {
UserId user = support.findUser(dbSession, request.mandatoryParam(PARAM_USER_LOGIN));
- Optional<ProjectRef> projectId = support.findProject(dbSession, request);
+ Optional<ProjectId> projectId = support.findProject(dbSession, request);
OrganizationDto org = support.findOrganization(dbSession, request.param(PARAM_ORGANIZATION_KEY));
PermissionChange change = new UserPermissionChange(
PermissionChange.Operation.ADD,
import org.sonar.db.permission.GroupPermissionDto;
import org.sonar.db.permission.PermissionQuery;
import org.sonar.db.user.GroupDto;
-import org.sonar.server.permission.ProjectRef;
+import org.sonar.server.permission.ProjectId;
import org.sonar.server.user.UserSession;
import org.sonarqube.ws.WsPermissions.Group;
import org.sonarqube.ws.WsPermissions.WsGroupsResponse;
@Override
public void handle(Request request, Response response) throws Exception {
try (DbSession dbSession = dbClient.openSession(false)) {
- Optional<ProjectRef> projectId = support.findProject(dbSession, request);
+ Optional<ProjectId> projectId = support.findProject(dbSession, request);
checkProjectAdminUserByComponentUuid(userSession, projectId.isPresent() ? projectId.get().getUuid() : null);
PermissionQuery query = buildPermissionQuery(request, projectId);
}
}
- private static PermissionQuery buildPermissionQuery(Request request, Optional<ProjectRef> project) {
+ private static PermissionQuery buildPermissionQuery(Request request, Optional<ProjectId> project) {
String textQuery = request.param(Param.TEXT_QUERY);
PermissionQuery.Builder permissionQuery = PermissionQuery.builder()
.setPermission(request.param(PARAM_PERMISSION))
return Ordering.explicit(orderedNames).onResultOf(GroupDto::getName).immutableSortedCopy(groups);
}
- private List<GroupPermissionDto> findGroupPermissions(DbSession dbSession, List<GroupDto> groups, Optional<ProjectRef> project) {
+ private List<GroupPermissionDto> findGroupPermissions(DbSession dbSession, List<GroupDto> groups, Optional<ProjectId> project) {
if (groups.isEmpty()) {
return emptyList();
}
import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.permission.ProjectRef;
+import org.sonar.server.permission.ProjectId;
import org.sonar.server.permission.UserId;
import org.sonar.server.permission.ws.template.WsTemplateRef;
import org.sonar.server.usergroups.ws.GroupIdOrAnyone;
/**
* @throws org.sonar.server.exceptions.NotFoundException if a project does not exist
*/
- public ProjectRef findProject(DbSession dbSession, WsProjectRef ref) {
+ public ProjectId findProject(DbSession dbSession, WsProjectRef ref) {
ComponentDto project = componentFinder.getRootComponentOrModuleByUuidOrKey(dbSession, ref.uuid(), ref.key(), resourceTypes);
- return new ProjectRef(project.getId(), project.uuid());
+ return new ProjectId(project.getId(), project.uuid());
}
- public Optional<ProjectRef> findProject(DbSession dbSession, Request request) {
+ public Optional<ProjectId> findProject(DbSession dbSession, Request request) {
String uuid = request.param(PermissionsWsParameters.PARAM_PROJECT_ID);
String key = request.param(PermissionsWsParameters.PARAM_PROJECT_KEY);
if (uuid != null || key != null) {
import org.sonar.server.permission.GroupPermissionChange;
import org.sonar.server.permission.PermissionChange;
import org.sonar.server.permission.PermissionUpdater;
-import org.sonar.server.permission.ProjectRef;
+import org.sonar.server.permission.ProjectId;
import org.sonar.server.usergroups.ws.GroupIdOrAnyone;
import static java.util.Arrays.asList;
public void handle(Request request, Response response) throws Exception {
try (DbSession dbSession = dbClient.openSession(false)) {
GroupIdOrAnyone group = support.findGroup(dbSession, request);
- Optional<ProjectRef> projectId = support.findProject(dbSession, request);
+ Optional<ProjectId> projectId = support.findProject(dbSession, request);
PermissionChange change = new GroupPermissionChange(
PermissionChange.Operation.REMOVE,
import org.sonar.db.organization.OrganizationDto;
import org.sonar.server.permission.PermissionChange;
import org.sonar.server.permission.PermissionUpdater;
-import org.sonar.server.permission.ProjectRef;
+import org.sonar.server.permission.ProjectId;
import org.sonar.server.permission.UserId;
import org.sonar.server.permission.UserPermissionChange;
public void handle(Request request, Response response) throws Exception {
try (DbSession dbSession = dbClient.openSession(false)) {
UserId user = support.findUser(dbSession, request.mandatoryParam(PARAM_USER_LOGIN));
- Optional<ProjectRef> projectId = support.findProject(dbSession, request);
+ Optional<ProjectId> projectId = support.findProject(dbSession, request);
OrganizationDto org = support.findOrganization(dbSession, request.param(PARAM_ORGANIZATION_KEY));
PermissionChange change = new UserPermissionChange(
import org.sonar.db.permission.ExtendedUserPermissionDto;
import org.sonar.db.permission.PermissionQuery;
import org.sonar.db.user.UserDto;
-import org.sonar.server.permission.ProjectRef;
+import org.sonar.server.permission.ProjectId;
import org.sonar.server.user.UserSession;
import org.sonarqube.ws.WsPermissions;
import org.sonarqube.ws.WsPermissions.UsersWsResponse;
@Override
public void handle(Request request, Response response) throws Exception {
try (DbSession dbSession = dbClient.openSession(false)) {
- Optional<ProjectRef> projectId = support.findProject(dbSession, request);
+ Optional<ProjectId> projectId = support.findProject(dbSession, request);
checkProjectAdminUserByComponentUuid(userSession, projectId.isPresent() ? projectId.get().getUuid() : null);
PermissionQuery query = buildPermissionQuery(request, projectId);
}
}
- private static PermissionQuery buildPermissionQuery(Request request, Optional<ProjectRef> project) {
+ private static PermissionQuery buildPermissionQuery(Request request, Optional<ProjectId> project) {
String textQuery = request.param(Param.TEXT_QUERY);
String permission = request.param(PARAM_PERMISSION);
PermissionQuery.Builder permissionQuery = PermissionQuery.builder()
return Ordering.explicit(orderedLogins).onResultOf(UserDto::getLogin).immutableSortedCopy(dbClient.userDao().selectByLogins(dbSession, orderedLogins));
}
- private List<ExtendedUserPermissionDto> findUserPermissions(DbSession dbSession, List<UserDto> users, Optional<ProjectRef> project) {
+ private List<ExtendedUserPermissionDto> findUserPermissions(DbSession dbSession, List<UserDto> users, Optional<ProjectId> project) {
if (users.isEmpty()) {
return emptyList();
}
import org.sonar.server.permission.GroupPermissionChange;
import org.sonar.server.permission.PermissionChange;
import org.sonar.server.permission.PermissionUpdater;
-import org.sonar.server.permission.ProjectRef;
+import org.sonar.server.permission.ProjectId;
import org.sonar.server.tester.ServerTester;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.user.UserSession;
userSessionRule.login("admin").setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
// TODO correctly feed default organization. Not a problem as long as issues search does not support "anyone"
// for each organization
- GroupPermissionChange permissionChange = new GroupPermissionChange(PermissionChange.Operation.ADD, UserRole.USER, new ProjectRef(project), GroupIdOrAnyone.forAnyone("TODO"));
+ GroupPermissionChange permissionChange = new GroupPermissionChange(PermissionChange.Operation.ADD, UserRole.USER, new ProjectId(project), GroupIdOrAnyone.forAnyone("TODO"));
tester.get(PermissionUpdater.class).apply(session, asList(permissionChange));
userSession = userSessionRule.login("john")
import org.sonar.server.permission.GroupPermissionChange;
import org.sonar.server.permission.PermissionChange;
import org.sonar.server.permission.PermissionUpdater;
-import org.sonar.server.permission.ProjectRef;
+import org.sonar.server.permission.ProjectId;
import org.sonar.server.tester.ServerTester;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.usergroups.ws.GroupIdOrAnyone;
userSessionRule.login("admin").setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
// TODO correctly feed default organization. Not a problem as long as issues search does not support "anyone"
// for each organization
- GroupPermissionChange permissionChange = new GroupPermissionChange(PermissionChange.Operation.ADD, UserRole.USER, new ProjectRef(project), GroupIdOrAnyone.forAnyone("TODO"));
+ GroupPermissionChange permissionChange = new GroupPermissionChange(PermissionChange.Operation.ADD, UserRole.USER, new ProjectId(project), GroupIdOrAnyone.forAnyone("TODO"));
tester.get(PermissionUpdater.class).apply(session, asList(permissionChange));
userSessionRule.login("gandalf");
import org.sonar.server.permission.GroupPermissionChange;
import org.sonar.server.permission.PermissionChange;
import org.sonar.server.permission.PermissionUpdater;
-import org.sonar.server.permission.ProjectRef;
+import org.sonar.server.permission.ProjectId;
import org.sonar.server.rule.index.RuleIndexer;
import org.sonar.server.tester.ServerTester;
import org.sonar.server.tester.UserSessionRule;
// project can be seen by group "anyone"
// TODO correctly feed default organization. Not a problem as long as issues search does not support "anyone"
// for each organization
- GroupPermissionChange permissionChange = new GroupPermissionChange(PermissionChange.Operation.ADD, UserRole.USER, new ProjectRef(project), GroupIdOrAnyone.forAnyone("TODO"));
+ GroupPermissionChange permissionChange = new GroupPermissionChange(PermissionChange.Operation.ADD, UserRole.USER, new ProjectId(project), GroupIdOrAnyone.forAnyone("TODO"));
tester.get(PermissionUpdater.class).apply(session, asList(permissionChange));
userSessionRule.login();
import org.sonar.server.permission.GroupPermissionChange;
import org.sonar.server.permission.PermissionChange;
import org.sonar.server.permission.PermissionUpdater;
-import org.sonar.server.permission.ProjectRef;
+import org.sonar.server.permission.ProjectId;
import org.sonar.server.tester.ServerTester;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.usergroups.ws.GroupIdOrAnyone;
userSessionRule.login("admin").setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
// TODO correctly feed default organization. Not a problem as long as issues search does not support "anyone"
// for each organization
- GroupPermissionChange permissionChange = new GroupPermissionChange(PermissionChange.Operation.ADD, permission, new ProjectRef(project), GroupIdOrAnyone.forAnyone("TODO"));
+ GroupPermissionChange permissionChange = new GroupPermissionChange(PermissionChange.Operation.ADD, permission, new ProjectId(project), GroupIdOrAnyone.forAnyone("TODO"));
tester.get(PermissionUpdater.class).apply(session, asList(permissionChange));
}
import org.sonar.server.permission.GroupPermissionChange;
import org.sonar.server.permission.PermissionChange;
import org.sonar.server.permission.PermissionUpdater;
-import org.sonar.server.permission.ProjectRef;
+import org.sonar.server.permission.ProjectId;
import org.sonar.server.search.QueryContext;
import org.sonar.server.tester.ServerTester;
import org.sonar.server.tester.UserSessionRule;
userSessionRule.login("admin").setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
// TODO correctly feed default organization. Not a problem as long as issues search does not support "anyone"
// for each organization
- GroupPermissionChange permissionChange = new GroupPermissionChange(PermissionChange.Operation.ADD, UserRole.USER, new ProjectRef(project), GroupIdOrAnyone.forAnyone("TODO"));
+ GroupPermissionChange permissionChange = new GroupPermissionChange(PermissionChange.Operation.ADD, UserRole.USER, new ProjectId(project), GroupIdOrAnyone.forAnyone("TODO"));
tester.get(PermissionUpdater.class).apply(session, asList(permissionChange));
}
GroupIdOrAnyone groupId = new GroupIdOrAnyone(group);
loginAsAdmin();
- apply(new GroupPermissionChange(PermissionChange.Operation.ADD, UserRole.ISSUE_ADMIN, new ProjectRef(project), groupId));
+ apply(new GroupPermissionChange(PermissionChange.Operation.ADD, UserRole.ISSUE_ADMIN, new ProjectId(project), groupId));
assertThat(db.users().selectGroupPermissions(group, null)).isEmpty();
assertThat(db.users().selectGroupPermissions(group, project)).containsOnly(UserRole.ISSUE_ADMIN);
GroupIdOrAnyone groupId = new GroupIdOrAnyone(db.getDefaultOrganization().getUuid(), null);
loginAsAdmin();
- apply(new GroupPermissionChange(PermissionChange.Operation.ADD, UserRole.ISSUE_ADMIN, new ProjectRef(project), groupId));
+ apply(new GroupPermissionChange(PermissionChange.Operation.ADD, UserRole.ISSUE_ADMIN, new ProjectId(project), groupId));
assertThat(db.users().selectAnyonePermissions(null)).isEmpty();
assertThat(db.users().selectAnyonePermissions(project)).containsOnly(UserRole.ISSUE_ADMIN);
expectedException.expect(ForbiddenException.class);
userSession.login("a_guy");
- underTest.apply(db.getSession(), new GroupPermissionChange(PermissionChange.Operation.ADD, UserRole.ISSUE_ADMIN, new ProjectRef(project), groupId));
+ underTest.apply(db.getSession(), new GroupPermissionChange(PermissionChange.Operation.ADD, UserRole.ISSUE_ADMIN, new ProjectId(project), groupId));
}
@Test
expectedException.expectMessage("Invalid project permission 'gateadmin'. Valid values are [admin, codeviewer, issueadmin, scan, user]");
loginAsAdmin();
- apply(new GroupPermissionChange(PermissionChange.Operation.ADD, GlobalPermissions.QUALITY_GATE_ADMIN, new ProjectRef(project), groupId));
+ apply(new GroupPermissionChange(PermissionChange.Operation.ADD, GlobalPermissions.QUALITY_GATE_ADMIN, new ProjectId(project), groupId));
}
@Test
db.users().insertProjectPermissionOnGroup(group, UserRole.CODEVIEWER, project);
loginAsAdmin();
- apply(new GroupPermissionChange(PermissionChange.Operation.REMOVE, UserRole.ISSUE_ADMIN, new ProjectRef(project), groupId));
+ apply(new GroupPermissionChange(PermissionChange.Operation.REMOVE, UserRole.ISSUE_ADMIN, new ProjectId(project), groupId));
assertThat(db.users().selectGroupPermissions(group, null)).containsOnly(GlobalPermissions.QUALITY_GATE_ADMIN);
assertThat(db.users().selectGroupPermissions(group, project)).containsOnly(UserRole.CODEVIEWER);
GroupIdOrAnyone groupId = new GroupIdOrAnyone(group);
loginAsAdmin();
- apply(new GroupPermissionChange(PermissionChange.Operation.REMOVE, UserRole.ISSUE_ADMIN, new ProjectRef(project), groupId));
+ apply(new GroupPermissionChange(PermissionChange.Operation.REMOVE, UserRole.ISSUE_ADMIN, new ProjectId(project), groupId));
assertThat(db.users().selectGroupPermissions(group, null)).isEmpty();
assertThat(db.users().selectGroupPermissions(group, project)).isEmpty();
expectedException.expect(ForbiddenException.class);
userSession.login("a_guy");
- underTest.apply(db.getSession(), new GroupPermissionChange(PermissionChange.Operation.REMOVE, UserRole.ISSUE_ADMIN, new ProjectRef(project), groupId));
+ underTest.apply(db.getSession(), new GroupPermissionChange(PermissionChange.Operation.REMOVE, UserRole.ISSUE_ADMIN, new ProjectId(project), groupId));
}
@Test