public static Page GetCurrentPage(this StoreFront storeFront, HttpRequestBase request, bool throwErrorIfNotFound = true) { string url = request.Url.AbsolutePath.Trim('/').ToLower(); string appPath = request.ApplicationPath.Trim('/').ToLower(); //remove app path for virtual directories running if (!string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(appPath) && url.StartsWith(appPath)) { url = url.Remove(0, appPath.Length).Trim('/'); } string urlStoreName = request.BindingUrlStoreName(); if (!string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(urlStoreName) && url.StartsWith("stores/" + urlStoreName)) { url = url.Remove(0, ("stores/" + urlStoreName).Length).Trim('/'); } url = "/" + url; return storeFront.GetCurrentPage(url, throwErrorIfNotFound); }
public static List<StoreBinding> GetInactiveStoreBindingMatches(this IGstoreDb db, HttpRequestBase request) { return db.GetInactiveStoreBindingMatches(request.BindingUrlStoreName(), request.BindingHostName(), request.BindingRootPath(), request.BindingPort()); }
public static void SetDefaultsForNew(this StoreBinding storeBinding, HttpRequestBase request, int? clientId, int? storeFrontId) { if (clientId.HasValue) { storeBinding.ClientId = clientId.Value; } if (storeFrontId.HasValue) { storeBinding.StoreFrontId = storeFrontId.Value; } storeBinding.Order = 1000; storeBinding.HostName = request.BindingHostName(); storeBinding.Port = request.BindingPort(); storeBinding.RootPath = request.BindingRootPath(); storeBinding.UrlStoreName = request.BindingUrlStoreName(); storeBinding.UseUrlStoreName = !string.IsNullOrEmpty(storeBinding.UrlStoreName); storeBinding.IsPending = false; storeBinding.StartDateTimeUtc = DateTime.UtcNow.AddMinutes(-1); storeBinding.EndDateTimeUtc = DateTime.UtcNow.AddYears(100); }
/// <summary> /// Gets the current store front, or null if anonymous; uses CachedStoreFront from context if available /// </summary> /// <param name="db"></param> /// <param name="request"></param> /// <param name="throwErrorIfNotFound">If true, throws an error when storefront is not found</param> /// <returns></returns> public static StoreFrontConfiguration GetCurrentStoreFrontConfig(this IGstoreDb db, HttpRequestBase request, bool throwErrorIfNotFound, bool returnInactiveIfFound) { //if context has already set the current store front, return it if (db.CachedStoreFrontConfig != null) { //note: only active storefront is cached, inactives are always queried from database return db.CachedStoreFrontConfig; } if (db.CachedStoreFront != null && db.CachedStoreFrontConfig == null) { //storefront is set, but not config, try to get current config StoreFrontConfiguration storeFrontActiveCurrentConfig = db.CachedStoreFront.CurrentConfig(); if (storeFrontActiveCurrentConfig != null) { db.CachedStoreFrontConfig = storeFrontActiveCurrentConfig; return storeFrontActiveCurrentConfig; } } if (request == null) { if (throwErrorIfNotFound) { throw new ApplicationException("Request context is null, cannot get current store front"); } return null; } //verify database has been seeded and not blank if (db.StoreBindings.IsEmpty()) { if (throwErrorIfNotFound) { string error = "No Store bindings in database."; if (db.StoreFronts.IsEmpty()) { error = "No Store Fronts in database. Be sure database is seeded or force a seed of the database." + "\n" + error; } if (db.Clients.IsEmpty()) { error = "No Clients in database. Be sure database is seeded or force a seed of the database." + "\n" + error; } if (Settings.AppDoNotSeedDatabase) { error += "\nSettings.AppDoNotSeedDatabase = true. Change this settings to false to allow the system to seed the database with client and store front data."; } throw new Exceptions.NoMatchingBindingException(error, request.Url); } return null; } StoreBinding activeStoreBinding = db.GetCurrentStoreBindingOrNull(request); if (activeStoreBinding != null) { //active match found, update cache and return the active match db.CachedStoreFront = activeStoreBinding.StoreFront; db.CachedStoreFrontConfig = activeStoreBinding.StoreFrontConfiguration; return activeStoreBinding.StoreFrontConfiguration; } if ((throwErrorIfNotFound == false) && (returnInactiveIfFound == false)) { //if throwErrorIfNotFound = false (means no error throw) //and //if returnInactiveIfFound = false (means I don't want an inactive record //so: if we're not throwing an exception and we're not returning an inactive, just quit with null return null; } string errorMessage = "No match found in active store bindings.\n" + " \n BindingHostName: " + request.BindingHostName() + " \n BindingRootPath:" + request.BindingRootPath() + " \n BindingPort:" + request.BindingPort().ToString() + " \n UrlStoreName: " + request.BindingUrlStoreName() + " \n RawUrl: " + request.RawUrl + " \n IP Address: " + request.UserHostAddress + "\n You may want to add a binding catch-all such as HostName=*, RootPath=*, Port=0"; //why can't we find an active binding? get inactive matches to find if it's an inactive record List<StoreBinding> inactiveBindings = db.GetInactiveStoreBindingMatches(request); if (inactiveBindings.Count == 0) { //No match in the database for this host name, root path, and port. //throw error to show ("no store page: GStoreNotFound.html") applies also to hostname hackers and spoofs with host headers errorMessage = "Error! StoreFront not found. \nNo StoreBindings match the current host name, port, and RootPath.\n" + "\n No matching bindings found in the inactive records. This is an unhandled URL or a hostname hack." + "\n\n" + errorMessage; //we could not find an inactive record, so throw a no match exception or return null if throwErrorIfNotFound = false if (throwErrorIfNotFound) { throw new Exceptions.NoMatchingBindingException(errorMessage, request.Url); } return null; } if (returnInactiveIfFound) { //if returnInactiveIfFound = true; return the best matching inactive record (first) return inactiveBindings[0].StoreFrontConfiguration; } ///build error message with details that might help find the inactive record for system admin to fix errorMessage = "Error! No ACTIVE StoreFront found in bindings match." + " \n The best match for this URL is inactive." + "\n\n " + errorMessage + "\n" + inactiveBindings.StoreBindingsErrorHelper(); throw new Exceptions.StoreFrontInactiveException(errorMessage, request.Url, inactiveBindings[0].StoreFront); }