Add error checking on cron validation

This commit is contained in:
amcmanu3 2023-03-04 22:45:31 -05:00
parent c02472fb16
commit 1804d35156
5 changed files with 66 additions and 13 deletions

View File

@ -3,6 +3,7 @@
import json import json
import logging import logging
from croniter import croniter
from jsonschema import ValidationError, validate from jsonschema import ValidationError, validate
from app.classes.models.server_permissions import EnumPermissionsServer from app.classes.models.server_permissions import EnumPermissionsServer
from app.classes.web.base_api_handler import BaseApiHandler from app.classes.web.base_api_handler import BaseApiHandler
@ -89,6 +90,22 @@ class ApiServersServerTasksIndexHandler(BaseApiHandler):
data["server_id"] = server_id data["server_id"] = server_id
if not data.get("start_time"): if not data.get("start_time"):
data["start_time"] = "00:00" data["start_time"] = "00:00"
# validate cron string
if data["cron_string"] != "" and not croniter.is_valid(data["cron_string"]):
return self.finish_json(
405,
{
"status": "error",
"error": self.helper.translation.translate(
"error",
"cronFormat",
self.controller.users.get_user_lang_by_id(
auth_data[4]["user_id"]
),
),
},
)
task_id = self.tasks_manager.schedule_job(data) task_id = self.tasks_manager.schedule_job(data)
self.controller.management.add_to_audit_log( self.controller.management.add_to_audit_log(

View File

@ -3,6 +3,7 @@
import json import json
import logging import logging
from croniter import croniter
from jsonschema import ValidationError, validate from jsonschema import ValidationError, validate
from app.classes.models.server_permissions import EnumPermissionsServer from app.classes.models.server_permissions import EnumPermissionsServer
@ -135,6 +136,20 @@ class ApiServersServerTasksTaskIndexHandler(BaseApiHandler):
data["parent"] = None data["parent"] = None
data["server_id"] = server_id data["server_id"] = server_id
if data["cron_string"] != "" and not croniter.is_valid(data["cron_string"]):
return self.finish_json(
405,
{
"status": "error",
"error": self.helper.translation.translate(
"error",
"cronFormat",
self.controller.users.get_user_lang_by_id(
auth_data[4]["user_id"]
),
),
},
)
self.tasks_manager.update_job(task_id, data) self.tasks_manager.update_job(task_id, data)
self.controller.management.add_to_audit_log( self.controller.management.add_to_audit_log(

View File

@ -622,9 +622,15 @@
}, },
body: formDataJsonString, body: formDataJsonString,
}); });
let responseData = await res; let responseData = await res.json();
if (responseData.statusText === "OK") { if (responseData.status === "ok") {
window.location.reload(); window.location.reload();
} else {
bootbox.alert({
title: responseData.status,
message: responseData.error
});
} }
}); });
}); });

View File

@ -278,13 +278,20 @@
let res = await fetch(`/api/v2/servers/${serverId}/tasks/`, { let res = await fetch(`/api/v2/servers/${serverId}/tasks/`, {
method: 'POST', method: 'POST',
headers: { headers: {
'X-XSRFToken': token 'X-XSRFToken': token,
"Content-Type": "application/json",
}, },
body: formDataJsonString, body: formDataJsonString,
}); });
let responseData = await res; let responseData = await res.json();
if (responseData.statusText === "OK") { if (responseData.status === "ok") {
window.location.href = `/panel/server_detail?id=${serverId}&subpage=schedules`; window.location.href = `/panel/server_detail?id=${serverId}&subpage=schedules`;
} else {
bootbox.alert({
title: responseData.status,
message: responseData.error
});
} }
}); });
@ -315,13 +322,20 @@
let res = await fetch(`/api/v2/servers/${serverId}/tasks/${schId}`, { let res = await fetch(`/api/v2/servers/${serverId}/tasks/${schId}`, {
method: 'PATCH', method: 'PATCH',
headers: { headers: {
'X-XSRFToken': token 'X-XSRFToken': token,
"Content-Type": "application/json",
}, },
body: formDataJsonString, body: formDataJsonString,
}); });
let responseData = await res; let responseData = await res.json();
if (responseData.statusText === "OK") { if (responseData.status === "ok") {
window.location.href = `/panel/server_detail?id=${serverId}&subpage=schedules`; window.location.href = `/panel/server_detail?id=${serverId}&subpage=schedules`;
} else {
bootbox.alert({
title: responseData.status,
message: responseData.error
});
} }
}); });

View File

@ -186,7 +186,8 @@
"terribleFailure": "What a Terrible Failure!", "terribleFailure": "What a Terrible Failure!",
"superError": "You must be a super user to complete this action.", "superError": "You must be a super user to complete this action.",
"fileError": "File type must be an image.", "fileError": "File type must be an image.",
"migration": "Crafty's main server storage is being mirgated to a new location. All server starts have been suspended during this time. Please wait while we finish this migration" "migration": "Crafty's main server storage is being mirgated to a new location. All server starts have been suspended during this time. Please wait while we finish this migration",
"cronFormat": "Invalid Cron format detected"
}, },
"footer": { "footer": {
"allRightsReserved": "All rights reserved", "allRightsReserved": "All rights reserved",