Exemplo n.º 1
0
        public async Task <IActionResult> SettingsPost(EditTenantSettingsViewModel viewModel)
        {
            // Ensure we have permission
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.EditSettings))
            {
                return(Unauthorized());
            }

            // Execute view providers ProvideUpdateAsync method
            await _settingsViewProvider.ProvideUpdateAsync(new DefaultTenantSettings(), this);

            // Add alert
            _alerter.Success(T["Settings Updated Successfully!"]);

            return(RedirectToAction(nameof(Settings)));
        }
Exemplo n.º 2
0
        public override async Task <IViewProviderResult> BuildUpdateAsync(DefaultTenantSettings settings, IViewProviderContext context)
        {
            var model = new EditTenantSettingsViewModel();

            // Validate model
            if (!await context.Updater.TryUpdateModelAsync(model))
            {
                return(await BuildEditAsync(settings, context));
            }

            // Update settings
            if (context.Updater.ModelState.IsValid)
            {
                // Encrypt connection string
                var connectionString = string.Empty;
                if (!string.IsNullOrWhiteSpace(model.ConnectionString))
                {
                    try
                    {
                        connectionString = _encrypter.Encrypt(model.ConnectionString);
                    }
                    catch (Exception e)
                    {
                        if (_logger.IsEnabled(LogLevel.Error))
                        {
                            _logger.LogError($"There was a problem encrypting the default tenant connection string. {e.Message}");
                        }
                    }
                }

                // Encrypt password
                var password = string.Empty;
                if (!string.IsNullOrWhiteSpace(model.SmtpSettings.Password))
                {
                    try
                    {
                        password = _encrypter.Encrypt(model.SmtpSettings.Password);
                    }
                    catch (Exception e)
                    {
                        if (_logger.IsEnabled(LogLevel.Error))
                        {
                            _logger.LogError($"There was a problem encrypting the SMTP server password. {e.Message}");
                        }
                    }
                }

                settings = new DefaultTenantSettings()
                {
                    ConnectionString = connectionString,
                    TablePrefix      = model.TablePrefix,
                    SmtpSettings     = new SmtpSettings()
                    {
                        DefaultFrom        = model.SmtpSettings.DefaultFrom,
                        Host               = model.SmtpSettings.Host,
                        Port               = model.SmtpSettings.Port,
                        UserName           = model.SmtpSettings.UserName,
                        Password           = password,
                        RequireCredentials = model.SmtpSettings.RequireCredentials,
                        EnableSsl          = model.SmtpSettings.EnableSsl
                    }
                };

                var result = await _tenantSettingsStore.SaveAsync(settings);

                if (result != null)
                {
                    // Recycle shell context to ensure changes take effect
                    _platoHost.RecycleShell(_shellSettings);
                }
            }

            return(await BuildEditAsync(settings, context));
        }