DI: ConfigurationManager

This commit is contained in:
pavelbannov 2019-09-20 18:08:45 +03:00
parent f992e1a763
commit 8184492a4f
13 changed files with 91 additions and 71 deletions

View File

@ -15,16 +15,19 @@ namespace ASC.Api.Core.Middleware
LogManager logManager,
AuthContext authContext,
IPRestrictionsSettings iPRestrictionsSettings,
IPSecurity.IPSecurity IPSecurity,
TenantManager tenantManager)
{
log = logManager.Get("Api");
AuthContext = authContext;
IPRestrictionsSettings = iPRestrictionsSettings;
this.IPSecurity = IPSecurity;
TenantManager = tenantManager;
}
public AuthContext AuthContext { get; }
public IPRestrictionsSettings IPRestrictionsSettings { get; }
public IPSecurity.IPSecurity IPSecurity { get; }
public TenantManager TenantManager { get; }
public void OnResourceExecuted(ResourceExecutedContext context)
@ -35,7 +38,7 @@ namespace ASC.Api.Core.Middleware
{
var tenant = TenantManager.GetCurrentTenant();
var settings = IPRestrictionsSettings.Load();
if (settings.Enable && AuthContext.IsAuthenticated && !IPSecurity.IPSecurity.Verify(context.HttpContext, tenant, AuthContext))
if (settings.Enable && AuthContext.IsAuthenticated && !IPSecurity.Verify(tenant))
{
context.Result = new StatusCodeResult((int)HttpStatusCode.Forbidden);
log.WarnFormat("IPSecurity: Tenant {0}, user {1}", tenant.TenantId, AuthContext.CurrentAccount.ID);

View File

@ -31,35 +31,31 @@ using System.Security.Cryptography;
using System.Text;
using ASC.Common.Logging;
using ASC.Common.Utils;
using ASC.Core.Tenants;
using ASC.Core.Users;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Configuration;
namespace ASC.Core.Billing
{
public class LicenseReader
{
private static readonly ILog Log = LogManager.GetLogger("ASC");
private static readonly string LicensePath;
private static readonly string LicensePathTemp;
private readonly string LicensePath;
private readonly string LicensePathTemp;
public const string CustomerIdKey = "CustomerId";
public const int MaxUserCount = 10000;
static LicenseReader()
{
LicensePath = ConfigurationManager.AppSettings["license:file:path"];
LicensePathTemp = LicensePath + ".tmp";
}
public LicenseReader(UserManager userManager, TenantManager tenantManager, PaymentManager paymentManager, CoreSettings coreSettings)
public LicenseReader(UserManager userManager, TenantManager tenantManager, PaymentManager paymentManager, CoreSettings coreSettings, IConfiguration configuration)
{
UserManager = userManager;
TenantManager = tenantManager;
PaymentManager = paymentManager;
CoreSettings = coreSettings;
Configuration = configuration;
LicensePath = Configuration["license:file:path"];
LicensePathTemp = LicensePath + ".tmp";
}
public string CustomerId
@ -68,7 +64,7 @@ namespace ASC.Core.Billing
private set { CoreSettings.SaveSetting(CustomerIdKey, value); }
}
private static Stream GetLicenseStream(bool temp = false)
private Stream GetLicenseStream(bool temp = false)
{
var path = temp ? LicensePathTemp : LicensePath;
if (!File.Exists(path)) throw new BillingNotFoundException("License not found");
@ -272,7 +268,7 @@ namespace ASC.Core.Billing
private static DateTime _date = DateTime.MinValue;
public static DateTime VersionReleaseDate
public DateTime VersionReleaseDate
{
get
{
@ -281,8 +277,8 @@ namespace ASC.Core.Billing
_date = DateTime.MaxValue;
try
{
var versionDate = ConfigurationManager.AppSettings["version:release:date"];
var sign = ConfigurationManager.AppSettings["version:release:sign"];
var versionDate = Configuration["version:release:date"];
var sign = Configuration["version:release:sign"];
if (!sign.StartsWith("ASC "))
{
@ -299,7 +295,7 @@ namespace ASC.Core.Billing
var date = splitted[1];
var orighash = splitted[2];
var skey = ConfigurationManager.AppSettings["core:machinekey"];
var skey = Configuration["core:machinekey"];
using (var hasher = new HMACSHA1(Encoding.UTF8.GetBytes(skey)))
{
@ -328,5 +324,6 @@ namespace ASC.Core.Billing
public TenantManager TenantManager { get; }
public PaymentManager PaymentManager { get; }
public CoreSettings CoreSettings { get; }
public IConfiguration Configuration { get; }
}
}

View File

@ -39,6 +39,7 @@ using ASC.Common.Utils;
using ASC.Core.Data;
using ASC.Core.Tenants;
using Microsoft.Extensions.Configuration;
namespace ASC.Core.Billing
{
@ -96,7 +97,8 @@ namespace ASC.Core.Billing
IQuotaService quotaService,
ITenantService tenantService,
CoreBaseSettings coreBaseSettings,
CoreSettings coreSettings)
CoreSettings coreSettings,
IConfiguration configuration)
: base(connectionString, "tenant")
{
this.quotaService = quotaService;
@ -104,8 +106,8 @@ namespace ASC.Core.Billing
CoreSettings = coreSettings;
CoreBaseSettings = coreBaseSettings;
CacheExpiration = DEFAULT_CACHE_EXPIRATION;
test = ConfigurationManager.AppSettings["core:payment:test"] == "true";
int.TryParse(ConfigurationManager.AppSettings["core:payment:delay"], out paymentDelay);
test = configuration["core:payment:test"] == "true";
int.TryParse(configuration["core:payment:delay"], out paymentDelay);
}

View File

@ -67,14 +67,15 @@ namespace ASC.Core
throw new ConfigurationErrorsException("Can not configure CoreContext: connection string with name core not found.");
}
var coreBaseSettings = new CoreBaseSettings(CommonServiceProvider.GetService<IConfiguration>());
var configuration = CommonServiceProvider.GetService<IConfiguration>();
var coreBaseSettings = new CoreBaseSettings(configuration);
var tenantService = new CachedTenantService(new DbTenantService(cs), coreBaseSettings);
var coreSettings = new CoreSettings(tenantService, coreBaseSettings);
var coreSettings = new CoreSettings(tenantService, coreBaseSettings, configuration);
var quotaService = QuotaCacheEnabled ? (IQuotaService)new CachedQuotaService(new DbQuotaService(cs)) : new DbQuotaService(cs);
var tariffService = new TariffService(cs, quotaService, tenantService, coreBaseSettings, coreSettings);
var tariffService = new TariffService(cs, quotaService, tenantService, coreBaseSettings, coreSettings, configuration);
TenantManager = new TenantManager(tenantService, quotaService, tariffService, null, coreBaseSettings, coreSettings);
Configuration = new CoreConfiguration(coreBaseSettings, coreSettings, TenantManager);
Configuration = new CoreConfiguration(coreBaseSettings, coreSettings, TenantManager, configuration);
}
}
}

View File

@ -73,7 +73,7 @@ namespace ASC.Core
{
if (basedomain == null)
{
basedomain = ConfigurationManager.AppSettings["core:base-domain"] ?? string.Empty;
basedomain = Configuration["core:base-domain"] ?? string.Empty;
}
string result;
@ -98,11 +98,13 @@ namespace ASC.Core
public ITenantService TenantService { get; }
public CoreBaseSettings CoreBaseSettings { get; }
public IConfiguration Configuration { get; }
public CoreSettings(ITenantService tenantService, CoreBaseSettings coreBaseSettings)
public CoreSettings(ITenantService tenantService, CoreBaseSettings coreBaseSettings, IConfiguration configuration)
{
TenantService = tenantService;
CoreBaseSettings = coreBaseSettings;
Configuration = configuration;
}
public void SaveSetting(string key, string value, int tenant = Tenant.DEFAULT_TENANT)
@ -157,7 +159,7 @@ namespace ASC.Core
if (t != null && !string.IsNullOrWhiteSpace(t.PaymentId))
return t.PaymentId;
return ConfigurationManager.AppSettings["core:payment:region"] + tenant;
return Configuration["core:payment:region"] + tenant;
}
}
@ -175,11 +177,12 @@ namespace ASC.Core
{
private long? personalMaxSpace;
public CoreConfiguration(CoreBaseSettings coreBaseSettings, CoreSettings coreSettings, TenantManager tenantManager)
public CoreConfiguration(CoreBaseSettings coreBaseSettings, CoreSettings coreSettings, TenantManager tenantManager, IConfiguration configuration)
{
CoreBaseSettings = coreBaseSettings;
CoreSettings = coreSettings;
TenantManager = tenantManager;
Configuration = configuration;
}
public bool Standalone
@ -192,11 +195,6 @@ namespace ASC.Core
get => CoreBaseSettings.Personal;
}
public bool CustomMode
{
get => CoreBaseSettings.CustomMode;
}
public long PersonalMaxSpace(PersonalQuotaSettings personalQuotaSettings)
{
var quotaSettings = personalQuotaSettings.LoadForCurrentUser();
@ -208,7 +206,7 @@ namespace ASC.Core
return personalMaxSpace.Value;
if (!long.TryParse(ConfigurationManager.AppSettings["core.personal.maxspace"], out var value))
if (!long.TryParse(Configuration["core.personal.maxspace"], out var value))
value = long.MaxValue;
personalMaxSpace = value;
@ -256,6 +254,7 @@ namespace ASC.Core
public CoreBaseSettings CoreBaseSettings { get; }
public CoreSettings CoreSettings { get; }
public TenantManager TenantManager { get; }
public IConfiguration Configuration { get; }
#region Methods Get/Save Setting

View File

@ -39,6 +39,7 @@ using ASC.Common.Utils;
using ASC.Core.Billing;
using ASC.Core.Users;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
@ -53,15 +54,17 @@ namespace ASC.Core
private readonly string partnerKey;
public TenantManager TenantManager { get; }
public IConfiguration Configuration { get; }
public PaymentManager(CoreSettings config, TenantManager tenantManager, IQuotaService quotaService, ITariffService tariffService)
public PaymentManager(CoreSettings config, TenantManager tenantManager, IQuotaService quotaService, ITariffService tariffService, IConfiguration configuration)
{
this.config = config;
TenantManager = tenantManager;
this.quotaService = quotaService;
this.tariffService = tariffService;
partnerUrl = (ConfigurationManager.AppSettings["core:payment:partners"] ?? "https://partners.onlyoffice.com/api").TrimEnd('/');
partnerKey = (ConfigurationManager.AppSettings["core:machinekey"] ?? "C5C1F4E85A3A43F5B3202C24D97351DF");
Configuration = configuration;
partnerUrl = (Configuration["core:payment:partners"] ?? "https://partners.onlyoffice.com/api").TrimEnd('/');
partnerKey = (Configuration["core:machinekey"] ?? "C5C1F4E85A3A43F5B3202C24D97351DF");
}
@ -115,7 +118,7 @@ namespace ASC.Core
var trial = quotaService.GetTenantQuotas().FirstOrDefault(q => q.Trial);
if (trial != null)
{
var uri = ConfigurationManager.AppSettings["core:payment:request"] ?? "http://billing.onlyoffice.com/avangate/requestatrialversion.aspx";
var uri = Configuration["core:payment:request"] ?? "http://billing.onlyoffice.com/avangate/requestatrialversion.aspx";
uri += uri.Contains('?') ? "&" : "?";
uri += "FIRSTNAME=" + HttpUtility.UrlEncode(user.FirstName) +
"&LASTNAME=" + HttpUtility.UrlEncode(user.FirstName) +

View File

@ -68,13 +68,14 @@ namespace ASC.Core
public HostedSolution(ConnectionStringSettings connectionString, string region)
{
var configuration = CommonServiceProvider.GetService<IConfiguration>();
tenantService = new DbTenantService(connectionString);
var baseSettings = new CoreBaseSettings(CommonServiceProvider.GetService<IConfiguration>());
var coreSettings = new CoreSettings(tenantService, baseSettings);
var baseSettings = new CoreBaseSettings(configuration);
var coreSettings = new CoreSettings(tenantService, baseSettings, configuration);
userService = new DbUserService(connectionString);
quotaService = new DbQuotaService(connectionString);
tariffService = new TariffService(connectionString, quotaService, tenantService, baseSettings, coreSettings);
tariffService = new TariffService(connectionString, quotaService, tenantService, baseSettings, coreSettings, configuration);
clientTenantManager = new TenantManager(tenantService, quotaService, tariffService, null, baseSettings, coreSettings);
settingsManager = new DbSettingsManager(connectionString);
Region = region ?? string.Empty;

View File

@ -34,7 +34,7 @@ namespace ASC.Core.Common.Tests
[TestFixture]
public class ClientPaymentManagerTest
{
private readonly PaymentManager paymentManager = new PaymentManager(null, null, null, null);
private readonly PaymentManager paymentManager = new PaymentManager(null, null, null, null, null);
private IServiceProvider serviceProvider;
[Test]

View File

@ -43,10 +43,11 @@ namespace ASC.Core.Common.Tests
public TariffServiceTest()
{
var configuration = CommonServiceProvider.GetService<IConfiguration>();
var cs = ConfigurationManager.ConnectionStrings["core"];
var tenantService = new DbTenantService(cs);
var baseSettings = new CoreBaseSettings(CommonServiceProvider.GetService<IConfiguration>());
tariffService = new TariffService(cs, new DbQuotaService(cs), tenantService, baseSettings, new CoreSettings(tenantService, baseSettings));
var baseSettings = new CoreBaseSettings(configuration);
tariffService = new TariffService(cs, new DbQuotaService(cs), tenantService, baseSettings, new CoreSettings(tenantService, baseSettings, configuration), configuration);
}

View File

@ -34,34 +34,39 @@ using ASC.Core;
using ASC.Core.Tenants;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.Extensions.Configuration;
namespace ASC.IPSecurity
{
public static class IPSecurity
public class IPSecurity
{
private static readonly ILog Log = LogManager.GetLogger("ASC.IPSecurity");
private static bool? _ipSecurityEnabled;
public bool IpSecurityEnabled { get; }
public static bool IpSecurityEnabled
public IConfiguration Configuration { get; }
public IHttpContextAccessor HttpContextAccessor { get; }
public AuthContext AuthContext { get; }
private readonly string CurrentIpForTest;
public IPSecurity(IConfiguration configuration, IHttpContextAccessor httpContextAccessor, AuthContext authContext)
{
get
{
if (_ipSecurityEnabled.HasValue) return _ipSecurityEnabled.Value;
var hideSettings = (ConfigurationManager.AppSettings["web.hide-settings"] ?? "").Split(new[] { ',', ';', ' ' });
return (_ipSecurityEnabled = !hideSettings.Contains("IpSecurity", StringComparer.CurrentCultureIgnoreCase)).Value;
}
Configuration = configuration;
HttpContextAccessor = httpContextAccessor;
AuthContext = authContext;
CurrentIpForTest = configuration["ipsecurity:test"];
var hideSettings = (configuration["web:hide-settings"] ?? "").Split(new[] { ',', ';', ' ' });
IpSecurityEnabled = !hideSettings.Contains("IpSecurity", StringComparer.CurrentCultureIgnoreCase);
}
private static readonly string CurrentIpForTest = ConfigurationManager.AppSettings["ipsecurity.test"];
public static bool Verify(HttpContext httpContext, Tenant tenant, AuthContext authContext)
public bool Verify(Tenant tenant)
{
if (!IpSecurityEnabled) return true;
if (httpContext == null) return true;
if (HttpContextAccessor?.HttpContext == null) return true;
if (tenant == null || authContext.CurrentAccount.ID == tenant.OwnerId) return true;
if (tenant == null || AuthContext.CurrentAccount.ID == tenant.OwnerId) return true;
string requestIps = null;
try
@ -72,7 +77,7 @@ namespace ASC.IPSecurity
if (string.IsNullOrWhiteSpace(requestIps = CurrentIpForTest))
{
var request = httpContext.Request;
var request = HttpContextAccessor.HttpContext.Request;
requestIps = request.Headers["X-Forwarded-For"].FirstOrDefault() ?? request.GetUserHostAddress();
}
@ -91,7 +96,7 @@ namespace ASC.IPSecurity
return false;
}
Log.InfoFormat("Restricted from IP-address: {0}. Tenant: {1}. Request to: {2}", requestIps ?? "", tenant, httpContext.Request.GetDisplayUrl());
Log.InfoFormat("Restricted from IP-address: {0}. Tenant: {1}. Request to: {2}", requestIps ?? "", tenant, HttpContextAccessor.HttpContext.Request.GetDisplayUrl());
return false;
}

View File

@ -162,7 +162,7 @@ namespace ASC.People
{
throw new ConfigurationErrorsException("Can not configure CoreContext: connection string with name core not found.");
}
return (ITariffService)new TariffService(cs, r.GetService<IQuotaService>(), r.GetService<ITenantService>(), r.GetService<CoreBaseSettings>(), r.GetService<CoreSettings>());
return (ITariffService)new TariffService(cs, r.GetService<IQuotaService>(), r.GetService<ITenantService>(), r.GetService<CoreBaseSettings>(), r.GetService<CoreSettings>(), Configuration);
})
.AddScoped<ApiContext>()
.AddScoped<StudioNotifyService>()
@ -213,11 +213,13 @@ namespace ASC.People
.AddScoped<CoreConfiguration>()
.AddScoped<BaseCommonLinkUtility>()
.AddScoped<CommonLinkUtility>()
.AddScoped<LicenseReader>()
.AddSingleton<CoreSettings>()
.AddSingleton<WebPathSettings>()
.AddSingleton<BaseStorageSettingsListener>()
.AddSingleton<CoreBaseSettings>()
.AddScoped<SubscriptionManager>()
.AddScoped<IPSecurity.IPSecurity>()
.AddScoped(typeof(IRecipientProvider), typeof(RecipientProviderImpl))
.AddSingleton(typeof(IRoleProvider), typeof(RoleProvider))
.AddScoped(typeof(IPermissionResolver), typeof(PermissionResolver))

View File

@ -58,6 +58,7 @@ namespace ASC.Web.Core.Users
public CustomNamingPeople CustomNamingPeople { get; }
public TenantUtil TenantUtil { get; }
public CoreBaseSettings CoreBaseSettings { get; }
public IPSecurity.IPSecurity IPSecurity { get; }
private Tenant tenant;
public Tenant Tenant { get { return tenant ?? (tenant = TenantManager.GetCurrentTenant()); } }
@ -74,7 +75,8 @@ namespace ASC.Web.Core.Users
IHttpContextAccessor httpContextAccessor,
CustomNamingPeople customNamingPeople,
TenantUtil tenantUtil,
CoreBaseSettings coreBaseSettings
CoreBaseSettings coreBaseSettings,
IPSecurity.IPSecurity iPSecurity
)
{
StudioNotifyService = studioNotifyService;
@ -89,6 +91,7 @@ namespace ASC.Web.Core.Users
CustomNamingPeople = customNamingPeople;
TenantUtil = tenantUtil;
CoreBaseSettings = coreBaseSettings;
IPSecurity = iPSecurity;
}
private bool TestUniqueUserName(string uniqueName)
@ -215,7 +218,7 @@ namespace ASC.Web.Core.Users
var tenant = TenantManager.GetCurrentTenant();
var settings = IPRestrictionsSettings.Load();
if (settings.Enable && !IPSecurity.IPSecurity.Verify(HttpContextAccessor.HttpContext, tenant, AuthContext))
if (settings.Enable && !IPSecurity.Verify(tenant))
{
throw new Exception(Resource.ErrorAccessRestricted);
}

View File

@ -48,6 +48,7 @@ namespace ASC.Web.Studio.Utility
public TenantManager TenantManager { get; }
public PaymentManager PaymentManager { get; }
public CoreBaseSettings CoreBaseSettings { get; }
public LicenseReader LicenseReader { get; }
public TenantExtra(
UserManager userManager,
@ -56,7 +57,8 @@ namespace ASC.Web.Studio.Utility
TenantAccessSettings tenantAccessSettings,
TenantManager tenantManager,
PaymentManager paymentManager,
CoreBaseSettings coreBaseSettings)
CoreBaseSettings coreBaseSettings,
LicenseReader licenseReader)
{
UserManager = userManager;
TenantStatisticsProvider = tenantStatisticsProvider;
@ -65,6 +67,7 @@ namespace ASC.Web.Studio.Utility
TenantManager = tenantManager;
PaymentManager = paymentManager;
CoreBaseSettings = coreBaseSettings;
LicenseReader = licenseReader;
}
public bool EnableTarrifSettings