Esempio n. 1
0
        public async Task <OperationResult> EnableTwoFactorAuthForce()
        {
            try
            {
                await _appSettings.UpdateDb((options) => { options.IsTwoFactorForced = true; }, _dbContext);
            }
            catch (Exception)
            {
                return(new OperationResult(false));
            }

            return(new OperationResult(true));
        }
        public async Task <OperationResult> UpdateAdminSettings(AdminSettingsModel adminSettingsModel)
        {
            try
            {
                var core = await _coreHelper.GetCore();

                if (adminSettingsModel.SMTPSettingsModel != null)
                {
                    await _emailSettings.UpdateDb((option) =>
                    {
                        option.SmtpHost    = adminSettingsModel.SMTPSettingsModel.Host;
                        option.SmtpPort    = int.Parse(adminSettingsModel.SMTPSettingsModel.Port);
                        option.Login       = adminSettingsModel.SMTPSettingsModel.Login;
                        option.Password    = adminSettingsModel.SMTPSettingsModel.Password;
                        option.SendGridKey = adminSettingsModel.SendGridSettingsModel.ApiKey;
                    }, _dbContext);
                }

                if (adminSettingsModel.HeaderSettingsModel != null)
                {
                    await _headerSettings.UpdateDb((option) =>
                    {
                        option.ImageLink            = adminSettingsModel.HeaderSettingsModel.ImageLink;
                        option.ImageLinkVisible     = adminSettingsModel.HeaderSettingsModel.ImageLinkVisible;
                        option.MainText             = adminSettingsModel.HeaderSettingsModel.MainText;
                        option.MainTextVisible      = adminSettingsModel.HeaderSettingsModel.MainTextVisible;
                        option.SecondaryText        = adminSettingsModel.HeaderSettingsModel.SecondaryText;
                        option.SecondaryTextVisible = adminSettingsModel.HeaderSettingsModel.SecondaryTextVisible;
                    }, _dbContext);
                }

                if (adminSettingsModel.LoginPageSettingsModel != null)
                {
                    await _loginPageSettings.UpdateDb((option) =>
                    {
                        option.ImageLink            = adminSettingsModel.LoginPageSettingsModel.ImageLink;
                        option.ImageLinkVisible     = adminSettingsModel.LoginPageSettingsModel.ImageLinkVisible;
                        option.MainText             = adminSettingsModel.LoginPageSettingsModel.MainText;
                        option.MainTextVisible      = adminSettingsModel.LoginPageSettingsModel.MainTextVisible;
                        option.SecondaryText        = adminSettingsModel.LoginPageSettingsModel.SecondaryText;
                        option.SecondaryTextVisible = adminSettingsModel.LoginPageSettingsModel.SecondaryTextVisible;
                    }, _dbContext);
                }

                if (!string.IsNullOrEmpty(adminSettingsModel.SiteLink))
                {
                    await core.SetSdkSetting(Settings.httpServerAddress, adminSettingsModel.SiteLink);
                }

                if (adminSettingsModel.SwiftSettingsModel != null)
                {
                    await core.SetSdkSetting(
                        Settings.swiftEnabled,
                        adminSettingsModel.SwiftSettingsModel.SwiftEnabled.ToString());

                    await core.SetSdkSetting(
                        Settings.swiftUserName,
                        adminSettingsModel.SwiftSettingsModel.SwiftUserName);

                    if (adminSettingsModel.SwiftSettingsModel.SwiftPassword != "SOMESECRETPASSWORD")
                    {
                        await core.SetSdkSetting(Settings.swiftPassword,
                                                 adminSettingsModel.SwiftSettingsModel.SwiftPassword);
                    }

                    await core.SetSdkSetting(Settings.swiftEndPoint,
                                             adminSettingsModel.SwiftSettingsModel.SwiftEndpoint);

                    await core.SetSdkSetting(Settings.keystoneEndPoint,
                                             adminSettingsModel.SwiftSettingsModel.KeystoneEndpoint);
                }

                if (adminSettingsModel.SdkSettingsModel != null)
                {
                    await core.SetSdkSetting(Settings.customerNo, adminSettingsModel.SdkSettingsModel.CustomerNo);

                    await core.SetSdkSetting(Settings.logLevel, adminSettingsModel.SdkSettingsModel.LogLevel);

                    await core.SetSdkSetting(Settings.logLimit, adminSettingsModel.SdkSettingsModel.LogLimit);

                    await core.SetSdkSetting(Settings.fileLocationPicture, adminSettingsModel.SdkSettingsModel.FileLocationPicture);

                    await core.SetSdkSetting(Settings.fileLocationPdf, adminSettingsModel.SdkSettingsModel.FileLocationPdf);

                    await core.SetSdkSetting(Settings.fileLocationJasper, adminSettingsModel.SdkSettingsModel.FileLocationReports);

                    await core.SetSdkSetting(Settings.httpServerAddress, adminSettingsModel.SdkSettingsModel.HttpServerAddress);
                }

                if (adminSettingsModel.S3SettingsModel != null)
                {
                    await core.SetSdkSetting(Settings.s3Enabled, adminSettingsModel.S3SettingsModel.S3Enabled.ToString());

                    await core.SetSdkSetting(Settings.s3AccessKeyId, adminSettingsModel.S3SettingsModel.S3AccessKeyId);

                    await core.SetSdkSetting(Settings.s3BucketName, adminSettingsModel.S3SettingsModel.S3BucketName);

                    if (adminSettingsModel.S3SettingsModel.S3SecrectAccessKey != "SOMESECRETPASSWORD")
                    {
                        await core.SetSdkSetting(Settings.s3SecrectAccessKey, adminSettingsModel.S3SettingsModel.S3SecrectAccessKey);
                    }

                    await core.SetSdkSetting(Settings.s3Endpoint, adminSettingsModel.S3SettingsModel.S3Endpoint);
                }

                return(new OperationResult(true, _localizationService.GetString("SettingsUpdatedSuccessfully")));
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
                return(new OperationResult(false, _localizationService.GetString("CantUpdateSettingsInWebConfig")));
            }
        }
        public async Task <OperationResult> UpdateConnectionString(InitialSettingsModel initialSettingsModel)
        {
            var customerNo   = initialSettingsModel.GeneralAppSetupSettingsModel.CustomerNo.ToString();
            var dbNamePrefix = "";

            if (initialSettingsModel.ConnectionStringSdk.PrefixAllDatabases)
            {
                dbNamePrefix = "Microting_";
            }

            var sdkDbName     = $"{dbNamePrefix}{customerNo}_SDK";
            var angularDbName = $"{dbNamePrefix}{customerNo}_Angular";

            var sdkConnectionString =
                $"host= {initialSettingsModel.ConnectionStringSdk.Host};" +
                $"Database={sdkDbName};{initialSettingsModel.ConnectionStringSdk.Auth}" +
                $"port={initialSettingsModel.ConnectionStringSdk.Port};" +
                "Convert Zero Datetime = true;SslMode=none;";

            var angularConnectionString =
                $"host= {initialSettingsModel.ConnectionStringSdk.Host};" +
                $"Database={angularDbName};{initialSettingsModel.ConnectionStringSdk.Auth}" +
                $"port={initialSettingsModel.ConnectionStringSdk.Port};" +
                "Convert Zero Datetime = true;SslMode=none;";


            if (!string.IsNullOrEmpty(_connectionStringsSdk.Value.SdkConnection))
            {
                return(new OperationResult(false,
                                           _localizationService.GetString("ConnectionStringAlreadyExist")));
            }

            try
            {
                Log.LogEvent($"SettingsService.ConnectionStringExist: connection string is {sdkConnectionString}");
                var adminTools = new AdminTools(sdkConnectionString);
                //                 Setup SDK DB
                await adminTools.DbSetup(initialSettingsModel.ConnectionStringSdk.Token);

                //                var core = await _coreHelper.GetCore();
                Core core = new Core();
                await core.StartSqlOnly(sdkConnectionString);

                await core.SetSdkSetting(Settings.customerNo, customerNo);
            }
            catch (Exception exception)
            {
                _logger.LogError(exception.Message);
                _logger.LogError(exception.StackTrace);
                if (exception.InnerException != null)
                {
                    return(new OperationResult(false, exception.Message + " - " + exception.InnerException.Message));
                }

                return(new OperationResult(false, exception.Message));

                //return new OperationResult(false,
                //    _localizationService.GetString("SDKConnectionStringIsInvalid"));
            }

            // Migrate DB
            var dbContextOptionsBuilder = new DbContextOptionsBuilder <BaseDbContext>();

            try
            {
                dbContextOptionsBuilder.UseMySql(angularConnectionString,
                                                 new MariaDbServerVersion(
                                                     new Version(10, 4, 0)),
                                                 b =>
                                                 b.EnableRetryOnFailure());


                await using var dbContext = new BaseDbContext(dbContextOptionsBuilder.Options);
                await dbContext.Database.MigrateAsync();

                if (initialSettingsModel.AdminSetupModel != null)
                {
                    // Seed admin and demo users
                    await SeedAdminHelper.SeedAdmin(initialSettingsModel.AdminSetupModel,
                                                    initialSettingsModel.GeneralAppSetupSettingsModel.DefaultLocale, dbContext);
                }
            }
            catch (Exception exception)
            {
                _logger.LogError(exception.Message);
                _logger.LogError(exception.StackTrace);
                //return new OperationResult(false,
                //    _localizationService.GetString("MainConnectionStringIsInvalid"));
                if (exception.Message == "Could not create the user")
                {
                    return(new OperationResult(false,
                                               _localizationService.GetString(exception.Message)));
                }

                if (exception.InnerException != null)
                {
                    return(new OperationResult(false, exception.Message + " - " + exception.InnerException.Message));
                }

                return(new OperationResult(false, exception.Message));
            }

            try
            {
                // Generate SigningKey
                var key = new byte[32];
                RandomNumberGenerator.Create().GetBytes(key);
                var signingKey = Convert.ToBase64String(key);

                // Update Database settings
                await using var dbContext = new BaseDbContext(dbContextOptionsBuilder.Options);
                await _tokenOptions.UpdateDb((options) => { options.SigningKey = signingKey; }, dbContext);

                await _applicationSettings.UpdateDb(
                    options =>
                {
                    options.DefaultLocale = initialSettingsModel.GeneralAppSetupSettingsModel.DefaultLocale;
                }, dbContext);

                await _connectionStringsSdk.UpdateDb((options) =>
                {
                    options.SdkConnection = sdkConnectionString;
                }, dbContext);

                // Update connection string
                _connectionStrings.UpdateFile((options) =>
                {
                    options.DefaultConnection = angularConnectionString;
                });
            }

            catch (Exception exception)
            {
                _logger.LogError(exception.Message);
                _logger.LogError(exception.StackTrace);
                return(exception.InnerException != null
                    ? new OperationResult(false, exception.Message + " - " + exception.InnerException.Message)
                    : new OperationResult(false, exception.Message));

                //return new OperationResult(false,
                //    _localizationService.GetString("CouldNotWriteConnectionString"));
            }

            Program.Restart();
            return(new OperationResult(true));
        }