-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathCourseDataFilterAttribute.cs
More file actions
62 lines (56 loc) · 2.34 KB
/
CourseDataFilterAttribute.cs
File metadata and controls
62 lines (56 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using HwProj.CoursesService.API.Models;
using HwProj.Models;
using HwProj.Models.CoursesService;
using HwProj.Models.CoursesService.ViewModels;
using HwProj.Utils.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
namespace HwProj.CoursesService.API.Filters
{
public class CourseDataFilterAttribute : ResultFilterAttribute
{
public override async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
{
var isGuest = context.HttpContext.Request.GetGuestModeFromHeader();
var userId = context.HttpContext.Request.GetUserIdFromHeader();
if (userId == null && isGuest != "true")
{
context.Result = new ForbidResult();
}
else if (isGuest == null)
{
var result = context.Result as ObjectResult;
if (result?.Value is CourseDTO courseDto && !courseDto.MentorIds.Contains(userId))
{
var currentDate = DateTime.UtcNow;
var isCourseStudent = false;
courseDto.CourseMates = courseDto.CourseMates
.Where(t =>
{
if (!t.IsAccepted) return t.StudentId == userId;
if (t.StudentId == userId) isCourseStudent = true;
return true;
})
.ToArray();
courseDto.Homeworks = courseDto.Homeworks
.Where(h =>
currentDate >= h.PublicationDate &&
(isCourseStudent || !h.Tags.Contains(HomeworkTags.Test)))
.ToArray();
foreach (var homework in courseDto.Homeworks)
{
homework.Tasks =
new List<HomeworkTaskViewModel>(homework.Tasks.Where(t =>
currentDate >= t.PublicationDate));
}
courseDto.Groups = courseDto.Groups.Where(g => g.StudentsIds.Contains(userId)).ToArray();
}
}
await next();
}
}
}