Skip to content

Commit

Permalink
Merge pull request #166 from MathisBurger/feature/limitt-options
Browse files Browse the repository at this point in the history
Limit runner options for unverified groups
  • Loading branch information
MathisBurger authored Nov 20, 2024
2 parents 1341356 + 1ac97d0 commit 1e8ec9e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
30 changes: 24 additions & 6 deletions tasky/src/handler/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
error::ApiError,
models::{
assignment::{Assignment, AssignmentRepository},
group::Group,
DB,
},
mongo::test_file::{TestFile, TestFileCollection},
Expand Down Expand Up @@ -42,8 +43,9 @@ pub async fn handle_create_multipart(
mongodb: &Database,
db: &mut DB,
mut assignment: Assignment,
group: &Group,
) -> Result<Assignment, ApiError> {
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) {
Expand Down Expand Up @@ -87,8 +89,9 @@ pub async fn handle_update_multipart(
mongodb: &Database,
db: &mut DB,
mut assignment: Assignment,
group: &Group,
) -> Result<Assignment, ApiError> {
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) {
Expand Down Expand Up @@ -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(),
});
Expand Down
8 changes: 4 additions & 4 deletions tasky/src/routes/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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);
Expand All @@ -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(),
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -37,6 +35,8 @@ const AssignmentCreateOrUpdateCodeTestModal = ({
const [files, setFiles] = useState<FileWithPath[]>([]);
const { t } = useTranslation(["assignment", "common"]);

const {cpuOptions, memoryOptions, timeoutOptions} = useRunnerOptions(groupId);

useEffect(() => {
if (assignment.file_structure) {
setFileStructure(assignment.file_structure);
Expand Down
24 changes: 24 additions & 0 deletions web/hooks/useRunnerOptions.ts
Original file line number Diff line number Diff line change
@@ -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<boolean>(() => 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;

0 comments on commit 1e8ec9e

Please sign in to comment.