using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using EonaCat.LogStack.Status.Services; namespace EonaCat.LogStack.Status.Pages.Admin; public class SettingsModel : PageModel { // This file is part of the EonaCat project(s) which is released under the Apache License. // See the LICENSE file or go to https://EonaCat.com/License for full license details. private readonly AuthenticationService _authenticationService; public SettingsModel(AuthenticationService authentication) => _authenticationService = authentication; [BindProperty] public string SiteName { get; set; } = ""; [BindProperty] public bool ShowLogsPublicly { get; set; } [BindProperty] public bool ShowUptimePublicly { get; set; } [BindProperty] public int MaxLogRetentionDays { get; set; } = 30; [BindProperty] public string AlertEmail { get; set; } = ""; [BindProperty] public string CurrentPassword { get; set; } = ""; [BindProperty] public string NewPassword { get; set; } = ""; [BindProperty] public string ConfirmPassword { get; set; } = ""; public string? Message { get; set; } public string? PasswordMessage { get; set; } public async Task OnGetAsync() { if (HttpContext.Session.GetString("IsAdmin") != "true") { return RedirectToPage("/Admin/Login"); } SiteName = await _authenticationService.GetSettingAsync("SiteName", "Status"); ShowLogsPublicly = (await _authenticationService.GetSettingAsync("ShowLogsPublicly", "false")) == "true"; ShowUptimePublicly = (await _authenticationService.GetSettingAsync("ShowUptimePublicly", "true")) == "true"; MaxLogRetentionDays = int.TryParse(await _authenticationService.GetSettingAsync("MaxLogRetentionDays", "30"), out var d) ? d : 30; AlertEmail = await _authenticationService.GetSettingAsync("AlertEmail", ""); return Page(); } public async Task OnPostSaveSettingsAsync() { if (HttpContext.Session.GetString("IsAdmin") != "true") { return RedirectToPage("/Admin/Login"); } await _authenticationService.SetSettingAsync("SiteName", SiteName ?? "Status"); await _authenticationService.SetSettingAsync("ShowLogsPublicly", ShowLogsPublicly ? "true" : "false"); await _authenticationService.SetSettingAsync("ShowUptimePublicly", ShowUptimePublicly ? "true" : "false"); await _authenticationService.SetSettingAsync("MaxLogRetentionDays", MaxLogRetentionDays.ToString()); await _authenticationService.SetSettingAsync("AlertEmail", AlertEmail ?? ""); Message = "Settings saved."; return await OnGetAsync(); } public async Task OnPostChangePasswordAsync() { if (HttpContext.Session.GetString("IsAdmin") != "true") { return RedirectToPage("/Admin/Login"); } if (NewPassword != ConfirmPassword) { PasswordMessage = "error:Passwords do not match."; return await OnGetAsync(); } if (NewPassword.Length < 6) { PasswordMessage = "error:Password must be at least 6 characters."; return await OnGetAsync(); } if (await _authenticationService.ChangePasswordAsync(CurrentPassword, NewPassword)) { PasswordMessage = "success:Password changed successfully."; } else { PasswordMessage = "error:Current password is incorrect."; } return await OnGetAsync(); } }