-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathCourseDataFilterAttribute.cs
More file actions
46 lines (42 loc) · 1.56 KB
/
CourseDataFilterAttribute.cs
File metadata and controls
46 lines (42 loc) · 1.56 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using HwProj.Models;
using HwProj.Models.AuthService.DTO;
using HwProj.Models.CoursesService.ViewModels;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
namespace HwProj.APIGateway.API.Filters
{
public class CourseDataFilterAttribute : ResultFilterAttribute
{
public override async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
{
var userId = context.HttpContext.User.Claims
.FirstOrDefault(claim => claim.Type.ToString() == "_id")
?.Value;;
if (userId == null)
{
context.Result = new ForbidResult();
}
else
{
var result = context.Result as ObjectResult;
if (result?.Value is CourseViewModel courseViewModel &&
courseViewModel.Mentors.All(mentor => mentor.UserId != userId))
{
var currentDate = DateTimeUtils.GetMoscowNow();
foreach (var homework in courseViewModel.Homeworks)
{
homework.Tasks =
new List<HomeworkTaskViewModel>(homework.Tasks.Where(t =>
currentDate >= t.PublicationDate));
}
courseViewModel.NewStudents = Array.Empty<AccountDataDto>();
}
}
await next();
}
}
}