import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.text.JsonWriter;
-import org.sonar.api.web.UserRole;
+import org.sonar.core.permission.GlobalPermissions;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.organization.OrganizationDto;
json.name("organization")
.beginObject()
.prop("isDefault", organization.getKey().equals(defaultOrganizationProvider.get().getKey()))
- .prop("canAdmin", userSession.hasOrganizationPermission(organization.getUuid(), UserRole.ADMIN))
+ .prop("canAdmin", userSession.hasOrganizationPermission(organization.getUuid(), GlobalPermissions.SYSTEM_ADMIN))
+ .prop("canProvisionProjects", userSession.hasOrganizationPermission(organization.getUuid(), GlobalPermissions.PROVISIONING))
.endObject();
}
}
@Test
public void verify_example() {
-
OrganizationDto defaultOrganization = dbTester.getDefaultOrganization();
- userSession.logIn().addOrganizationPermission(defaultOrganization.getUuid(), "admin");
+ userSession.logIn()
+ .addOrganizationPermission(defaultOrganization, "admin")
+ .addOrganizationPermission(defaultOrganization, "provisioning");
TestResponse response = executeRequest(defaultOrganization);
public void returns_non_admin_and_default_true_when_user_not_logged_in_and_key_is_the_default_organization() {
TestResponse response = executeRequest(dbTester.getDefaultOrganization());
- verifyResponse(response, true, false);
+ verifyResponse(response, true, false, false);
}
@Test
TestResponse response = executeRequest(dbTester.getDefaultOrganization());
- verifyResponse(response, true, false);
+ verifyResponse(response, true, false, false);
}
@Test
TestResponse response = executeRequest(defaultOrganization);
- verifyResponse(response, true, true);
+ verifyResponse(response, true, true, false);
}
@Test
OrganizationDto organization = dbTester.organizations().insert();
TestResponse response = executeRequest(organization);
- verifyResponse(response, false, false);
+ verifyResponse(response, false, false, false);
}
@Test
TestResponse response = executeRequest(organization);
- verifyResponse(response, false, false);
+ verifyResponse(response, false, false, false);
}
@Test
TestResponse response = executeRequest(organization);
- verifyResponse(response, false, false);
+ verifyResponse(response, false, false, false);
}
@Test
TestResponse response = executeRequest(organization);
- verifyResponse(response, false, true);
+ verifyResponse(response, false, true, false);
+ }
+
+ @Test
+ public void returns_provisioning_true_when_user_can_provision_projects_in_organization() {
+ // user can provision projects in org2 but not in org1
+ OrganizationDto org1 = dbTester.organizations().insert();
+ OrganizationDto org2 = dbTester.organizations().insert();
+ userSession.logIn().addOrganizationPermission(org2, "provisioning");
+
+ verifyResponse(executeRequest(org1), false, false, false);
+ verifyResponse(executeRequest(org2), false, false, true);
}
private TestResponse executeRequest(@Nullable OrganizationDto organization) {
return request.execute();
}
- private static void verifyResponse(TestResponse response, boolean isDefault, boolean canAdmin) {
+ private static void verifyResponse(TestResponse response, boolean isDefault, boolean canAdmin, boolean canProvisionProjects) {
assertJson(response.getInput())
.isSimilarTo("{" +
" \"organization\": {" +
" \"isDefault\": " + isDefault + "," +
- " \"canAdmin\": " + canAdmin +
+ " \"canAdmin\": " + canAdmin + "," +
+ " \"canProvisionProjects\": " + canProvisionProjects +
" }" +
"}");
}