public async Task <bool> SetConfigAsync(string pluginId, int siteId, string name, object config)
        {
            if (name == null)
            {
                name = string.Empty;
            }

            try
            {
                if (config == null)
                {
                    await DeleteAsync(pluginId, siteId, name);
                }
                else
                {
                    var settings = new JsonSerializerSettings
                    {
                        NullValueHandling = NullValueHandling.Ignore
                    };
                    var json = JsonConvert.SerializeObject(config, Formatting.Indented, settings);
                    if (await IsExistsAsync(pluginId, siteId, name))
                    {
                        var pluginConfig = new PluginConfig
                        {
                            PluginId    = pluginId,
                            SiteId      = siteId,
                            ConfigName  = name,
                            ConfigValue = json
                        };
                        await UpdateAsync(pluginConfig);
                    }
                    else
                    {
                        var pluginConfig = new PluginConfig
                        {
                            PluginId    = pluginId,
                            SiteId      = siteId,
                            ConfigName  = name,
                            ConfigValue = json
                        };
                        await InsertAsync(pluginConfig);
                    }
                }
            }
            catch (Exception ex)
            {
                await _errorLogRepository.AddErrorLogAsync(pluginId, ex);

                return(false);
            }
            return(true);
        }
Exemple #2
0
        public async Task AddAdminLogAsync(Administrator admin, string ipAddress, string action, string summary = "")
        {
            var config = await _configRepository.GetAsync();

            if (!config.IsLogAdmin)
            {
                return;
            }

            try
            {
                await DeleteIfThresholdAsync();

                if (!string.IsNullOrEmpty(action))
                {
                    action = StringUtils.MaxLengthText(action, 250);
                }
                if (!string.IsNullOrEmpty(summary))
                {
                    summary = StringUtils.MaxLengthText(summary, 250);
                }

                var log = new Log
                {
                    Id        = 0,
                    AdminId   = admin.Id,
                    IpAddress = ipAddress,
                    Action    = action,
                    Summary   = summary
                };

                await _repository.InsertAsync(log);

                await _administratorRepository.UpdateLastActivityDateAsync(admin);
            }
            catch (Exception ex)
            {
                await _errorLogRepository.AddErrorLogAsync(ex);
            }
        }
        public async Task <T> GetAsync <T>(string pluginId, int siteId, string name)
        {
            if (name == null)
            {
                name = string.Empty;
            }

            try
            {
                var value = await GetConfigValueAsync(pluginId, siteId, name);

                var typeCode = Type.GetTypeCode(typeof(T));
                if (typeCode == TypeCode.Int32)
                {
                    return(TranslateUtils.Get <T>(TranslateUtils.ToInt(value)));
                }
                if (typeCode == TypeCode.Decimal)
                {
                    return(TranslateUtils.Get <T>(TranslateUtils.ToDecimal(value)));
                }
                if (typeCode == TypeCode.DateTime)
                {
                    return(TranslateUtils.Get <T>(TranslateUtils.ToDateTime(value)));
                }
                if (typeCode == TypeCode.Boolean)
                {
                    return(TranslateUtils.Get <T>(TranslateUtils.ToBool(value)));
                }
                if (typeCode == TypeCode.String)
                {
                    return(TranslateUtils.Get <T>(value));
                }
                if (!string.IsNullOrEmpty(value))
                {
                    return(JsonConvert.DeserializeObject <T>(value, Settings));
                }
            }
            catch (Exception ex)
            {
                await _errorLogRepository.AddErrorLogAsync(pluginId, ex);
            }
            return(default);
Exemple #4
0
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ISettingsManager settingsManager, IPluginManager pluginManager, IErrorLogRepository errorLogRepository, IOptions <SenparcSetting> senparcSetting)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseExceptionHandler(a => a.Run(async context =>
            {
                var exceptionHandlerPathFeature = context.Features.Get <IExceptionHandlerPathFeature>();
                var exception = exceptionHandlerPathFeature.Error;

                try
                {
                    errorLogRepository.AddErrorLogAsync(exception).GetAwaiter().GetResult();
                }
                catch
                {
                    // ignored
                }

                string result;
                if (env.IsDevelopment())
                {
                    result = TranslateUtils.JsonSerialize(new
                    {
                        exception.Message,
                        exception.StackTrace,
                        AddDate = DateTime.Now
                    });
                }
                else
                {
                    result = TranslateUtils.JsonSerialize(new
                    {
                        exception.Message,
                        AddDate = DateTime.Now
                    });
                }
                context.Response.ContentType = "application/json";
                await context.Response.WriteAsync(result);
            }));

            app.UseCors(CorsPolicy);

            app.UseForwardedHeaders(new ForwardedHeadersOptions
            {
                ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
            });

            //app.UseHttpsRedirection();

            var options = new DefaultFilesOptions();

            options.DefaultFileNames.Clear();
            options.DefaultFileNames.Add("index.html");
            app.UseDefaultFiles(options);

            //if (settingsManager.Containerized)
            //{
            //    app.Map($"/{DirectoryUtils.SiteFiles.DirectoryName}/assets", assets =>
            //    {
            //        assets.UseStaticFiles(new StaticFileOptions
            //        {
            //            FileProvider = new PhysicalFileProvider(PathUtils.Combine(settingsManager.ContentRootPath, "assets"))
            //        });
            //    });
            //}

            app.UseStaticFiles();

            var supportedCultures = new[]
            {
                new CultureInfo("en-US"),
                new CultureInfo("zh-CN")
            };

            app.UseRequestLocalization(new RequestLocalizationOptions
            {
                DefaultRequestCulture = new RequestCulture("zh-CN"),
                // Formatting numbers, dates, etc.
                SupportedCultures = supportedCultures,
                // UI strings that we have localized.
                SupportedUICultures = supportedCultures
            });

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UsePluginsAsync(settingsManager, pluginManager, errorLogRepository).GetAwaiter().GetResult();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapHealthChecks("/healthz");

                endpoints.MapControllers();
                endpoints.MapRazorPages();
            });
            //api.UseEndpoints(endpoints => { endpoints.MapControllerRoute("default", "{controller}/{action}/{id?}"); });

            app.UseRequestLocalization();

            RegisterService.Start(senparcSetting.Value)
            //自动扫描自定义扩展缓存(二选一)
            .UseSenparcGlobal(true)
            //指定自定义扩展缓存(二选一)
            //.UseSenparcGlobal(false, () => GetExCacheStrategies(senparcSetting.Value))
            ;

            app.UseOpenApi();
            app.UseSwaggerUi3();
            app.UseReDoc(settings =>
            {
                settings.Path         = "/api/docs";
                settings.DocumentPath = "/swagger/v1/swagger.json";
            });
        }