Skip to content

Commit

Permalink
Add additional validation for view configuration options
Browse files Browse the repository at this point in the history
  • Loading branch information
svenschoenung committed Jul 13, 2018
1 parent a8e8589 commit 4b19aac
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/main/java/io/jenkins/plugins/view/calendar/CalendarView.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ public class CalendarView extends ListView {
private static final String FORMAT_DATE = "yyyy-MM-dd";
private static final String FORMAT_ISO8601 = "yyyy-MM-dd'T'HH:mm:ss";

private static final List<String> SLOT_DURATIONS = Collections.unmodifiableList(Arrays.asList(
"00:05:00",
"00:10:00",
"00:15:00",
"00:20:00",
"00:30:00",
"01:00:00"
));

public static enum CalendarViewType {
MONTH, WEEK, DAY;
Expand Down Expand Up @@ -421,6 +429,17 @@ public boolean isAutomaticRefreshEnabled() {
@Override
protected void submit(StaplerRequest req) throws ServletException, Descriptor.FormException, IOException {
super.submit(req);

validateRange(req, "weekSettingsFirstDay", 0, 7);

validateInList(req, "weekSlotDuration", SLOT_DURATIONS);
validatePattern(req, "weekMinTime", "(0[0-9]|1[0-9]|2[0-4]):00:00");
validatePattern(req, "weekMaxTime", "(0[0-9]|1[0-9]|2[0-4]):00:00");

validateInList(req, "daySlotDuration", SLOT_DURATIONS);
validatePattern(req, "dayMinTime", "(0[0-9]|1[0-9]|2[0-4]):00:00");
validatePattern(req, "dayMaxTime", "(0[0-9]|1[0-9]|2[0-4]):00:00");

setCalendarViewType(CalendarViewType.valueOf(req.getParameter("calendarViewType")));

setUseCustomFormats(req.getParameter("useCustomFormats") != null);
Expand Down Expand Up @@ -453,6 +472,26 @@ protected void submit(StaplerRequest req) throws ServletException, Descriptor.Fo
setDayMaxTime(req.getParameter("dayMaxTime"));
}

private void validateInList(StaplerRequest req, String formField, List<String> possibleValues) throws Descriptor.FormException {
if (!possibleValues.contains(req.getParameter(formField))) {
throw new Descriptor.FormException(formField + " must be one of " + possibleValues, formField);
}
}

private void validatePattern(StaplerRequest req, String formField, String pattern) throws Descriptor.FormException {
String value = req.getParameter(formField);
if (value == null || !value.matches(pattern)) {
throw new Descriptor.FormException(formField + " must match " + pattern, formField);
}
}

private void validateRange(StaplerRequest req, String formField, int min, int max) throws Descriptor.FormException {
int value = Integer.parseInt(req.getParameter("weekSettingsFirstDay"));
if (value < min || value > max) {
throw new Descriptor.FormException(formField + " must be: " + min + " <= " + formField + " <= " + max, formField);
}
}

public List<Event> getEvents() throws ParseException {
Calendar start = getCalendarFromRequestParameter("start");
Calendar end = getCalendarFromRequestParameter("end");
Expand Down

0 comments on commit 4b19aac

Please sign in to comment.