// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, TenantsDbContext tenantsDbContext) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); } else { app.UseExceptionHandler("/Home/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseMultiTenancy(); app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { if (Configuration.GetValue <bool>("UsePathToResolveTenant")) { endpoints.MapControllerRoute( name: "default", pattern: "{tenant}/{controller=Home}/{action=Index}/{id?}"); } else { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); } endpoints.MapRazorPages(); }); tenantsDbContext.Database.Migrate(); SeedData.SeedTenants(GetConnectionStrings(), tenantsDbContext); CustomTenantsFileManager.AddAnyNewCustomDomainsOrIps(tenantsDbContext); }
public async Task <IActionResult> UpdateIpAddresses(Tenant tenant) { try { if (tenant == null || string.IsNullOrWhiteSpace(tenant.Name)) { return(StatusCode((int)HttpStatusCode.UnprocessableEntity, "The tenant name must not be null")); } var tenantName = tenant.Name; if (_tenantsDbContext.Tenants.Any(tenant => tenant.Name == tenantName) == false) { return(StatusCode((int)HttpStatusCode.InternalServerError, $"The tenant: {tenantName}. Is not in the database so the ip addresses could not be updated.")); } var updatedTenant = _tenantsDbContext.Tenants.SingleOrDefault(tenant => tenant.Name == tenantName); if (updatedTenant.IpAddresses != tenant.IpAddresses) { var oldIpAddresses = updatedTenant.IpAddresses; updatedTenant.IpAddresses = tenant.IpAddresses; _tenantsDbContext.Update(updatedTenant); await _tenantsDbContext.SaveChangesAsync(); CustomTenantsFileManager.UpdateCustomTenant(updatedTenant); return(Ok($"Updated {tenantName}'s Domain Names from: {oldIpAddresses} to: {tenant.IpAddresses}")); } else { return(StatusCode((int)HttpStatusCode.InternalServerError, $"The ip addresses for {tenantName} are the same.")); } } catch (Exception ex) { _logger.LogError(ex.Message); return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message)); } }
public async Task <IActionResult> DeleteTenant(string tenantName) { try { if (string.IsNullOrWhiteSpace(tenantName)) { return(StatusCode((int)HttpStatusCode.UnprocessableEntity, "The tenant name cannot be empty")); } if (_configuration.GetValue <string>("DefaultTenant") == tenantName) { return(StatusCode((int)HttpStatusCode.InternalServerError, "You cannot delete the default tenant")); } var tenantToDelete = _tenantsDbContext.Tenants.FirstOrDefault(tenant => tenant.Name == tenantName); if (tenantToDelete != null) { TenantsCustomFolderManager.DeleteTenantsCustomFolder(_tenantsDirectory, tenantName); TenantsCustomFolderManager.DeleteTenantFromConfiguration(tenantToDelete.Name); CustomTenantsFileManager.RemoveCustomTenant(tenantToDelete.Name); _tenantsDbContext.Tenants.Remove(tenantToDelete); await _tenantsDbContext.SaveChangesAsync(); return(Ok($"Tenant: {tenantName} has been deleted")); } else { return(StatusCode((int)HttpStatusCode.InternalServerError, $"The tenant: {tenantName}. Does not exist.")); } } catch (Exception ex) { _logger.LogError(ex.Message); return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message)); } }