Compare commits
28 Commits
b2c22ab65c
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| c098e6430d | |||
| 3f30812d7a | |||
| bed42a83bf | |||
| 33814bb6f4 | |||
| daf3639038 | |||
| 8ba1aea46a | |||
| f7c8db4921 | |||
| 86ab9a6a42 | |||
| b868ee66e6 | |||
| aed3ab3e20 | |||
| 0021210724 | |||
| eb89b98326 | |||
| a86ffdf5e7 | |||
| 4ac3494833 | |||
| aa0e181222 | |||
| e34ef136ff | |||
| 45deadc037 | |||
| d726e28876 | |||
| fd6942960c | |||
| ad5576958f | |||
| e0fffbd4b5 | |||
| ec389bb596 | |||
| 498a183be5 | |||
| 496def0166 | |||
| 88dbbbee5d | |||
| a7f6d4de9f | |||
| e7526241d1 | |||
| 2ac561b46f |
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
[submodule "ModeusSchedule.MSAuth"]
|
||||
path = ModeusSchedule.MSAuth
|
||||
url = https://git.zetcraft.ru/serega404/ModeusSchedule.MSAuth
|
||||
14
Dockerfile
14
Dockerfile
@@ -2,7 +2,7 @@ FROM mcr.microsoft.com/dotnet/sdk:9.0-alpine3.22 AS build
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
WORKDIR /src
|
||||
COPY ./SfeduSchedule ./SfeduSchedule
|
||||
COPY ./SfeduSchedule.Abstractions ./SfeduSchedule.Abstractions
|
||||
COPY ./ModeusSchedule.Abstractions ./ModeusSchedule.Abstractions
|
||||
WORKDIR /src/SfeduSchedule
|
||||
RUN dotnet restore "SfeduSchedule.csproj"
|
||||
RUN dotnet publish "SfeduSchedule.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
|
||||
@@ -10,17 +10,5 @@ RUN dotnet publish "SfeduSchedule.csproj" -c $BUILD_CONFIGURATION -o /app/publis
|
||||
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS final
|
||||
EXPOSE 8080
|
||||
WORKDIR /app
|
||||
|
||||
RUN apt-get update && \
|
||||
apt-get install -y --no-install-recommends wget && \
|
||||
wget -q https://github.com/PowerShell/PowerShell/releases/download/v7.5.2/powershell_7.5.2-1.deb_amd64.deb && \
|
||||
apt-get install -y ./powershell_7.5.2-1.deb_amd64.deb && \
|
||||
rm -f powershell_7.5.2-1.deb_amd64.deb && \
|
||||
rm -rf /var/lib/apt/lists/*
|
||||
|
||||
ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright
|
||||
|
||||
COPY --from=build /app/publish .
|
||||
RUN pwsh ./playwright.ps1 install --with-deps chromium
|
||||
|
||||
ENTRYPOINT ["dotnet", "SfeduSchedule.dll"]
|
||||
51
ModeusSchedule.Abstractions/DTO/AttendeesDTO.cs
Normal file
51
ModeusSchedule.Abstractions/DTO/AttendeesDTO.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
#pragma warning disable CS8618
|
||||
#pragma warning disable CS8601
|
||||
#pragma warning disable CS8603
|
||||
|
||||
namespace ModeusSchedule.Abstractions.DTO;
|
||||
|
||||
public partial class Attendees
|
||||
{
|
||||
public static List<Attendees> FromJson(string json)
|
||||
{
|
||||
return JsonSerializer.Deserialize<List<Attendees>>(json);
|
||||
}
|
||||
}
|
||||
|
||||
public partial class Attendees
|
||||
{
|
||||
[JsonPropertyName("id")] public Guid Id { get; set; }
|
||||
|
||||
[JsonPropertyName("roleId")] public string RoleId { get; set; }
|
||||
|
||||
[JsonPropertyName("roleName")] public string RoleName { get; set; }
|
||||
|
||||
[JsonPropertyName("roleNamePlural")] public string RoleNamePlural { get; set; }
|
||||
|
||||
[JsonPropertyName("roleDisplayOrder")] public long RoleDisplayOrder { get; set; }
|
||||
|
||||
[JsonPropertyName("personId")] public Guid PersonId { get; set; }
|
||||
|
||||
[JsonPropertyName("lastName")] public string LastName { get; set; }
|
||||
|
||||
[JsonPropertyName("firstName")] public string FirstName { get; set; }
|
||||
|
||||
[JsonPropertyName("middleName")] public string MiddleName { get; set; }
|
||||
|
||||
[JsonPropertyName("fullName")] public string FullName { get; set; }
|
||||
|
||||
[JsonPropertyName("studentId")] public Guid? StudentId { get; set; }
|
||||
|
||||
[JsonPropertyName("specialtyCode")] public string SpecialtyCode { get; set; }
|
||||
|
||||
[JsonPropertyName("specialtyName")] public string SpecialtyName { get; set; }
|
||||
|
||||
[JsonPropertyName("specialtyProfile")] public string SpecialtyProfile { get; set; }
|
||||
}
|
||||
|
||||
#pragma warning restore CS8618
|
||||
#pragma warning restore CS8601
|
||||
#pragma warning restore CS8603
|
||||
@@ -1,9 +1,9 @@
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace SfeduSchedule.Abstractions;
|
||||
namespace ModeusSchedule.Abstractions.DTO;
|
||||
|
||||
/// <summary>
|
||||
/// DTO для запроса расписания в Modeus.
|
||||
/// DTO для запроса расписания в Modeus.
|
||||
/// </summary>
|
||||
public class ModeusScheduleRequest(
|
||||
int size,
|
||||
@@ -20,28 +20,28 @@ public class ModeusScheduleRequest(
|
||||
List<string>? typeId)
|
||||
{
|
||||
/// <summary>
|
||||
/// Количество элементов в ответе.
|
||||
/// Количество элементов в ответе.
|
||||
/// </summary>
|
||||
[DefaultValue(10)]
|
||||
public int Size { get; set; } = size;
|
||||
|
||||
/// <summary>
|
||||
/// Начальная дата и время.
|
||||
/// Начальная дата и время.
|
||||
/// </summary>
|
||||
public DateTime TimeMin { get; set; } = timeMin;
|
||||
|
||||
/// <summary>
|
||||
/// Конечная дата и время.
|
||||
/// Конечная дата и время.
|
||||
/// </summary>
|
||||
public DateTime TimeMax { get; set; } = timeMax;
|
||||
|
||||
/// <summary>
|
||||
/// Список идентификаторов аудиторий. (Guid)
|
||||
/// Список идентификаторов аудиторий. (Guid)
|
||||
/// </summary>
|
||||
public List<Guid>? RoomId { get; set; } = roomId;
|
||||
|
||||
/// <summary>
|
||||
/// Список идентификаторов участников.
|
||||
/// Список идентификаторов участников.
|
||||
/// </summary>
|
||||
public List<Guid>? AttendeePersonId { get; set; } = attendeePersonId;
|
||||
|
||||
@@ -49,66 +49,66 @@ public class ModeusScheduleRequest(
|
||||
public List<Guid>? CycleRealizationId { get; set; } = cycleRealizationId;
|
||||
|
||||
/// <summary>
|
||||
/// Список кодов специальностей.
|
||||
/// Список кодов специальностей.
|
||||
/// </summary>
|
||||
[DefaultValue(new string[] { "09.03.04" })]
|
||||
[DefaultValue(new[] { "09.03.04" })]
|
||||
public List<string>? SpecialtyCode { get; set; } = specialtyCode;
|
||||
|
||||
/// <summary>
|
||||
/// Список годов начала обучения.
|
||||
/// Список годов начала обучения.
|
||||
/// </summary>
|
||||
[DefaultValue(new int[] { 2022, 2023, 2024, 2025 })]
|
||||
[DefaultValue(new[] { 2022, 2023, 2024, 2025 })]
|
||||
public List<int>? LearningStartYear { get; set; } = learningStartYear;
|
||||
|
||||
/// <summary>
|
||||
/// Список названий профилей подготовки.
|
||||
/// Список названий профилей подготовки.
|
||||
/// </summary>
|
||||
[DefaultValue(new string[] { "Методы и средства разработки программного обеспечения" })]
|
||||
[DefaultValue(new[] { "Методы и средства разработки программного обеспечения" })]
|
||||
public List<string>? ProfileName { get; set; } = profileName;
|
||||
|
||||
/// <summary>
|
||||
/// Список идентификаторов учебных планов.
|
||||
/// Список идентификаторов учебных планов.
|
||||
/// </summary>
|
||||
public List<Guid>? CurriculumId { get; set; } = curriculumId;
|
||||
|
||||
/// <summary>
|
||||
/// Список типов мероприятий.
|
||||
/// Список типов мероприятий.
|
||||
/// </summary>
|
||||
[DefaultValue(new string[] { "MID_CHECK", "CONS", "LAB", "LECT", "SEMI", "EVENT_OTHER", "SELF", "CUR_CHECK" })]
|
||||
[DefaultValue(new[] { "MID_CHECK", "CONS", "LAB", "LECT", "SEMI", "EVENT_OTHER", "SELF", "CUR_CHECK" })]
|
||||
public List<string>? TypeId { get; set; } = typeId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// DTO для поиска аудиторий.
|
||||
/// DTO для поиска аудиторий.
|
||||
/// </summary>
|
||||
public class RoomSearchRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Название аудитории.
|
||||
/// Название аудитории.
|
||||
/// </summary>
|
||||
[DefaultValue("")]
|
||||
public string Name { get; set; } = "";
|
||||
|
||||
/// <summary>
|
||||
/// Сортировка.
|
||||
/// Сортировка.
|
||||
/// </summary>
|
||||
[DefaultValue("+building.name,+name")]
|
||||
public string Sort { get; set; } = "+building.name,+name";
|
||||
|
||||
/// <summary>
|
||||
/// Количество элементов в ответе.
|
||||
/// Количество элементов в ответе.
|
||||
/// </summary>
|
||||
[DefaultValue(10)]
|
||||
public int Size { get; set; } = 10;
|
||||
|
||||
/// <summary>
|
||||
/// Номер страницы. (пагинация)
|
||||
/// Номер страницы. (пагинация)
|
||||
/// </summary>
|
||||
[DefaultValue(0)]
|
||||
public int Page { get; set; } = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Исключать архивные аудитории. false = да, true = нет
|
||||
/// Исключать архивные аудитории. false = да, true = нет
|
||||
/// </summary>
|
||||
[DefaultValue(false)]
|
||||
public bool Deleted { get; set; } = false;
|
||||
591
ModeusSchedule.Abstractions/DTO/ScheduleDTO.CS
Normal file
591
ModeusSchedule.Abstractions/DTO/ScheduleDTO.CS
Normal file
@@ -0,0 +1,591 @@
|
||||
// <auto-generated />
|
||||
// Вот этим сайтом https://app.quicktype.io/?l=csharp
|
||||
// Не является точной копией ответа, могут быть отличия
|
||||
|
||||
#nullable enable
|
||||
#pragma warning disable CS8618
|
||||
#pragma warning disable CS8601
|
||||
#pragma warning disable CS8603
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Globalization;
|
||||
|
||||
namespace SfeduSchedule;
|
||||
|
||||
public partial class Schedule
|
||||
{
|
||||
[JsonPropertyName("_embedded")] public Embedded Embedded { get; set; }
|
||||
|
||||
[JsonPropertyName("page")] public Page Page { get; set; }
|
||||
}
|
||||
|
||||
public partial class Embedded
|
||||
{
|
||||
[JsonPropertyName("events")] public Event[] Events { get; set; }
|
||||
|
||||
[JsonPropertyName("course-unit-realizations")]
|
||||
public CourseUnitRealization[] CourseUnitRealizations { get; set; }
|
||||
|
||||
[JsonPropertyName("cycle-realizations")]
|
||||
public CycleRealization[] CycleRealizations { get; set; }
|
||||
|
||||
[JsonPropertyName("lesson-realization-teams")]
|
||||
public LessonRealizationTeam[] LessonRealizationTeams { get; set; }
|
||||
|
||||
[JsonPropertyName("lesson-realizations")]
|
||||
public LessonRealization[] LessonRealizations { get; set; }
|
||||
|
||||
[JsonPropertyName("event-locations")] public EventLocation[] EventLocations { get; set; }
|
||||
|
||||
[JsonPropertyName("durations")] public Duration[] Durations { get; set; }
|
||||
|
||||
[JsonPropertyName("event-rooms")] public EventRoom[] EventRooms { get; set; }
|
||||
|
||||
[JsonPropertyName("rooms")] public Room[] Rooms { get; set; }
|
||||
|
||||
[JsonPropertyName("buildings")] public BuildingElement[] Buildings { get; set; }
|
||||
|
||||
[JsonPropertyName("event-teams")] public EventTeam[] EventTeams { get; set; }
|
||||
|
||||
[JsonPropertyName("event-organizers")] public EventOrganizer[] EventOrganizers { get; set; }
|
||||
|
||||
[JsonPropertyName("event-attendees")] public EventAttendee[] EventAttendees { get; set; }
|
||||
|
||||
[JsonPropertyName("persons")] public Person[] Persons { get; set; }
|
||||
}
|
||||
|
||||
public partial class BuildingElement
|
||||
{
|
||||
[JsonPropertyName("name")] public string Name { get; set; }
|
||||
|
||||
[JsonPropertyName("nameShort")] public string NameShort { get; set; }
|
||||
|
||||
[JsonPropertyName("address")] public string Address { get; set; }
|
||||
|
||||
[JsonPropertyName("searchableAddress")]
|
||||
public string SearchableAddress { get; set; }
|
||||
|
||||
[JsonPropertyName("displayOrder")] public long DisplayOrder { get; set; }
|
||||
|
||||
[JsonPropertyName("_links")] public BuildingLinks Links { get; set; }
|
||||
|
||||
[JsonPropertyName("id")] public Guid Id { get; set; }
|
||||
}
|
||||
|
||||
public partial class BuildingLinks
|
||||
{
|
||||
[JsonPropertyName("self")] public Self Self { get; set; }
|
||||
}
|
||||
|
||||
public partial class Self
|
||||
{
|
||||
[JsonPropertyName("href")] public string Href { get; set; }
|
||||
}
|
||||
|
||||
public partial class CourseUnitRealization
|
||||
{
|
||||
[JsonPropertyName("name")] public string Name { get; set; }
|
||||
|
||||
[JsonPropertyName("nameShort")] public string NameShort { get; set; }
|
||||
|
||||
[JsonPropertyName("prototypeId")] public Guid PrototypeId { get; set; }
|
||||
|
||||
[JsonPropertyName("_links")] public CourseUnitRealizationLinks Links { get; set; }
|
||||
|
||||
[JsonPropertyName("id")] public Guid Id { get; set; }
|
||||
}
|
||||
|
||||
public partial class CourseUnitRealizationLinks
|
||||
{
|
||||
[JsonPropertyName("self")] public Self Self { get; set; }
|
||||
|
||||
[JsonPropertyName("planning-period")] public Self PlanningPeriod { get; set; }
|
||||
}
|
||||
|
||||
public partial class CycleRealization
|
||||
{
|
||||
[JsonPropertyName("name")] public string Name { get; set; }
|
||||
|
||||
[JsonPropertyName("nameShort")] public string NameShort { get; set; }
|
||||
|
||||
[JsonPropertyName("code")] public string Code { get; set; }
|
||||
|
||||
[JsonPropertyName("courseUnitRealizationNameShort")]
|
||||
public string CourseUnitRealizationNameShort { get; set; }
|
||||
|
||||
[JsonPropertyName("_links")] public CycleRealizationLinks Links { get; set; }
|
||||
|
||||
[JsonPropertyName("id")] public Guid Id { get; set; }
|
||||
}
|
||||
|
||||
public partial class CycleRealizationLinks
|
||||
{
|
||||
[JsonPropertyName("self")] public Self Self { get; set; }
|
||||
|
||||
[JsonPropertyName("course-unit-realization")]
|
||||
public Self CourseUnitRealization { get; set; }
|
||||
}
|
||||
|
||||
public partial class Duration
|
||||
{
|
||||
[JsonPropertyName("eventId")] public Guid EventId { get; set; }
|
||||
|
||||
[JsonPropertyName("value")] public long Value { get; set; }
|
||||
|
||||
[JsonPropertyName("timeUnitId")] public string TimeUnitId { get; set; }
|
||||
|
||||
[JsonPropertyName("minutes")] public long Minutes { get; set; }
|
||||
|
||||
[JsonPropertyName("_links")] public DurationLinks Links { get; set; }
|
||||
}
|
||||
|
||||
public partial class DurationLinks
|
||||
{
|
||||
[JsonPropertyName("self")] public Self[] Self { get; set; }
|
||||
|
||||
[JsonPropertyName("time-unit")] public Self TimeUnit { get; set; }
|
||||
}
|
||||
|
||||
public partial class EventAttendee
|
||||
{
|
||||
[JsonPropertyName("roleId")] public string RoleId { get; set; }
|
||||
|
||||
[JsonPropertyName("roleName")] public string RoleName { get; set; }
|
||||
|
||||
[JsonPropertyName("roleNamePlural")] public string RoleNamePlural { get; set; }
|
||||
|
||||
[JsonPropertyName("roleDisplayOrder")] public long RoleDisplayOrder { get; set; }
|
||||
|
||||
[JsonPropertyName("_links")] public EventAttendeeLinks Links { get; set; }
|
||||
|
||||
[JsonPropertyName("id")] public Guid Id { get; set; }
|
||||
}
|
||||
|
||||
public partial class EventAttendeeLinks
|
||||
{
|
||||
[JsonPropertyName("self")] public Self Self { get; set; }
|
||||
|
||||
[JsonPropertyName("event")] public Self Event { get; set; }
|
||||
|
||||
[JsonPropertyName("person")] public Self Person { get; set; }
|
||||
}
|
||||
|
||||
public partial class EventLocation
|
||||
{
|
||||
[JsonPropertyName("eventId")] public Guid EventId { get; set; }
|
||||
|
||||
[JsonPropertyName("customLocation")] public string CustomLocation { get; set; }
|
||||
|
||||
[JsonPropertyName("_links")] public EventLocationLinks Links { get; set; }
|
||||
}
|
||||
|
||||
public partial class EventLocationLinks
|
||||
{
|
||||
[JsonPropertyName("self")] public Self[] Self { get; set; }
|
||||
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonPropertyName("event-rooms")]
|
||||
public Self EventRooms { get; set; }
|
||||
}
|
||||
|
||||
public partial class EventOrganizer
|
||||
{
|
||||
[JsonPropertyName("eventId")] public Guid EventId { get; set; }
|
||||
|
||||
[JsonPropertyName("_links")] public EventOrganizerLinks Links { get; set; }
|
||||
}
|
||||
|
||||
public partial class EventOrganizerLinks
|
||||
{
|
||||
[JsonPropertyName("self")] public Self Self { get; set; }
|
||||
|
||||
[JsonPropertyName("event")] public Self Event { get; set; }
|
||||
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonPropertyName("event-attendees")]
|
||||
public EventAttendees? EventAttendees { get; set; }
|
||||
}
|
||||
|
||||
public partial class EventRoom
|
||||
{
|
||||
[JsonPropertyName("_links")] public EventRoomLinks Links { get; set; }
|
||||
|
||||
[JsonPropertyName("id")] public Guid Id { get; set; }
|
||||
}
|
||||
|
||||
public partial class EventRoomLinks
|
||||
{
|
||||
[JsonPropertyName("self")] public Self Self { get; set; }
|
||||
|
||||
[JsonPropertyName("event")] public Self Event { get; set; }
|
||||
|
||||
[JsonPropertyName("room")] public Self Room { get; set; }
|
||||
}
|
||||
|
||||
public partial class EventTeam
|
||||
{
|
||||
[JsonPropertyName("eventId")] public Guid EventId { get; set; }
|
||||
|
||||
[JsonPropertyName("size")] public long Size { get; set; }
|
||||
|
||||
[JsonPropertyName("_links")] public EventTeamLinks Links { get; set; }
|
||||
}
|
||||
|
||||
public partial class EventTeamLinks
|
||||
{
|
||||
[JsonPropertyName("self")] public Self Self { get; set; }
|
||||
|
||||
[JsonPropertyName("event")] public Self Event { get; set; }
|
||||
}
|
||||
|
||||
public partial class Event
|
||||
{
|
||||
[JsonPropertyName("name")] public string Name { get; set; }
|
||||
|
||||
[JsonPropertyName("nameShort")] public string NameShort { get; set; }
|
||||
|
||||
[JsonPropertyName("description")] public object Description { get; set; }
|
||||
|
||||
[JsonPropertyName("typeId")] public string TypeId { get; set; }
|
||||
|
||||
[JsonPropertyName("formatId")] public string FormatId { get; set; }
|
||||
|
||||
[JsonPropertyName("start")] public DateTime Start { get; set; }
|
||||
|
||||
[JsonPropertyName("end")] public DateTime End { get; set; }
|
||||
|
||||
[JsonPropertyName("startsAtLocal")] public DateTime StartsAtLocal { get; set; }
|
||||
|
||||
[JsonPropertyName("endsAtLocal")] public DateTime EndsAtLocal { get; set; }
|
||||
|
||||
[JsonPropertyName("startsAt")] public DateTime StartsAt { get; set; }
|
||||
|
||||
[JsonPropertyName("endsAt")] public DateTime EndsAt { get; set; }
|
||||
|
||||
[JsonPropertyName("holdingStatus")] public HoldingStatus HoldingStatus { get; set; }
|
||||
|
||||
[JsonPropertyName("repeatedLessonRealization")]
|
||||
public RepeatedLessonRealization RepeatedLessonRealization { get; set; }
|
||||
|
||||
[JsonPropertyName("userRoleIds")] public string[] UserRoleIds { get; set; }
|
||||
|
||||
[JsonPropertyName("lessonTemplateId")] public Guid? LessonTemplateId { get; set; }
|
||||
|
||||
[JsonPropertyName("__version")] public long Version { get; set; }
|
||||
|
||||
[JsonPropertyName("_links")] public Dictionary<string, Self> Links { get; set; }
|
||||
|
||||
[JsonPropertyName("id")] public Guid Id { get; set; }
|
||||
}
|
||||
|
||||
public partial class HoldingStatus
|
||||
{
|
||||
[JsonPropertyName("id")] public string Id { get; set; }
|
||||
|
||||
[JsonPropertyName("name")] public string Name { get; set; }
|
||||
|
||||
[JsonPropertyName("audModifiedAt")] public DateTimeOffset? AudModifiedAt { get; set; }
|
||||
|
||||
[JsonPropertyName("audModifiedBy")] public Guid? AudModifiedBy { get; set; }
|
||||
|
||||
[JsonPropertyName("audModifiedBySystem")]
|
||||
public bool? AudModifiedBySystem { get; set; }
|
||||
}
|
||||
|
||||
public partial class RepeatedLessonRealization
|
||||
{
|
||||
[JsonPropertyName("name")] public string Name { get; set; }
|
||||
|
||||
[JsonPropertyName("lessonTeamName")] public string LessonTeamName { get; set; }
|
||||
}
|
||||
|
||||
public partial class LessonRealizationTeam
|
||||
{
|
||||
[JsonPropertyName("name")] public string Name { get; set; }
|
||||
|
||||
[JsonPropertyName("cycleRealizationId")]
|
||||
public Guid CycleRealizationId { get; set; }
|
||||
|
||||
[JsonPropertyName("_links")] public BuildingLinks Links { get; set; }
|
||||
|
||||
[JsonPropertyName("id")] public Guid Id { get; set; }
|
||||
}
|
||||
|
||||
public partial class LessonRealization
|
||||
{
|
||||
[JsonPropertyName("name")] public string Name { get; set; }
|
||||
|
||||
[JsonPropertyName("nameShort")] public string NameShort { get; set; }
|
||||
|
||||
[JsonPropertyName("prototypeId")] public Guid PrototypeId { get; set; }
|
||||
|
||||
[JsonPropertyName("ordinal")] public long Ordinal { get; set; }
|
||||
|
||||
[JsonPropertyName("_links")] public BuildingLinks Links { get; set; }
|
||||
|
||||
[JsonPropertyName("id")] public Guid Id { get; set; }
|
||||
}
|
||||
|
||||
public partial class Person
|
||||
{
|
||||
[JsonPropertyName("lastName")] public string LastName { get; set; }
|
||||
|
||||
[JsonPropertyName("firstName")] public string FirstName { get; set; }
|
||||
|
||||
[JsonPropertyName("middleName")] public string MiddleName { get; set; }
|
||||
|
||||
[JsonPropertyName("fullName")] public string FullName { get; set; }
|
||||
|
||||
[JsonPropertyName("_links")] public BuildingLinks Links { get; set; }
|
||||
|
||||
[JsonPropertyName("id")] public Guid Id { get; set; }
|
||||
}
|
||||
|
||||
public partial class Room
|
||||
{
|
||||
[JsonPropertyName("name")] public string Name { get; set; }
|
||||
|
||||
[JsonPropertyName("nameShort")] public string NameShort { get; set; }
|
||||
|
||||
[JsonPropertyName("building")] public RoomBuilding Building { get; set; }
|
||||
|
||||
[JsonPropertyName("projectorAvailable")]
|
||||
public bool ProjectorAvailable { get; set; }
|
||||
|
||||
[JsonPropertyName("totalCapacity")] public long TotalCapacity { get; set; }
|
||||
|
||||
[JsonPropertyName("workingCapacity")] public long WorkingCapacity { get; set; }
|
||||
|
||||
[JsonPropertyName("deletedAtUtc")] public object DeletedAtUtc { get; set; }
|
||||
|
||||
[JsonPropertyName("_links")] public RoomLinks Links { get; set; }
|
||||
|
||||
[JsonPropertyName("id")] public Guid Id { get; set; }
|
||||
}
|
||||
|
||||
public partial class RoomBuilding
|
||||
{
|
||||
[JsonPropertyName("id")] public Guid Id { get; set; }
|
||||
|
||||
[JsonPropertyName("name")] public string Name { get; set; }
|
||||
|
||||
[JsonPropertyName("nameShort")] public string NameShort { get; set; }
|
||||
|
||||
[JsonPropertyName("address")] public string Address { get; set; }
|
||||
|
||||
[JsonPropertyName("displayOrder")] public long DisplayOrder { get; set; }
|
||||
}
|
||||
|
||||
public partial class RoomLinks
|
||||
{
|
||||
[JsonPropertyName("self")] public Self Self { get; set; }
|
||||
|
||||
[JsonPropertyName("type")] public Self Type { get; set; }
|
||||
|
||||
[JsonPropertyName("building")] public Self Building { get; set; }
|
||||
}
|
||||
|
||||
public partial class Page
|
||||
{
|
||||
[JsonPropertyName("size")] public long Size { get; set; }
|
||||
|
||||
[JsonPropertyName("totalElements")] public long TotalElements { get; set; }
|
||||
|
||||
[JsonPropertyName("totalPages")] public long TotalPages { get; set; }
|
||||
|
||||
[JsonPropertyName("number")] public long Number { get; set; }
|
||||
}
|
||||
|
||||
public partial struct EventAttendees
|
||||
{
|
||||
public Self Self;
|
||||
public Self[] SelfArray;
|
||||
|
||||
public static implicit operator EventAttendees(Self Self) => new EventAttendees { Self = Self };
|
||||
public static implicit operator EventAttendees(Self[] SelfArray) => new EventAttendees { SelfArray = SelfArray };
|
||||
}
|
||||
|
||||
public partial class Schedule
|
||||
{
|
||||
public static Schedule FromJson(string json) =>
|
||||
JsonSerializer.Deserialize<Schedule>(json, SfeduSchedule.Converter.Settings);
|
||||
}
|
||||
|
||||
public static class Serialize
|
||||
{
|
||||
public static string ToJson(this Schedule self) => JsonSerializer.Serialize(self, SfeduSchedule.Converter.Settings);
|
||||
}
|
||||
|
||||
internal static class Converter
|
||||
{
|
||||
public static readonly JsonSerializerOptions Settings = new(JsonSerializerDefaults.General)
|
||||
{
|
||||
Converters =
|
||||
{
|
||||
EventAttendeesConverter.Singleton,
|
||||
new DateOnlyConverter(),
|
||||
new TimeOnlyConverter(),
|
||||
IsoDateTimeOffsetConverter.Singleton
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
internal class EventAttendeesConverter : JsonConverter<EventAttendees>
|
||||
{
|
||||
public override bool CanConvert(Type t) => t == typeof(EventAttendees);
|
||||
|
||||
public override EventAttendees Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
switch (reader.TokenType)
|
||||
{
|
||||
case JsonTokenType.StartObject:
|
||||
var objectValue = JsonSerializer.Deserialize<Self>(ref reader, options);
|
||||
return new EventAttendees { Self = objectValue };
|
||||
case JsonTokenType.StartArray:
|
||||
var arrayValue = JsonSerializer.Deserialize<Self[]>(ref reader, options);
|
||||
return new EventAttendees { SelfArray = arrayValue };
|
||||
}
|
||||
|
||||
throw new Exception("Cannot unmarshal type EventAttendees");
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, EventAttendees value, JsonSerializerOptions options)
|
||||
{
|
||||
if (value.SelfArray != null)
|
||||
{
|
||||
JsonSerializer.Serialize(writer, value.SelfArray, options);
|
||||
return;
|
||||
}
|
||||
|
||||
if (value.Self != null)
|
||||
{
|
||||
JsonSerializer.Serialize(writer, value.Self, options);
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception("Cannot marshal type EventAttendees");
|
||||
}
|
||||
|
||||
public static readonly EventAttendeesConverter Singleton = new EventAttendeesConverter();
|
||||
}
|
||||
|
||||
public class DateOnlyConverter : JsonConverter<DateOnly>
|
||||
{
|
||||
private readonly string serializationFormat;
|
||||
|
||||
public DateOnlyConverter() : this(null)
|
||||
{
|
||||
}
|
||||
|
||||
public DateOnlyConverter(string? serializationFormat)
|
||||
{
|
||||
this.serializationFormat = serializationFormat ?? "yyyy-MM-dd";
|
||||
}
|
||||
|
||||
public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
var value = reader.GetString();
|
||||
return DateOnly.Parse(value!);
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options)
|
||||
=> writer.WriteStringValue(value.ToString(serializationFormat));
|
||||
}
|
||||
|
||||
public class TimeOnlyConverter : JsonConverter<TimeOnly>
|
||||
{
|
||||
private readonly string serializationFormat;
|
||||
|
||||
public TimeOnlyConverter() : this(null)
|
||||
{
|
||||
}
|
||||
|
||||
public TimeOnlyConverter(string? serializationFormat)
|
||||
{
|
||||
this.serializationFormat = serializationFormat ?? "HH:mm:ss.fff";
|
||||
}
|
||||
|
||||
public override TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
var value = reader.GetString();
|
||||
return TimeOnly.Parse(value!);
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options)
|
||||
=> writer.WriteStringValue(value.ToString(serializationFormat));
|
||||
}
|
||||
|
||||
internal class IsoDateTimeOffsetConverter : JsonConverter<DateTimeOffset>
|
||||
{
|
||||
public override bool CanConvert(Type t) => t == typeof(DateTimeOffset);
|
||||
|
||||
private const string DefaultDateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK";
|
||||
|
||||
private DateTimeStyles _dateTimeStyles = DateTimeStyles.RoundtripKind;
|
||||
private string? _dateTimeFormat;
|
||||
private CultureInfo? _culture;
|
||||
|
||||
public DateTimeStyles DateTimeStyles
|
||||
{
|
||||
get => _dateTimeStyles;
|
||||
set => _dateTimeStyles = value;
|
||||
}
|
||||
|
||||
public string? DateTimeFormat
|
||||
{
|
||||
get => _dateTimeFormat ?? string.Empty;
|
||||
set => _dateTimeFormat = (string.IsNullOrEmpty(value)) ? null : value;
|
||||
}
|
||||
|
||||
public CultureInfo Culture
|
||||
{
|
||||
get => _culture ?? CultureInfo.CurrentCulture;
|
||||
set => _culture = value;
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options)
|
||||
{
|
||||
string text;
|
||||
|
||||
|
||||
if ((_dateTimeStyles & DateTimeStyles.AdjustToUniversal) == DateTimeStyles.AdjustToUniversal
|
||||
|| (_dateTimeStyles & DateTimeStyles.AssumeUniversal) == DateTimeStyles.AssumeUniversal)
|
||||
{
|
||||
value = value.ToUniversalTime();
|
||||
}
|
||||
|
||||
text = value.ToString(_dateTimeFormat ?? DefaultDateTimeFormat, Culture);
|
||||
|
||||
writer.WriteStringValue(text);
|
||||
}
|
||||
|
||||
public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
string? dateText = reader.GetString();
|
||||
|
||||
if (string.IsNullOrEmpty(dateText) == false)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(_dateTimeFormat))
|
||||
{
|
||||
return DateTimeOffset.ParseExact(dateText, _dateTimeFormat, Culture, _dateTimeStyles);
|
||||
}
|
||||
else
|
||||
{
|
||||
return DateTimeOffset.Parse(dateText, Culture, _dateTimeStyles);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return default(DateTimeOffset);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static readonly IsoDateTimeOffsetConverter Singleton = new IsoDateTimeOffsetConverter();
|
||||
}
|
||||
#pragma warning restore CS8618
|
||||
#pragma warning restore CS8601
|
||||
#pragma warning restore CS8603
|
||||
12
ModeusSchedule.Abstractions/GlobalConsts.cs
Normal file
12
ModeusSchedule.Abstractions/GlobalConsts.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System.Text.Encodings.Web;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace ModeusSchedule.Abstractions;
|
||||
|
||||
public static class GlobalConsts
|
||||
{
|
||||
public static readonly JsonSerializerOptions JsonSerializerOptions = new()
|
||||
{ PropertyNamingPolicy = JsonNamingPolicy.CamelCase, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping };
|
||||
|
||||
public static string JwtFilePath { get; set; } = "data/jwt.txt";
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace SfeduSchedule.Abstractions;
|
||||
namespace ModeusSchedule.Abstractions;
|
||||
|
||||
// Базовый контракт плагина (общий для хоста и плагинов)
|
||||
public interface IPlugin
|
||||
@@ -5,6 +5,7 @@
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<OutputType>Library</OutputType>
|
||||
<RootNamespace>ModeusSchedule.Abstractions</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
1
ModeusSchedule.MSAuth
Submodule
1
ModeusSchedule.MSAuth
Submodule
Submodule ModeusSchedule.MSAuth added at 5b906d6d07
@@ -3,4 +3,6 @@
|
||||
## TODO
|
||||
|
||||
- [x] Добавить RateLimiter
|
||||
- [x] Добавить обработку ошибок при запросах к modeus
|
||||
- [x] Добавить обработку ошибок при запросах к modeus
|
||||
- [ ] Добавить кэширование расписания
|
||||
- [ ] Сделать передачу ошибок выше по цепочке
|
||||
@@ -1,709 +0,0 @@
|
||||
// <auto-generated />
|
||||
// Вот этим сайтом https://app.quicktype.io/?l=csharp
|
||||
// Не является точной копией ответа, могут быть отличия
|
||||
|
||||
#nullable enable
|
||||
#pragma warning disable CS8618
|
||||
#pragma warning disable CS8601
|
||||
#pragma warning disable CS8603
|
||||
|
||||
namespace SfeduSchedule
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Globalization;
|
||||
|
||||
public partial class Schedule
|
||||
{
|
||||
[JsonPropertyName("_embedded")]
|
||||
public Embedded Embedded { get; set; }
|
||||
|
||||
[JsonPropertyName("page")]
|
||||
public Page Page { get; set; }
|
||||
}
|
||||
|
||||
public partial class Embedded
|
||||
{
|
||||
[JsonPropertyName("events")]
|
||||
public Event[] Events { get; set; }
|
||||
|
||||
[JsonPropertyName("course-unit-realizations")]
|
||||
public CourseUnitRealization[] CourseUnitRealizations { get; set; }
|
||||
|
||||
[JsonPropertyName("cycle-realizations")]
|
||||
public CycleRealization[] CycleRealizations { get; set; }
|
||||
|
||||
[JsonPropertyName("lesson-realization-teams")]
|
||||
public LessonRealizationTeam[] LessonRealizationTeams { get; set; }
|
||||
|
||||
[JsonPropertyName("lesson-realizations")]
|
||||
public LessonRealization[] LessonRealizations { get; set; }
|
||||
|
||||
[JsonPropertyName("event-locations")]
|
||||
public EventLocation[] EventLocations { get; set; }
|
||||
|
||||
[JsonPropertyName("durations")]
|
||||
public Duration[] Durations { get; set; }
|
||||
|
||||
[JsonPropertyName("event-rooms")]
|
||||
public EventRoom[] EventRooms { get; set; }
|
||||
|
||||
[JsonPropertyName("rooms")]
|
||||
public Room[] Rooms { get; set; }
|
||||
|
||||
[JsonPropertyName("buildings")]
|
||||
public BuildingElement[] Buildings { get; set; }
|
||||
|
||||
[JsonPropertyName("event-teams")]
|
||||
public EventTeam[] EventTeams { get; set; }
|
||||
|
||||
[JsonPropertyName("event-organizers")]
|
||||
public EventOrganizer[] EventOrganizers { get; set; }
|
||||
|
||||
[JsonPropertyName("event-attendees")]
|
||||
public EventAttendee[] EventAttendees { get; set; }
|
||||
|
||||
[JsonPropertyName("persons")]
|
||||
public Person[] Persons { get; set; }
|
||||
}
|
||||
|
||||
public partial class BuildingElement
|
||||
{
|
||||
[JsonPropertyName("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[JsonPropertyName("nameShort")]
|
||||
public string NameShort { get; set; }
|
||||
|
||||
[JsonPropertyName("address")]
|
||||
public string Address { get; set; }
|
||||
|
||||
[JsonPropertyName("searchableAddress")]
|
||||
public string SearchableAddress { get; set; }
|
||||
|
||||
[JsonPropertyName("displayOrder")]
|
||||
public long DisplayOrder { get; set; }
|
||||
|
||||
[JsonPropertyName("_links")]
|
||||
public BuildingLinks Links { get; set; }
|
||||
|
||||
[JsonPropertyName("id")]
|
||||
public Guid Id { get; set; }
|
||||
}
|
||||
|
||||
public partial class BuildingLinks
|
||||
{
|
||||
[JsonPropertyName("self")]
|
||||
public Self Self { get; set; }
|
||||
}
|
||||
|
||||
public partial class Self
|
||||
{
|
||||
[JsonPropertyName("href")]
|
||||
public string Href { get; set; }
|
||||
}
|
||||
|
||||
public partial class CourseUnitRealization
|
||||
{
|
||||
[JsonPropertyName("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[JsonPropertyName("nameShort")]
|
||||
public string NameShort { get; set; }
|
||||
|
||||
[JsonPropertyName("prototypeId")]
|
||||
public Guid PrototypeId { get; set; }
|
||||
|
||||
[JsonPropertyName("_links")]
|
||||
public CourseUnitRealizationLinks Links { get; set; }
|
||||
|
||||
[JsonPropertyName("id")]
|
||||
public Guid Id { get; set; }
|
||||
}
|
||||
|
||||
public partial class CourseUnitRealizationLinks
|
||||
{
|
||||
[JsonPropertyName("self")]
|
||||
public Self Self { get; set; }
|
||||
|
||||
[JsonPropertyName("planning-period")]
|
||||
public Self PlanningPeriod { get; set; }
|
||||
}
|
||||
|
||||
public partial class CycleRealization
|
||||
{
|
||||
[JsonPropertyName("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[JsonPropertyName("nameShort")]
|
||||
public string NameShort { get; set; }
|
||||
|
||||
[JsonPropertyName("code")]
|
||||
public string Code { get; set; }
|
||||
|
||||
[JsonPropertyName("courseUnitRealizationNameShort")]
|
||||
public string CourseUnitRealizationNameShort { get; set; }
|
||||
|
||||
[JsonPropertyName("_links")]
|
||||
public CycleRealizationLinks Links { get; set; }
|
||||
|
||||
[JsonPropertyName("id")]
|
||||
public Guid Id { get; set; }
|
||||
}
|
||||
|
||||
public partial class CycleRealizationLinks
|
||||
{
|
||||
[JsonPropertyName("self")]
|
||||
public Self Self { get; set; }
|
||||
|
||||
[JsonPropertyName("course-unit-realization")]
|
||||
public Self CourseUnitRealization { get; set; }
|
||||
}
|
||||
|
||||
public partial class Duration
|
||||
{
|
||||
[JsonPropertyName("eventId")]
|
||||
public Guid EventId { get; set; }
|
||||
|
||||
[JsonPropertyName("value")]
|
||||
public long Value { get; set; }
|
||||
|
||||
[JsonPropertyName("timeUnitId")]
|
||||
public string TimeUnitId { get; set; }
|
||||
|
||||
[JsonPropertyName("minutes")]
|
||||
public long Minutes { get; set; }
|
||||
|
||||
[JsonPropertyName("_links")]
|
||||
public DurationLinks Links { get; set; }
|
||||
}
|
||||
|
||||
public partial class DurationLinks
|
||||
{
|
||||
[JsonPropertyName("self")]
|
||||
public Self[] Self { get; set; }
|
||||
|
||||
[JsonPropertyName("time-unit")]
|
||||
public Self TimeUnit { get; set; }
|
||||
}
|
||||
|
||||
public partial class EventAttendee
|
||||
{
|
||||
[JsonPropertyName("roleId")]
|
||||
public string RoleId { get; set; }
|
||||
|
||||
[JsonPropertyName("roleName")]
|
||||
public string RoleName { get; set; }
|
||||
|
||||
[JsonPropertyName("roleNamePlural")]
|
||||
public string RoleNamePlural { get; set; }
|
||||
|
||||
[JsonPropertyName("roleDisplayOrder")]
|
||||
public long RoleDisplayOrder { get; set; }
|
||||
|
||||
[JsonPropertyName("_links")]
|
||||
public EventAttendeeLinks Links { get; set; }
|
||||
|
||||
[JsonPropertyName("id")]
|
||||
public Guid Id { get; set; }
|
||||
}
|
||||
|
||||
public partial class EventAttendeeLinks
|
||||
{
|
||||
[JsonPropertyName("self")]
|
||||
public Self Self { get; set; }
|
||||
|
||||
[JsonPropertyName("event")]
|
||||
public Self Event { get; set; }
|
||||
|
||||
[JsonPropertyName("person")]
|
||||
public Self Person { get; set; }
|
||||
}
|
||||
|
||||
public partial class EventLocation
|
||||
{
|
||||
[JsonPropertyName("eventId")]
|
||||
public Guid EventId { get; set; }
|
||||
|
||||
[JsonPropertyName("customLocation")]
|
||||
public string CustomLocation { get; set; }
|
||||
|
||||
[JsonPropertyName("_links")]
|
||||
public EventLocationLinks Links { get; set; }
|
||||
}
|
||||
|
||||
public partial class EventLocationLinks
|
||||
{
|
||||
[JsonPropertyName("self")]
|
||||
public Self[] Self { get; set; }
|
||||
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonPropertyName("event-rooms")]
|
||||
public Self EventRooms { get; set; }
|
||||
}
|
||||
|
||||
public partial class EventOrganizer
|
||||
{
|
||||
[JsonPropertyName("eventId")]
|
||||
public Guid EventId { get; set; }
|
||||
|
||||
[JsonPropertyName("_links")]
|
||||
public EventOrganizerLinks Links { get; set; }
|
||||
}
|
||||
|
||||
public partial class EventOrganizerLinks
|
||||
{
|
||||
[JsonPropertyName("self")]
|
||||
public Self Self { get; set; }
|
||||
|
||||
[JsonPropertyName("event")]
|
||||
public Self Event { get; set; }
|
||||
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonPropertyName("event-attendees")]
|
||||
public EventAttendees? EventAttendees { get; set; }
|
||||
}
|
||||
|
||||
public partial class EventRoom
|
||||
{
|
||||
[JsonPropertyName("_links")]
|
||||
public EventRoomLinks Links { get; set; }
|
||||
|
||||
[JsonPropertyName("id")]
|
||||
public Guid Id { get; set; }
|
||||
}
|
||||
|
||||
public partial class EventRoomLinks
|
||||
{
|
||||
[JsonPropertyName("self")]
|
||||
public Self Self { get; set; }
|
||||
|
||||
[JsonPropertyName("event")]
|
||||
public Self Event { get; set; }
|
||||
|
||||
[JsonPropertyName("room")]
|
||||
public Self Room { get; set; }
|
||||
}
|
||||
|
||||
public partial class EventTeam
|
||||
{
|
||||
[JsonPropertyName("eventId")]
|
||||
public Guid EventId { get; set; }
|
||||
|
||||
[JsonPropertyName("size")]
|
||||
public long Size { get; set; }
|
||||
|
||||
[JsonPropertyName("_links")]
|
||||
public EventTeamLinks Links { get; set; }
|
||||
}
|
||||
|
||||
public partial class EventTeamLinks
|
||||
{
|
||||
[JsonPropertyName("self")]
|
||||
public Self Self { get; set; }
|
||||
|
||||
[JsonPropertyName("event")]
|
||||
public Self Event { get; set; }
|
||||
}
|
||||
|
||||
public partial class Event
|
||||
{
|
||||
[JsonPropertyName("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[JsonPropertyName("nameShort")]
|
||||
public string NameShort { get; set; }
|
||||
|
||||
[JsonPropertyName("description")]
|
||||
public object Description { get; set; }
|
||||
|
||||
[JsonPropertyName("typeId")]
|
||||
public string TypeId { get; set; }
|
||||
|
||||
[JsonPropertyName("formatId")]
|
||||
public string FormatId { get; set; }
|
||||
|
||||
[JsonPropertyName("start")]
|
||||
public DateTime Start { get; set; }
|
||||
|
||||
[JsonPropertyName("end")]
|
||||
public DateTime End { get; set; }
|
||||
|
||||
[JsonPropertyName("startsAtLocal")]
|
||||
public DateTime StartsAtLocal { get; set; }
|
||||
|
||||
[JsonPropertyName("endsAtLocal")]
|
||||
public DateTime EndsAtLocal { get; set; }
|
||||
|
||||
[JsonPropertyName("startsAt")]
|
||||
public DateTime StartsAt { get; set; }
|
||||
|
||||
[JsonPropertyName("endsAt")]
|
||||
public DateTime EndsAt { get; set; }
|
||||
|
||||
[JsonPropertyName("holdingStatus")]
|
||||
public HoldingStatus HoldingStatus { get; set; }
|
||||
|
||||
[JsonPropertyName("repeatedLessonRealization")]
|
||||
public RepeatedLessonRealization RepeatedLessonRealization { get; set; }
|
||||
|
||||
[JsonPropertyName("userRoleIds")]
|
||||
public string[] UserRoleIds { get; set; }
|
||||
|
||||
[JsonPropertyName("lessonTemplateId")]
|
||||
public Guid? LessonTemplateId { get; set; }
|
||||
|
||||
[JsonPropertyName("__version")]
|
||||
public long Version { get; set; }
|
||||
|
||||
[JsonPropertyName("_links")]
|
||||
public Dictionary<string, Self> Links { get; set; }
|
||||
|
||||
[JsonPropertyName("id")]
|
||||
public Guid Id { get; set; }
|
||||
}
|
||||
|
||||
public partial class HoldingStatus
|
||||
{
|
||||
[JsonPropertyName("id")]
|
||||
public string Id { get; set; }
|
||||
|
||||
[JsonPropertyName("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[JsonPropertyName("audModifiedAt")]
|
||||
public DateTimeOffset? AudModifiedAt { get; set; }
|
||||
|
||||
[JsonPropertyName("audModifiedBy")]
|
||||
public Guid? AudModifiedBy { get; set; }
|
||||
|
||||
[JsonPropertyName("audModifiedBySystem")]
|
||||
public bool? AudModifiedBySystem { get; set; }
|
||||
}
|
||||
|
||||
public partial class RepeatedLessonRealization
|
||||
{
|
||||
[JsonPropertyName("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[JsonPropertyName("lessonTeamName")]
|
||||
public string LessonTeamName { get; set; }
|
||||
}
|
||||
|
||||
public partial class LessonRealizationTeam
|
||||
{
|
||||
[JsonPropertyName("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[JsonPropertyName("cycleRealizationId")]
|
||||
public Guid CycleRealizationId { get; set; }
|
||||
|
||||
[JsonPropertyName("_links")]
|
||||
public BuildingLinks Links { get; set; }
|
||||
|
||||
[JsonPropertyName("id")]
|
||||
public Guid Id { get; set; }
|
||||
}
|
||||
|
||||
public partial class LessonRealization
|
||||
{
|
||||
[JsonPropertyName("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[JsonPropertyName("nameShort")]
|
||||
public string NameShort { get; set; }
|
||||
|
||||
[JsonPropertyName("prototypeId")]
|
||||
public Guid PrototypeId { get; set; }
|
||||
|
||||
[JsonPropertyName("ordinal")]
|
||||
public long Ordinal { get; set; }
|
||||
|
||||
[JsonPropertyName("_links")]
|
||||
public BuildingLinks Links { get; set; }
|
||||
|
||||
[JsonPropertyName("id")]
|
||||
public Guid Id { get; set; }
|
||||
}
|
||||
|
||||
public partial class Person
|
||||
{
|
||||
[JsonPropertyName("lastName")]
|
||||
public string LastName { get; set; }
|
||||
|
||||
[JsonPropertyName("firstName")]
|
||||
public string FirstName { get; set; }
|
||||
|
||||
[JsonPropertyName("middleName")]
|
||||
public string MiddleName { get; set; }
|
||||
|
||||
[JsonPropertyName("fullName")]
|
||||
public string FullName { get; set; }
|
||||
|
||||
[JsonPropertyName("_links")]
|
||||
public BuildingLinks Links { get; set; }
|
||||
|
||||
[JsonPropertyName("id")]
|
||||
public Guid Id { get; set; }
|
||||
}
|
||||
|
||||
public partial class Room
|
||||
{
|
||||
[JsonPropertyName("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[JsonPropertyName("nameShort")]
|
||||
public string NameShort { get; set; }
|
||||
|
||||
[JsonPropertyName("building")]
|
||||
public RoomBuilding Building { get; set; }
|
||||
|
||||
[JsonPropertyName("projectorAvailable")]
|
||||
public bool ProjectorAvailable { get; set; }
|
||||
|
||||
[JsonPropertyName("totalCapacity")]
|
||||
public long TotalCapacity { get; set; }
|
||||
|
||||
[JsonPropertyName("workingCapacity")]
|
||||
public long WorkingCapacity { get; set; }
|
||||
|
||||
[JsonPropertyName("deletedAtUtc")]
|
||||
public object DeletedAtUtc { get; set; }
|
||||
|
||||
[JsonPropertyName("_links")]
|
||||
public RoomLinks Links { get; set; }
|
||||
|
||||
[JsonPropertyName("id")]
|
||||
public Guid Id { get; set; }
|
||||
}
|
||||
|
||||
public partial class RoomBuilding
|
||||
{
|
||||
[JsonPropertyName("id")]
|
||||
public Guid Id { get; set; }
|
||||
|
||||
[JsonPropertyName("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[JsonPropertyName("nameShort")]
|
||||
public string NameShort { get; set; }
|
||||
|
||||
[JsonPropertyName("address")]
|
||||
public string Address { get; set; }
|
||||
|
||||
[JsonPropertyName("displayOrder")]
|
||||
public long DisplayOrder { get; set; }
|
||||
}
|
||||
|
||||
public partial class RoomLinks
|
||||
{
|
||||
[JsonPropertyName("self")]
|
||||
public Self Self { get; set; }
|
||||
|
||||
[JsonPropertyName("type")]
|
||||
public Self Type { get; set; }
|
||||
|
||||
[JsonPropertyName("building")]
|
||||
public Self Building { get; set; }
|
||||
}
|
||||
|
||||
public partial class Page
|
||||
{
|
||||
[JsonPropertyName("size")]
|
||||
public long Size { get; set; }
|
||||
|
||||
[JsonPropertyName("totalElements")]
|
||||
public long TotalElements { get; set; }
|
||||
|
||||
[JsonPropertyName("totalPages")]
|
||||
public long TotalPages { get; set; }
|
||||
|
||||
[JsonPropertyName("number")]
|
||||
public long Number { get; set; }
|
||||
}
|
||||
|
||||
public partial struct EventAttendees
|
||||
{
|
||||
public Self Self;
|
||||
public Self[] SelfArray;
|
||||
|
||||
public static implicit operator EventAttendees(Self Self) => new EventAttendees { Self = Self };
|
||||
public static implicit operator EventAttendees(Self[] SelfArray) => new EventAttendees { SelfArray = SelfArray };
|
||||
}
|
||||
|
||||
public partial class Schedule
|
||||
{
|
||||
public static Schedule FromJson(string json) => JsonSerializer.Deserialize<Schedule>(json, SfeduSchedule.Converter.Settings);
|
||||
}
|
||||
|
||||
public static class Serialize
|
||||
{
|
||||
public static string ToJson(this Schedule self) => JsonSerializer.Serialize(self, SfeduSchedule.Converter.Settings);
|
||||
}
|
||||
|
||||
internal static class Converter
|
||||
{
|
||||
public static readonly JsonSerializerOptions Settings = new(JsonSerializerDefaults.General)
|
||||
{
|
||||
Converters =
|
||||
{
|
||||
EventAttendeesConverter.Singleton,
|
||||
new DateOnlyConverter(),
|
||||
new TimeOnlyConverter(),
|
||||
IsoDateTimeOffsetConverter.Singleton
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
internal class EventAttendeesConverter : JsonConverter<EventAttendees>
|
||||
{
|
||||
public override bool CanConvert(Type t) => t == typeof(EventAttendees);
|
||||
|
||||
public override EventAttendees Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
switch (reader.TokenType)
|
||||
{
|
||||
case JsonTokenType.StartObject:
|
||||
var objectValue = JsonSerializer.Deserialize<Self>(ref reader, options);
|
||||
return new EventAttendees { Self = objectValue };
|
||||
case JsonTokenType.StartArray:
|
||||
var arrayValue = JsonSerializer.Deserialize<Self[]>(ref reader, options);
|
||||
return new EventAttendees { SelfArray = arrayValue };
|
||||
}
|
||||
throw new Exception("Cannot unmarshal type EventAttendees");
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, EventAttendees value, JsonSerializerOptions options)
|
||||
{
|
||||
if (value.SelfArray != null)
|
||||
{
|
||||
JsonSerializer.Serialize(writer, value.SelfArray, options);
|
||||
return;
|
||||
}
|
||||
if (value.Self != null)
|
||||
{
|
||||
JsonSerializer.Serialize(writer, value.Self, options);
|
||||
return;
|
||||
}
|
||||
throw new Exception("Cannot marshal type EventAttendees");
|
||||
}
|
||||
|
||||
public static readonly EventAttendeesConverter Singleton = new EventAttendeesConverter();
|
||||
}
|
||||
|
||||
public class DateOnlyConverter : JsonConverter<DateOnly>
|
||||
{
|
||||
private readonly string serializationFormat;
|
||||
public DateOnlyConverter() : this(null) { }
|
||||
|
||||
public DateOnlyConverter(string? serializationFormat)
|
||||
{
|
||||
this.serializationFormat = serializationFormat ?? "yyyy-MM-dd";
|
||||
}
|
||||
|
||||
public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
var value = reader.GetString();
|
||||
return DateOnly.Parse(value!);
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options)
|
||||
=> writer.WriteStringValue(value.ToString(serializationFormat));
|
||||
}
|
||||
|
||||
public class TimeOnlyConverter : JsonConverter<TimeOnly>
|
||||
{
|
||||
private readonly string serializationFormat;
|
||||
|
||||
public TimeOnlyConverter() : this(null) { }
|
||||
|
||||
public TimeOnlyConverter(string? serializationFormat)
|
||||
{
|
||||
this.serializationFormat = serializationFormat ?? "HH:mm:ss.fff";
|
||||
}
|
||||
|
||||
public override TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
var value = reader.GetString();
|
||||
return TimeOnly.Parse(value!);
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options)
|
||||
=> writer.WriteStringValue(value.ToString(serializationFormat));
|
||||
}
|
||||
|
||||
internal class IsoDateTimeOffsetConverter : JsonConverter<DateTimeOffset>
|
||||
{
|
||||
public override bool CanConvert(Type t) => t == typeof(DateTimeOffset);
|
||||
|
||||
private const string DefaultDateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK";
|
||||
|
||||
private DateTimeStyles _dateTimeStyles = DateTimeStyles.RoundtripKind;
|
||||
private string? _dateTimeFormat;
|
||||
private CultureInfo? _culture;
|
||||
|
||||
public DateTimeStyles DateTimeStyles
|
||||
{
|
||||
get => _dateTimeStyles;
|
||||
set => _dateTimeStyles = value;
|
||||
}
|
||||
|
||||
public string? DateTimeFormat
|
||||
{
|
||||
get => _dateTimeFormat ?? string.Empty;
|
||||
set => _dateTimeFormat = (string.IsNullOrEmpty(value)) ? null : value;
|
||||
}
|
||||
|
||||
public CultureInfo Culture
|
||||
{
|
||||
get => _culture ?? CultureInfo.CurrentCulture;
|
||||
set => _culture = value;
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options)
|
||||
{
|
||||
string text;
|
||||
|
||||
|
||||
if ((_dateTimeStyles & DateTimeStyles.AdjustToUniversal) == DateTimeStyles.AdjustToUniversal
|
||||
|| (_dateTimeStyles & DateTimeStyles.AssumeUniversal) == DateTimeStyles.AssumeUniversal)
|
||||
{
|
||||
value = value.ToUniversalTime();
|
||||
}
|
||||
|
||||
text = value.ToString(_dateTimeFormat ?? DefaultDateTimeFormat, Culture);
|
||||
|
||||
writer.WriteStringValue(text);
|
||||
}
|
||||
|
||||
public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
string? dateText = reader.GetString();
|
||||
|
||||
if (string.IsNullOrEmpty(dateText) == false)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(_dateTimeFormat))
|
||||
{
|
||||
return DateTimeOffset.ParseExact(dateText, _dateTimeFormat, Culture, _dateTimeStyles);
|
||||
}
|
||||
else
|
||||
{
|
||||
return DateTimeOffset.Parse(dateText, Culture, _dateTimeStyles);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return default(DateTimeOffset);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static readonly IsoDateTimeOffsetConverter Singleton = new IsoDateTimeOffsetConverter();
|
||||
}
|
||||
}
|
||||
#pragma warning restore CS8618
|
||||
#pragma warning restore CS8601
|
||||
#pragma warning restore CS8603
|
||||
@@ -1,5 +1,5 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SfeduSchedule.Abstractions;
|
||||
using ModeusSchedule.Abstractions;
|
||||
|
||||
namespace SfeduSchedule.Plugin.Sample;
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SfeduSchedule.Abstractions\SfeduSchedule.Abstractions.csproj" />
|
||||
<ProjectReference Include="..\ModeusSchedule.Abstractions\ModeusSchedule.Abstractions.csproj"/>
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SfeduSchedule", "SfeduSchedule\SfeduSchedule.csproj", "{57B088A7-D7E2-4B5D-82A4-A3070A78A3E4}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SfeduSchedule.Abstractions", "SfeduSchedule.Abstractions\SfeduSchedule.Abstractions.csproj", "{B2E8DAD7-7373-4155-B230-4E53DFC04445}"
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModeusSchedule.Abstractions", "ModeusSchedule.Abstractions\ModeusSchedule.Abstractions.csproj", "{B2E8DAD7-7373-4155-B230-4E53DFC04445}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SfeduSchedule.Plugin.Sample", "SfeduSchedule.Plugin.Sample\SfeduSchedule.Plugin.Sample.csproj", "{B2B6D730-46AE-40ED-815F-81176FB4E545}"
|
||||
EndProject
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using System.Security.Claims;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.Encodings.Web;
|
||||
using System.Security.Cryptography;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
@@ -41,7 +41,7 @@ public class ApiKeyAuthenticationHandler(
|
||||
var claims = new[]
|
||||
{
|
||||
new Claim(ClaimTypes.NameIdentifier, "api-key"),
|
||||
new Claim(ClaimTypes.Name, "api-key-user"),
|
||||
new Claim(ClaimTypes.Name, "api-key-user")
|
||||
};
|
||||
|
||||
var identity = new ClaimsIdentity(claims, ApiKeyAuthenticationDefaults.Scheme);
|
||||
|
||||
@@ -1,33 +1,33 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
|
||||
namespace SfeduSchedule.Auth
|
||||
namespace SfeduSchedule.Auth;
|
||||
|
||||
public class SwaggerAuthorizeOperationFilter : IOperationFilter
|
||||
{
|
||||
public class SwaggerAuthorizeOperationFilter : IOperationFilter
|
||||
public void Apply(OpenApiOperation operation, OperationFilterContext context)
|
||||
{
|
||||
public void Apply(OpenApiOperation operation, OperationFilterContext context)
|
||||
var hasAuthorize = context.MethodInfo.GetCustomAttributes(true).OfType<AuthorizeAttribute>().Any() ||
|
||||
context.MethodInfo.DeclaringType?.GetCustomAttributes(true).OfType<AuthorizeAttribute>()
|
||||
.Any() == true;
|
||||
if (hasAuthorize)
|
||||
{
|
||||
var hasAuthorize = context.MethodInfo.GetCustomAttributes(true).OfType<AuthorizeAttribute>().Any() ||
|
||||
context.MethodInfo.DeclaringType?.GetCustomAttributes(true).OfType<AuthorizeAttribute>().Any() == true;
|
||||
if (hasAuthorize)
|
||||
operation.Security ??= new List<OpenApiSecurityRequirement>();
|
||||
operation.Security.Add(new OpenApiSecurityRequirement
|
||||
{
|
||||
operation.Security ??= new List<OpenApiSecurityRequirement>();
|
||||
operation.Security.Add(new OpenApiSecurityRequirement
|
||||
{
|
||||
new OpenApiSecurityScheme
|
||||
{
|
||||
new OpenApiSecurityScheme
|
||||
Reference = new OpenApiReference
|
||||
{
|
||||
Reference = new OpenApiReference
|
||||
{
|
||||
Type = ReferenceType.SecurityScheme,
|
||||
Id = ApiKeyAuthenticationDefaults.Scheme
|
||||
}
|
||||
},
|
||||
new List<string>()
|
||||
}
|
||||
});
|
||||
}
|
||||
Type = ReferenceType.SecurityScheme,
|
||||
Id = ApiKeyAuthenticationDefaults.Scheme
|
||||
}
|
||||
},
|
||||
new List<string>()
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
70
SfeduSchedule/Controllers/ProxyController.cs
Normal file
70
SfeduSchedule/Controllers/ProxyController.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using System.Net;
|
||||
using System.Text.Json;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.RateLimiting;
|
||||
using ModeusSchedule.Abstractions;
|
||||
using ModeusSchedule.Abstractions.DTO;
|
||||
using SfeduSchedule.Services;
|
||||
|
||||
namespace SfeduSchedule.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/proxy")]
|
||||
[EnableRateLimiting("throttle")]
|
||||
public class ProxyController(ModeusService modeusService, ILogger<ProxyController> logger) : ControllerBase
|
||||
{
|
||||
/// <summary>
|
||||
/// Получить расписание по пользовательскому запросу.
|
||||
/// </summary>
|
||||
/// <param name="request">Объект запроса, содержащий параметры фильтрации расписания.</param>
|
||||
/// <returns>Список событий расписания.</returns>
|
||||
/// <response code="200">Возвращает расписание</response>
|
||||
/// <response code="429">Слишком много запросов</response>
|
||||
[HttpPost]
|
||||
[Route("events/search")]
|
||||
public async Task<IActionResult> Post([FromBody] ModeusScheduleRequest request)
|
||||
{
|
||||
string? schedule;
|
||||
try
|
||||
{
|
||||
schedule = await modeusService.GetScheduleAsync(request);
|
||||
}
|
||||
catch (HttpRequestException e)
|
||||
{
|
||||
logger.LogError("Ошибка при получении расписания\n\n" + e.Message + "\n\n" + e.StackTrace +
|
||||
"\n\n JSON: " +
|
||||
JsonSerializer.Serialize(request, GlobalConsts.JsonSerializerOptions));
|
||||
return StatusCode((int)(e.StatusCode ?? HttpStatusCode.InternalServerError),
|
||||
"Proxied Modeus: " + e.Message);
|
||||
}
|
||||
|
||||
return Ok(schedule);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Поиск аудиторий по пользовательскому запросу.
|
||||
/// </summary>
|
||||
/// <param name="request">Объект запроса, содержащий параметры фильтрации аудиторий.</param>
|
||||
/// <returns>Список аудиторий.</returns>
|
||||
/// <response code="200">Возвращает список аудиторий</response>
|
||||
/// <response code="429">Слишком много запросов</response>
|
||||
[HttpPost]
|
||||
[Route("rooms/search")]
|
||||
public async Task<IActionResult> SearchRooms([FromBody] RoomSearchRequest request)
|
||||
{
|
||||
string? rooms;
|
||||
try
|
||||
{
|
||||
rooms = await modeusService.SearchRoomsAsync(request);
|
||||
}
|
||||
catch (HttpRequestException e)
|
||||
{
|
||||
logger.LogError("Ошибка при поиске аудиторий\n\n" + e.Message + "\n\n" + e.StackTrace + "\n\n JSON: " +
|
||||
JsonSerializer.Serialize(request, GlobalConsts.JsonSerializerOptions));
|
||||
return StatusCode((int)(e.StatusCode ?? HttpStatusCode.InternalServerError),
|
||||
"Proxied Modeus: " + e.Message);
|
||||
}
|
||||
|
||||
return Ok(rooms);
|
||||
}
|
||||
}
|
||||
@@ -1,74 +1,19 @@
|
||||
using System.Net;
|
||||
using System.Text.Json;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.RateLimiting;
|
||||
using SfeduSchedule.Abstractions;
|
||||
using ModeusSchedule.Abstractions.DTO;
|
||||
using SfeduSchedule.Services;
|
||||
|
||||
namespace SfeduSchedule.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
[Route("api/schedule")]
|
||||
[EnableRateLimiting("throttle")]
|
||||
public class ScheduleController(ModeusService modeusService, ILogger<ScheduleController> logger) : ControllerBase
|
||||
{
|
||||
/// <summary>
|
||||
/// Получить расписание по пользовательскому запросу.
|
||||
/// </summary>
|
||||
/// <param name="request">Объект запроса, содержащий параметры фильтрации расписания.</param>
|
||||
/// <returns>Список событий расписания.</returns>
|
||||
/// <response code="200">Возвращает расписание</response>
|
||||
/// <response code="429">Слишком много запросов</response>
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Post([FromBody] ModeusScheduleRequest request)
|
||||
{
|
||||
string? schedule;
|
||||
try
|
||||
{
|
||||
schedule = await modeusService.GetScheduleAsync(request);
|
||||
}
|
||||
catch (HttpRequestException e)
|
||||
{
|
||||
logger.LogError("Ошибка при получении расписания\n\n" + e.Message + "\n\n" + e.StackTrace +
|
||||
"\n\n JSON: " +
|
||||
JsonSerializer.Serialize(request, GlobalVariables.jsonSerializerOptions));
|
||||
return StatusCode((int)(e.StatusCode ?? HttpStatusCode.InternalServerError),
|
||||
"Proxied Modeus: " + e.Message);
|
||||
}
|
||||
|
||||
return Ok(schedule);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Поиск аудиторий по пользовательскому запросу.
|
||||
/// </summary>
|
||||
/// <param name="request">Объект запроса, содержащий параметры фильтрации аудиторий.</param>
|
||||
/// <returns>Список аудиторий.</returns>
|
||||
/// <response code="200">Возвращает список аудиторий</response>
|
||||
/// <response code="429">Слишком много запросов</response>
|
||||
[HttpPost]
|
||||
[Route("rooms/search")]
|
||||
public async Task<IActionResult> SearchRooms([FromBody] RoomSearchRequest request)
|
||||
{
|
||||
string? rooms;
|
||||
try
|
||||
{
|
||||
rooms = await modeusService.SearchRoomsAsync(request);
|
||||
}
|
||||
catch (HttpRequestException e)
|
||||
{
|
||||
logger.LogError("Ошибка при поиске аудиторий\n\n" + e.Message + "\n\n" + e.StackTrace + "\n\n JSON: " +
|
||||
JsonSerializer.Serialize(request, GlobalVariables.jsonSerializerOptions));
|
||||
return StatusCode((int)(e.StatusCode ?? HttpStatusCode.InternalServerError),
|
||||
"Proxied Modeus: " + e.Message);
|
||||
}
|
||||
|
||||
return Ok(rooms);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получить GUID пользователя по полному имени. (требуется авторизация)
|
||||
/// Получить GUID пользователя по полному имени. (требуется авторизация)
|
||||
/// </summary>
|
||||
/// <param name="fullname">Полное имя пользователя.</param>
|
||||
/// <returns>GUID пользователя.</returns>
|
||||
@@ -89,7 +34,7 @@ public class ScheduleController(ModeusService modeusService, ILogger<ScheduleCon
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получить расписание в формате ICS по пользовательскому запросу.
|
||||
/// Получить расписание в формате ICS по пользовательскому запросу.
|
||||
/// </summary>
|
||||
/// <param name="request">Объект запроса, содержащий параметры фильтрации расписания.</param>
|
||||
/// <returns>Файл ICS с расписанием за -1 неделя + 1 месяц</returns>
|
||||
@@ -104,14 +49,14 @@ public class ScheduleController(ModeusService modeusService, ILogger<ScheduleCon
|
||||
if (string.IsNullOrEmpty(ics))
|
||||
return NotFound();
|
||||
|
||||
return new FileContentResult(System.Text.Encoding.UTF8.GetBytes(ics), "text/calendar")
|
||||
return new FileContentResult(Encoding.UTF8.GetBytes(ics), "text/calendar")
|
||||
{
|
||||
FileDownloadName = "schedule.ics"
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Получить расписание в формате ICS для указанного пользователя за -1 неделя + 1 месяц.
|
||||
/// Получить расписание в формате ICS для указанного пользователя за -1 неделя + 1 месяц.
|
||||
/// </summary>
|
||||
/// <param name="attendeePersonId"></param>
|
||||
/// <returns>Файл ICS с расписанием</returns>
|
||||
|
||||
@@ -2,45 +2,39 @@ using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SfeduSchedule.Services;
|
||||
|
||||
namespace SfeduSchedule.Controllers
|
||||
namespace SfeduSchedule.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/sfedu")]
|
||||
[Authorize(AuthenticationSchemes = "OpenIdConnect")]
|
||||
public class SfeduController(ModeusService modeusService) : ControllerBase
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
[Authorize(AuthenticationSchemes = "OpenIdConnect")]
|
||||
public class SfeduController : ControllerBase
|
||||
/// <summary>
|
||||
/// Получить GUID пользователя через авторизацию Microsoft.
|
||||
/// </summary>
|
||||
/// <param name="redirectUri">
|
||||
/// Необязательный параметр. Если указан, произойдет редирект на указанный URI после получения
|
||||
/// GUID. ([url]/?guid=XXX)
|
||||
/// </param>
|
||||
/// <returns>Строка GUID пользователя или редирект на указанный URI.</returns>
|
||||
/// <response code="200">Возвращает GUID пользователя</response>
|
||||
/// <response code="302">Редирект на указанный URI</response>
|
||||
/// <response code="404">Пользователь не найден</response>
|
||||
/// <response code="401">Неавторизованный</response>
|
||||
[HttpGet]
|
||||
[Route("guid")]
|
||||
public async Task<IActionResult> Get([FromQuery] string? redirectUri)
|
||||
{
|
||||
private readonly ModeusService _modeusService;
|
||||
public SfeduController(ModeusService modeusService) =>
|
||||
_modeusService = modeusService;
|
||||
var name = User.FindFirst("name")?.Value;
|
||||
if (string.IsNullOrEmpty(name))
|
||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
||||
|
||||
/// <summary>
|
||||
/// Получить GUID пользователя через авторизацию Microsoft.
|
||||
/// </summary>
|
||||
/// <param name="redirectUri">Необязательный параметр. Если указан, произойдет редирект на указанный URI после получения GUID. ([url]/?guid=XXX)</param>
|
||||
/// <returns>Строка GUID пользователя или редирект на указанный URI.</returns>
|
||||
/// <response code="200">Возвращает GUID пользователя</response>
|
||||
/// <response code="302">Редирект на указанный URI</response>
|
||||
/// <response code="404">Пользователь не найден</response>
|
||||
/// <response code="401">Неавторизованный</response>
|
||||
[HttpGet]
|
||||
[Route("guid")]
|
||||
public async Task<IActionResult> Get([FromQuery] string? redirectUri)
|
||||
{
|
||||
var name = User.FindFirst("name")?.Value;
|
||||
if (string.IsNullOrEmpty(name))
|
||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
||||
var guid = await modeusService.GetGuidAsync(name);
|
||||
if (string.IsNullOrEmpty(guid))
|
||||
return NotFound();
|
||||
|
||||
var guid = await _modeusService.GetGuidAsync(name);
|
||||
if (string.IsNullOrEmpty(guid))
|
||||
return NotFound();
|
||||
|
||||
if (!string.IsNullOrEmpty(redirectUri))
|
||||
{
|
||||
return Redirect(redirectUri + "?guid=" + guid);
|
||||
}
|
||||
|
||||
return Ok(guid);
|
||||
}
|
||||
if (!string.IsNullOrEmpty(redirectUri)) return Redirect(redirectUri + "?guid=" + guid);
|
||||
|
||||
return Ok(guid);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace SfeduSchedule
|
||||
{
|
||||
public static class GlobalVariables
|
||||
{
|
||||
public static string JwtFilePath { get; set; } = "data/jwt.txt";
|
||||
public static readonly JsonSerializerOptions jsonSerializerOptions = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
|
||||
}
|
||||
}
|
||||
@@ -1,86 +1,108 @@
|
||||
using Microsoft.Playwright;
|
||||
using ModeusSchedule.Abstractions;
|
||||
using Quartz;
|
||||
using SfeduSchedule.Services;
|
||||
|
||||
namespace SfeduSchedule.Jobs;
|
||||
|
||||
public class UpdateJwtJob(IConfiguration configuration, ILogger<UpdateJwtJob> logger) : IJob
|
||||
public class UpdateJwtJob(
|
||||
IConfiguration configuration,
|
||||
ILogger<UpdateJwtJob> logger,
|
||||
IHttpClientFactory httpClientFactory,
|
||||
ModeusHttpClient modeusHttpClient,
|
||||
ModeusService modeusService) : IJob
|
||||
{
|
||||
private const int MaxAttempts = 5; // Максимальное число попыток
|
||||
private const int DelaySeconds = 20; // Задержка между попытками в секундах
|
||||
private const int TimeoutSeconds = 60; // Таймаут для каждого запроса в секундах
|
||||
|
||||
public async Task Execute(IJobExecutionContext jobContext)
|
||||
{
|
||||
logger.LogInformation("Начало выполнения UpdateJwtJob");
|
||||
|
||||
string? username = configuration["MS_USERNAME"];
|
||||
string? password = configuration["MS_PASSWORD"];
|
||||
var authUrl = configuration["AUTH_URL"] ?? "http://msauth:8080/auth/ms";
|
||||
var apiKey = configuration["AUTH_API_KEY"] ?? string.Empty;
|
||||
|
||||
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
|
||||
var client = httpClientFactory.CreateClient("authClient");
|
||||
client.Timeout = TimeSpan.FromSeconds(TimeoutSeconds + 10);
|
||||
|
||||
if (!string.IsNullOrEmpty(apiKey))
|
||||
{
|
||||
logger.LogError("Не указаны учетные данные для входа");
|
||||
return;
|
||||
client.DefaultRequestHeaders.Remove("X-API-Key");
|
||||
client.DefaultRequestHeaders.Add("X-API-Key", apiKey);
|
||||
}
|
||||
|
||||
using var playwright = await Playwright.CreateAsync();
|
||||
await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
|
||||
{
|
||||
Headless = true
|
||||
});
|
||||
var context = await browser.NewContextAsync(new BrowserNewContextOptions
|
||||
{
|
||||
ViewportSize = null
|
||||
});
|
||||
var page = await context.NewPageAsync();
|
||||
|
||||
try
|
||||
{
|
||||
logger.LogInformation("Начало выполнения авторизации Microsoft");
|
||||
await MicrosoftLoginHelper.LoginMicrosoftAsync(page, username, password);
|
||||
|
||||
var sessionStorageJson = await page.EvaluateAsync<string>(@"
|
||||
JSON.stringify(sessionStorage)
|
||||
");
|
||||
|
||||
// Извлечение id_token из sessionStorageJson
|
||||
string? idToken = null;
|
||||
for (var attempt = 1; attempt <= MaxAttempts; attempt++)
|
||||
try
|
||||
{
|
||||
var sessionStorageDict = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, object>>(sessionStorageJson);
|
||||
if (sessionStorageDict != null)
|
||||
logger.LogInformation("Попытка {Attempt}/{MaxAttempts} получения JWT из {AuthUrl}", attempt,
|
||||
MaxAttempts, authUrl);
|
||||
|
||||
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(TimeoutSeconds));
|
||||
var response = await client.GetAsync(authUrl, cts.Token);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
var oidcKey = sessionStorageDict.Keys.FirstOrDefault(k => k.StartsWith("oidc.user:"));
|
||||
if (oidcKey != null)
|
||||
logger.LogWarning("Неуспешный статус при получении JWT: {StatusCode}", response.StatusCode);
|
||||
|
||||
if (attempt == MaxAttempts)
|
||||
{
|
||||
var oidcValueJson = sessionStorageDict[oidcKey]?.ToString();
|
||||
if (!string.IsNullOrEmpty(oidcValueJson))
|
||||
{
|
||||
using var doc = System.Text.Json.JsonDocument.Parse(oidcValueJson);
|
||||
if (doc.RootElement.TryGetProperty("id_token", out var idTokenElement))
|
||||
{
|
||||
idToken = idTokenElement.GetString();
|
||||
}
|
||||
}
|
||||
logger.LogError("Достигнуто максимальное число попыток получения JWT");
|
||||
return;
|
||||
}
|
||||
|
||||
await Task.Delay(TimeSpan.FromSeconds(DelaySeconds), jobContext.CancellationToken);
|
||||
continue;
|
||||
}
|
||||
|
||||
var body = await response.Content.ReadFromJsonAsync<JwtResponse>(jobContext.CancellationToken);
|
||||
|
||||
if (body is null || string.IsNullOrWhiteSpace(body.Jwt))
|
||||
{
|
||||
logger.LogWarning("Ответ от MSAuth не содержит jwt");
|
||||
|
||||
if (attempt == MaxAttempts)
|
||||
{
|
||||
logger.LogError("Достигнуто максимальное число попыток получения корректного JWT");
|
||||
return;
|
||||
}
|
||||
|
||||
await Task.Delay(TimeSpan.FromSeconds(DelaySeconds), jobContext.CancellationToken);
|
||||
continue;
|
||||
}
|
||||
|
||||
configuration["TOKEN"] = body.Jwt;
|
||||
modeusHttpClient.SetToken(body.Jwt);
|
||||
await File.WriteAllTextAsync(GlobalConsts.JwtFilePath,
|
||||
body.Jwt + "\n" + DateTime.Now.ToString("O"), cts.Token);
|
||||
logger.LogInformation("JWT успешно обновлён");
|
||||
return;
|
||||
}
|
||||
catch (OperationCanceledException ex)
|
||||
{
|
||||
logger.LogWarning(ex, "Таймаут при получении JWT (попытка {Attempt}/{MaxAttempts})", attempt,
|
||||
MaxAttempts);
|
||||
|
||||
if (attempt == MaxAttempts)
|
||||
{
|
||||
logger.LogError("Достигнут лимит по таймаутам при запросе JWT");
|
||||
return;
|
||||
}
|
||||
|
||||
await Task.Delay(TimeSpan.FromSeconds(DelaySeconds), jobContext.CancellationToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Ошибка при извлечении id_token из sessionStorageJson");
|
||||
return;
|
||||
logger.LogError(ex, "Ошибка при получении JWT (попытка {Attempt}/{MaxAttempts})", attempt, MaxAttempts);
|
||||
|
||||
if (attempt == MaxAttempts)
|
||||
{
|
||||
logger.LogError("Достигнуто максимальное число попыток из-за ошибок при запросе JWT");
|
||||
return;
|
||||
}
|
||||
|
||||
await Task.Delay(TimeSpan.FromSeconds(DelaySeconds), jobContext.CancellationToken);
|
||||
}
|
||||
|
||||
configuration["TOKEN"] = idToken;
|
||||
|
||||
await File.WriteAllTextAsync(GlobalVariables.JwtFilePath, idToken + "\n" + DateTime.Now.ToString("O"));
|
||||
|
||||
logger.LogInformation("UpdateJwtJob выполнен успешно");
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Ошибка при выполнении UpdateJwtJob");
|
||||
}
|
||||
finally
|
||||
{
|
||||
await context.CloseAsync();
|
||||
await browser.CloseAsync();
|
||||
}
|
||||
}
|
||||
|
||||
private sealed record JwtResponse(string Jwt);
|
||||
}
|
||||
124
SfeduSchedule/Logging/ConsoleFormatter.cs
Normal file
124
SfeduSchedule/Logging/ConsoleFormatter.cs
Normal file
@@ -0,0 +1,124 @@
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Microsoft.Extensions.Logging.Console;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace SfeduSchedule.Logging;
|
||||
|
||||
public sealed class ConsoleFormatter : Microsoft.Extensions.Logging.Console.ConsoleFormatter, IDisposable
|
||||
{
|
||||
private readonly IDisposable? _optionsReloadToken;
|
||||
private ConsoleFormatterOptions _formatterOptions;
|
||||
|
||||
public ConsoleFormatter(IOptionsMonitor<ConsoleFormatterOptions> options)
|
||||
: base("CustomConsoleFormatter")
|
||||
{
|
||||
_optionsReloadToken = options.OnChange(ReloadLoggerOptions);
|
||||
_formatterOptions = options.CurrentValue;
|
||||
}
|
||||
|
||||
private void ReloadLoggerOptions(ConsoleFormatterOptions options)
|
||||
{
|
||||
_formatterOptions = options;
|
||||
}
|
||||
|
||||
public override void Write<TState>(in LogEntry<TState> logEntry, IExternalScopeProvider? scopeProvider, TextWriter textWriter)
|
||||
{
|
||||
var message = logEntry.Formatter(logEntry.State, logEntry.Exception);
|
||||
if (logEntry.Exception == null && message == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Timestamp
|
||||
if (!string.IsNullOrEmpty(_formatterOptions.TimestampFormat))
|
||||
{
|
||||
textWriter.Write(DateTime.Now.ToString(_formatterOptions.TimestampFormat));
|
||||
}
|
||||
|
||||
// Level
|
||||
|
||||
// Нужно для удаления цвета в логах при перенаправлении вывода
|
||||
var useColor = _formatterOptions.ColorBehavior == LoggerColorBehavior.Enabled ||
|
||||
(_formatterOptions.ColorBehavior == LoggerColorBehavior.Default && !System.Console.IsOutputRedirected);
|
||||
|
||||
textWriter.Write(GetLogLevelString(logEntry.LogLevel, useColor));
|
||||
|
||||
// Write :
|
||||
textWriter.Write(":");
|
||||
|
||||
// TraceId
|
||||
var traceIdHolder = new TraceIdHolder();
|
||||
scopeProvider?.ForEachScope((scope, state) =>
|
||||
{
|
||||
if (scope is not IEnumerable<KeyValuePair<string, object>> props) return;
|
||||
foreach (var pair in props)
|
||||
{
|
||||
if (pair.Key == "TraceId")
|
||||
state.TraceId = pair.Value?.ToString();
|
||||
}
|
||||
}, traceIdHolder);
|
||||
|
||||
if (!string.IsNullOrEmpty(traceIdHolder.TraceId))
|
||||
{
|
||||
textWriter.Write($" [{traceIdHolder.TraceId}]");
|
||||
}
|
||||
|
||||
// Category
|
||||
textWriter.Write($" {logEntry.Category}: ");
|
||||
|
||||
// Message
|
||||
textWriter.WriteLine(message);
|
||||
|
||||
if (logEntry.Exception != null)
|
||||
{
|
||||
textWriter.WriteLine(logEntry.Exception.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetLogLevelString(LogLevel logLevel, bool useColor)
|
||||
{
|
||||
var logLevelString = logLevel switch
|
||||
{
|
||||
LogLevel.Trace => "trce",
|
||||
LogLevel.Debug => "dbug",
|
||||
LogLevel.Information => "info",
|
||||
LogLevel.Warning => "warn",
|
||||
LogLevel.Error => "fail",
|
||||
LogLevel.Critical => "crit",
|
||||
_ => "unknown"
|
||||
};
|
||||
|
||||
if (!useColor)
|
||||
{
|
||||
return logLevelString;
|
||||
}
|
||||
|
||||
var color = logLevel switch
|
||||
{
|
||||
LogLevel.Trace => "\x1B[90m",
|
||||
LogLevel.Debug => "\x1B[37m",
|
||||
LogLevel.Information => "\x1B[32m",
|
||||
LogLevel.Warning => "\x1B[33m",
|
||||
LogLevel.Error => "\x1B[31m",
|
||||
LogLevel.Critical => "\x1B[41m\x1B[37m",
|
||||
_ => "\x1B[39m"
|
||||
};
|
||||
|
||||
return $"{color}{logLevelString}\x1B[0m";
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_optionsReloadToken?.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public class ConsoleFormatterOptions : Microsoft.Extensions.Logging.Console.ConsoleFormatterOptions
|
||||
{
|
||||
public LoggerColorBehavior ColorBehavior { get; set; }
|
||||
}
|
||||
|
||||
internal class TraceIdHolder
|
||||
{
|
||||
public string? TraceId { get; set; }
|
||||
}
|
||||
46
SfeduSchedule/Logging/LoggerExtensions.cs
Normal file
46
SfeduSchedule/Logging/LoggerExtensions.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace SfeduSchedule.Logging;
|
||||
|
||||
public static class LoggerExtensions
|
||||
{
|
||||
public static void LogTraceHere(this ILogger logger, string message, [CallerMemberName] string memberName = "")
|
||||
{
|
||||
logger.LogTrace("[{Member}] {Message}", memberName, message);
|
||||
}
|
||||
|
||||
public static void LogDebugHere(this ILogger logger, string message, [CallerMemberName] string memberName = "")
|
||||
{
|
||||
logger.LogDebug("[{Member}] {Message}", memberName, message);
|
||||
}
|
||||
|
||||
public static void LogInformationHere(this ILogger logger, string message, [CallerMemberName] string memberName = "")
|
||||
{
|
||||
logger.LogInformation("[{Member}] {Message}", memberName, message);
|
||||
}
|
||||
|
||||
public static void LogWarningHere(this ILogger logger, string message, [CallerMemberName] string memberName = "")
|
||||
{
|
||||
logger.LogWarning("[{Member}] {Message}", memberName, message);
|
||||
}
|
||||
|
||||
public static void LogErrorHere(this ILogger logger, string message, [CallerMemberName] string memberName = "")
|
||||
{
|
||||
logger.LogError("[{Member}] {Message}", memberName, message);
|
||||
}
|
||||
|
||||
public static void LogErrorHere(this ILogger logger, Exception exception, string message, [CallerMemberName] string memberName = "")
|
||||
{
|
||||
logger.LogError(exception, "[{Member}] {Message}", memberName, message);
|
||||
}
|
||||
|
||||
public static void LogCriticalHere(this ILogger logger, string message, [CallerMemberName] string memberName = "")
|
||||
{
|
||||
logger.LogCritical("[{Member}] {Message}", memberName, message);
|
||||
}
|
||||
|
||||
public static void LogCriticalHere(this ILogger logger, Exception exception, string message, [CallerMemberName] string memberName = "")
|
||||
{
|
||||
logger.LogCritical(exception, "[{Member}] {Message}", memberName, message);
|
||||
}
|
||||
}
|
||||
34
SfeduSchedule/Middleware/CorrelationIdMiddleware.cs
Normal file
34
SfeduSchedule/Middleware/CorrelationIdMiddleware.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace SfeduSchedule.Middleware;
|
||||
|
||||
/// <summary>
|
||||
/// Middleware для добавления и обработки Correlation ID в HTTP запросах.
|
||||
/// Нужно для трассировки запросов.
|
||||
/// </summary>
|
||||
public sealed class CorrelationIdMiddleware(RequestDelegate next, ILogger<CorrelationIdMiddleware> logger)
|
||||
{
|
||||
private const string HeaderName = "X-Correlation-ID";
|
||||
|
||||
public async Task InvokeAsync(HttpContext context)
|
||||
{
|
||||
// 1. Берём из заголовка, если клиент прислал
|
||||
if (!context.Request.Headers.TryGetValue(HeaderName, out var correlationId) ||
|
||||
string.IsNullOrWhiteSpace(correlationId))
|
||||
{
|
||||
// 2. Иначе используем Activity TraceId или TraceIdentifier
|
||||
var activityId = Activity.Current?.TraceId.ToString();
|
||||
correlationId = !string.IsNullOrEmpty(activityId)
|
||||
? activityId
|
||||
: context.TraceIdentifier;
|
||||
}
|
||||
|
||||
// Положим в Items, чтобы можно было достать из сервисов
|
||||
// context.Items[HeaderName] = correlationId.ToString();
|
||||
|
||||
// 3. Прокинем в ответ
|
||||
context.Response.Headers[HeaderName] = correlationId.ToString();
|
||||
|
||||
await next(context);
|
||||
}
|
||||
}
|
||||
120
SfeduSchedule/Middleware/LocalNetworksOnlyMiddleware.cs
Normal file
120
SfeduSchedule/Middleware/LocalNetworksOnlyMiddleware.cs
Normal file
@@ -0,0 +1,120 @@
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace SfeduSchedule.Middleware;
|
||||
|
||||
/// <summary>
|
||||
/// Middleware ограничивает доступ к endpoint'у (сделано для /metrics) только приватными сетями.
|
||||
/// Допускаются: loopback, RFC1918 (10/8, 172.16/12, 192.168/16), link-local (169.254/16, IPv6 link-local),
|
||||
/// а также уникальные локальные адреса IPv6 (fc00::/7). Любой другой источник получает 403.
|
||||
/// Только метод GET.
|
||||
/// </summary>
|
||||
public class LocalNetworksOnlyMiddleware
|
||||
{
|
||||
private readonly ILogger<LocalNetworksOnlyMiddleware> _logger;
|
||||
private readonly RequestDelegate _next;
|
||||
|
||||
public LocalNetworksOnlyMiddleware(RequestDelegate next, ILogger<LocalNetworksOnlyMiddleware> logger)
|
||||
{
|
||||
_next = next;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task InvokeAsync(HttpContext context)
|
||||
{
|
||||
// Разрешаем только GET
|
||||
if (!HttpMethods.IsGet(context.Request.Method))
|
||||
{
|
||||
_logger.LogWarning("Metrics method not allowed: {Method} {Path}", context.Request.Method,
|
||||
context.Request.Path);
|
||||
context.Response.StatusCode = StatusCodes.Status405MethodNotAllowed;
|
||||
context.Response.Headers["Allow"] = "GET";
|
||||
await context.Response.WriteAsync("Method Not Allowed. Only GET is supported for metrics.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Получаем реальный клиентский IP. Если есть X-Forwarded-For, берём самый первый IP из списка.
|
||||
var ip = ExtractClientIp(context) ?? context.Connection.RemoteIpAddress;
|
||||
// Если пришёл IPv4, инкапсулированный в IPv6 (::ffff:x.y.z.w), разворачиваем в чистый IPv4.
|
||||
if (ip is { IsIPv4MappedToIPv6: true }) ip = ip.MapToIPv4();
|
||||
|
||||
// Проверяем принадлежность IP локальным/приватным сетям.
|
||||
if (ip is null || !IsLocalNetwork(ip))
|
||||
{
|
||||
// Фиксируем X-Forwarded-For (если есть) для диагностики за обратными прокси.
|
||||
var xff = context.Request.Headers.TryGetValue("X-Forwarded-For", out var xffVal) ? xffVal.ToString() : null;
|
||||
_logger.LogWarning("Metrics access forbidden. RemoteIP={RemoteIP}, XFF={XFF}, Path={Path}",
|
||||
ip?.ToString() ?? "null", xff, context.Request.Path);
|
||||
context.Response.StatusCode = StatusCodes.Status403Forbidden;
|
||||
await context.Response.WriteAsync("Forbidden: metrics available only from local networks");
|
||||
return;
|
||||
}
|
||||
|
||||
// Продолжаем конвейер, если IP допустим.
|
||||
await _next(context);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Определяет, принадлежит ли адрес локальным / приватным диапазонам.
|
||||
/// </summary>
|
||||
private static bool IsLocalNetwork(IPAddress ip)
|
||||
{
|
||||
// Loopback (127.0.0.0/8, ::1)
|
||||
if (IPAddress.IsLoopback(ip))
|
||||
return true;
|
||||
|
||||
if (ip.AddressFamily == AddressFamily.InterNetwork)
|
||||
{
|
||||
var b = ip.GetAddressBytes();
|
||||
// RFC1918: 10.0.0.0/8
|
||||
if (b[0] == 10) return true;
|
||||
// RFC1918: 172.16.0.0 – 172.31.255.255 (172.16/12)
|
||||
if (b[0] == 172 && b[1] >= 16 && b[1] <= 31) return true;
|
||||
// RFC1918: 192.168.0.0/16
|
||||
if (b[0] == 192 && b[1] == 168) return true;
|
||||
// RFC3927: link-local 169.254.0.0/16 (APIPA)
|
||||
if (b[0] == 169 && b[1] == 254) return true; // link-local
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ip.AddressFamily == AddressFamily.InterNetworkV6)
|
||||
{
|
||||
// Link-local (fe80::/10)
|
||||
if (ip.IsIPv6LinkLocal) return true;
|
||||
var b = ip.GetAddressBytes();
|
||||
// ULA (Unique Local Address) RFC4193: fc00::/7 (fc00 – fdff)
|
||||
if (b.Length > 0 && (b[0] == 0xFC || b[0] == 0xFD)) return true; // ULA
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Извлекает IP клиента из заголовка X-Forwarded-For (если присутствует). Берется первый IP.
|
||||
/// Возвращает null, если заголовок отсутствует или содержит некорректные значения.
|
||||
/// </summary>
|
||||
private static IPAddress? ExtractClientIp(HttpContext context)
|
||||
{
|
||||
if (!context.Request.Headers.TryGetValue("X-Forwarded-For", out var xffValues))
|
||||
return null;
|
||||
|
||||
var xff = xffValues.ToString();
|
||||
if (string.IsNullOrWhiteSpace(xff))
|
||||
return null;
|
||||
|
||||
// Формат может быть: "client, proxy1, proxy2" — берём первый
|
||||
var first = xff.Split(',')[0].Trim();
|
||||
if (string.IsNullOrEmpty(first))
|
||||
return null;
|
||||
|
||||
// Возможны IPv6 адреса в квадратных скобках [::1]
|
||||
if (first.StartsWith("[") && first.EndsWith("]"))
|
||||
first = first.Substring(1, first.Length - 2);
|
||||
|
||||
// Возможен порт через ':' в IPv4, удалим порт если он указан (для IPv6 двоеточия являются частью адреса)
|
||||
if (first.Count(c => c == ':') == 1 && first.Contains('.') && first.Contains(':')) first = first.Split(':')[0];
|
||||
|
||||
return IPAddress.TryParse(first, out var parsed) ? parsed : null;
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
using Microsoft.Playwright;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
public static class MicrosoftLoginHelper
|
||||
{
|
||||
private static readonly string LoginUrl = "https://sfedu.modeus.org/";
|
||||
// private static readonly string StorageStatePath = "ms_storage_state.json";
|
||||
|
||||
public static async Task LoginMicrosoftAsync(IPage page, string username, string password)
|
||||
{
|
||||
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
|
||||
throw new Exception("username и password обязательны для авторизации Microsoft");
|
||||
|
||||
await page.GotoAsync(LoginUrl, new PageGotoOptions { WaitUntil = WaitUntilState.DOMContentLoaded });
|
||||
|
||||
await page.WaitForURLAsync(new Regex("login\\.(microsoftonline|live)\\.com", RegexOptions.IgnoreCase), new PageWaitForURLOptions { Timeout = 60_000 });
|
||||
|
||||
var useAnotherAccount = page.Locator("div#otherTile, #otherTileText, div[data-test-id='useAnotherAccount']").First;
|
||||
try
|
||||
{
|
||||
await Assertions.Expect(useAnotherAccount).ToBeVisibleAsync(new() { Timeout = 2000 });
|
||||
await useAnotherAccount.ClickAsync();
|
||||
}
|
||||
catch (PlaywrightException)
|
||||
{
|
||||
// Кнопка не появилась — пропускаем
|
||||
}
|
||||
|
||||
var emailInput = page.Locator("input[name='loginfmt'], input#i0116");
|
||||
await emailInput.WaitForAsync(new LocatorWaitForOptions { State = WaitForSelectorState.Visible, Timeout = 30_000 });
|
||||
await emailInput.FillAsync(username);
|
||||
|
||||
var nextButton = page.Locator("#idSIButton9, input#idSIButton9");
|
||||
await nextButton.ClickAsync();
|
||||
|
||||
var passwordInput = page.Locator("input[name='passwd'], input#i0118");
|
||||
await passwordInput.WaitForAsync(new LocatorWaitForOptions { State = WaitForSelectorState.Visible, Timeout = 30_000 });
|
||||
await passwordInput.FillAsync(password);
|
||||
await nextButton.ClickAsync();
|
||||
|
||||
await page.WaitForSelectorAsync("button, input[type='submit'], a", new PageWaitForSelectorOptions { Timeout = 8000 });
|
||||
|
||||
var locator = page.Locator("#idSIButton9, #idBtn_Back").First;
|
||||
try
|
||||
{
|
||||
await Assertions.Expect(locator).ToBeVisibleAsync(new() { Timeout = 3000 });
|
||||
var noBtn = page.Locator("#idBtn_Back");
|
||||
if (await noBtn.IsVisibleAsync())
|
||||
await noBtn.ClickAsync();
|
||||
else
|
||||
await page.Locator("#idSIButton9").ClickAsync();
|
||||
}
|
||||
catch (PlaywrightException)
|
||||
{
|
||||
// Кнопки не появились — пропускаем этот шаг
|
||||
}
|
||||
|
||||
await page.WaitForURLAsync(url => !Regex.IsMatch(new Uri(url).Host, "login\\.(microsoftonline|live)\\.com", RegexOptions.IgnoreCase), new PageWaitForURLOptions { Timeout = 60_000 });
|
||||
await page.WaitForLoadStateAsync(LoadState.NetworkIdle);
|
||||
|
||||
// Сохраняем storage state после успешного входа
|
||||
// await page.Context.StorageStateAsync(new BrowserContextStorageStateOptions { Path = StorageStatePath });
|
||||
|
||||
var currentHost = new Uri(page.Url).Host;
|
||||
if (Regex.IsMatch(currentHost, "login\\.(microsoftonline|live)\\.com", RegexOptions.IgnoreCase))
|
||||
throw new Exception("Авторизация не завершена: остались на странице Microsoft Login");
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.Loader;
|
||||
using SfeduSchedule.Abstractions;
|
||||
using ModeusSchedule.Abstractions;
|
||||
|
||||
namespace SfeduSchedule;
|
||||
|
||||
@@ -17,40 +17,38 @@ public static class PluginLoader
|
||||
return result;
|
||||
|
||||
foreach (var file in Directory.EnumerateFiles(pluginsDir, "*.plugin.dll", SearchOption.AllDirectories))
|
||||
{
|
||||
var path = Path.GetFullPath(file);
|
||||
var alc = new PluginLoadContext(path);
|
||||
var asm = alc.LoadFromAssemblyPath(path);
|
||||
try
|
||||
{
|
||||
var path = Path.GetFullPath(file);
|
||||
var alc = new PluginLoadContext(path);
|
||||
var asm = alc.LoadFromAssemblyPath(path);
|
||||
|
||||
// Ищем реализацию IPlugin
|
||||
var pluginType = asm
|
||||
.GetTypes()
|
||||
.FirstOrDefault(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface);
|
||||
var pluginType = asm
|
||||
.GetTypes()
|
||||
.FirstOrDefault(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface);
|
||||
|
||||
if (pluginType is null)
|
||||
continue;
|
||||
if (pluginType is null)
|
||||
continue;
|
||||
|
||||
var instance = (IPlugin)Activator.CreateInstance(pluginType)!;
|
||||
result.Add(new LoadedPlugin(instance, asm, alc));
|
||||
}
|
||||
var instance = (IPlugin)Activator.CreateInstance(pluginType)!;
|
||||
result.Add(new LoadedPlugin(instance, asm, alc));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Ошибка загрузки плагина {file}: {ex.Message}");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
// Отдельный контекст загрузки для изоляции зависимостей плагина
|
||||
public sealed class PluginLoadContext : AssemblyLoadContext
|
||||
public sealed class PluginLoadContext(string pluginMainAssemblyPath) : AssemblyLoadContext(true)
|
||||
{
|
||||
private readonly AssemblyDependencyResolver _resolver;
|
||||
|
||||
public PluginLoadContext(string pluginMainAssemblyPath)
|
||||
: base(isCollectible: true)
|
||||
{
|
||||
_resolver = new AssemblyDependencyResolver(pluginMainAssemblyPath);
|
||||
}
|
||||
private readonly AssemblyDependencyResolver _resolver = new(pluginMainAssemblyPath);
|
||||
|
||||
// Разрешаем управляемые зависимости плагина из его папки.
|
||||
// Возвращаем null, чтобы отдать решение в Default ALC для общих сборок (например, SfeduSchedule.Abstractions).
|
||||
// Возвращаем null, чтобы отдать решение в Default ALC для общих сборок (например, ModeusSchedule.Abstractions).
|
||||
protected override Assembly? Load(AssemblyName assemblyName)
|
||||
{
|
||||
var path = _resolver.ResolveAssemblyToPath(assemblyName);
|
||||
|
||||
@@ -1,62 +1,94 @@
|
||||
using System.Net;
|
||||
using System.Reflection;
|
||||
using System.Threading.RateLimiting;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.Identity.Web;
|
||||
using Quartz;
|
||||
using SfeduSchedule;
|
||||
using SfeduSchedule.Jobs;
|
||||
using SfeduSchedule.Services;
|
||||
using X.Extensions.Logging.Telegram.Extensions;
|
||||
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
using Microsoft.AspNetCore.Mvc.ApplicationParts;
|
||||
using Microsoft.Identity.Web;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using ModeusSchedule.Abstractions;
|
||||
using Prometheus;
|
||||
using Quartz;
|
||||
using SfeduSchedule;
|
||||
using SfeduSchedule.Auth;
|
||||
using SfeduSchedule.Jobs;
|
||||
using SfeduSchedule.Logging;
|
||||
using SfeduSchedule.Middleware;
|
||||
using SfeduSchedule.Services;
|
||||
using X.Extensions.Logging.Telegram.Extensions;
|
||||
using IPNetwork = Microsoft.AspNetCore.HttpOverrides.IPNetwork;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
#region Работа с конфигурацией
|
||||
|
||||
var configuration = builder.Configuration;
|
||||
string? preinstalledJwtToken = configuration["TOKEN"];
|
||||
string? tgChatId = configuration["TG_CHAT_ID"];
|
||||
string? tgToken = configuration["TG_TOKEN"];
|
||||
string updateJwtCron = configuration["UPDATE_JWT_CRON"] ?? "0 0 4 ? * *";
|
||||
var preinstalledJwtToken = configuration["TOKEN"];
|
||||
var tgChatId = configuration["TG_CHAT_ID"];
|
||||
var tgToken = configuration["TG_TOKEN"];
|
||||
var updateJwtCron = configuration["UPDATE_JWT_CRON"] ?? "0 0 4 ? * *";
|
||||
|
||||
// Если не указана TZ, ставим Europe/Moscow
|
||||
if (string.IsNullOrEmpty(configuration["TZ"]))
|
||||
{
|
||||
configuration["TZ"] = "Europe/Moscow";
|
||||
}
|
||||
|
||||
int permitLimit = int.TryParse(configuration["PERMIT_LIMIT"], out var parsedPermitLimit) ? parsedPermitLimit : 40;
|
||||
int timeLimit = int.TryParse(configuration["TIME_LIMIT"], out var parsedTimeLimit) ? parsedTimeLimit : 10;
|
||||
if (string.IsNullOrEmpty(configuration["MODEUS_URL"]))
|
||||
configuration["MODEUS_URL"] = "https://sfedu.modeus.org/";
|
||||
|
||||
// создать папку data если не существует
|
||||
var permitLimit = int.TryParse(configuration["PERMIT_LIMIT"], out var parsedPermitLimit) ? parsedPermitLimit : 40;
|
||||
var timeLimit = int.TryParse(configuration["TIME_LIMIT"], out var parsedTimeLimit) ? parsedTimeLimit : 10;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Работа с папкой данных
|
||||
// Создать папку data если не существует
|
||||
var dataDirectory = Path.Combine(AppContext.BaseDirectory, "data");
|
||||
if (!Directory.Exists(dataDirectory))
|
||||
{
|
||||
Directory.CreateDirectory(dataDirectory);
|
||||
}
|
||||
if (!Directory.Exists(dataDirectory)) Directory.CreateDirectory(dataDirectory);
|
||||
|
||||
GlobalVariables.JwtFilePath = Path.Combine(dataDirectory, "jwt.txt");
|
||||
GlobalConsts.JwtFilePath = Path.Combine(dataDirectory, "jwt.txt");
|
||||
|
||||
// Создать подкаталог для плагинов
|
||||
var pluginsPath = Path.Combine(dataDirectory, "Plugins");
|
||||
if (!Directory.Exists(pluginsPath)) Directory.CreateDirectory(pluginsPath);
|
||||
|
||||
// Создать подкаталог для ключей Data Protection
|
||||
var dataProtectionKeysDirectory = Path.Combine(dataDirectory, "keys");
|
||||
if (!Directory.Exists(dataProtectionKeysDirectory)) Directory.CreateDirectory(dataProtectionKeysDirectory);
|
||||
#endregion
|
||||
|
||||
#region Работа с логированием
|
||||
builder.Logging.ClearProviders();
|
||||
builder.Logging.AddConsole();
|
||||
builder.Logging.AddConsole(options => options.FormatterName = "CustomConsoleFormatter")
|
||||
.AddConsoleFormatter<ConsoleFormatter, ConsoleFormatterOptions>();
|
||||
|
||||
builder.Logging.AddFilter("Quartz", LogLevel.Warning);
|
||||
if (!string.IsNullOrEmpty(tgChatId) && !string.IsNullOrEmpty(tgToken))
|
||||
builder.Logging.AddTelegram(options =>
|
||||
{
|
||||
options.ChatId = tgChatId;
|
||||
options.AccessToken = tgToken;
|
||||
options.FormatterConfiguration.UseEmoji = true;
|
||||
options.FormatterConfiguration.ReadableApplicationName = "Sfedu Schedule";
|
||||
options.FormatterConfiguration.ReadableApplicationName = "Modeus Schedule Proxy";
|
||||
options.LogLevel = new Dictionary<string, LogLevel>
|
||||
{
|
||||
{ "Default", LogLevel.Error },
|
||||
{ "SfeduSchedule.Jobs.UpdateJwtJob", LogLevel.Information },
|
||||
{ "Program", LogLevel.Information }
|
||||
{ "Program", LogLevel.Information },
|
||||
{ "Quartz", LogLevel.Warning }
|
||||
};
|
||||
});
|
||||
#endregion
|
||||
|
||||
// Включаем MVC контроллеры
|
||||
var mvcBuilder = builder.Services.AddControllers();
|
||||
builder.Services.AddHttpClient<ModeusService>();
|
||||
builder.Services.AddHttpClient("modeus", client =>
|
||||
{
|
||||
client.BaseAddress = new Uri(configuration["MODEUS_URL"]!);
|
||||
});
|
||||
builder.Services.AddSingleton<ModeusHttpClient>();
|
||||
builder.Services.AddSingleton<ModeusService>();
|
||||
builder.Services.AddHttpClient("authClient");
|
||||
|
||||
builder.Services.AddAuthentication()
|
||||
.AddScheme<AuthenticationSchemeOptions, ApiKeyAuthenticationHandler>(
|
||||
@@ -66,10 +98,43 @@ builder.Services.AddAuthorization();
|
||||
|
||||
builder.Services.AddMicrosoftIdentityWebAppAuthentication(builder.Configuration);
|
||||
|
||||
// Загружаем плагины (изолированно), даём им зарегистрировать DI и подключаем контроллеры
|
||||
var loaded = PluginLoader.LoadPlugins(pluginsPath);
|
||||
foreach (var p in loaded)
|
||||
builder.Services.Configure<OpenIdConnectOptions>(OpenIdConnectDefaults.AuthenticationScheme, options =>
|
||||
{
|
||||
options.Events.OnRemoteFailure = context =>
|
||||
{
|
||||
context.HandleResponse();
|
||||
|
||||
if (context.Failure != null && context.Failure.Message.Contains("AADSTS65004"))
|
||||
{
|
||||
context.Response.Redirect("/?error_msg=Вы отклонили запрос на вход в систему.");
|
||||
}
|
||||
else
|
||||
{
|
||||
var redirectUri = context.Properties?.RedirectUri;
|
||||
context.Response.Redirect(string.IsNullOrEmpty(redirectUri) ? "/" : redirectUri);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
options.Events.OnAuthenticationFailed = context =>
|
||||
{
|
||||
var logger = context.HttpContext.RequestServices.GetRequiredService<ILogger<Program>>();
|
||||
logger.LogError(context.Exception, "OIDC authentication failed");
|
||||
|
||||
context.HandleResponse();
|
||||
var redirectUri = context.Properties?.RedirectUri;
|
||||
context.Response.Redirect(string.IsNullOrEmpty(redirectUri) ? "/" : redirectUri);
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
});
|
||||
// Загружаем плагины (изолированно), даём им зарегистрировать DI и подключаем контроллеры
|
||||
var loadedPlugins = PluginLoader.LoadPlugins(pluginsPath);
|
||||
Console.WriteLine("Plugins count: " + loadedPlugins.Count);
|
||||
foreach (var p in loadedPlugins)
|
||||
{
|
||||
Console.WriteLine("Loading plugin: " + p.Instance.Name);
|
||||
|
||||
// DI из плагина
|
||||
p.Instance.ConfigureServices(builder.Services);
|
||||
|
||||
@@ -98,21 +163,30 @@ if (string.IsNullOrEmpty(preinstalledJwtToken))
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen(options =>
|
||||
{
|
||||
var mainXmlFile = $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
||||
var mainXmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
||||
var mainXmlPath = Path.Combine(AppContext.BaseDirectory, mainXmlFile);
|
||||
options.IncludeXmlComments(mainXmlPath);
|
||||
|
||||
var pluginXmlFile = "SfeduSchedule.Abstractions.xml";
|
||||
var pluginXmlFile = "ModeusSchedule.Abstractions.xml";
|
||||
var pluginXmlPath = Path.Combine(AppContext.BaseDirectory, pluginXmlFile);
|
||||
options.IncludeXmlComments(pluginXmlPath);
|
||||
|
||||
// Добавляем только схему авторизации по ApiKey
|
||||
options.AddSecurityDefinition(ApiKeyAuthenticationDefaults.Scheme, new Microsoft.OpenApi.Models.OpenApiSecurityScheme
|
||||
// Добавление документации плагинов
|
||||
foreach (var p in loadedPlugins)
|
||||
{
|
||||
Description = $"Api Key needed to access the endpoints. {ApiKeyAuthenticationDefaults.HeaderName}: Your_API_Key",
|
||||
var pluginXmlFullPath = p.Assembly.Location.Replace("dll", "xml");
|
||||
if (File.Exists(pluginXmlFullPath))
|
||||
options.IncludeXmlComments(pluginXmlFullPath);
|
||||
}
|
||||
|
||||
// Добавляем только схему авторизации по ApiKey
|
||||
options.AddSecurityDefinition(ApiKeyAuthenticationDefaults.Scheme, new OpenApiSecurityScheme
|
||||
{
|
||||
Description =
|
||||
$"Api Key needed to access the endpoints. {ApiKeyAuthenticationDefaults.HeaderName}: Your_API_Key",
|
||||
Name = ApiKeyAuthenticationDefaults.HeaderName,
|
||||
In = Microsoft.OpenApi.Models.ParameterLocation.Header,
|
||||
Type = Microsoft.OpenApi.Models.SecuritySchemeType.ApiKey,
|
||||
In = ParameterLocation.Header,
|
||||
Type = SecuritySchemeType.ApiKey,
|
||||
Scheme = ApiKeyAuthenticationDefaults.Scheme
|
||||
});
|
||||
options.OperationFilter<SwaggerAuthorizeOperationFilter>();
|
||||
@@ -122,10 +196,11 @@ builder.Services.AddRateLimiter(options =>
|
||||
{
|
||||
options.AddPolicy("throttle", httpContext =>
|
||||
RateLimitPartition.GetFixedWindowLimiter(
|
||||
partitionKey: (httpContext.Request.Headers.TryGetValue("X-Forwarded-For", out var xff) && !string.IsNullOrWhiteSpace(xff.ToString()))
|
||||
httpContext.Request.Headers.TryGetValue("X-Forwarded-For", out var xff) &&
|
||||
!string.IsNullOrWhiteSpace(xff.ToString())
|
||||
? xff.ToString().Split(',')[0].Trim()
|
||||
: (httpContext.Connection.RemoteIpAddress?.ToString() ?? "unknown"),
|
||||
factory: _ => new FixedWindowRateLimiterOptions
|
||||
: httpContext.Connection.RemoteIpAddress?.ToString() ?? "unknown",
|
||||
_ => new FixedWindowRateLimiterOptions
|
||||
{
|
||||
PermitLimit = permitLimit,
|
||||
Window = TimeSpan.FromSeconds(timeLimit)
|
||||
@@ -140,7 +215,8 @@ builder.Services.AddRateLimiter(options =>
|
||||
cancellationToken);
|
||||
|
||||
var reqLogger = context.HttpContext.RequestServices.GetRequiredService<ILogger<Program>>();
|
||||
var clientIp = (context.HttpContext.Request.Headers.TryGetValue("X-Forwarded-For", out var xff) && !string.IsNullOrWhiteSpace(xff.ToString()))
|
||||
var clientIp = context.HttpContext.Request.Headers.TryGetValue("X-Forwarded-For", out var xff) &&
|
||||
!string.IsNullOrWhiteSpace(xff.ToString())
|
||||
? xff.ToString().Split(',')[0].Trim()
|
||||
: context.HttpContext.Connection.RemoteIpAddress?.ToString();
|
||||
reqLogger.LogWarning("Rate limit exceeded for IP: {IpAddress}", clientIp);
|
||||
@@ -150,24 +226,30 @@ builder.Services.AddRateLimiter(options =>
|
||||
builder.Services.Configure<ForwardedHeadersOptions>(options =>
|
||||
{
|
||||
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor |
|
||||
ForwardedHeaders.XForwardedProto;
|
||||
ForwardedHeaders.XForwardedProto |
|
||||
ForwardedHeaders.XForwardedHost;
|
||||
options.KnownNetworks.Clear();
|
||||
options.KnownProxies.Clear();
|
||||
|
||||
options.KnownNetworks.Add(new IPNetwork(System.Net.IPAddress.Parse("127.0.0.1"), 8)); // localhost
|
||||
options.KnownNetworks.Add(new IPNetwork(System.Net.IPAddress.Parse("10.0.0.0"), 8)); // 10.x.x.x
|
||||
options.KnownNetworks.Add(new IPNetwork(System.Net.IPAddress.Parse("192.168.0.0"), 16)); // 192.168.x.x
|
||||
options.KnownNetworks.Add(new IPNetwork(System.Net.IPAddress.Parse("172.16.0.0"), 12)); // 172.16.x.x - 172.31.x.x
|
||||
options.KnownNetworks.Add(new IPNetwork(IPAddress.Parse("127.0.0.1"), 8)); // localhost
|
||||
options.KnownNetworks.Add(new IPNetwork(IPAddress.Parse("10.0.0.0"), 8)); // 10.x.x.x
|
||||
options.KnownNetworks.Add(new IPNetwork(IPAddress.Parse("192.168.0.0"), 16)); // 192.168.x.x
|
||||
options.KnownNetworks.Add(new IPNetwork(IPAddress.Parse("172.16.0.0"), 12)); // 172.16.x.x - 172.31.x.x
|
||||
});
|
||||
|
||||
// Хранение ключей Data Protection в папке data
|
||||
builder.Services.AddDataProtection()
|
||||
.PersistKeysToFileSystem(new DirectoryInfo(dataProtectionKeysDirectory));
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
var logger = app.Services.GetRequiredService<ILogger<Program>>();
|
||||
|
||||
app.UseForwardedHeaders(new ForwardedHeadersOptions
|
||||
{
|
||||
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost
|
||||
});
|
||||
// Используем настройки из DI (Configure<ForwardedHeadersOptions>)
|
||||
app.UseForwardedHeaders();
|
||||
|
||||
// Корреляция логов по запросам
|
||||
app.UseMiddleware<CorrelationIdMiddleware>();
|
||||
|
||||
if (string.IsNullOrEmpty(preinstalledJwtToken))
|
||||
{
|
||||
@@ -175,10 +257,10 @@ if (string.IsNullOrEmpty(preinstalledJwtToken))
|
||||
var scheduler = await schedulerFactory.GetScheduler();
|
||||
|
||||
// Проверить существование файла jwt.txt
|
||||
if (File.Exists(GlobalVariables.JwtFilePath))
|
||||
if (File.Exists(GlobalConsts.JwtFilePath))
|
||||
{
|
||||
logger.LogInformation("Обнаружена прошлая сессия");
|
||||
var lines = await File.ReadAllLinesAsync(GlobalVariables.JwtFilePath);
|
||||
var lines = await File.ReadAllLinesAsync(GlobalConsts.JwtFilePath);
|
||||
if (lines.Length > 1 && DateTime.TryParse(lines[1], out var expirationDate))
|
||||
{
|
||||
logger.LogInformation("Дата истечения токена: {ExpirationDate}", expirationDate);
|
||||
@@ -202,16 +284,23 @@ if (string.IsNullOrEmpty(preinstalledJwtToken))
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
await scheduler.TriggerJob(jobKey);
|
||||
}
|
||||
}
|
||||
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
|
||||
app.UseStaticFiles();
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.UseStaticFiles();
|
||||
app.UseHttpMetrics();
|
||||
app.UseRateLimiter();
|
||||
|
||||
app.MapGet("/", async context =>
|
||||
{
|
||||
@@ -221,13 +310,13 @@ app.MapGet("/", async context =>
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
// Маршруты Minimal API из плагинов
|
||||
foreach (var p in loaded)
|
||||
{
|
||||
logger.LogInformation("Mapping endpoints for plugin: {PluginName}", p.Instance.Name);
|
||||
p.Instance.MapEndpoints(app);
|
||||
}
|
||||
// Ограничим доступ к /metrics только локальными сетями
|
||||
app.UseWhen(ctx => ctx.Request.Path.StartsWithSegments("/metrics", StringComparison.OrdinalIgnoreCase),
|
||||
branch => { branch.UseMiddleware<LocalNetworksOnlyMiddleware>(); });
|
||||
|
||||
app.UseRateLimiter();
|
||||
app.MapMetrics();
|
||||
|
||||
// Маршруты Minimal API из плагинов
|
||||
foreach (var p in loadedPlugins) p.Instance.MapEndpoints(app);
|
||||
|
||||
app.Run();
|
||||
127
SfeduSchedule/Services/ModeusHttpClient.cs
Normal file
127
SfeduSchedule/Services/ModeusHttpClient.cs
Normal file
@@ -0,0 +1,127 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
using ModeusSchedule.Abstractions;
|
||||
using ModeusSchedule.Abstractions.DTO;
|
||||
using SfeduSchedule.Logging;
|
||||
|
||||
namespace SfeduSchedule.Services;
|
||||
|
||||
public class ModeusHttpClient
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly ILogger<ModeusHttpClient> _logger;
|
||||
public ModeusHttpClient(IHttpClientFactory httpClientFactory,
|
||||
ILogger<ModeusHttpClient> logger,
|
||||
IConfiguration configuration)
|
||||
{
|
||||
_httpClient = httpClientFactory.CreateClient("modeus");
|
||||
_logger = logger;
|
||||
_configuration = configuration;
|
||||
SetToken(_configuration["TOKEN"]); // Установка предустановленного токена при инициализации, на случай если нет возможности связи с AUTH сервисом
|
||||
}
|
||||
public void SetToken(string? token)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(token)) {
|
||||
_logger.LogInformationHere("Предоставленный токен пустой.");
|
||||
return;
|
||||
}
|
||||
|
||||
_httpClient.DefaultRequestHeaders.Remove(HeaderNames.Authorization);
|
||||
_httpClient.DefaultRequestHeaders.Add(HeaderNames.Authorization, $"Bearer {token}");
|
||||
}
|
||||
|
||||
public async Task<string?> GetScheduleAsync(ModeusScheduleRequest msr)
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Post,
|
||||
$"schedule-calendar-v2/api/calendar/events/search?tz={_configuration["TZ"]!}");
|
||||
request.Content = new StringContent(JsonSerializer.Serialize(msr, GlobalConsts.JsonSerializerOptions),
|
||||
Encoding.UTF8, "application/json");
|
||||
var response = await _httpClient.SendAsync(request);
|
||||
if (response.StatusCode != System.Net.HttpStatusCode.OK)
|
||||
{
|
||||
_logger.LogErrorHere($"Неуспешный статус при получении расписания: {response.StatusCode}, Request: {JsonSerializer.Serialize(msr, GlobalConsts.JsonSerializerOptions)}");
|
||||
return null;
|
||||
}
|
||||
return await response.Content.ReadAsStringAsync();
|
||||
}
|
||||
|
||||
public async Task<List<Attendees>> GetAttendeesAsync(Guid eventId)
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Get,
|
||||
$"schedule-calendar-v2/api/calendar/events/{eventId}/attendees");
|
||||
var response = await _httpClient.SendAsync(request);
|
||||
if (response.StatusCode != System.Net.HttpStatusCode.OK)
|
||||
{
|
||||
_logger.LogErrorHere($"Неуспешный статус при получении расписания: {response.StatusCode}, eventId: {eventId}");
|
||||
}
|
||||
List<Attendees>? attendees;
|
||||
try
|
||||
{
|
||||
attendees = Attendees.FromJson(await response.Content.ReadAsStringAsync());
|
||||
return attendees;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogErrorHere(ex, "Deserialization failed.");
|
||||
}
|
||||
|
||||
return new List<Attendees>();
|
||||
}
|
||||
|
||||
public async Task<string?> SearchRoomsAsync(RoomSearchRequest requestDto)
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, "schedule-calendar-v2/api/campus/rooms/search");
|
||||
request.Content =
|
||||
new StringContent(JsonSerializer.Serialize(requestDto, GlobalConsts.JsonSerializerOptions),
|
||||
Encoding.UTF8, "application/json");
|
||||
var response = await _httpClient.SendAsync(request);
|
||||
if (response.StatusCode != System.Net.HttpStatusCode.OK)
|
||||
{
|
||||
_logger.LogErrorHere($"Неуспешный статус при получении расписания: {response.StatusCode}, Request: {JsonSerializer.Serialize(requestDto, GlobalConsts.JsonSerializerOptions)}");
|
||||
return null;
|
||||
}
|
||||
return await response.Content.ReadAsStringAsync();
|
||||
}
|
||||
|
||||
public async Task<string?> GetGuidAsync(string fullName)
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, "schedule-calendar-v2/api/people/persons/search");
|
||||
request.Content = new StringContent(JsonSerializer.Serialize(new
|
||||
{
|
||||
fullName,
|
||||
sort = "+fullName",
|
||||
size = 10,
|
||||
page = 0
|
||||
}), Encoding.UTF8, "application/json");
|
||||
var response = await _httpClient.SendAsync(request);
|
||||
|
||||
_logger.LogInformationHere($"Ответ получен: {response.StatusCode}");
|
||||
if (response.StatusCode != System.Net.HttpStatusCode.OK)
|
||||
{
|
||||
_logger.LogErrorHere($"Неуспешный статус при получении расписания: {response.StatusCode}, Name: {fullName}");
|
||||
return null;
|
||||
}
|
||||
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
|
||||
string? personId;
|
||||
try
|
||||
{
|
||||
personId = JsonDocument.Parse(json).RootElement
|
||||
.GetProperty("_embedded")
|
||||
.GetProperty("persons")[0]
|
||||
.GetProperty("id")
|
||||
.GetString();
|
||||
}
|
||||
catch
|
||||
{
|
||||
_logger.LogWarningHere($"Не удалось получить идентификатор пользователя. FullName={fullName}");
|
||||
return null;
|
||||
}
|
||||
|
||||
return personId;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,235 +1,208 @@
|
||||
using System.Text.Json;
|
||||
using Ical.Net;
|
||||
using Ical.Net.CalendarComponents;
|
||||
using Ical.Net.DataTypes;
|
||||
using Ical.Net.Serialization;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
using SfeduSchedule.Abstractions;
|
||||
using ModeusSchedule.Abstractions;
|
||||
using ModeusSchedule.Abstractions.DTO;
|
||||
using SfeduSchedule.Logging;
|
||||
|
||||
namespace SfeduSchedule.Services
|
||||
namespace SfeduSchedule.Services;
|
||||
|
||||
public class ModeusService
|
||||
{
|
||||
public class ModeusService
|
||||
private readonly IConfiguration _configuration;
|
||||
|
||||
private readonly ILogger<ModeusService> _logger;
|
||||
private readonly ModeusHttpClient _modeusHttpClient;
|
||||
|
||||
public ModeusService(
|
||||
ILogger<ModeusService> logger,
|
||||
IConfiguration configuration,
|
||||
ModeusHttpClient modeusHttpClient)
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly ILogger<ModeusService> _logger;
|
||||
private readonly IConfiguration _configuration;
|
||||
_modeusHttpClient = modeusHttpClient;
|
||||
_logger = logger;
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
public ModeusService(HttpClient httpClient, ILogger<ModeusService> logger, IConfiguration configuration)
|
||||
public async Task<Schedule?> GetScheduleJsonAsync(ModeusScheduleRequest msr)
|
||||
{
|
||||
var schedule = await GetScheduleAsync(msr);
|
||||
if (schedule == null)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_logger = logger;
|
||||
_configuration = configuration;
|
||||
_httpClient.BaseAddress = new Uri("https://sfedu.modeus.org/");
|
||||
var token = _configuration["TOKEN"];
|
||||
_httpClient.DefaultRequestHeaders.Add(HeaderNames.Authorization, $"Bearer {token}");
|
||||
_logger.LogErrorHere($"schedule is null. {JsonSerializer.Serialize(msr, GlobalConsts.JsonSerializerOptions)}");
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<string?> GetScheduleAsync(ModeusScheduleRequest msr)
|
||||
Schedule? scheduleJson;
|
||||
try
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Post,
|
||||
$"schedule-calendar-v2/api/calendar/events/search?tz={_configuration["TZ"]!}");
|
||||
request.Content = new StringContent(JsonSerializer.Serialize(msr, GlobalVariables.jsonSerializerOptions),
|
||||
System.Text.Encoding.UTF8, "application/json");
|
||||
var response = await _httpClient.SendAsync(request);
|
||||
_logger.LogInformation("GetScheduleAsync: Ответ получен: {StatusCode}", response.StatusCode);
|
||||
response.EnsureSuccessStatusCode();
|
||||
return await response.Content.ReadAsStringAsync();
|
||||
scheduleJson = Schedule.FromJson(schedule);
|
||||
switch (scheduleJson)
|
||||
{
|
||||
case null:
|
||||
_logger.LogErrorHere($"scheduleJson is null. Schedule: {schedule}\n Request: {JsonSerializer.Serialize(msr, GlobalConsts.JsonSerializerOptions)}");
|
||||
break;
|
||||
case { Embedded: null }:
|
||||
_logger.LogErrorHere($"scheduleJson.Embedded is null. Response: {schedule}\nscheduleJson: {scheduleJson}\n Request: {JsonSerializer.Serialize(msr, GlobalConsts.JsonSerializerOptions)}");
|
||||
break;
|
||||
case { Embedded.Events: null }:
|
||||
_logger.LogErrorHere($"scheduleJson.Embedded.Events is null. Response: {schedule}\nscheduleJson: {scheduleJson}\n Request: {JsonSerializer.Serialize(msr, GlobalConsts.JsonSerializerOptions)}");
|
||||
break;
|
||||
case { Embedded.Events.Length: 0 }:
|
||||
_logger.LogWarningHere($"scheduleJson.Embedded.Events is empty. Embedded: {scheduleJson.Embedded}\n Request: {JsonSerializer.Serialize(msr, GlobalConsts.JsonSerializerOptions)}");
|
||||
break;
|
||||
default:
|
||||
return scheduleJson;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogErrorHere($"Deserialization failed. Schedule: {schedule}\n Request: {JsonSerializer.Serialize(msr, GlobalConsts.JsonSerializerOptions)}\n Exception: {ex}");
|
||||
}
|
||||
|
||||
public async Task<string?> SearchRoomsAsync(RoomSearchRequest requestDto)
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<string?> GetIcsAsync(ModeusScheduleRequest msr)
|
||||
{
|
||||
var scheduleJson = await GetScheduleJsonAsync(msr);
|
||||
if (scheduleJson == null)
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, $"schedule-calendar-v2/api/campus/rooms/search");
|
||||
request.Content =
|
||||
new StringContent(JsonSerializer.Serialize(requestDto, GlobalVariables.jsonSerializerOptions),
|
||||
System.Text.Encoding.UTF8, "application/json");
|
||||
var response = await _httpClient.SendAsync(request);
|
||||
_logger.LogInformation("SearchRoomsAsync: Ответ получен: {StatusCode}", response.StatusCode);
|
||||
response.EnsureSuccessStatusCode();
|
||||
return await response.Content.ReadAsStringAsync();
|
||||
_logger.LogErrorHere($"scheduleJson is null after deserialization. Request: {JsonSerializer.Serialize(msr, GlobalConsts.JsonSerializerOptions)}");
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<string?> GetGuidAsync(string fullName)
|
||||
var calendar = new Calendar();
|
||||
calendar.AddTimeZone(new VTimeZone(_configuration["TZ"]!));
|
||||
|
||||
foreach (var e in scheduleJson.Embedded.Events)
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, $"schedule-calendar-v2/api/people/persons/search");
|
||||
request.Content = new StringContent(JsonSerializer.Serialize(new
|
||||
// Получение названия аудитории для события
|
||||
string? roomName = null;
|
||||
if (scheduleJson.Embedded.EventLocations != null && scheduleJson.Embedded.Rooms != null &&
|
||||
scheduleJson.Embedded.EventRooms != null)
|
||||
{
|
||||
fullName = fullName,
|
||||
sort = "+fullName",
|
||||
size = 10,
|
||||
page = 0
|
||||
}), System.Text.Encoding.UTF8, "application/json");
|
||||
var response = await _httpClient.SendAsync(request);
|
||||
|
||||
_logger.LogInformation("GetGuidAsync: Ответ получен: {StatusCode}", response.StatusCode);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
|
||||
string? personId;
|
||||
try
|
||||
{
|
||||
personId = JsonDocument.Parse(json).RootElement
|
||||
.GetProperty("_embedded")
|
||||
.GetProperty("persons")[0]
|
||||
.GetProperty("id")
|
||||
.GetString();
|
||||
}
|
||||
catch
|
||||
{
|
||||
_logger.LogWarning(
|
||||
"GetGuidAsync: Не удалось получить идентификатор пользователя, {FullName}, json: {Json}", fullName,
|
||||
json);
|
||||
return null;
|
||||
}
|
||||
|
||||
return personId;
|
||||
}
|
||||
|
||||
public async Task<string?> GetIcsAsync(ModeusScheduleRequest msr)
|
||||
{
|
||||
var schedule = await GetScheduleAsync(msr);
|
||||
if (schedule == null)
|
||||
{
|
||||
_logger.LogError("GetIcsAsync: Schedule is null. Request: {@msr}", msr);
|
||||
return null;
|
||||
}
|
||||
|
||||
Schedule? scheduleJson;
|
||||
try
|
||||
{
|
||||
scheduleJson = Schedule.FromJson(schedule);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "GetIcsAsync: Deserialization failed. Schedule: {Schedule}\n Request: {@msr}", schedule, msr);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (scheduleJson?.Embedded?.Events is not { Length: > 0 } events)
|
||||
{
|
||||
if (scheduleJson == null)
|
||||
_logger.LogError("GetIcsAsync: scheduleJson is null. Schedule: {Schedule}\n Request: {@msr}", schedule, msr);
|
||||
else if (scheduleJson.Embedded == null)
|
||||
_logger.LogError("GetIcsAsync: scheduleJson.Embedded is null. scheduleJson: {@scheduleJson}\n Request: {@msr}", scheduleJson, msr);
|
||||
else if (scheduleJson.Embedded.Events == null)
|
||||
_logger.LogError("GetIcsAsync: scheduleJson.Embedded.Events is null. Embedded: {@Embedded}\n Request: {@msr}",
|
||||
scheduleJson.Embedded, msr);
|
||||
else
|
||||
_logger.LogWarning("GetIcsAsync: scheduleJson.Embedded.Events is empty. Embedded: {@Embedded}\n Request: {@msr}",
|
||||
scheduleJson.Embedded, msr);
|
||||
return null;
|
||||
}
|
||||
|
||||
var calendar = new Ical.Net.Calendar();
|
||||
calendar.AddTimeZone(new VTimeZone(_configuration["TZ"]!));
|
||||
|
||||
foreach (var e in scheduleJson.Embedded.Events)
|
||||
{
|
||||
// Получение названия аудитории для события
|
||||
string? roomName = null;
|
||||
if (scheduleJson.Embedded.EventLocations != null && scheduleJson.Embedded.Rooms != null &&
|
||||
scheduleJson.Embedded.EventRooms != null)
|
||||
var eventLocation = scheduleJson.Embedded.EventLocations.FirstOrDefault(el => el.EventId == e.Id);
|
||||
if (eventLocation != null
|
||||
&& eventLocation.Links != null
|
||||
&& eventLocation.Links.EventRooms != null
|
||||
&& eventLocation.Links.EventRooms.Href != null)
|
||||
{
|
||||
var eventLocation = scheduleJson.Embedded.EventLocations.FirstOrDefault(el => el.EventId == e.Id);
|
||||
if (eventLocation != null
|
||||
&& eventLocation.Links != null
|
||||
&& eventLocation.Links.EventRooms != null
|
||||
&& eventLocation.Links.EventRooms.Href != null)
|
||||
var eventRoomId = eventLocation.Links.EventRooms.Href.Split('/').Last();
|
||||
var EventRoom =
|
||||
scheduleJson.Embedded.EventRooms.FirstOrDefault(er =>
|
||||
er.Id.ToString().ToLower() == eventRoomId);
|
||||
if (EventRoom != null)
|
||||
{
|
||||
var eventRoomId = eventLocation.Links.EventRooms.Href.Split('/').Last();
|
||||
var EventRoom =
|
||||
scheduleJson.Embedded.EventRooms.FirstOrDefault(er =>
|
||||
er.Id.ToString().ToLower() == eventRoomId);
|
||||
if (EventRoom != null)
|
||||
{
|
||||
var roomId = EventRoom.Links.Room.Href.Split('/').Last();
|
||||
var room = scheduleJson.Embedded.Rooms.FirstOrDefault(r =>
|
||||
r.Id.ToString().ToLower() == roomId);
|
||||
if (room != null)
|
||||
roomName = room.Name;
|
||||
}
|
||||
var roomId = EventRoom.Links.Room.Href.Split('/').Last();
|
||||
var room = scheduleJson.Embedded.Rooms.FirstOrDefault(r =>
|
||||
r.Id.ToString().ToLower() == roomId);
|
||||
if (room != null)
|
||||
roomName = room.Name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Получение преподавателей для события
|
||||
string teachersNames = "";
|
||||
if (scheduleJson.Embedded.EventOrganizers != null && scheduleJson.Embedded.EventAttendees != null &&
|
||||
scheduleJson.Embedded.Persons != null)
|
||||
// Получение преподавателей для события
|
||||
var teachersNames = "";
|
||||
if (scheduleJson.Embedded.EventOrganizers != null && scheduleJson.Embedded.EventAttendees != null &&
|
||||
scheduleJson.Embedded.Persons != null)
|
||||
{
|
||||
// Получаем eventOrganizer
|
||||
var eventOrganizers =
|
||||
scheduleJson.Embedded.EventOrganizers.FirstOrDefault(eo => eo.EventId == e.Id);
|
||||
if (eventOrganizers != null &&
|
||||
eventOrganizers.Links.EventAttendees != null)
|
||||
{
|
||||
// Получаем eventOrganizer
|
||||
var eventOrganizers =
|
||||
scheduleJson.Embedded.EventOrganizers.FirstOrDefault(eo => eo.EventId == e.Id);
|
||||
if (eventOrganizers != null &&
|
||||
eventOrganizers.Links.EventAttendees != null)
|
||||
{
|
||||
// Получаем eventAttendee id
|
||||
// Тут может прийти массив или 1 объект
|
||||
Self[] eventAttendeeIds = Array.Empty<Self>();
|
||||
if (eventOrganizers.Links.EventAttendees.Value.Self != null)
|
||||
eventAttendeeIds = new[] { eventOrganizers.Links.EventAttendees.Value.Self };
|
||||
else if (eventOrganizers.Links.EventAttendees.Value.SelfArray != null)
|
||||
eventAttendeeIds = eventOrganizers.Links.EventAttendees.Value.SelfArray;
|
||||
// Получаем eventAttendee id
|
||||
// Тут может прийти массив или 1 объект
|
||||
var eventAttendeeIds = Array.Empty<Self>();
|
||||
if (eventOrganizers.Links.EventAttendees.Value.Self != null)
|
||||
eventAttendeeIds = new[] { eventOrganizers.Links.EventAttendees.Value.Self };
|
||||
else if (eventOrganizers.Links.EventAttendees.Value.SelfArray != null)
|
||||
eventAttendeeIds = eventOrganizers.Links.EventAttendees.Value.SelfArray;
|
||||
|
||||
if (eventAttendeeIds.Length > 0)
|
||||
if (eventAttendeeIds.Length > 0)
|
||||
foreach (var eventAttendeeId in eventAttendeeIds)
|
||||
{
|
||||
foreach (var eventAttendeeId in eventAttendeeIds)
|
||||
var attendeeId = eventAttendeeId.Href.Split('/').Last();
|
||||
// Получаем eventAttendee
|
||||
var eventAttendee =
|
||||
scheduleJson.Embedded.EventAttendees.FirstOrDefault(ea =>
|
||||
ea.Id.ToString().ToLower() == attendeeId);
|
||||
if (eventAttendee != null)
|
||||
{
|
||||
var attendeeId = eventAttendeeId.Href.Split('/').Last();
|
||||
// Получаем eventAttendee
|
||||
var eventAttendee =
|
||||
scheduleJson.Embedded.EventAttendees.FirstOrDefault(ea =>
|
||||
ea.Id.ToString().ToLower() == attendeeId);
|
||||
if (eventAttendee != null)
|
||||
{
|
||||
var personId = eventAttendee.Links.Person.Href.Split('/').Last();
|
||||
// Получаем person
|
||||
var teacher = scheduleJson.Embedded.Persons.FirstOrDefault(p =>
|
||||
p.Id.ToString().ToLower() == personId);
|
||||
if (teacher != null)
|
||||
teachersNames += (string.IsNullOrEmpty(teachersNames) ? "" : ", ") +
|
||||
teacher.FullName;
|
||||
}
|
||||
var personId = eventAttendee.Links.Person.Href.Split('/').Last();
|
||||
// Получаем person
|
||||
var teacher = scheduleJson.Embedded.Persons.FirstOrDefault(p =>
|
||||
p.Id.ToString().ToLower() == personId);
|
||||
if (teacher != null)
|
||||
teachersNames += (string.IsNullOrEmpty(teachersNames) ? "" : ", ") +
|
||||
teacher.FullName;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Получение короткого названия для события
|
||||
string shortNameCourse = "";
|
||||
if (scheduleJson.Embedded.CourseUnitRealizations != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var courseUnitRealizationsLinks = e.Links["course-unit-realization"];
|
||||
var courseUnitRealizationId = courseUnitRealizationsLinks.Href.Split('/').Last();
|
||||
if (!string.IsNullOrEmpty(courseUnitRealizationId))
|
||||
{
|
||||
var courseUnitRealization = scheduleJson.Embedded.CourseUnitRealizations
|
||||
.FirstOrDefault(cu => cu.Id.ToString().ToLower() == courseUnitRealizationId);
|
||||
if (courseUnitRealization != null)
|
||||
shortNameCourse = courseUnitRealization.NameShort ?? "";
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Ignored
|
||||
}
|
||||
}
|
||||
|
||||
calendar.Events.Add(new CalendarEvent
|
||||
{
|
||||
Summary = (string.IsNullOrEmpty(shortNameCourse) ? "" : shortNameCourse + " / ") + e.Name,
|
||||
Description = e.NameShort + (string.IsNullOrEmpty(roomName) ? "" : $"\nАудитория: {roomName}") +
|
||||
(string.IsNullOrEmpty(teachersNames) ? "" : $"\nПреподаватели: {teachersNames}"),
|
||||
Start = new CalDateTime(e.StartsAtLocal, _configuration["TZ"]!),
|
||||
End = new CalDateTime(e.EndsAtLocal, _configuration["TZ"]!),
|
||||
});
|
||||
}
|
||||
|
||||
var serializer = new CalendarSerializer();
|
||||
var serializedCalendar = serializer.SerializeToString(calendar);
|
||||
_logger.LogInformation("GetIcsAsync: Serialized calendar created. Length: {Length}",
|
||||
serializedCalendar?.Length ?? 0);
|
||||
return serializedCalendar;
|
||||
// Получение короткого названия для события
|
||||
var shortNameCourse = "";
|
||||
if (scheduleJson.Embedded.CourseUnitRealizations != null)
|
||||
try
|
||||
{
|
||||
var courseUnitRealizationsLinks = e.Links["course-unit-realization"];
|
||||
var courseUnitRealizationId = courseUnitRealizationsLinks.Href.Split('/').Last();
|
||||
if (!string.IsNullOrEmpty(courseUnitRealizationId))
|
||||
{
|
||||
var courseUnitRealization = scheduleJson.Embedded.CourseUnitRealizations
|
||||
.FirstOrDefault(cu => cu.Id.ToString().ToLower() == courseUnitRealizationId);
|
||||
if (courseUnitRealization != null)
|
||||
shortNameCourse = courseUnitRealization.NameShort ?? "";
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// Ignored
|
||||
}
|
||||
|
||||
calendar.Events.Add(new CalendarEvent
|
||||
{
|
||||
Summary = (string.IsNullOrEmpty(shortNameCourse) ? "" : shortNameCourse + " / ") + e.Name,
|
||||
Description = e.NameShort + (string.IsNullOrEmpty(roomName) ? "" : $"\nАудитория: {roomName}") +
|
||||
(string.IsNullOrEmpty(teachersNames) ? "" : $"\nПреподаватели: {teachersNames}"),
|
||||
Start = new CalDateTime(e.StartsAtLocal, _configuration["TZ"]!),
|
||||
End = new CalDateTime(e.EndsAtLocal, _configuration["TZ"]!)
|
||||
});
|
||||
}
|
||||
|
||||
var serializer = new CalendarSerializer();
|
||||
var serializedCalendar = serializer.SerializeToString(calendar);
|
||||
_logger.LogInformationHere($"serialized calendar created. Length: {serializedCalendar?.Length ?? 0}");
|
||||
return serializedCalendar;
|
||||
}
|
||||
|
||||
#region Проксирование методов из ModeusHttpClient
|
||||
|
||||
public async Task<string?> SearchRoomsAsync(RoomSearchRequest request)
|
||||
{
|
||||
return await _modeusHttpClient.SearchRoomsAsync(request);
|
||||
}
|
||||
|
||||
public async Task<string?> GetScheduleAsync(ModeusScheduleRequest msr)
|
||||
{
|
||||
return await _modeusHttpClient.GetScheduleAsync(msr);
|
||||
}
|
||||
|
||||
public async Task<string?> GetGuidAsync(string fullname)
|
||||
{
|
||||
return await _modeusHttpClient.GetGuidAsync(fullname);
|
||||
}
|
||||
|
||||
public async Task<List<Attendees>> GetAttendeesAsync(Guid eventId)
|
||||
{
|
||||
return await _modeusHttpClient.GetAttendeesAsync(eventId);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -10,16 +10,16 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Ical.Net" Version="5.1.0" />
|
||||
<PackageReference Include="Microsoft.Identity.Web" Version="3.14.0" />
|
||||
<PackageReference Include="Microsoft.Playwright" Version="1.55.0" />
|
||||
<PackageReference Include="Quartz.AspNetCore" Version="3.15.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="9.0.4" />
|
||||
<PackageReference Include="X.Extensions.Logging.Telegram" Version="2.0.2" />
|
||||
<PackageReference Include="Ical.Net" Version="5.1.2"/>
|
||||
<PackageReference Include="Microsoft.Identity.Web" Version="3.14.1"/>
|
||||
<PackageReference Include="prometheus-net.AspNetCore" Version="8.2.1"/>
|
||||
<PackageReference Include="Quartz.AspNetCore" Version="3.15.1"/>
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="9.0.6"/>
|
||||
<PackageReference Include="X.Extensions.Logging.Telegram" Version="2.0.2"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SfeduSchedule.Abstractions\SfeduSchedule.Abstractions.csproj" />
|
||||
<ProjectReference Include="..\ModeusSchedule.Abstractions\ModeusSchedule.Abstractions.csproj"/>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
24
SfeduSchedule/SfeduSchedule.sln
Normal file
24
SfeduSchedule/SfeduSchedule.sln
Normal file
@@ -0,0 +1,24 @@
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.5.2.0
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SfeduSchedule", "SfeduSchedule.csproj", "{E8436480-8A01-6D45-1BA4-C84E185346D1}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{E8436480-8A01-6D45-1BA4-C84E185346D1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{E8436480-8A01-6D45-1BA4-C84E185346D1}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{E8436480-8A01-6D45-1BA4-C84E185346D1}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{E8436480-8A01-6D45-1BA4-C84E185346D1}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {59AA22AD-DE1E-4BBF-AE10-5644E7594DCF}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
@@ -15,12 +15,13 @@
|
||||
align-items: center;
|
||||
background: #fff;
|
||||
}
|
||||
|
||||
img {
|
||||
height: auto;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<img src="/main.jpg" alt="Вот так">
|
||||
<img alt="Вот так" src="/main.jpg">
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -11,12 +11,12 @@ services:
|
||||
- AzureAd:ClientSecret=
|
||||
- AzureAd:Domain=sfedu.onmicrosoft.com
|
||||
- AzureAd:CallbackPath=/signin-oidc
|
||||
- MS_USERNAME=${MS_USERNAME}
|
||||
- MS_PASSWORD=${MS_PASSWORD}
|
||||
- TG_CHAT_ID=${TG_CHAT_ID}
|
||||
- TG_TOKEN=${TG_TOKEN}
|
||||
- API_KEY=${API_KEY}
|
||||
# - TOKEN=${TOKEN}
|
||||
- AUTH_URL=${AUTH_URL}
|
||||
- AUTH_API_KEY=${AUTH_API_KEY}
|
||||
volumes:
|
||||
- data:/app/data
|
||||
restart: always
|
||||
|
||||
@@ -11,12 +11,12 @@ services:
|
||||
- AzureAd:ClientSecret=
|
||||
- AzureAd:Domain=sfedu.onmicrosoft.com
|
||||
- AzureAd:CallbackPath=/signin-oidc
|
||||
- MS_USERNAME=${MS_USERNAME}
|
||||
- MS_PASSWORD=${MS_PASSWORD}
|
||||
- TG_CHAT_ID=${TG_CHAT_ID}
|
||||
- TG_TOKEN=${TG_TOKEN}
|
||||
- API_KEY=${API_KEY}
|
||||
# - TOKEN=${TOKEN}
|
||||
- AUTH_URL=${AUTH_URL}
|
||||
- AUTH_API_KEY=${AUTH_API_KEY}
|
||||
volumes:
|
||||
- ./data:/app/data
|
||||
restart: unless-stopped
|
||||
|
||||
Reference in New Issue
Block a user