add setting import
This commit is contained in:
@ -14,6 +14,8 @@ using Microsoft.AspNetCore.Authentication.OAuth;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.IO;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Oqtane.Controllers
|
||||
{
|
||||
@ -264,6 +266,70 @@ namespace Oqtane.Controllers
|
||||
return _settings.GetEntityIds(entityName);
|
||||
}
|
||||
|
||||
// POST api/<controller>/import?settings=x
|
||||
[HttpPost("import")]
|
||||
[Authorize(Roles = RoleNames.Host)]
|
||||
public bool Import(string settings)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(settings))
|
||||
{
|
||||
using (StringReader reader = new StringReader(settings))
|
||||
{
|
||||
// regex to split by comma - ignoring commas within double quotes
|
||||
string pattern = ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)";
|
||||
string row;
|
||||
|
||||
while ((row = reader.ReadLine()) != null)
|
||||
{
|
||||
List<string> cols = new List<string>();
|
||||
string col = "";
|
||||
int startIndex = 0;
|
||||
|
||||
MatchCollection matches = Regex.Matches(row, pattern);
|
||||
foreach (Match match in matches)
|
||||
{
|
||||
col = row.Substring(startIndex, match.Index - startIndex);
|
||||
if (col.StartsWith("\"") && col.EndsWith("\""))
|
||||
{
|
||||
col = col.Substring(1, col.Length - 2).Replace("\"\"", "\"");
|
||||
}
|
||||
cols.Add(col.Trim());
|
||||
startIndex = match.Index + match.Length;
|
||||
}
|
||||
col = row.Substring(startIndex);
|
||||
if (col.StartsWith("\"") && col.EndsWith("\""))
|
||||
{
|
||||
col = col.Substring(1, col.Length - 2).Replace("\"\"", "\"");
|
||||
}
|
||||
cols.Add(col.Trim());
|
||||
|
||||
if (cols.Count == 5 && cols[0].ToLower() != "entity" && int.TryParse(cols[1], out int entityId) && bool.TryParse(cols[4], out bool isPrivate))
|
||||
{
|
||||
var setting = _settings.GetSetting(cols[0], entityId, cols[2]);
|
||||
if (setting == null)
|
||||
{
|
||||
_settings.AddSetting(new Setting { EntityName = cols[0], EntityId = entityId, SettingName = cols[2], SettingValue = cols[3], IsPrivate = isPrivate });
|
||||
}
|
||||
else
|
||||
{
|
||||
setting.SettingValue = cols[3];
|
||||
setting.IsPrivate = isPrivate;
|
||||
_settings.UpdateSetting(setting);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Settings Import Attempt {Settings}", settings);
|
||||
HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE api/<controller>/clear
|
||||
[HttpDelete("clear")]
|
||||
[Authorize(Roles = RoleNames.Admin)]
|
||||
|
@ -833,6 +833,34 @@ namespace Oqtane.Infrastructure.SiteTemplates
|
||||
}
|
||||
}
|
||||
});
|
||||
pageTemplates.Add(new PageTemplate
|
||||
{
|
||||
Name = "Setting Management",
|
||||
Parent = "Admin",
|
||||
Order = 67,
|
||||
Path = "admin/settings",
|
||||
Icon = Icons.Cog,
|
||||
IsNavigation = false,
|
||||
IsPersonalizable = false,
|
||||
PermissionList = new List<Permission>
|
||||
{
|
||||
new Permission(PermissionNames.View, RoleNames.Host, true),
|
||||
new Permission(PermissionNames.Edit, RoleNames.Host, true)
|
||||
},
|
||||
PageTemplateModules = new List<PageTemplateModule>
|
||||
{
|
||||
new PageTemplateModule
|
||||
{
|
||||
ModuleDefinitionName = typeof(Oqtane.Modules.Admin.Settings.Index).ToModuleDefinitionName(), Title = "Setting Management", Pane = PaneNames.Default,
|
||||
PermissionList = new List<Permission>
|
||||
{
|
||||
new Permission(PermissionNames.View, RoleNames.Host, true),
|
||||
new Permission(PermissionNames.Edit, RoleNames.Host, true)
|
||||
},
|
||||
Content = ""
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return pageTemplates;
|
||||
}
|
||||
|
@ -87,6 +87,9 @@ namespace Oqtane.Infrastructure
|
||||
case "6.1.5":
|
||||
Upgrade_6_1_5(tenant, scope);
|
||||
break;
|
||||
case "6.2.0":
|
||||
Upgrade_6_2_0(tenant, scope);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -557,6 +560,43 @@ namespace Oqtane.Infrastructure
|
||||
RemoveAssemblies(tenant, assemblies, "6.1.5");
|
||||
}
|
||||
|
||||
private void Upgrade_6_2_0(Tenant tenant, IServiceScope scope)
|
||||
{
|
||||
var pageTemplates = new List<PageTemplate>
|
||||
{
|
||||
new PageTemplate
|
||||
{
|
||||
Update = false,
|
||||
Name = "Setting Management",
|
||||
Parent = "Admin",
|
||||
Order = 67,
|
||||
Path = "admin/settings",
|
||||
Icon = Icons.Cog,
|
||||
IsNavigation = false,
|
||||
IsPersonalizable = false,
|
||||
PermissionList = new List<Permission>
|
||||
{
|
||||
new Permission(PermissionNames.View, RoleNames.Host, true),
|
||||
new Permission(PermissionNames.Edit, RoleNames.Host, true)
|
||||
},
|
||||
PageTemplateModules = new List<PageTemplateModule>
|
||||
{
|
||||
new PageTemplateModule
|
||||
{
|
||||
ModuleDefinitionName = typeof(Oqtane.Modules.Admin.Settings.Index).ToModuleDefinitionName(), Title = "Setting Management", Pane = PaneNames.Default,
|
||||
PermissionList = new List<Permission>
|
||||
{
|
||||
new Permission(PermissionNames.View, RoleNames.Host, true),
|
||||
new Permission(PermissionNames.Edit, RoleNames.Host, true)
|
||||
},
|
||||
Content = ""
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
AddPagesToSites(scope, tenant, pageTemplates);
|
||||
}
|
||||
|
||||
private void AddPagesToSites(IServiceScope scope, Tenant tenant, List<PageTemplate> pageTemplates)
|
||||
{
|
||||
|
@ -1,9 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Oqtane.Enums;
|
||||
using Oqtane.Infrastructure;
|
||||
using Oqtane.Models;
|
||||
using Oqtane.Modules.Admin.Users;
|
||||
using Oqtane.Shared;
|
||||
|
||||
namespace Oqtane.Repository
|
||||
@ -21,6 +26,7 @@ namespace Oqtane.Repository
|
||||
void DeleteSettings(string entityName, int entityId);
|
||||
IEnumerable<string> GetEntityNames();
|
||||
IEnumerable<int> GetEntityIds(string entityName);
|
||||
|
||||
string GetSettingValue(IEnumerable<Setting> settings, string settingName, string defaultValue);
|
||||
string GetSettingValue(string entityName, int entityId, string settingName, string defaultValue);
|
||||
}
|
||||
|
Reference in New Issue
Block a user