public Redirect SaveRedirect(ViewModels.SiteSettings.SiteSettingRedirectViewModel model, bool createIfNotExists = false)
{
bool isDestinationInternal;
bool isDestinationInternalWithQueryString = false;
isDestinationInternal = urlService.ValidateInternalUrl(model.RedirectUrl);
if (!isDestinationInternal && urlService.ValidateInternalUrl(urlService.FixUrl(model.RedirectUrl)))
{
isDestinationInternal = true;
}
if (urlService.ValidateInternalUrlWithQueryString(model.RedirectUrl))
{
isDestinationInternalWithQueryString = true;
}
// Validate redirect source field
var isSourceUrlInternal = urlService.ValidateInternalUrl(model.PageUrl);
var isSourceUrlInternalWithQueryString = urlService.ValidateInternalUrlWithQueryString(model.PageUrl);
if (isSourceUrlInternal)
{
model.PageUrl = urlService.FixUrl(model.PageUrl);
}
else if (isSourceUrlInternalWithQueryString)
{
model.PageUrl = urlService.FixUrlFront(model.PageUrl);
}
else
{
var message = PagesGlobalization.SaveRedirect_InvalidPageUrl_Message;
var logMessage = string.Format("Invalid page url {0}.", model.PageUrl);
throw new ValidationException(() => message, logMessage);
}
if (isDestinationInternal)
{
model.RedirectUrl = urlService.FixUrl(model.RedirectUrl);
}
else if (isDestinationInternalWithQueryString)
{
model.RedirectUrl = urlService.FixUrlFront(model.RedirectUrl);
}
else if (!urlService.ValidateExternalUrl(model.RedirectUrl))
{
var message = PagesGlobalization.SaveRedirect_InvalidRedirectUrl_Message;
var logMessage = string.Format("Invalid redirect url {0}.", model.RedirectUrl);
throw new ValidationException(() => message, logMessage);
}
// Validate for url patterns
string patternsValidationMessage;
if (!urlService.ValidateUrlPatterns(model.PageUrl, out patternsValidationMessage))
{
var logMessage = string.Format("{0}. URL: {1}.", patternsValidationMessage, model.PageUrl);
throw new ValidationException(() => patternsValidationMessage, logMessage);
}
if ((isDestinationInternal || isDestinationInternalWithQueryString) && !urlService.ValidateUrlPatterns(model.RedirectUrl, out patternsValidationMessage, PagesGlobalization.SaveRedirect_RedirectUrl_Name))
{
var logMessage = string.Format("{0}. URL: {1}.", patternsValidationMessage, model.PageUrl);
throw new ValidationException(() => patternsValidationMessage, logMessage);
}
ValidateRedirectExists(model.PageUrl, model.Id);
ValidateForCircularLoop(model.PageUrl, model.RedirectUrl, model.Id);
Redirect redirect = null;
var isNew = model.Id.HasDefaultValue();
if (!isNew)
{
redirect = repository.FirstOrDefault<Redirect>(model.Id);
isNew = redirect == null;
if (isNew && !createIfNotExists)
{
throw new EntityNotFoundException(typeof(Redirect), model.Id);
}
}
if (isNew)
{
redirect = new Redirect { Id = model.Id };
}
if (model.Version > 0)
{
redirect.Version = model.Version;
}
redirect.PageUrl = model.PageUrl;
redirect.RedirectUrl = model.RedirectUrl;
repository.Save(redirect);
unitOfWork.Commit();
// Notify.
if (isNew)
{
Events.PageEvents.Instance.OnRedirectCreated(redirect);
}
else
{
Events.PageEvents.Instance.OnRedirectUpdated(redirect);
}
return redirect;
}