Quota: CountRoomFeature

This commit is contained in:
pavelbannov 2022-09-09 10:27:35 +03:00
parent 354c4a69ab
commit 87cfb294ab
13 changed files with 120 additions and 49 deletions

View File

@ -32,12 +32,12 @@ public interface ITariffService
IDictionary<string, Dictionary<string, decimal>> GetProductPriceInfo(params string[] productIds);
IEnumerable<PaymentInfo> GetPayments(int tenantId);
Tariff GetTariff(int tenantId, bool withRequestToPaymentSystem = true);
Uri GetShoppingUri(int tenant, string currency = null, string language = null, string customerEmail = null, Dictionary<string, int> quantity = null, string backUrl = null);
Task<Uri> GetShoppingUri(int tenant, string currency = null, string language = null, string customerEmail = null, Dictionary<string, int> quantity = null, string backUrl = null);
Uri GetShoppingUri(int? tenant, int quotaId, string affiliateId, string currency = null, string language = null, string customerId = null, string quantity = null);
Uri GetShoppingUri(string[] productIds, string affiliateId = null, string currency = null, string language = null, string customerId = null, string quantity = null);
void ClearCache(int tenantId);
void DeleteDefaultBillingInfo();
void SetTariff(int tenantId, Tariff tariff);
Uri GetAccountLink(int tenant, string backUrl);
bool PaymentChange(int tenant, Dictionary<string, int> quantity);
Task<bool> PaymentChange(int tenant, Dictionary<string, int> quantity);
}

View File

@ -222,11 +222,13 @@ public class TariffService : ITariffService
return tariff;
}
public bool PaymentChange(int tenantId, Dictionary<string, int> quantity)
public async Task<bool> PaymentChange(int tenantId, Dictionary<string, int> quantity)
{
if (quantity == null || !quantity.Any()
|| !_billingClient.Configured)
{
return false;
}
var allQuotas = _quotaService.GetTenantQuotas().Where(q => !string.IsNullOrEmpty(q.ProductId));
var newQuotas = quantity.Keys.Select(name => allQuotas.FirstOrDefault(q => q.Name == name));
@ -264,7 +266,7 @@ public class TariffService : ITariffService
updatedQuota += quota;
}
updatedQuota.Check(_serviceProvider);
await updatedQuota.Check(_serviceProvider);
var productIds = newQuotas.Select(q => q.ProductId);
@ -272,7 +274,10 @@ public class TariffService : ITariffService
{
var changed = _billingClient.ChangePayment(GetPortalId(tenantId), productIds.ToArray(), quantity.Values.ToArray());
if (!changed) return false;
if (!changed)
{
return false;
}
ClearCache(tenantId);
}
@ -366,7 +371,7 @@ public class TariffService : ITariffService
return payments;
}
public Uri GetShoppingUri(int tenant, string currency = null, string language = null, string customerEmail = null, Dictionary<string, int> quantity = null, string backUrl = null)
public async Task<Uri> GetShoppingUri(int tenant, string currency = null, string language = null, string customerEmail = null, Dictionary<string, int> quantity = null, string backUrl = null)
{
var hasQuantity = quantity != null && quantity.Any();
var key = "shopingurl_" + (hasQuantity ? string.Join('_', quantity.Keys.ToArray()) : "all");
@ -390,7 +395,7 @@ public class TariffService : ITariffService
updatedQuota += quota;
}
updatedQuota.Check(_serviceProvider);
await updatedQuota.Check(_serviceProvider);
var productIds = newQuotas.Select(q => q.ProductId);

View File

@ -44,14 +44,14 @@ public class CountManagerChecker : ITenantQuotaFeatureChecker
_tenantQuotaFeatureStatistic = tenantQuotaFeatureStatistic;
}
public bool Check(TenantQuota quota)
public async Task<bool> Check(TenantQuota quota)
{
return quota.ActiveUsers <= _tenantQuotaFeatureStatistic.GetValue();
return await _tenantQuotaFeatureStatistic.GetValue() <= quota.ActiveUsers;
}
public string Exception(TenantQuota quota)
{
return "The number of active users should not exceed " + quota.ActiveUsers;
return "The number of managers should not exceed " + quota.CountManager;
}
}
@ -64,8 +64,8 @@ public class CountManagerStatistic : ITenantQuotaFeatureStatisticCount<CountMana
_userManager = userManager;
}
public int GetValue()
public Task<int> GetValue()
{
return _userManager.GetUsersByGroup(Users.Constants.GroupUser.ID).Length;
return Task.FromResult(_userManager.GetUsersByGroup(Users.Constants.GroupUser.ID).Length);
}
}

View File

@ -44,9 +44,9 @@ public class MaxTotalSizeChecker : ITenantQuotaFeatureChecker
_tenantQuotaFeatureStatistic = tenantQuotaFeatureStatistic;
}
public bool Check(TenantQuota quota)
public async Task<bool> Check(TenantQuota quota)
{
return quota.MaxTotalSize <= _tenantQuotaFeatureStatistic.GetValue();
return await _tenantQuotaFeatureStatistic.GetValue() <= quota.MaxTotalSize;
}
public string Exception(TenantQuota quota)
@ -64,12 +64,12 @@ public class MaxTotalSizeStatistic : ITenantQuotaFeatureStatisticLength<MaxTotal
_tenantManager = tenantManager;
}
public long GetValue()
public Task<long> GetValue()
{
var tenant = _tenantManager.GetCurrentTenant().Id;
return _tenantManager.FindTenantQuotaRows(tenant)
return Task.FromResult(_tenantManager.FindTenantQuotaRows(tenant)
.Where(r => !string.IsNullOrEmpty(r.Tag) && new Guid(r.Tag) != Guid.Empty)
.Sum(r => r.Counter);
.Sum(r => r.Counter));
}
}

View File

@ -27,6 +27,6 @@
namespace ASC.Core.Common.Quota;
public interface ITenantQuotaFeatureChecker
{
public bool Check(TenantQuota value);
public Task<bool> Check(TenantQuota value);
public string Exception(TenantQuota value);
}

View File

@ -28,7 +28,7 @@ namespace ASC.Core.Common.Quota;
public interface ITenantQuotaFeatureStat<T>
{
T GetValue();
Task<T> GetValue();
}
public interface ITenantQuotaFeatureStatisticCount<T> : ITenantQuotaFeatureStat<int> where T : TenantQuotaFeature<int>

View File

@ -35,7 +35,7 @@ public class TenantQuota : IMapFrom<DbQuota>
MaxFileSize = 25 * 1024 * 1024, // 25Mb
MaxTotalSize = long.MaxValue,
ActiveUsers = int.MaxValue,
CountAdmin = int.MaxValue,
CountManager = int.MaxValue,
CountRoom = int.MaxValue
};
@ -84,11 +84,11 @@ public class TenantQuota : IMapFrom<DbQuota>
set => _activeUsersFeature.Value = value;
}
private readonly CountManagerFeature _countAdminFeature;
public int CountAdmin
private readonly CountManagerFeature _countManagerFeature;
public int CountManager
{
get => _countAdminFeature.Value;
set => _countAdminFeature.Value = value;
get => _countManagerFeature.Value;
set => _countManagerFeature.Value = value;
}
private readonly CountRoomFeature _countRoomFeature;
@ -208,7 +208,7 @@ public class TenantQuota : IMapFrom<DbQuota>
_featuresList = new List<string>();
_activeUsersFeature = new ActiveUsersFeature(this) { Order = 1 };
_countAdminFeature = new CountManagerFeature(this);
_countManagerFeature = new CountManagerFeature(this);
_countRoomFeature = new CountRoomFeature(this) { Order = 2 };
_maxTotalSizeFeature = new MaxTotalSizeFeature(this);
_nonProfitFeature = new TenantQuotaFeatureFlag(this) { Name = "non-profit", Visible = false };
@ -230,7 +230,7 @@ public class TenantQuota : IMapFrom<DbQuota>
TenantQuotaFeatures = new List<TenantQuotaFeature>
{
_activeUsersFeature,
_countAdminFeature,
_countManagerFeature,
_countRoomFeature,
_maxTotalSizeFeature,
_nonProfitFeature,
@ -277,20 +277,15 @@ public class TenantQuota : IMapFrom<DbQuota>
return obj is TenantQuota q && q.Tenant == Tenant;
}
public void Check(IServiceProvider serviceProvider)
public async Task Check(IServiceProvider serviceProvider)
{
foreach (var checker in serviceProvider.GetServices<ITenantQuotaFeatureChecker>())
{
if (!checker.Check(this))
if (!await checker.Check(this))
{
throw new Exception(checker.Exception(this));
}
}
//if (CountAdmin != int.MaxValue
// && false) throw new Exception("The number of managers should not exceed " + CountAdmin);
//if (CountRoom != int.MaxValue
// && false) throw new Exception("The number of rooms should not exceed " + CountRoom);
}
public static TenantQuota operator *(TenantQuota quota, int quantity)

View File

@ -0,0 +1,67 @@
// (c) Copyright Ascensio System SIA 2010-2022
//
// This program is a free software product.
// You can redistribute it and/or modify it under the terms
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended
// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of
// any third-party rights.
//
// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
//
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
//
// The interactive user interfaces in modified source and object code versions of the Program must
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
//
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
// trademark law for use of our trademarks.
//
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
namespace ASC.Files.Core.Core;
public class CountRoomChecker : ITenantQuotaFeatureChecker
{
private readonly ITenantQuotaFeatureStatisticCount<CountRoomFeature> _tenantQuotaFeatureStatistic;
public CountRoomChecker(ITenantQuotaFeatureStatisticCount<CountRoomFeature> tenantQuotaFeatureStatistic)
{
_tenantQuotaFeatureStatistic = tenantQuotaFeatureStatistic;
}
public async Task<bool> Check(TenantQuota quota)
{
return await _tenantQuotaFeatureStatistic.GetValue() <= quota.CountRoom;
}
public string Exception(TenantQuota quota)
{
return "The number of rooms should not exceed " + quota.MaxTotalSize;
}
}
public class CountRoomCheckerStatistic : ITenantQuotaFeatureStatisticCount<CountRoomFeature>
{
private readonly IFolderDao<int> _folderDao;
private readonly GlobalFolderHelper _globalFolderHelper;
public CountRoomCheckerStatistic(
IFolderDao<int> folderDao,
GlobalFolderHelper globalFolderHelper)
{
_folderDao = folderDao;
_globalFolderHelper = globalFolderHelper;
}
public async Task<int> GetValue()
{
var parentId = await _globalFolderHelper.GetFolderVirtualRooms<int>();
return await _folderDao.GetFoldersAsync(parentId).CountAsync();
}
}

View File

@ -55,8 +55,6 @@ global using ASC.Api.Collections;
global using ASC.Api.Core;
global using ASC.Api.Utils;
global using ASC.AuditTrail;
global using ASC.AuditTrail.Models;
global using ASC.AuditTrail.Models.Mappings;
global using ASC.Common;
global using ASC.Common.Caching;
global using ASC.Common.Log;
@ -73,6 +71,8 @@ global using ASC.Core.Common.Configuration;
global using ASC.Core.Common.EF;
global using ASC.Core.Common.EF.Context;
global using ASC.Core.Common.EF.Model;
global using ASC.Core.Common.Quota;
global using ASC.Core.Common.Quota.Features;
global using ASC.Core.Common.Settings;
global using ASC.Core.Notify.Signalr;
global using ASC.Core.Tenants;
@ -157,9 +157,6 @@ global using Box.V2.Auth;
global using Box.V2.Config;
global using Box.V2.Models;
global using CsvHelper;
global using CsvHelper.Configuration;
global using DocuSign.eSign.Api;
global using DocuSign.eSign.Client;
global using DocuSign.eSign.Model;
@ -167,8 +164,6 @@ global using DocuSign.eSign.Model;
global using Dropbox.Api;
global using Dropbox.Api.Files;
global using Flurl;
global using Google;
global using Google.Apis.Auth.OAuth2;
global using Google.Apis.Auth.OAuth2.Flows;
@ -199,7 +194,6 @@ global using Microsoft.EntityFrameworkCore.Storage;
global using Microsoft.Extensions.Caching.Memory;
global using Microsoft.Extensions.Configuration;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Hosting;
global using Microsoft.Extensions.Logging;
global using Microsoft.Extensions.Options;
global using Microsoft.Extensions.Primitives;

View File

@ -75,7 +75,7 @@ public class PaymentController : ControllerBase
}
[HttpPut("payment/url")]
public Uri GetPaymentUrl(PaymentUrlRequestsDto inDto)
public async Task<Uri> GetPaymentUrl(PaymentUrlRequestsDto inDto)
{
if (_tariffService.GetPayments(Tenant.Id).Any() ||
!_userManager.GetUsers(_securityContext.CurrentAccount.ID).IsAdmin(_userManager))
@ -85,7 +85,7 @@ public class PaymentController : ControllerBase
var currency = _regionHelper.GetCurrencyFromRequest();
return _tariffService.GetShoppingUri(Tenant.Id, currency,
return await _tariffService.GetShoppingUri(Tenant.Id, currency,
Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName,
_userManager.GetUsers(_securityContext.CurrentAccount.ID).Email,
inDto.Quantity,
@ -93,7 +93,7 @@ public class PaymentController : ControllerBase
}
[HttpPut("payment/update")]
public bool PaymentUpdate(PaymentUrlRequestsDto inDto)
public async Task<bool> PaymentUpdate(PaymentUrlRequestsDto inDto)
{
if (!_tariffService.GetPayments(Tenant.Id).Any() ||
!_userManager.GetUsers(_securityContext.CurrentAccount.ID).IsAdmin(_userManager))
@ -101,7 +101,7 @@ public class PaymentController : ControllerBase
return false;
}
return _tariffService.PaymentChange(Tenant.Id, inDto.Quantity);
return await _tariffService.PaymentChange(Tenant.Id, inDto.Quantity);
}
[HttpGet("payment/account")]

View File

@ -37,7 +37,7 @@ public class QuotaDto
public bool Free { get; set; }
public bool Trial { get; set; }
public IEnumerable<QuotaFeatureDto> Features { get; set; }
public IAsyncEnumerable<QuotaFeatureDto> Features { get; set; }
}
public class QuotaFeatureDto : IEquatable<QuotaFeatureDto>

View File

@ -108,7 +108,7 @@ public class QuotaHelper
return string.Format("{0}{1}", currentRegion.CurrencySymbol, priceString);
}
private IEnumerable<QuotaFeatureDto> GetFeatures(TenantQuota quota, string price, bool getUsed)
private async IAsyncEnumerable<QuotaFeatureDto> GetFeatures(TenantQuota quota, string price, bool getUsed)
{
var assembly = GetType().Assembly;
@ -140,7 +140,7 @@ public class QuotaHelper
if (statisticProvider != null)
{
used = statisticProvider.GetValue();
used = await statisticProvider.GetValue();
}
}
else if (feature is TenantQuotaFeature<int> count)
@ -152,7 +152,7 @@ public class QuotaHelper
if (statisticProvider != null)
{
used = statisticProvider.GetValue();
used = await statisticProvider.GetValue();
}
}
else if (feature is TenantQuotaFeature<bool> flag)

View File

@ -24,6 +24,10 @@
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
using ASC.Core.Common.Quota;
using ASC.Core.Common.Quota.Features;
using ASC.Files.Core.Core;
var options = new WebApplicationOptions
{
Args = args,
@ -39,6 +43,12 @@ builder.Host.ConfigureDefault(args, configureServices: (hostContext, services, d
services.AddBaseDbContextPool<FilesDbContext>();
services.AddBaseDbContextPool<BackupsContext>();
services.AddTransient<QuotaUsageDto>();
services.AddScoped<ITenantQuotaFeatureChecker, CountRoomChecker>();
services.AddScoped<CountRoomChecker>();
services.AddScoped<ITenantQuotaFeatureStatisticCount<CountRoomFeature>, CountRoomCheckerStatistic>();
services.AddScoped<CountRoomCheckerStatistic>();
});
builder.WebHost.ConfigureDefaultKestrel();