From ddeb4157ad3b0a9145ed69bcfdcc02e88f0e8f1e Mon Sep 17 00:00:00 2001 From: pavelbannov Date: Tue, 6 Sep 2022 23:30:19 +0300 Subject: [PATCH] Quota: used field --- common/ASC.Api.Core/Core/BaseStartup.cs | 6 +++ .../Quota/Features/ActiveUsersFeature.cs | 9 +++-- .../Quota/Features/MaxTotalSizeFeature.cs | 39 +++++++++++++++++++ common/ASC.Core.Common/Tenants/TenantQuota.cs | 4 -- .../ApiModels/ResponseDto/QuotaDto.cs | 8 +++- web/ASC.Web.Api/Core/QuotaHelper.cs | 6 ++- .../PublicResources/Resource.Designer.cs | 18 +++++++++ .../PublicResources/Resource.resx | 6 +++ 8 files changed, 86 insertions(+), 10 deletions(-) diff --git a/common/ASC.Api.Core/Core/BaseStartup.cs b/common/ASC.Api.Core/Core/BaseStartup.cs index 0fb0842aa3..ced88f42f3 100644 --- a/common/ASC.Api.Core/Core/BaseStartup.cs +++ b/common/ASC.Api.Core/Core/BaseStartup.cs @@ -124,6 +124,12 @@ public abstract class BaseStartup services.AddScoped, ActiveUsersStatistic>(); services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + + services.AddScoped, MaxTotalSizeStatistic>(); + services.AddScoped(); + DIHelper.TryAdd(typeof(IWebhookPublisher), typeof(WebhookPublisher)); diff --git a/common/ASC.Core.Common/Quota/Features/ActiveUsersFeature.cs b/common/ASC.Core.Common/Quota/Features/ActiveUsersFeature.cs index be198ccdf3..25f0633adb 100644 --- a/common/ASC.Core.Common/Quota/Features/ActiveUsersFeature.cs +++ b/common/ASC.Core.Common/Quota/Features/ActiveUsersFeature.cs @@ -36,15 +36,16 @@ public class ActiveUsersFeature : TenantQuotaFeatureCount public class ActiveUsersChecker : ITenantQuotaFeatureChecker { - private readonly UserManager _userManager; + private readonly ITenantQuotaFeatureStatistic _tenantQuotaFeatureStatistic; - public ActiveUsersChecker(UserManager userManager) + public ActiveUsersChecker(ITenantQuotaFeatureStatistic tenantQuotaFeatureStatistic) { - _userManager = userManager; + _tenantQuotaFeatureStatistic = tenantQuotaFeatureStatistic; } + public bool Check(TenantQuota quota) { - return _userManager.GetUsersByGroup(Users.Constants.GroupUser.ID).Length >= quota.ActiveUsers; + return quota.ActiveUsers <= (int)_tenantQuotaFeatureStatistic.GetValue(); } public string Exception(TenantQuota quota) diff --git a/common/ASC.Core.Common/Quota/Features/MaxTotalSizeFeature.cs b/common/ASC.Core.Common/Quota/Features/MaxTotalSizeFeature.cs index 2b4ca49888..0c96d85ca6 100644 --- a/common/ASC.Core.Common/Quota/Features/MaxTotalSizeFeature.cs +++ b/common/ASC.Core.Common/Quota/Features/MaxTotalSizeFeature.cs @@ -33,3 +33,42 @@ public class MaxTotalSizeFeature : TenantQuotaFeatureLength { } } + +public class MaxTotalSizeChecker : ITenantQuotaFeatureChecker +{ + private readonly ITenantQuotaFeatureStatistic _tenantQuotaFeatureStatistic; + + public MaxTotalSizeChecker(ITenantQuotaFeatureStatistic tenantQuotaFeatureStatistic) + { + _tenantQuotaFeatureStatistic = tenantQuotaFeatureStatistic; + } + + public bool Check(TenantQuota quota) + { + return quota.MaxTotalSize <= (long)_tenantQuotaFeatureStatistic.GetValue(); + } + + public string Exception(TenantQuota quota) + { + return "The used storage size should not exceed " + quota.MaxTotalSize; + } +} + +public class MaxTotalSizeStatistic : ITenantQuotaFeatureStatistic +{ + private readonly TenantManager _tenantManager; + + public MaxTotalSizeStatistic(TenantManager tenantManager) + { + _tenantManager = tenantManager; + } + + public object GetValue() + { + var tenant = _tenantManager.GetCurrentTenant().Id; + + return _tenantManager.FindTenantQuotaRows(tenant) + .Where(r => !string.IsNullOrEmpty(r.Tag) && new Guid(r.Tag) != Guid.Empty) + .Sum(r => r.Counter); + } +} \ No newline at end of file diff --git a/common/ASC.Core.Common/Tenants/TenantQuota.cs b/common/ASC.Core.Common/Tenants/TenantQuota.cs index dd6ba16a2e..39bf56a56e 100644 --- a/common/ASC.Core.Common/Tenants/TenantQuota.cs +++ b/common/ASC.Core.Common/Tenants/TenantQuota.cs @@ -314,10 +314,6 @@ public class TenantQuota : IMapFrom } } - //if (MaxTotalSize != long.MaxValue - // && false) throw new Exception("The used storage size should not exceed " + MaxTotalSize); - //if (ActiveUsers != int.MaxValue - // && false) throw new Exception("The number of active users should not exceed " + ActiveUsers); //if (CountAdmin != int.MaxValue // && false) throw new Exception("The number of managers should not exceed " + CountAdmin); //if (CountRoom != int.MaxValue diff --git a/web/ASC.Web.Api/ApiModels/ResponseDto/QuotaDto.cs b/web/ASC.Web.Api/ApiModels/ResponseDto/QuotaDto.cs index e6e3e5ead5..ba18eedcb6 100644 --- a/web/ASC.Web.Api/ApiModels/ResponseDto/QuotaDto.cs +++ b/web/ASC.Web.Api/ApiModels/ResponseDto/QuotaDto.cs @@ -43,7 +43,7 @@ public class QuotaFeatureDto : IEquatable public string Id { get; set; } public string Title { get; set; } public string Image { get; set; } - public object Used { get; set; } + public FeatureUsedDto Used { get; set; } public FeaturePriceDto Price { get; set; } public bool Equals(QuotaFeatureDto other) @@ -69,6 +69,12 @@ public class FeaturePriceDto public FeaturePriceRangeDto Range { get; set; } } +public class FeatureUsedDto +{ + public object Value { get; set; } + public string Title { get; set; } +} + public class FeaturePriceRangeDto { public object Value { get; set; } diff --git a/web/ASC.Web.Api/Core/QuotaHelper.cs b/web/ASC.Web.Api/Core/QuotaHelper.cs index be81742719..cf1bbf467e 100644 --- a/web/ASC.Web.Api/Core/QuotaHelper.cs +++ b/web/ASC.Web.Api/Core/QuotaHelper.cs @@ -147,7 +147,11 @@ public class QuotaHelper if (statisticProvider != null) { - result.Used = statisticProvider.GetValue(); + result.Used = new FeatureUsedDto + { + Value = statisticProvider.GetValue(), + Title = Resource.ResourceManager.GetString($"TariffsFeature_used_{feature.Name}") + }; } yield return result; diff --git a/web/ASC.Web.Core/PublicResources/Resource.Designer.cs b/web/ASC.Web.Core/PublicResources/Resource.Designer.cs index 16842ebab7..0cfc401497 100644 --- a/web/ASC.Web.Core/PublicResources/Resource.Designer.cs +++ b/web/ASC.Web.Core/PublicResources/Resource.Designer.cs @@ -2211,6 +2211,24 @@ namespace ASC.Web.Core.PublicResources { } } + /// + /// Looks up a localized string similar to Storage space used:. + /// + public static string TariffsFeature_used_total_size { + get { + return ResourceManager.GetString("TariffsFeature_used_total_size", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Managers added:. + /// + public static string TariffsFeature_used_users { + get { + return ResourceManager.GetString("TariffsFeature_used_users", resourceCulture); + } + } + /// /// Looks up a localized string similar to Unlimited number of users. /// diff --git a/web/ASC.Web.Core/PublicResources/Resource.resx b/web/ASC.Web.Core/PublicResources/Resource.resx index 25a50aefbd..b3ae6a020d 100644 --- a/web/ASC.Web.Core/PublicResources/Resource.resx +++ b/web/ASC.Web.Core/PublicResources/Resource.resx @@ -855,4 +855,10 @@ Business plan + + Storage space used: + + + Managers added: + \ No newline at end of file