private bool IsModelValid(CustomSetupViewModel model) { if (String.IsNullOrEmpty(model.Password)) { ModelState.AddModelError(nameof(model.Password), S["The password is required."]); } if (model.Password != model.PasswordConfirmation) { ModelState.AddModelError(nameof(model.PasswordConfirmation), S["The password confirmation doesn't match the password."]); } if (!_emailAddressValidator.Validate(model.Email)) { ModelState.AddModelError(nameof(model.Email), S["Invalid email."]); } if (!ModelState.IsValid) { if (!String.IsNullOrEmpty(_shellSettings["Description"])) { model.Description = _shellSettings["Description"]; } return(false); } return(true); }
public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null) { var settings = (await _siteService.GetSiteSettingsAsync()).As <RegistrationSettings>(); if (settings.UsersCanRegister != UserRegistrationType.AllowRegistration) { return(NotFound()); } if (string.IsNullOrEmpty(model.Email)) { ModelState.AddModelError("Email", S["Email is required."]); } if (_emailAddressValidator.Validate(model.Email)) { // Check if user with same email already exists var userWithEmail = await _userManager.FindByEmailAsync(model.Email); if (userWithEmail != null) { ModelState.AddModelError("Email", S["A user with the same email already exists."]); } } else { ModelState.AddModelError("Email", S["Invalid email."]); } ViewData["ReturnUrl"] = returnUrl; if (TryValidateModel(model) && ModelState.IsValid) { var iUser = await this.RegisterUser(model, S["Confirm your account"], _logger); // If we get a user, redirect to returnUrl if (iUser is User user) { if (settings.UsersMustValidateEmail && !user.EmailConfirmed) { return(RedirectToAction("ConfirmEmailSent", new { ReturnUrl = returnUrl })); } if (settings.UsersAreModerated && !user.IsEnabled) { return(RedirectToAction("RegistrationPending", new { ReturnUrl = returnUrl })); } return(RedirectToLocal(returnUrl)); } } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <ActionResult> Setup(SetupApiViewModel model) { if (!IsDefaultShell()) { return(this.ChallengeOrForbid("Api")); } if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageTenants)) { return(this.ChallengeOrForbid("Api")); } if (!String.IsNullOrEmpty(model.UserName) && model.UserName.Any(c => !_identityOptions.User.AllowedUserNameCharacters.Contains(c))) { ModelState.AddModelError(nameof(model.UserName), S["User name '{0}' is invalid, can only contain letters or digits.", model.UserName]); } // Only add additional error if attribute validation has passed. if (!String.IsNullOrEmpty(model.Email) && !_emailAddressValidator.Validate(model.Email)) { ModelState.AddModelError(nameof(model.Email), S["The email is invalid."]); } if (!ModelState.IsValid) { return(BadRequest()); } if (!_shellHost.TryGetSettings(model.Name, out var shellSettings)) { ModelState.AddModelError(nameof(SetupApiViewModel.Name), S["Tenant not found: '{0}'", model.Name]); } if (shellSettings.State == TenantState.Running) { return(StatusCode(201)); } if (shellSettings.State != TenantState.Uninitialized) { return(BadRequest(S["The tenant can't be setup."])); } var databaseProvider = shellSettings["DatabaseProvider"]; if (String.IsNullOrEmpty(databaseProvider)) { databaseProvider = model.DatabaseProvider; } var selectedProvider = _databaseProviders.FirstOrDefault(x => String.Equals(x.Value, databaseProvider, StringComparison.OrdinalIgnoreCase)); if (selectedProvider == null) { return(BadRequest(S["The database provider is not defined."])); } var tablePrefix = shellSettings["TablePrefix"]; if (String.IsNullOrEmpty(tablePrefix)) { tablePrefix = model.TablePrefix; } var connectionString = shellSettings["connectionString"]; if (String.IsNullOrEmpty(connectionString)) { connectionString = model.ConnectionString; } if (selectedProvider.HasConnectionString && String.IsNullOrEmpty(connectionString)) { return(BadRequest(S["The connection string is required for this database provider."])); } var recipeName = shellSettings["RecipeName"]; if (String.IsNullOrEmpty(recipeName)) { recipeName = model.RecipeName; } RecipeDescriptor recipeDescriptor = null; if (String.IsNullOrEmpty(recipeName)) { if (model.Recipe == null) { return(BadRequest(S["Either 'Recipe' or 'RecipeName' is required."])); } var tempFilename = Path.GetTempFileName(); using (var fs = System.IO.File.Create(tempFilename)) { await model.Recipe.CopyToAsync(fs); } var fileProvider = new PhysicalFileProvider(Path.GetDirectoryName(tempFilename)); recipeDescriptor = new RecipeDescriptor { FileProvider = fileProvider, BasePath = "", RecipeFileInfo = fileProvider.GetFileInfo(Path.GetFileName(tempFilename)) }; } else { var setupRecipes = await _setupService.GetSetupRecipesAsync(); recipeDescriptor = setupRecipes.FirstOrDefault(x => String.Equals(x.Name, recipeName, StringComparison.OrdinalIgnoreCase)); if (recipeDescriptor == null) { return(BadRequest(S["Recipe '{0}' not found.", recipeName])); } } var setupContext = new SetupContext { ShellSettings = shellSettings, EnabledFeatures = null, // default list, Errors = new Dictionary <string, string>(), Recipe = recipeDescriptor, Properties = new Dictionary <string, object> { { SetupConstants.SiteName, model.SiteName }, { SetupConstants.AdminUsername, model.UserName }, { SetupConstants.AdminEmail, model.Email }, { SetupConstants.AdminPassword, model.Password }, { SetupConstants.SiteTimeZone, model.SiteTimeZone }, { SetupConstants.DatabaseProvider, selectedProvider.Value }, { SetupConstants.DatabaseConnectionString, connectionString }, { SetupConstants.DatabaseTablePrefix, tablePrefix }, } }; var executionId = await _setupService.SetupAsync(setupContext); // Check if a component in the Setup failed if (setupContext.Errors.Any()) { foreach (var error in setupContext.Errors) { ModelState.AddModelError(error.Key, error.Value); } return(StatusCode(500, ModelState)); } return(Ok(executionId)); }
public async Task <ActionResult> IndexPOST(SetupViewModel model) { if (!string.IsNullOrWhiteSpace(_shellSettings["Secret"])) { if (string.IsNullOrEmpty(model.Secret) || !await IsTokenValid(model.Secret)) { _logger.LogWarning("An attempt to access '{TenantName}' without providing a valid secret was made", _shellSettings.Name); return(StatusCode(404)); } } model.DatabaseProviders = _databaseProviders; model.Recipes = await _setupService.GetSetupRecipesAsync(); var selectedProvider = model.DatabaseProviders.FirstOrDefault(x => x.Value == model.DatabaseProvider); if (!model.DatabaseConfigurationPreset) { if (selectedProvider != null && selectedProvider.HasConnectionString && String.IsNullOrWhiteSpace(model.ConnectionString)) { ModelState.AddModelError(nameof(model.ConnectionString), S["The connection string is mandatory for this provider."]); } } if (String.IsNullOrEmpty(model.Password)) { ModelState.AddModelError(nameof(model.Password), S["The password is required."]); } if (model.Password != model.PasswordConfirmation) { ModelState.AddModelError(nameof(model.PasswordConfirmation), S["The password confirmation doesn't match the password."]); } RecipeDescriptor selectedRecipe = null; if (!string.IsNullOrEmpty(_shellSettings["RecipeName"])) { selectedRecipe = model.Recipes.FirstOrDefault(x => x.Name == _shellSettings["RecipeName"]); if (selectedRecipe == null) { ModelState.AddModelError(nameof(model.RecipeName), S["Invalid recipe."]); } } else if (String.IsNullOrEmpty(model.RecipeName) || (selectedRecipe = model.Recipes.FirstOrDefault(x => x.Name == model.RecipeName)) == null) { ModelState.AddModelError(nameof(model.RecipeName), S["Invalid recipe."]); } // Only add additional errors if attribute validation has passed. if (!String.IsNullOrEmpty(model.Email) && !_emailAddressValidator.Validate(model.Email)) { ModelState.AddModelError(nameof(model.Email), S["The email is invalid."]); } if (!String.IsNullOrEmpty(model.UserName) && model.UserName.Any(c => !_identityOptions.User.AllowedUserNameCharacters.Contains(c))) { ModelState.AddModelError(nameof(model.UserName), S["User name '{0}' is invalid, can only contain letters or digits.", model.UserName]); } if (!ModelState.IsValid) { CopyShellSettingsValues(model); return(View(model)); } var setupContext = new SetupContext { ShellSettings = _shellSettings, EnabledFeatures = null, // default list, Errors = new Dictionary <string, string>(), Recipe = selectedRecipe, Properties = new Dictionary <string, object> { { SetupConstants.SiteName, model.SiteName }, { SetupConstants.AdminUsername, model.UserName }, { SetupConstants.AdminEmail, model.Email }, { SetupConstants.AdminPassword, model.Password }, { SetupConstants.SiteTimeZone, model.SiteTimeZone }, } }; if (!string.IsNullOrEmpty(_shellSettings["ConnectionString"])) { setupContext.Properties[SetupConstants.DatabaseProvider] = _shellSettings["DatabaseProvider"]; setupContext.Properties[SetupConstants.DatabaseConnectionString] = _shellSettings["ConnectionString"]; setupContext.Properties[SetupConstants.DatabaseTablePrefix] = _shellSettings["TablePrefix"]; } else { setupContext.Properties[SetupConstants.DatabaseProvider] = model.DatabaseProvider; setupContext.Properties[SetupConstants.DatabaseConnectionString] = model.ConnectionString; setupContext.Properties[SetupConstants.DatabaseTablePrefix] = model.TablePrefix; } var executionId = await _setupService.SetupAsync(setupContext); // Check if a component in the Setup failed if (setupContext.Errors.Any()) { foreach (var error in setupContext.Errors) { ModelState.AddModelError(error.Key, error.Value); } return(View(model)); } return(Redirect("~/")); }