import java.util.List;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
+import org.sonar.api.utils.log.Loggers;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.step.DataChange;
import org.sonar.server.platform.db.migration.step.Select;
checkState(rawProperties.defaultGroupId != null, "No default group id is defined for default organization (uuid=%s)", defaultOrganizationUuid);
checkState(rawProperties.projectUuid != null || rawProperties.viewUuid == null,
"Inconsistent state for default organization (uuid=%s): no project default template is defined but view default template is", defaultOrganizationUuid);
+ Integer projectTemplateId = getPermTemplateId(context, rawProperties.projectUuid);
+ Integer viewTemplateId = getViewTemplateIdOrClearReference(context, rawProperties.viewUuid, defaultOrganizationUuid);
return new ResolvedOrganizationProperties(
rawProperties.defaultGroupId,
- getPermTemplateId(context, rawProperties.projectUuid),
- getPermTemplateId(context, rawProperties.viewUuid));
+ projectTemplateId,
+ viewTemplateId);
}
@CheckForNull
if (permissionTemplateUuid == null) {
return null;
}
- List<Integer> ids = context.prepareSelect("select id from permission_templates where kee=?")
- .setString(1, permissionTemplateUuid)
- .list(row -> row.getInt(1));
+ List<Integer> ids = getTemplateIds(context, permissionTemplateUuid);
checkState(!ids.isEmpty(), "Permission template with uuid %s not found", permissionTemplateUuid);
checkState(ids.size() == 1, "Multiple permission templates found with uuid %s", permissionTemplateUuid);
return ids.iterator().next();
}
+ @CheckForNull
+ private static Integer getViewTemplateIdOrClearReference(Context context, @Nullable String permissionTemplateUuid,
+ String defaultOrganizationUuid) throws SQLException {
+ if (permissionTemplateUuid == null) {
+ return null;
+ }
+ List<Integer> ids = getTemplateIds(context, permissionTemplateUuid);
+ if (ids.isEmpty()) {
+ clearViewTemplateReference(context, defaultOrganizationUuid);
+ return null;
+ }
+ checkState(ids.size() == 1, "Multiple permission templates found with uuid %s", permissionTemplateUuid);
+ return ids.iterator().next();
+ }
+
+ private static void clearViewTemplateReference(Context context, String defaultOrganizationUuid) throws SQLException {
+ context.prepareUpsert("update organizations set default_perm_template_view = null where uuid=?")
+ .setString(1, defaultOrganizationUuid)
+ .execute()
+ .commit();
+ Loggers.get(SupportPrivateProjectInDefaultPermissionTemplate.class)
+ .info("Permission template with uuid %s referenced as default permission template for view does not exist. Reference cleared.");
+ }
+
+ private static List<Integer> getTemplateIds(Context context, @Nullable String permissionTemplateUuid) throws SQLException {
+ return context.prepareSelect("select id from permission_templates where kee=?")
+ .setString(1, permissionTemplateUuid)
+ .list(row -> row.getInt(1));
+ }
+
private static final class OrganizationProperties {
private final Integer defaultGroupId;
private final String projectUuid;
import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
import static org.apache.commons.lang.math.RandomUtils.nextLong;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.tuple;
public class SupportPrivateProjectInDefaultPermissionTemplateTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();
- private SupportPrivateProjectInDefaultPermissionTemplate underTest = new SupportPrivateProjectInDefaultPermissionTemplate(db.database(), new DefaultOrganizationUuidProviderImpl());
+ private SupportPrivateProjectInDefaultPermissionTemplate underTest = new SupportPrivateProjectInDefaultPermissionTemplate(db.database(),
+ new DefaultOrganizationUuidProviderImpl());
@Test
public void fails_with_ISE_when_no_default_organization_is_set() throws SQLException {
}
@Test
- public void execute_fails_with_ISE_when_default_permission_template_for_projects_of_default_organization_does_not_exist() throws SQLException {
+ public void execute_ignores_default_permission_template_for_view_of_default_organization_if_it_does_not_exist_and_removes_the_reference() throws SQLException {
int groupId = insertGroup(DEFAULT_ORGANIZATION_UUID);
- setupDefaultOrganization(groupId, "foBar2000", "pt2");
-
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Permission template with uuid foBar2000 not found");
+ IdAndUuid projectDefPermTemplate = insertPermissionTemplate(DEFAULT_ORGANIZATION_UUID);
+ setupDefaultOrganization(groupId, projectDefPermTemplate.uuid, "fooBar");
underTest.execute();
+
+ assertThat(
+ db.select("select default_perm_template_project as \"project\", default_perm_template_view as \"view\" from organizations where uuid='" + DEFAULT_ORGANIZATION_UUID + "'"))
+ .extracting((row) -> row.get("project"), (row) -> row.get("view"))
+ .containsOnly(tuple(projectDefPermTemplate.uuid, null));
}
@Test