| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.AspNetCore.Mvc; |
| | | 3 | | using Microsoft.Extensions.Configuration; |
| | | 4 | | using Newtonsoft.Json.Linq; |
| | | 5 | | using SlqStudio.Application.Services.AppSettingsServices; |
| | | 6 | | |
| | | 7 | | namespace SlqStudio.Controllers; |
| | | 8 | | |
| | | 9 | | [Authorize(Roles = "Admin")] |
| | | 10 | | [Area("Studio2")] |
| | | 11 | | public class ConfigController : Controller |
| | | 12 | | { |
| | | 13 | | private readonly IAppSettingsService _appSettingsService; |
| | | 14 | | private readonly AppSettingsBuilder _appSettingsBuilder; |
| | 0 | 15 | | private readonly string _appSettingsPath = Path.Combine(Directory.GetCurrentDirectory(), "appsettings.json"); |
| | | 16 | | |
| | 0 | 17 | | public ConfigController(IAppSettingsService appSettingsService, AppSettingsBuilder appSettingsBuilder) |
| | | 18 | | { |
| | 0 | 19 | | _appSettingsService = appSettingsService; |
| | 0 | 20 | | _appSettingsBuilder = appSettingsBuilder; |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | // GET: /Config/Login |
| | | 24 | | public IActionResult Login() |
| | | 25 | | { |
| | 0 | 26 | | return View(); |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | [HttpPost] |
| | | 30 | | public IActionResult Login(string name, string password) |
| | | 31 | | { |
| | 0 | 32 | | var config = _appSettingsService.ReadConfig(_appSettingsPath); |
| | 0 | 33 | | var userSection = config["ConfigUser"]; |
| | | 34 | | |
| | 0 | 35 | | if (userSection != null && |
| | 0 | 36 | | name == userSection["Name"]?.ToString() && |
| | 0 | 37 | | password == userSection["Password"]?.ToString()) |
| | | 38 | | { |
| | 0 | 39 | | HttpContext.Session.SetString("IsConfigAuthorized", "true"); |
| | | 40 | | |
| | 0 | 41 | | return RedirectToAction("EditConfig"); |
| | | 42 | | } |
| | | 43 | | |
| | 0 | 44 | | ViewBag.Error = "Неверное имя пользователя или пароль."; |
| | 0 | 45 | | return View(); |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | public IActionResult EditConfig() |
| | | 49 | | { |
| | 0 | 50 | | if (HttpContext.Session.GetString("IsConfigAuthorized") != "true") |
| | 0 | 51 | | return RedirectToAction("Login"); |
| | | 52 | | |
| | 0 | 53 | | var jsonObj = _appSettingsService.ReadConfig(_appSettingsPath); |
| | 0 | 54 | | return View(jsonObj); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | [HttpPost] |
| | | 58 | | public IActionResult SaveConfig(IFormCollection form) |
| | | 59 | | { |
| | 0 | 60 | | var updatedConfig = _appSettingsBuilder.BuildAppSettings(form); |
| | 0 | 61 | | _appSettingsService.WriteConfig(updatedConfig, _appSettingsPath); |
| | 0 | 62 | | return RedirectToAction("EditConfig"); |
| | | 63 | | } |
| | | 64 | | } |