Merge pull request #5588 from sbwalker/dev

improve setting import
This commit is contained in:
Shaun Walker
2025-09-08 12:55:58 -04:00
committed by GitHub
3 changed files with 20 additions and 11 deletions

View File

@ -31,8 +31,8 @@
if (!string.IsNullOrEmpty(_settings)) if (!string.IsNullOrEmpty(_settings))
{ {
ShowProgressIndicator(); ShowProgressIndicator();
var success = await SettingService.ImportSettingsAsync(_settings); var result = await SettingService.ImportSettingsAsync(new Result { Message = _settings });
if (success) if (result.Success)
{ {
AddModuleMessage(Localizer["Message.Import.Success"], MessageType.Success); AddModuleMessage(Localizer["Message.Import.Success"], MessageType.Success);
} }

View File

@ -258,7 +258,7 @@ namespace Oqtane.Services
/// </summary> /// </summary>
/// <param name="settings"></param> /// <param name="settings"></param>
/// <returns></returns> /// <returns></returns>
Task<bool> ImportSettingsAsync(string settings); Task<Result> ImportSettingsAsync(Result settings);
/// <summary> /// <summary>
/// Gets the value of the given settingName (key) from the given key-value dictionary /// Gets the value of the given settingName (key) from the given key-value dictionary
@ -524,9 +524,9 @@ namespace Oqtane.Services
return await GetJsonAsync<List<int>>($"{Apiurl}/entityids?entityname={entityName}"); return await GetJsonAsync<List<int>>($"{Apiurl}/entityids?entityname={entityName}");
} }
public async Task<bool> ImportSettingsAsync(string settings) public async Task<Result> ImportSettingsAsync(Result settings)
{ {
return await PostJsonAsync<bool>($"{Apiurl}/import?settings={settings}", true); return await PostJsonAsync<Result>($"{Apiurl}/import", settings);
} }
public string GetSetting(Dictionary<string, string> settings, string settingName, string defaultValue) public string GetSetting(Dictionary<string, string> settings, string settingName, string defaultValue)

View File

@ -16,6 +16,7 @@ using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using System.IO; using System.IO;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Oqtane.Migrations.Tenant;
namespace Oqtane.Controllers namespace Oqtane.Controllers
{ {
@ -269,11 +270,13 @@ namespace Oqtane.Controllers
// POST api/<controller>/import?settings=x // POST api/<controller>/import?settings=x
[HttpPost("import")] [HttpPost("import")]
[Authorize(Roles = RoleNames.Host)] [Authorize(Roles = RoleNames.Host)]
public bool Import(string settings) public Result Import([FromBody] Result settings)
{ {
if (!string.IsNullOrEmpty(settings)) if (ModelState.IsValid && !string.IsNullOrEmpty(settings.Message))
{ {
using (StringReader reader = new StringReader(settings)) int rows = 0;
using (StringReader reader = new StringReader(settings.Message))
{ {
// regex to split by comma - ignoring commas within double quotes // regex to split by comma - ignoring commas within double quotes
string pattern = ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"; string pattern = ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)";
@ -316,17 +319,23 @@ namespace Oqtane.Controllers
setting.IsPrivate = isPrivate; setting.IsPrivate = isPrivate;
_settings.UpdateSetting(setting); _settings.UpdateSetting(setting);
} }
rows++;
} }
} }
} }
return true; _logger.Log(LogLevel.Information, this, LogFunction.Create, "Settings Imported {Settings}", settings.Message);
settings.Message = $"{rows} Settings Imported";
settings.Success = true;
return settings;
} }
else else
{ {
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Settings Import Attempt {Settings}", settings); _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Settings Import Attempt {Settings}", settings.Message);
HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden; HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden;
return false; settings.Message = "";
settings.Success = false;
return settings;
} }
} }