diff --git a/tasky/src/handler/assignment.rs b/tasky/src/handler/assignment.rs index e734afd..9202ade 100644 --- a/tasky/src/handler/assignment.rs +++ b/tasky/src/handler/assignment.rs @@ -9,6 +9,7 @@ use crate::{ error::ApiError, models::{ assignment::{Assignment, AssignmentRepository}, + group::Group, DB, }, mongo::test_file::{TestFile, TestFileCollection}, @@ -42,8 +43,9 @@ pub async fn handle_create_multipart( mongodb: &Database, db: &mut DB, mut assignment: Assignment, + group: &Group, ) -> Result { - validate_runner_config(&form.runner_config)?; + validate_runner_config(&form.runner_config, group)?; let mut file_structure = form.file_structure.0; if !file_structure_contains_files(&file_structure) { @@ -87,8 +89,9 @@ pub async fn handle_update_multipart( mongodb: &Database, db: &mut DB, mut assignment: Assignment, + group: &Group, ) -> Result { - validate_runner_config(&form.runner_config)?; + validate_runner_config(&form.runner_config, group)?; let mut new_file_structure = form.file_structure.0; if !file_structure_contains_files(&new_file_structure) { @@ -181,18 +184,33 @@ async fn create_files_and_update_ids( } /// Validates the runner config -fn validate_runner_config(config: &RunnerData) -> Result<(), ApiError> { - if ![".5", "1"].contains(&config.runner_cpu.as_str()) { +fn validate_runner_config(config: &RunnerData, group: &Group) -> Result<(), ApiError> { + let cpu_options = match group.verified { + true => vec![".5", "1"], + false => vec![".5"], + }; + + let memory_options = match group.verified { + true => vec!["50m", "100m", "200m", "300m", "500m"], + false => vec!["50m", "100m", "200m"], + }; + + let timeout_options = match group.verified { + true => vec!["20s", "60s", "120s", "180s", "240s", "300s"], + false => vec!["20s", "60s"], + }; + + if !cpu_options.contains(&config.runner_cpu.as_str()) { return Err(ApiError::BadRequest { message: "You entered an unallowed CPU value".to_string(), }); } - if !["50m", "100m", "200m", "300m", "500m"].contains(&config.runner_memory.as_str()) { + if !memory_options.contains(&config.runner_memory.as_str()) { return Err(ApiError::BadRequest { message: "You entered an unallowed memory value".to_string(), }); } - if !["20s", "60s", "120s", "180s", "240s", "300s"].contains(&config.runner_timeout.as_str()) { + if !timeout_options.contains(&config.runner_timeout.as_str()) { return Err(ApiError::BadRequest { message: "You entered an unallowed timeout value".to_string(), }); diff --git a/tasky/src/routes/assignment.rs b/tasky/src/routes/assignment.rs index 0b82b53..28576cf 100644 --- a/tasky/src/routes/assignment.rs +++ b/tasky/src/routes/assignment.rs @@ -183,7 +183,7 @@ pub async fn create_assignment_test( let path_data = path.into_inner(); let conn = &mut data.db.db.get().unwrap(); - let (_, mut assignment) = get_group_and_assignment(&user_data, path_data, conn)?; + let (group, mut assignment) = get_group_and_assignment(&user_data, path_data, conn)?; if !assignment.is_granted(SecurityAction::Update, &user_data) { return Err(ApiError::Forbidden { message: "You are not allowed to create code tests".to_string(), @@ -195,7 +195,7 @@ pub async fn create_assignment_test( message: "Cannot create code tests on question based assignment".to_string(), }); } - let updated = handle_create_multipart(form, &data.mongodb, conn, assignment).await?; + let updated = handle_create_multipart(form, &data.mongodb, conn, assignment, &group).await?; let mut enriched = AssignmentResponse::enrich(&updated, &mut data.user_api.clone(), conn).await?; enriched.authorize(&user_data); @@ -214,7 +214,7 @@ pub async fn update_assignment_test( let path_data = path.into_inner(); let conn = &mut data.db.db.get().unwrap(); - let (_, mut assignment) = get_group_and_assignment(&user_data, path_data, conn)?; + let (group, mut assignment) = get_group_and_assignment(&user_data, path_data, conn)?; if !assignment.is_granted(SecurityAction::Update, &user_data) { return Err(ApiError::Forbidden { message: "You are not allowed to create code tests".to_string(), @@ -226,7 +226,7 @@ pub async fn update_assignment_test( message: "Cannot create code tests on question based assignment".to_string(), }); } - let updated = handle_update_multipart(form, &data.mongodb, conn, assignment).await?; + let updated = handle_update_multipart(form, &data.mongodb, conn, assignment, &group).await?; let mut enriched = AssignmentResponse::enrich(&updated, &mut data.user_api.clone(), conn).await?; enriched.authorize(&user_data); diff --git a/web/components/assignments/AssignmentCreateOrUpdateCodeTestModal.tsx b/web/components/assignments/AssignmentCreateOrUpdateCodeTestModal.tsx index 58a723f..7e21662 100644 --- a/web/components/assignments/AssignmentCreateOrUpdateCodeTestModal.tsx +++ b/web/components/assignments/AssignmentCreateOrUpdateCodeTestModal.tsx @@ -11,6 +11,7 @@ import { useForm } from "@mantine/form"; import { Assignment, RunnerConfig } from "@/service/types/tasky"; import { useTranslation } from "react-i18next"; import { removeObjectIds } from "@/utils/FileStructure"; +import useRunnerOptions from "@/hooks/useRunnerOptions"; interface AssignmentCreateOrUpdateCodeTestModalProps { onClose: () => void; @@ -19,9 +20,6 @@ interface AssignmentCreateOrUpdateCodeTestModalProps { refetch: () => void; } -const cpuOptions = [".5", "1"]; -const memoryOptions = ["50m", "100m", "200m", "300m", "500m"]; -const timeoutOptions = ["20s", "60s", "120s", "180s", "240s", "300s"]; const AssignmentCreateOrUpdateCodeTestModal = ({ onClose, @@ -37,6 +35,8 @@ const AssignmentCreateOrUpdateCodeTestModal = ({ const [files, setFiles] = useState([]); const { t } = useTranslation(["assignment", "common"]); + const {cpuOptions, memoryOptions, timeoutOptions} = useRunnerOptions(groupId); + useEffect(() => { if (assignment.file_structure) { setFileStructure(assignment.file_structure); diff --git a/web/hooks/useRunnerOptions.ts b/web/hooks/useRunnerOptions.ts new file mode 100644 index 0000000..85d3470 --- /dev/null +++ b/web/hooks/useRunnerOptions.ts @@ -0,0 +1,24 @@ +import useApiServiceClient from "@/hooks/useApiServiceClient"; +import {useMemo} from "react"; +import useClientQuery from "@/hooks/useClientQuery"; + +/** + * Gets all runner options + * + * @param groupId The ID of the group + */ +const useRunnerOptions = (groupId: number) => { + + const api = useApiServiceClient(); + const [group] = useClientQuery(() => api.getGroup(groupId), [groupId]); + + const verified = useMemo(() => group?.verified ?? false, [group]); + + const cpuOptions = verified ? [".5", "1"] :[".5"]; + const memoryOptions = verified ? ["50m", "100m", "200m", "300m", "500m"] : ["50m", "100m", "200m"]; + const timeoutOptions = verified ? ["20s", "60s", "120s", "180s", "240s", "300s"] : ["20s", "60s"]; + + return {cpuOptions, memoryOptions, timeoutOptions}; +}; + +export default useRunnerOptions;