private async Task <IActionResult> ShowEditLabExecutionFormAsync(int?groupId, int?labId, AdminLabExecution labExecutionData = null) { // Retrieve by ID, if no object from a failed POST was passed if (groupId != null && labId != null) { labExecutionData = new AdminLabExecution { LabExecution = await _labExecutionService.GetLabExecutionAsync(groupId.Value, labId.Value, HttpContext.RequestAborted) }; if (labExecutionData.LabExecution == null) { AddStatusMessage(_localizer["ShowEditLabExecutionFormAsync:NotFound"], StatusMessageTypes.Error); return(await RenderLabExecutionListAsync()); } } if (labExecutionData?.LabExecution == null) { AddStatusMessage(_localizer["ShowEditLabExecutionFormAsync:MissingParameter"], StatusMessageTypes.Error); return(await RenderLabExecutionListAsync()); } return(await RenderAsync(ViewType.Edit, labExecutionData)); }
public async Task <IActionResult> CallLabServerAsync(int labId) { // Retrieve group ID var currentUser = await GetCurrentUserAsync(); if (currentUser?.GroupId == null) { AddStatusMessage(_localizer["CallLabServerAsync:NoGroup"], StatusMessageTypes.Error); return(await RenderViewAsync()); } // Retrieve lab data var lab = await _labService.GetLabAsync(labId, HttpContext.RequestAborted); if (lab == null) { AddStatusMessage(_localizer["CallLabServerAsync:LabNotFound"], StatusMessageTypes.Error); return(await RenderViewAsync()); } // Check whether lab is accessible by given group DateTime now = DateTime.Now; var labExecution = await _labExecutionService.GetLabExecutionAsync(currentUser.GroupId.Value, labId, HttpContext.RequestAborted); if (labExecution == null || now < labExecution.PreStart) { AddStatusMessage(_localizer["CallLabServerAsync:LabNotActive"], StatusMessageTypes.Error); return(await RenderViewAsync()); } // Build authentication string var authData = new UserLoginRequest { UserId = currentUser.Id, UserDisplayName = currentUser.DisplayName, GroupId = currentUser.GroupId, GroupName = currentUser.Group?.DisplayName, AdminMode = false }; string authString = new CryptoService(lab.ApiCode).Encrypt(authData.Serialize()); // Build final URL string url = lab.ServerBaseUrl.TrimEnd().TrimEnd('/') + "/auth/login?code=" + authString; // Forward to server return(Redirect(url)); }
public async Task <IActionResult> CreateGroupExerciseSubmissionAsync(CtfApiRequest request) { try { // Resolve lab var lab = await _labService.GetLabAsync(request.LabId, HttpContext.RequestAborted); if (lab == null) { return(BadRequest(new { error = $"Could not resolve requested lab {request.LabId}" })); } // Decode request var apiExerciseSubmission = request.Decode <ApiGroupExerciseSubmission>(new CryptoService(lab.ApiCode)); // Resolve exercise var exercise = await _exerciseService.FindExerciseAsync(lab.Id, apiExerciseSubmission.ExerciseNumber, HttpContext.RequestAborted); if (exercise == null) { return(NotFound(new { error = "Exercise not found" })); } // Check lab execution // This will also automatically check whether the given group exists var labExecution = await _labExecutionService.GetLabExecutionAsync(apiExerciseSubmission.GroupId, lab.Id, HttpContext.RequestAborted); var now = DateTime.Now; if (labExecution == null || now < labExecution.PreStart) { return(NotFound(new { error = "Lab is not active for this group" })); } // Some exercises may only be submitted after the pre-start phase has ended if (!exercise.IsPreStartAvailable && now < labExecution.Start) { return(NotFound(new { error = "This exercise may not be submitted in the pre-start phase" })); } // Create submission for each group member var groupMembers = await _userService.GetGroupMembersAsync(apiExerciseSubmission.GroupId).ToListAsync(HttpContext.RequestAborted); foreach (var groupMember in groupMembers) { var submission = new ExerciseSubmission { ExerciseId = exercise.Id, UserId = groupMember.Id, ExercisePassed = apiExerciseSubmission.ExercisePassed, SubmissionTime = apiExerciseSubmission.SubmissionTime ?? DateTime.Now, Weight = apiExerciseSubmission.ExercisePassed ? 1 : (apiExerciseSubmission.Weight >= 0 ? apiExerciseSubmission.Weight : 1) }; await _exerciseService.CreateExerciseSubmissionAsync(submission, HttpContext.RequestAborted); // If the exercise is not passed with weight > 0, do only insert it for a single group member // Else, the penalty points would be applied for each group member if (!apiExerciseSubmission.ExercisePassed && apiExerciseSubmission.Weight > 0) { break; } } return(Ok()); } catch (CryptographicException ex) { _logger.LogError(ex, "Create exercise submission for group"); return(StatusCode(StatusCodes.Status401Unauthorized, new { error = "Could not decode the request packet" })); } catch (Exception ex) { _logger.LogError(ex, "Create exercise submission for group"); return(StatusCode(StatusCodes.Status500InternalServerError, new { error = "An internal error occured during processing of the request" })); } }