Quota: fix

This commit is contained in:
pavelbannov 2022-10-18 21:37:16 +03:00
parent d30f95576b
commit 9df8c0d69f
20 changed files with 53 additions and 48 deletions

View File

@ -137,7 +137,7 @@ public class LdapUserManager
try
{
_countManagerChecker.CheckUsed().Wait();
_countManagerChecker.CheckAppend().Wait();
}
catch (Exception)
{

View File

@ -30,13 +30,13 @@ public static class QuotaExtension
public static void RegisterFeature(this IServiceCollection services)
{
services.AddScoped<ITenantQuotaFeatureChecker, CountManagerChecker>();
services.AddScoped<TenantQuotaFeatureChecker<CountManagerFeature, int>, CountManagerChecker>();
services.AddScoped<TenantQuotaFeatureCheckerCount<CountManagerFeature>, CountManagerChecker>();
services.AddScoped<CountManagerChecker>();
services.AddScoped<ITenantQuotaFeatureStat<CountManagerFeature, int>, CountManagerStatistic>();
services.AddScoped<CountManagerStatistic>();
services.AddScoped<ITenantQuotaFeatureChecker, CountUserChecker>();
services.AddScoped<TenantQuotaFeatureChecker<CountUserFeature, int>, CountUserChecker>();
services.AddScoped<TenantQuotaFeatureCheckerCount<CountUserFeature>, CountUserChecker>();
services.AddScoped<CountUserChecker>();
services.AddScoped<ITenantQuotaFeatureStat<CountUserFeature, int>, CountUserStatistic>();
services.AddScoped<CountUserStatistic>();

View File

@ -24,6 +24,8 @@
// 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.Billing;
namespace ASC.Api.Core.Middleware;
public class CustomExceptionFilterAttribute : ExceptionFilterAttribute
{
@ -39,7 +41,7 @@ public class CustomExceptionFilterAttribute : ExceptionFilterAttribute
var withStackTrace = true;
var exception = context.Exception.InnerException ?? context.Exception;
var exception = context.Exception.GetBaseException();
switch (exception)
{
@ -63,11 +65,12 @@ public class CustomExceptionFilterAttribute : ExceptionFilterAttribute
status = HttpStatusCode.Forbidden;
break;
case TenantQuotaException:
case BillingNotFoundException:
status = HttpStatusCode.PaymentRequired;
break;
}
var result = new ObjectResult(new ErrorApiResponse(status, context.Exception, message, withStackTrace))
var result = new ObjectResult(new ErrorApiResponse(status, exception, message, withStackTrace))
{
StatusCode = (int)status
};

View File

@ -61,8 +61,8 @@ public class UserManager
private readonly CardDavAddressbook _cardDavAddressbook;
private readonly ILogger<UserManager> _log;
private readonly ICache _cache;
private readonly TenantQuotaFeatureChecker<CountManagerFeature, int> _tenantQuotaFeatureChecker;
private readonly TenantQuotaFeatureChecker<CountUserFeature, int> _activeUsersFeatureChecker;
private readonly TenantQuotaFeatureCheckerCount<CountManagerFeature> _tenantQuotaFeatureChecker;
private readonly TenantQuotaFeatureCheckerCount<CountUserFeature> _activeUsersFeatureChecker;
private readonly Constants _constants;
private Tenant _tenant;
@ -85,8 +85,8 @@ public class UserManager
CardDavAddressbook cardDavAddressbook,
ILogger<UserManager> log,
ICache cache,
TenantQuotaFeatureChecker<CountManagerFeature, int> tenantQuotaFeatureChecker,
TenantQuotaFeatureChecker<CountUserFeature, int> activeUsersFeatureChecker
TenantQuotaFeatureCheckerCount<CountManagerFeature> tenantQuotaFeatureChecker,
TenantQuotaFeatureCheckerCount<CountUserFeature> activeUsersFeatureChecker
)
{
_userService = service;
@ -117,8 +117,8 @@ public class UserManager
CardDavAddressbook cardDavAddressbook,
ILogger<UserManager> log,
ICache cache,
TenantQuotaFeatureChecker<CountManagerFeature, int> tenantQuotaFeatureChecker,
TenantQuotaFeatureChecker<CountUserFeature, int> activeUsersFeatureChecker,
TenantQuotaFeatureCheckerCount<CountManagerFeature> tenantQuotaFeatureChecker,
TenantQuotaFeatureCheckerCount<CountUserFeature> activeUsersFeatureChecker,
IHttpContextAccessor httpContextAccessor)
: this(service, tenantManager, permissionContext, userManagerConstants, coreBaseSettings, coreSettings, instanceCrypto, radicaleClient, cardDavAddressbook, log, cache, tenantQuotaFeatureChecker, activeUsersFeatureChecker)
{
@ -339,11 +339,11 @@ public class UserManager
{
if (isVisitor)
{
_activeUsersFeatureChecker.CheckUsed().Wait();
_activeUsersFeatureChecker.CheckAppend().Wait();
}
else
{
_tenantQuotaFeatureChecker.CheckUsed().Wait();
_tenantQuotaFeatureChecker.CheckAppend().Wait();
}
}
}

View File

@ -79,7 +79,7 @@ public static class DbQuotaExtension
Tenant = -3,
Name = "startup",
Description = null,
Features = "free,total_size:2147483648,manager:1,rooms:12,usersInRoom:3",
Features = "free,total_size:2147483648,manager:1,room:12,usersInRoom:3",
Price = 0,
ProductId = null,
Visible = false

View File

@ -45,13 +45,6 @@ public abstract class TenantQuotaFeatureChecker<T, T1> : ITenantQuotaFeatureChec
_tenantManager = tenantManager;
}
public async Task CheckUsed()
{
var quota = _tenantManager.GetCurrentTenantQuota();
var used = await _tenantQuotaFeatureStatistic.GetValue();
Check(quota, used);
}
public virtual async Task CheckUsed(TenantQuota quota)
{
var used = await _tenantQuotaFeatureStatistic.GetValue();
@ -78,4 +71,16 @@ public abstract class TenantQuotaFeatureChecker<T, T1> : ITenantQuotaFeatureChec
throw new TenantQuotaException(string.Format(Exception, val));
}
}
}
public abstract class TenantQuotaFeatureCheckerCount<T> : TenantQuotaFeatureChecker<T, int> where T : TenantQuotaFeature<int>
{
protected TenantQuotaFeatureCheckerCount(ITenantQuotaFeatureStat<T, int> tenantQuotaFeatureStatistic, TenantManager tenantManager) : base(tenantQuotaFeatureStatistic, tenantManager)
{
}
public async Task CheckAppend()
{
CheckAdd((await _tenantQuotaFeatureStatistic.GetValue()) + 1);
}
}

View File

@ -99,7 +99,7 @@ namespace ASC.Migrations.MySql.Migrations.CoreDb
new
{
Tenant = -3,
Features = "free,total_size:2147483648,manager:1,rooms:12,usersInRoom:3",
Features = "free,total_size:2147483648,manager:1,room:12,usersInRoom:3",
MaxFileSize = 100L,
Name = "startup",
Price = 0.00m,

View File

@ -94,7 +94,7 @@ namespace ASC.Migrations.MySql.Migrations.CoreDb
migrationBuilder.InsertData(
table: "tenants_quota",
columns: new[] { "tenant", "description", "features", "name", "product_id" },
values: new object[] { -3, null, "free,total_size:2147483648,manager:1,rooms:12,usersInRoom:3", "startup", null });
values: new object[] { -3, null, "free,total_size:2147483648,manager:1,room:12,usersInRoom:3", "startup", null });
migrationBuilder.InsertData(
table: "tenants_quota",

View File

@ -87,7 +87,7 @@ namespace ASC.Migrations.MySql.Migrations
new
{
Tenant = -3,
Features = "free,total_size:2147483648,manager:1,rooms:12,usersInRoom:3",
Features = "free,total_size:2147483648,manager:1,room:12,usersInRoom:3",
Name = "startup",
Price = 0.00m,
Visible = false

View File

@ -92,7 +92,7 @@ namespace ASC.Migrations.PostgreSql.Migrations.CoreDb
new
{
Tenant = -3,
Features = "free,total_size:2147483648,manager:1,rooms:12,usersInRoom:3",
Features = "free,total_size:2147483648,manager:1,room:12,usersInRoom:3",
MaxFileSize = 100L,
Name = "startup",
Price = 0.00m,

View File

@ -100,7 +100,7 @@ namespace ASC.Migrations.PostgreSql.Migrations.CoreDb
schema: "onlyoffice",
table: "tenants_quota",
columns: new[] { "tenant", "description", "features", "name", "visible" },
values: new object[] { -3, null, "free,total_size:2147483648,manager:1,rooms:12,usersInRoom:3", "startup", false });
values: new object[] { -3, null, "free,total_size:2147483648,manager:1,room:12,usersInRoom:3", "startup", false });
migrationBuilder.InsertData(
schema: "onlyoffice",

View File

@ -82,7 +82,7 @@ namespace ASC.Migrations.PostgreSql.Migrations
new
{
Tenant = -3,
Features = "free,total_size:2147483648,manager:1,rooms:12,usersInRoom:3",
Features = "free,total_size:2147483648,manager:1,room:12,usersInRoom:3",
Name = "startup",
Price = 0.00m,
Visible = false

View File

@ -28,7 +28,7 @@ using ASC.Core.Billing;
namespace ASC.Files.Core.Core;
public class CountRoomChecker : TenantQuotaFeatureChecker<CountRoomFeature, int>
public class CountRoomChecker : TenantQuotaFeatureCheckerCount<CountRoomFeature>
{
private readonly ITariffService _tariffService;
public override string Exception => Resource.TariffsFeature_room_exception;

View File

@ -471,7 +471,7 @@ public class FileStorageService<T> //: IFileStorageService
{
ArgumentNullException.ThrowIfNull(title, nameof(title));
_countRoomChecker.CheckAdd((await _countRoomCheckerStatistic.GetValue()) + 1);
await _countRoomChecker.CheckAppend();
if (@private && (share == null || !share.Any()))
{

View File

@ -26,7 +26,7 @@
namespace ASC.Files.Core.Core;
public class UsersInRoomChecker : TenantQuotaFeatureChecker<UsersInRoomFeature, int>
public class UsersInRoomChecker : TenantQuotaFeatureCheckerCount<UsersInRoomFeature>
{
public override string Exception => Resource.TariffsFeature_usersInRoom_exception;

View File

@ -62,7 +62,6 @@ public class UserController : PeopleControllerBase
private readonly CountManagerChecker _countManagerChecker;
private readonly CountUserChecker _countUserChecker;
private readonly UsersInRoomChecker _usersInRoomChecker;
private readonly UsersInRoomStatistic _usersInRoomStatistic;
public UserController(
ICache cache,
@ -102,7 +101,6 @@ public class UserController : PeopleControllerBase
CountManagerChecker countManagerChecker,
CountUserChecker activeUsersChecker,
UsersInRoomChecker usersInRoomChecker,
UsersInRoomStatistic usersInRoomStatistic,
IQuotaService quotaService)
: base(userManager, permissionContext, apiContext, userPhotoManager, httpClientFactory, httpContextAccessor)
{
@ -137,7 +135,6 @@ public class UserController : PeopleControllerBase
_countManagerChecker = countManagerChecker;
_countUserChecker = activeUsersChecker;
_usersInRoomChecker = usersInRoomChecker;
_usersInRoomStatistic = usersInRoomStatistic;
_quotaService = quotaService;
}
@ -176,7 +173,7 @@ public class UserController : PeopleControllerBase
user.Notes = inDto.Comment;
user.Sex = "male".Equals(inDto.Sex, StringComparison.OrdinalIgnoreCase)
? true
: ("female".Equals(inDto.Sex, StringComparison.OrdinalIgnoreCase) ? (bool?)false : null);
: ("female".Equals(inDto.Sex, StringComparison.OrdinalIgnoreCase) ? false : null);
user.BirthDate = inDto.Birthday != null ? _tenantUtil.DateTimeFromUtc(inDto.Birthday) : null;
user.WorkFromDate = inDto.Worksfrom != null ? _tenantUtil.DateTimeFromUtc(inDto.Worksfrom) : DateTime.UtcNow.Date;
@ -255,7 +252,7 @@ public class UserController : PeopleControllerBase
user.Notes = inDto.Comment;
user.Sex = "male".Equals(inDto.Sex, StringComparison.OrdinalIgnoreCase)
? true
: ("female".Equals(inDto.Sex, StringComparison.OrdinalIgnoreCase) ? (bool?)false : null);
: ("female".Equals(inDto.Sex, StringComparison.OrdinalIgnoreCase) ? false : null);
user.BirthDate = inDto.Birthday != null && inDto.Birthday != DateTime.MinValue ? _tenantUtil.DateTimeFromUtc(inDto.Birthday) : null;
user.WorkFromDate = inDto.Worksfrom != null && inDto.Worksfrom != DateTime.MinValue ? _tenantUtil.DateTimeFromUtc(inDto.Worksfrom) : DateTime.UtcNow.Date;
@ -277,12 +274,12 @@ public class UserController : PeopleControllerBase
if (success)
{
_usersInRoomChecker.CheckAdd(await _usersInRoomStatistic.GetValue(id) + 1);
await _usersInRoomChecker.CheckAppend();
await _fileSecurity.ShareAsync(id, Files.Core.FileEntryType.Folder, user.Id, options.Share);
}
else
{
_usersInRoomChecker.CheckAdd(await _usersInRoomStatistic.GetValue(options.RoomId) + 1);
await _usersInRoomChecker.CheckAppend();
await _fileSecurity.ShareAsync(options.RoomId, Files.Core.FileEntryType.Folder, user.Id, options.Share);
}
}
@ -949,14 +946,14 @@ public class UserController : PeopleControllerBase
if (inDto.IsVisitor && !_userManager.IsVisitor(user) && canBeGuestFlag)
{
await _countUserChecker.CheckUsed();
await _countUserChecker.CheckAppend();
_userManager.AddUserIntoGroup(user.Id, Constants.GroupUser.ID);
_webItemSecurityCache.ClearCache(Tenant.Id);
}
if (!self && !inDto.IsVisitor && _userManager.IsVisitor(user))
{
await _countManagerChecker.CheckUsed();
await _countManagerChecker.CheckAppend();
_userManager.RemoveUserFromGroup(user.Id, Constants.GroupUser.ID);
_webItemSecurityCache.ClearCache(Tenant.Id);
}
@ -996,11 +993,11 @@ public class UserController : PeopleControllerBase
{
if (!_userManager.IsVisitor(user))
{
await _countManagerChecker.CheckUsed();
await _countManagerChecker.CheckAppend();
}
else
{
await _countUserChecker.CheckUsed();
await _countUserChecker.CheckAppend();
}
user.Status = EmployeeStatus.Active;
@ -1044,12 +1041,12 @@ public class UserController : PeopleControllerBase
switch (type)
{
case EmployeeType.User:
await _countManagerChecker.CheckUsed();
await _countManagerChecker.CheckAppend();
_userManager.RemoveUserFromGroup(user.Id, Constants.GroupUser.ID);
_webItemSecurityCache.ClearCache(Tenant.Id);
break;
case EmployeeType.Visitor:
await _countUserChecker.CheckUsed();
await _countUserChecker.CheckAppend();
_userManager.AddUserIntoGroup(user.Id, Constants.GroupUser.ID);
_webItemSecurityCache.ClearCache(Tenant.Id);
break;

View File

@ -184,7 +184,7 @@ public class MessageSettingsController : BaseSettingsController
var enableInviteUsers = true;
try
{
await _countManagerChecker.CheckUsed();
await _countManagerChecker.CheckAppend();
}
catch (Exception)
{

View File

@ -107,7 +107,7 @@ public class SsoHandlerService
{
signature = signature + "=";
lastSignChar--;
}
}
};
}
@ -272,7 +272,7 @@ public class SsoHandlerService
try
{
await _countManagerChecker.CheckUsed();
await _countManagerChecker.CheckAppend();
}
catch (Exception)
{

View File

@ -28,7 +28,7 @@
namespace ASC.Web.Core.Quota;
public class CountManagerChecker : TenantQuotaFeatureChecker<CountManagerFeature, int>
public class CountManagerChecker : TenantQuotaFeatureCheckerCount<CountManagerFeature>
{
public override string Exception => Resource.TariffsFeature_manager_exception;

View File

@ -28,7 +28,7 @@ using Constants = ASC.Core.Users.Constants;
namespace ASC.Web.Core.Quota;
public class CountUserChecker : TenantQuotaFeatureChecker<CountUserFeature, int>
public class CountUserChecker : TenantQuotaFeatureCheckerCount<CountUserFeature>
{
private readonly ITariffService _tariffService;
public override string Exception => Resource.TariffsFeature_users_exception;