diff --git a/Moonlight/App/Models/Misc/Session.cs b/Moonlight/App/Models/Misc/Session.cs deleted file mode 100644 index 50676b0..0000000 --- a/Moonlight/App/Models/Misc/Session.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Microsoft.AspNetCore.Components; -using Moonlight.App.Database.Entities; -using Moonlight.App.Services.Interop; - -namespace Moonlight.App.Models.Misc; - -public class Session -{ - public string Ip { get; set; } = "N/A"; - public string Url { get; set; } = "N/A"; - public string Device { get; set; } = "N/A"; - public User? User { get; set; } - public DateTime CreatedAt { get; set; } - public NavigationManager Navigation { get; set; } - public AlertService AlertService { get; set; } -} \ No newline at end of file diff --git a/Moonlight/App/Repositories/SessionRepository.cs b/Moonlight/App/Repositories/SessionRepository.cs deleted file mode 100644 index 91cc0c2..0000000 --- a/Moonlight/App/Repositories/SessionRepository.cs +++ /dev/null @@ -1,37 +0,0 @@ -using Moonlight.App.Models.Misc; - -namespace Moonlight.App.Repositories; - -public class SessionRepository -{ - private readonly List Sessions; - - public SessionRepository() - { - Sessions = new(); - } - - public Session[] Get() - { - lock (Sessions) - { - return Sessions.ToArray(); - } - } - - public void Add(Session session) - { - lock (Sessions) - { - Sessions.Add(session); - } - } - - public void Delete(Session session) - { - lock (Sessions) - { - Sessions.Remove(session); - } - } -} \ No newline at end of file diff --git a/Moonlight/App/Services/Sessions/SessionClientService.cs b/Moonlight/App/Services/Sessions/SessionClientService.cs new file mode 100644 index 0000000..c152ffd --- /dev/null +++ b/Moonlight/App/Services/Sessions/SessionClientService.cs @@ -0,0 +1,56 @@ +using Microsoft.AspNetCore.Components; +using Microsoft.JSInterop; +using Moonlight.App.Database.Entities; +using Moonlight.App.Repositories; +using Moonlight.App.Services.Interop; + +namespace Moonlight.App.Services.Sessions; + +public class SessionClientService +{ + public readonly Guid Uuid = Guid.NewGuid(); + public readonly DateTime CreateTimestamp = DateTime.UtcNow; + public User? User { get; private set; } + + public readonly IdentityService IdentityService; + public readonly AlertService AlertService; + public readonly NavigationManager NavigationManager; + public readonly IJSRuntime JsRuntime; + + private readonly SessionServerService SessionServerService; + private readonly Repository UserRepository; + + public SessionClientService( + IdentityService identityService, + AlertService alertService, + NavigationManager navigationManager, + IJSRuntime jsRuntime, + SessionServerService sessionServerService, + Repository userRepository) + { + IdentityService = identityService; + AlertService = alertService; + NavigationManager = navigationManager; + JsRuntime = jsRuntime; + SessionServerService = sessionServerService; + UserRepository = userRepository; + } + + public async Task Start() + { + User = await IdentityService.Get(); + + if (User != null) // Track users last visit + { + User.LastVisitedAt = DateTime.UtcNow; + UserRepository.Update(User); + } + + await SessionServerService.Register(this); + } + + public async Task Stop() + { + await SessionServerService.UnRegister(this); + } +} \ No newline at end of file diff --git a/Moonlight/App/Services/Sessions/SessionServerService.cs b/Moonlight/App/Services/Sessions/SessionServerService.cs new file mode 100644 index 0000000..525be5e --- /dev/null +++ b/Moonlight/App/Services/Sessions/SessionServerService.cs @@ -0,0 +1,64 @@ +using Moonlight.App.Database.Entities; +using Moonlight.App.Events; + +namespace Moonlight.App.Services.Sessions; + +public class SessionServerService +{ + private readonly List Sessions = new(); + private readonly EventSystem Event; + + public SessionServerService(EventSystem eventSystem) + { + Event = eventSystem; + } + + public async Task Register(SessionClientService sessionClientService) + { + lock (Sessions) + { + if(!Sessions.Contains(sessionClientService)) + Sessions.Add(sessionClientService); + } + + await Event.Emit("sessions.add", sessionClientService); + } + public async Task UnRegister(SessionClientService sessionClientService) + { + lock (Sessions) + { + if(Sessions.Contains(sessionClientService)) + Sessions.Remove(sessionClientService); + } + + await Event.Emit("sessions.remove", sessionClientService); + } + + public Task GetSessions() + { + lock (Sessions) + { + return Task.FromResult(Sessions.ToArray()); + } + } + + public async Task ReloadUserSessions(User user) + { + var sessions = await GetSessions(); + + foreach (var session in sessions) + { + if (session.User != null && session.User.Id == user.Id) + { + try + { + session.NavigationManager.NavigateTo(session.NavigationManager.Uri, true); + } + catch (Exception) + { + // ignore + } + } + } + } +} \ No newline at end of file diff --git a/Moonlight/App/Services/Sessions/SessionService.cs b/Moonlight/App/Services/Sessions/SessionService.cs deleted file mode 100644 index dad4fa8..0000000 --- a/Moonlight/App/Services/Sessions/SessionService.cs +++ /dev/null @@ -1,83 +0,0 @@ -using Microsoft.AspNetCore.Components; -using Moonlight.App.Database.Entities; -using Moonlight.App.Models.Misc; -using Moonlight.App.Repositories; -using Moonlight.App.Services.Interop; - -namespace Moonlight.App.Services.Sessions; - -public class SessionService -{ - private readonly SessionRepository SessionRepository; - private Repository UserRepository; - private readonly IdentityService IdentityService; - private readonly NavigationManager NavigationManager; - private readonly AlertService AlertService; - private readonly DateTimeService DateTimeService; - - private Session? OwnSession; - - public SessionService( - SessionRepository sessionRepository, - IdentityService identityService, - NavigationManager navigationManager, - AlertService alertService, - DateTimeService dateTimeService, - Repository userRepository) - { - SessionRepository = sessionRepository; - IdentityService = identityService; - NavigationManager = navigationManager; - AlertService = alertService; - DateTimeService = dateTimeService; - UserRepository = userRepository; - } - - public async Task Register() - { - var user = await IdentityService.Get(); - - OwnSession = new Session() - { - Ip = IdentityService.GetIp(), - Url = NavigationManager.Uri, - Device = IdentityService.GetDevice(), - CreatedAt = DateTimeService.GetCurrent(), - User = user, - Navigation = NavigationManager, - AlertService = AlertService - }; - - SessionRepository.Add(OwnSession); - - if (user != null) // Track last session init of user as last visited timestamp - { - user.LastVisitedAt = DateTimeService.GetCurrent(); - UserRepository.Update(user); - } - } - - public void Refresh() - { - OwnSession.Url = NavigationManager.Uri; - } - - public void Close() - { - SessionRepository.Delete(OwnSession); - } - - public Session[] GetAll() - { - return SessionRepository.Get(); - } - - public void ReloadUserSessions(User user) - { - foreach (var session in SessionRepository.Get()) - { - if(session.User != null && session.User.Id == user.Id) - session.Navigation.NavigateTo(session.Navigation.Uri, true); - } - } -} \ No newline at end of file diff --git a/Moonlight/App/Services/Statistics/StatisticsCaptureService.cs b/Moonlight/App/Services/Statistics/StatisticsCaptureService.cs index 28333f4..43a0ec2 100644 --- a/Moonlight/App/Services/Statistics/StatisticsCaptureService.cs +++ b/Moonlight/App/Services/Statistics/StatisticsCaptureService.cs @@ -44,7 +44,7 @@ public class StatisticsCaptureService var domainsRepo = scope.ServiceProvider.GetRequiredService>(); var webspacesRepo = scope.ServiceProvider.GetRequiredService>(); var databasesRepo = scope.ServiceProvider.GetRequiredService>(); - var sessionService = scope.ServiceProvider.GetRequiredService(); + var sessionService = scope.ServiceProvider.GetRequiredService(); void AddEntry(string chart, int value) { @@ -61,7 +61,7 @@ public class StatisticsCaptureService AddEntry("domainsCount", domainsRepo.Get().Count()); AddEntry("webspacesCount", webspacesRepo.Get().Count()); AddEntry("databasesCount", databasesRepo.Get().Count()); - AddEntry("sessionsCount", sessionService.GetAll().Length); + AddEntry("sessionsCount", (await sessionService.GetSessions()).Length); } } catch (Exception e) diff --git a/Moonlight/Program.cs b/Moonlight/Program.cs index d78d796..1a5160a 100644 --- a/Moonlight/Program.cs +++ b/Moonlight/Program.cs @@ -152,7 +152,6 @@ namespace Moonlight builder.Services.AddDbContext(); // Repositories - builder.Services.AddSingleton(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); @@ -179,7 +178,6 @@ namespace Moonlight builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); - builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); @@ -215,6 +213,9 @@ namespace Moonlight builder.Services.AddScoped(); builder.Services.AddScoped(); + builder.Services.AddScoped(); + builder.Services.AddSingleton(); + // Loggers builder.Services.AddScoped(); builder.Services.AddSingleton(); diff --git a/Moonlight/Shared/Layouts/MainLayout.razor b/Moonlight/Shared/Layouts/MainLayout.razor index 8eac6b5..0730bd5 100644 --- a/Moonlight/Shared/Layouts/MainLayout.razor +++ b/Moonlight/Shared/Layouts/MainLayout.razor @@ -14,7 +14,7 @@ @inject IJSRuntime JsRuntime @inject IdentityService IdentityService -@inject SessionService SessionService +@inject SessionClientService SessionClientService @inject NavigationManager NavigationManager @inject EventSystem Event @inject ToastService ToastService @@ -215,12 +215,10 @@ } catch (Exception){ /* ignore errors to make sure that the session call is executed */ } - await SessionService.Register(); + await SessionClientService.Start(); NavigationManager.LocationChanged += async (_, _) => { - SessionService.Refresh(); - if (!NavigationManager.Uri.Contains("/server/")) await DynamicBackgroundService.Reset(); }; @@ -255,7 +253,7 @@ public async void Dispose() { - SessionService.Close(); + await SessionClientService.Stop(); await KeyListenerService.DisposeAsync(); diff --git a/Moonlight/Shared/Layouts/NotFoundLayout.razor b/Moonlight/Shared/Layouts/NotFoundLayout.razor index 8a792ae..7f0a158 100644 --- a/Moonlight/Shared/Layouts/NotFoundLayout.razor +++ b/Moonlight/Shared/Layouts/NotFoundLayout.razor @@ -9,7 +9,7 @@ @inject IJSRuntime JsRuntime @inject IdentityService IdentityService -@inject SessionService SessionService +@inject SessionClientService SessionClientService @inject NavigationManager NavigationManager @@ -106,9 +106,7 @@ await JsRuntime.InvokeVoidAsync("KTDrawer.createInstances"); await JsRuntime.InvokeVoidAsync("createSnow"); - await SessionService.Register(); - - NavigationManager.LocationChanged += (sender, args) => { SessionService.Refresh(); }; + await SessionClientService.Start(); } catch (Exception) { @@ -117,9 +115,9 @@ } } - public void Dispose() + public async void Dispose() { - SessionService.Close(); + await SessionClientService.Stop(); } private void AddBodyAttribute(string attribute, string value) diff --git a/Moonlight/Shared/Views/Admin/Users/Edit.razor b/Moonlight/Shared/Views/Admin/Users/Edit.razor index 846af80..8893f11 100644 --- a/Moonlight/Shared/Views/Admin/Users/Edit.razor +++ b/Moonlight/Shared/Views/Admin/Users/Edit.razor @@ -8,7 +8,7 @@ @inject UserRepository UserRepository @inject UserService UserService -@inject SessionService SessionService +@inject SessionServerService SessionServerService @inject ToastService ToastService @inject SmartTranslateService SmartTranslateService @@ -174,7 +174,7 @@ user.Status = User!.Status; UserRepository.Update(user); - SessionService.ReloadUserSessions(User); + await SessionServerService.ReloadUserSessions(User); await ToastService.Success(SmartTranslateService.Translate("Successfully updated user")); } @@ -191,7 +191,7 @@ await UserService.ChangePassword(User!, NewPassword, true); NewPassword = ""; - SessionService.ReloadUserSessions(User); + await SessionServerService.ReloadUserSessions(User!); await ToastService.Success(SmartTranslateService.Translate("Successfully updated password")); } diff --git a/Moonlight/Shared/Views/Admin/Users/Sessions.razor b/Moonlight/Shared/Views/Admin/Users/Sessions.razor index a828634..4d793cc 100644 --- a/Moonlight/Shared/Views/Admin/Users/Sessions.razor +++ b/Moonlight/Shared/Views/Admin/Users/Sessions.razor @@ -8,9 +8,10 @@ @using Moonlight.App.Services @using Moonlight.App.Services.Interop -@inject SessionService SessionService +@inject SessionServerService SessionServerService @inject SmartTranslateService SmartTranslateService @inject AlertService AlertService +@inject ToastService ToastService @@ -44,8 +45,8 @@ } else { - - +
+ - - - - + + + + + + + + - + - +