package org.sonar.server.permission.ws.template;
import java.util.List;
+import java.util.Optional;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.server.user.UserSession;
import org.sonarqube.ws.client.permission.AddUserToTemplateWsRequest;
-import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
+import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdmin;
import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createProjectPermissionParameter;
import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateParameters;
import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createUserLoginParameter;
import static org.sonar.server.permission.ws.template.WsTemplateRef.newTemplateRef;
+import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ORGANIZATION_KEY;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
@Override
public void handle(Request request, Response response) throws Exception {
- checkGlobalAdminUser(userSession);
doHandle(toAddUserToTemplateWsRequest(request));
response.noContent();
}
String userLogin = request.getLogin();
try (DbSession dbSession = dbClient.openSession(false)) {
- PermissionTemplateDto template = wsSupport.findTemplate(dbSession, newTemplateRef(request.getTemplateId(), request.getTemplateName()));
+ PermissionTemplateDto template = wsSupport.findTemplate(dbSession, newTemplateRef(
+ request.getTemplateId(), request.getOrganization(), request.getTemplateName()));
+ checkProjectAdmin(userSession, template.getOrganizationUuid(), Optional.empty());
+
UserId user = wsSupport.findUser(dbSession, userLogin);
if (!isUserAlreadyAdded(dbSession, template.getId(), userLogin, permission)) {
.setLogin(request.mandatoryParam(PARAM_USER_LOGIN))
.setPermission(request.mandatoryParam(PARAM_PERMISSION))
.setTemplateId(request.param(PARAM_TEMPLATE_ID))
+ .setOrganization(request.param(PARAM_ORGANIZATION_KEY))
.setTemplateName(request.param(PARAM_TEMPLATE_NAME));
}
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.exceptions.UnauthorizedException;
import org.sonar.server.permission.ws.BasePermissionWsTest;
import org.sonar.server.ws.WsTester;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.api.web.UserRole.CODEVIEWER;
import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
+import static org.sonar.core.permission.GlobalPermissions.QUALITY_PROFILE_ADMIN;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.CONTROLLER;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
@Before
public void setUp() {
- loginAsAdmin();
-
user = db.users().insertUser("user-login");
permissionTemplate = insertTemplate();
}
@Test
public void add_user_to_template() throws Exception {
+ loginAsAdminOnDefaultOrganization();
+
newRequest(user.getLogin(), permissionTemplate.getUuid(), CODEVIEWER);
assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), CODEVIEWER)).containsExactly(user.getLogin());
@Test
public void add_user_to_template_by_name() throws Exception {
+ loginAsAdminOnDefaultOrganization();
+
wsTester.newPostRequest(CONTROLLER, ACTION)
.setParam(PARAM_USER_LOGIN, user.getLogin())
.setParam(PARAM_PERMISSION, CODEVIEWER)
@Test
public void does_not_add_a_user_twice() throws Exception {
+ loginAsAdminOnDefaultOrganization();
+
newRequest(user.getLogin(), permissionTemplate.getUuid(), ISSUE_ADMIN);
newRequest(user.getLogin(), permissionTemplate.getUuid(), ISSUE_ADMIN);
@Test
public void fail_if_not_a_project_permission() throws Exception {
+ loginAsAdminOnDefaultOrganization();
+
expectedException.expect(IllegalArgumentException.class);
newRequest(user.getLogin(), permissionTemplate.getUuid(), GlobalPermissions.PROVISIONING);
}
@Test
- public void fail_if_insufficient_privileges() throws Exception {
- expectedException.expect(ForbiddenException.class);
- userSession.setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
-
- newRequest(user.getLogin(), permissionTemplate.getUuid(), CODEVIEWER);
- }
+ public void fail_if_not_admin_of_default_organization() throws Exception {
+ userSession.login().addOrganizationPermission(db.getDefaultOrganization().getUuid(), QUALITY_PROFILE_ADMIN);
- @Test
- public void fail_if_not_logged_in() throws Exception {
- expectedException.expect(UnauthorizedException.class);
- userSession.anonymous();
+ expectedException.expect(ForbiddenException.class);
newRequest(user.getLogin(), permissionTemplate.getUuid(), CODEVIEWER);
}
@Test
public void fail_if_user_missing() throws Exception {
+ loginAsAdminOnDefaultOrganization();
+
expectedException.expect(IllegalArgumentException.class);
newRequest(null, permissionTemplate.getUuid(), CODEVIEWER);
@Test
public void fail_if_permission_missing() throws Exception {
+ loginAsAdminOnDefaultOrganization();
+
expectedException.expect(IllegalArgumentException.class);
newRequest(user.getLogin(), permissionTemplate.getUuid(), null);
@Test
public void fail_if_template_uuid_and_name_are_missing() throws Exception {
+ loginAsAdminOnDefaultOrganization();
+
expectedException.expect(BadRequestException.class);
newRequest(user.getLogin(), null, CODEVIEWER);
@Test
public void fail_if_user_does_not_exist() throws Exception {
+ loginAsAdminOnDefaultOrganization();
+
expectedException.expect(NotFoundException.class);
expectedException.expectMessage("User with login 'unknown-login' is not found");
@Test
public void fail_if_template_key_does_not_exist() throws Exception {
+ loginAsAdminOnDefaultOrganization();
+
expectedException.expect(NotFoundException.class);
expectedException.expectMessage("Permission template with id 'unknown-key' is not found");
return db.getDbClient().permissionTemplateDao()
.selectUserLoginsByQueryAndTemplate(db.getSession(), permissionQuery, templateId);
}
-
- private void loginAsAdmin() {
- userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
- }
}