Пример #1
0
        public async Task <IActionResult> Create(CreateApiViewModel model)
        {
            if (!_currentShellSettings.IsDefaultShell())
            {
                return(Forbid());
            }

            if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageTenants))
            {
                return(this.ChallengeOrForbid("Api"));
            }

            // Creates a default shell settings based on the configuration.
            var shellSettings = _shellSettingsManager.CreateDefaultSettings();

            shellSettings.Name             = model.Name;
            shellSettings.RequestUrlHost   = model.RequestUrlHost;
            shellSettings.RequestUrlPrefix = model.RequestUrlPrefix;
            shellSettings.State            = TenantState.Uninitialized;

            shellSettings["ConnectionString"] = model.ConnectionString;
            shellSettings["TablePrefix"]      = model.TablePrefix;
            shellSettings["DatabaseProvider"] = model.DatabaseProvider;
            shellSettings["Secret"]           = Guid.NewGuid().ToString();
            shellSettings["RecipeName"]       = model.RecipeName;
            shellSettings["FeatureProfile"]   = model.FeatureProfile;

            model.IsNewTenant = true;

            ModelState.AddModelErrors(await _tenantValidator.ValidateAsync(model));

            if (ModelState.IsValid)
            {
                if (_shellHost.TryGetSettings(model.Name, out var settings))
                {
                    // Site already exists, return 201 for indempotency purpose

                    var token = CreateSetupToken(settings);

                    return(StatusCode(201, GetEncodedUrl(settings, token)));
                }
                else
                {
                    await _shellHost.UpdateShellSettingsAsync(shellSettings);

                    var token = CreateSetupToken(shellSettings);

                    return(Ok(GetEncodedUrl(shellSettings, token)));
                }
            }

            return(BadRequest(ModelState));
        }
Пример #2
0
        private async Task ValidateViewModelAsync(EditTenantViewModel model, bool newTenant)
        {
            model.IsNewTenant = newTenant;

            ModelState.AddModelErrors(await _tenantValidator.ValidateAsync(model));
        }