improve validation in Url Mapping

This commit is contained in:
sbwalker
2025-04-21 14:13:49 -04:00
parent 53de1ddb36
commit e0044658f9
2 changed files with 27 additions and 22 deletions

View File

@ -26,35 +26,37 @@
</form> </form>
@code { @code {
private ElementReference form; private ElementReference form;
private bool validated = false; private bool validated = false;
private string _url = string.Empty; private string _url = string.Empty;
private string _mappedurl = string.Empty; private string _mappedurl = string.Empty;
public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Admin; public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Admin;
private async Task SaveUrlMapping() private async Task SaveUrlMapping()
{ {
validated = true; validated = true;
var interop = new Interop(JSRuntime); var interop = new Interop(JSRuntime);
if (await interop.FormValid(form)) if (await interop.FormValid(form))
{ {
if (_url != _mappedurl) if (_url != _mappedurl)
{ {
var url = PageState.Uri.Scheme + "://" + PageState.Uri.Authority + "/"; var url = PageState.Uri.Scheme + "://" + PageState.Uri.Authority + "/";
url = url + (!string.IsNullOrEmpty(PageState.Alias.Path) ? PageState.Alias.Path + "/" : ""); url = url + (!string.IsNullOrEmpty(PageState.Alias.Path) ? PageState.Alias.Path + "/" : "");
_url = (_url.StartsWith("/")) ? _url.Substring(1) : _url; _url = (_url.StartsWith("/")) ? _url.Substring(1) : _url;
_url = (!_url.StartsWith("http")) ? url + _url : _url; _url = (!_url.StartsWith("http")) ? url + _url : _url;
if (_url.StartsWith(url)) _mappedurl = _mappedurl.Replace(url, "");
_mappedurl = (_mappedurl.StartsWith("/")) ? _mappedurl.Substring(1) : _mappedurl;
if (_url.StartsWith(url))
{ {
var urlmapping = new UrlMapping(); var urlmapping = new UrlMapping();
urlmapping.SiteId = PageState.Site.SiteId; urlmapping.SiteId = PageState.Site.SiteId;
var route = new Route(_url, PageState.Alias.Path); urlmapping.Url = new Route(_url, PageState.Alias.Path).PagePath;
urlmapping.Url = route.PagePath; urlmapping.MappedUrl = _mappedurl;
urlmapping.MappedUrl = _mappedurl.Replace(url, "");
urlmapping.Requests = 0; urlmapping.Requests = 0;
urlmapping.CreatedOn = DateTime.UtcNow; urlmapping.CreatedOn = DateTime.UtcNow;
urlmapping.RequestedOn = DateTime.UtcNow; urlmapping.RequestedOn = DateTime.UtcNow;

View File

@ -67,8 +67,11 @@
var url = PageState.Uri.Scheme + "://" + PageState.Uri.Authority + "/"; var url = PageState.Uri.Scheme + "://" + PageState.Uri.Authority + "/";
url = url + (!string.IsNullOrEmpty(PageState.Alias.Path) ? PageState.Alias.Path + "/" : ""); url = url + (!string.IsNullOrEmpty(PageState.Alias.Path) ? PageState.Alias.Path + "/" : "");
_mappedurl = _mappedurl.Replace(url, "");
_mappedurl = (_mappedurl.StartsWith("/")) ? _mappedurl.Substring(1) : _mappedurl;
var urlmapping = await UrlMappingService.GetUrlMappingAsync(_urlmappingid); var urlmapping = await UrlMappingService.GetUrlMappingAsync(_urlmappingid);
urlmapping.MappedUrl = _mappedurl.Replace(url, ""); urlmapping.MappedUrl = _mappedurl;
urlmapping = await UrlMappingService.UpdateUrlMappingAsync(urlmapping); urlmapping = await UrlMappingService.UpdateUrlMappingAsync(urlmapping);
await logger.LogInformation("UrlMapping Saved {UrlMapping}", urlmapping); await logger.LogInformation("UrlMapping Saved {UrlMapping}", urlmapping);
NavigationManager.NavigateTo(NavigateUrl()); NavigationManager.NavigateTo(NavigateUrl());