DocSpace-buildtools/common/ASC.Core.Common/Tenants/TenantQuota.cs

384 lines
12 KiB
C#
Raw Normal View History

2022-03-15 18:00:53 +00:00
// (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
2022-02-15 11:52:43 +00:00
namespace ASC.Core.Tenants;
[DebuggerDisplay("{Tenant} {Name}")]
2022-09-06 09:14:08 +00:00
public class TenantQuota : IMapFrom<DbQuota>
2022-02-15 11:52:43 +00:00
{
public static readonly TenantQuota Default = new TenantQuota(Tenants.Tenant.DefaultTenant)
{
Name = "Default",
MaxFileSize = 25 * 1024 * 1024, // 25Mb
MaxTotalSize = long.MaxValue,
ActiveUsers = int.MaxValue,
2022-09-09 07:27:35 +00:00
CountManager = int.MaxValue,
CountRoom = int.MaxValue
2022-02-15 11:52:43 +00:00
};
2022-02-15 11:52:43 +00:00
public int Tenant { get; set; }
public string Name { get; set; }
2022-08-25 10:41:52 +00:00
public decimal Price { get; set; }
public string ProductId { get; set; }
public bool Visible { get; set; }
2022-08-24 16:05:15 +00:00
2022-09-06 15:35:30 +00:00
[JsonIgnore]
2022-09-06 09:14:08 +00:00
public IReadOnlyList<TenantQuotaFeature> TenantQuotaFeatures { get; private set; }
private List<string> _featuresList;
2022-08-24 16:05:15 +00:00
public string Features
{
get
{
2022-08-24 16:05:15 +00:00
return string.Join(",", _featuresList);
}
set
{
2022-08-24 16:05:15 +00:00
if (value != null)
{
2022-08-24 16:05:15 +00:00
_featuresList = value.Split(' ', ',', ';').ToList();
}
else
{
_featuresList = new List<string>();
}
}
}
2022-09-06 09:14:08 +00:00
2022-09-09 12:13:54 +00:00
private readonly MaxFileSizeFeature _maxFileSizeFeature;
public long MaxFileSize
{
get => ByteConverter.GetInBytes(_maxFileSizeFeature.Value);
set => _maxFileSizeFeature.Value = ByteConverter.GetInMBytes(value);
}
2022-09-06 09:14:08 +00:00
private readonly MaxTotalSizeFeature _maxTotalSizeFeature;
2022-08-24 16:05:15 +00:00
public long MaxTotalSize
{
2022-09-06 09:14:08 +00:00
get => ByteConverter.GetInBytes(_maxTotalSizeFeature.Value);
set => _maxTotalSizeFeature.Value = ByteConverter.GetInMBytes(value);
2022-08-24 16:05:15 +00:00
}
2022-09-06 09:14:08 +00:00
private readonly ActiveUsersFeature _activeUsersFeature;
public int ActiveUsers
{
2022-09-06 09:14:08 +00:00
get => _activeUsersFeature.Value;
set => _activeUsersFeature.Value = value;
2022-08-24 16:05:15 +00:00
}
2022-09-09 07:27:35 +00:00
private readonly CountManagerFeature _countManagerFeature;
public int CountManager
2022-08-24 16:05:15 +00:00
{
2022-09-09 07:27:35 +00:00
get => _countManagerFeature.Value;
set => _countManagerFeature.Value = value;
}
2022-09-13 19:35:54 +00:00
private readonly UsersInRoomFeature _usersInRoomFeature;
public int UsersInRoom
{
get => _usersInRoomFeature.Value;
set => _usersInRoomFeature.Value = value;
}
2022-09-06 09:14:08 +00:00
private readonly CountRoomFeature _countRoomFeature;
2022-08-24 16:05:15 +00:00
public int CountRoom
{
2022-09-06 09:14:08 +00:00
get => _countRoomFeature.Value;
set => _countRoomFeature.Value = value;
2022-08-24 16:05:15 +00:00
}
2022-09-06 09:14:08 +00:00
private readonly TenantQuotaFeatureFlag _nonProfitFeature;
2022-02-15 11:52:43 +00:00
public bool NonProfit
{
2022-09-06 09:14:08 +00:00
get => _nonProfitFeature.Value;
set => _nonProfitFeature.Value = value;
2022-02-15 11:52:43 +00:00
}
2022-09-06 09:14:08 +00:00
private readonly TenantQuotaFeatureFlag _trialFeature;
2022-02-15 11:52:43 +00:00
public bool Trial
{
2022-09-06 09:14:08 +00:00
get => _trialFeature.Value;
set => _trialFeature.Value = value;
2022-02-15 11:52:43 +00:00
}
2021-05-17 11:35:00 +00:00
2022-09-06 09:14:08 +00:00
private readonly TenantQuotaFeatureFlag _freeFeature;
2022-02-15 11:52:43 +00:00
public bool Free
{
2022-09-06 09:14:08 +00:00
get => _freeFeature.Value;
set => _freeFeature.Value = value;
2022-02-15 11:52:43 +00:00
}
2022-09-06 09:14:08 +00:00
private readonly TenantQuotaFeatureFlag _updateFeature;
2022-02-15 11:52:43 +00:00
public bool Update
{
2022-09-06 09:14:08 +00:00
get => _updateFeature.Value;
set => _updateFeature.Value = value;
2022-02-15 11:52:43 +00:00
}
2022-09-06 09:14:08 +00:00
private readonly TenantQuotaFeatureFlag _auditFeature;
2022-02-15 11:52:43 +00:00
public bool Audit
{
2022-09-06 09:14:08 +00:00
get => _auditFeature.Value;
set => _auditFeature.Value = value;
2022-02-15 11:52:43 +00:00
}
2022-09-06 09:14:08 +00:00
private readonly TenantQuotaFeatureFlag _docsEditionFeature;
2022-02-15 11:52:43 +00:00
public bool DocsEdition
{
2022-09-06 09:14:08 +00:00
get => _docsEditionFeature.Value;
set => _docsEditionFeature.Value = value;
2022-02-15 11:52:43 +00:00
}
2022-09-06 09:14:08 +00:00
private readonly TenantQuotaFeatureFlag _ldapFeature;
2022-02-15 11:52:43 +00:00
public bool Ldap
{
2022-09-06 09:14:08 +00:00
get => _ldapFeature.Value;
set => _ldapFeature.Value = value;
2022-02-15 11:52:43 +00:00
}
2022-09-06 09:14:08 +00:00
private readonly TenantQuotaFeatureFlag _ssoFeature;
2022-02-15 11:52:43 +00:00
public bool Sso
{
2022-09-06 09:14:08 +00:00
get => _ssoFeature.Value;
set => _ssoFeature.Value = value;
2022-02-15 11:52:43 +00:00
}
2022-09-06 09:14:08 +00:00
private readonly TenantQuotaFeatureFlag _whiteLabelFeature;
2022-02-15 11:52:43 +00:00
public bool WhiteLabel
{
2022-09-06 09:14:08 +00:00
get => _whiteLabelFeature.Value;
set => _whiteLabelFeature.Value = value;
2022-02-15 11:52:43 +00:00
}
2022-09-06 09:14:08 +00:00
private readonly TenantQuotaFeatureFlag _customizationFeature;
2022-02-15 11:52:43 +00:00
public bool Customization
{
2022-09-06 09:14:08 +00:00
get => _customizationFeature.Value;
set => _customizationFeature.Value = value;
2022-02-15 11:52:43 +00:00
}
2022-09-06 09:14:08 +00:00
private readonly TenantQuotaFeatureFlag _customFeature;
2022-06-01 14:07:08 +00:00
public bool Custom
{
2022-09-06 09:14:08 +00:00
get => _customFeature.Value;
set => _customFeature.Value = value;
2022-06-01 14:07:08 +00:00
}
private readonly TenantQuotaFeatureFlag _autoBackupRestoreFeature;
public bool AutoBackupRestore
2022-02-15 11:52:43 +00:00
{
get => _autoBackupRestoreFeature.Value;
set => _autoBackupRestoreFeature.Value = value;
2022-02-15 11:52:43 +00:00
}
2022-09-06 09:14:08 +00:00
private readonly TenantQuotaFeatureFlag _oauthFeature;
2022-02-15 11:52:43 +00:00
public bool Oauth
{
2022-09-06 09:14:08 +00:00
get => _oauthFeature.Value;
set => _oauthFeature.Value = value;
2022-02-15 11:52:43 +00:00
}
2022-09-06 09:14:08 +00:00
private readonly TenantQuotaFeatureFlag _contentSearchFeature;
2022-02-15 11:52:43 +00:00
public bool ContentSearch
{
2022-09-06 09:14:08 +00:00
get => _contentSearchFeature.Value;
set => _contentSearchFeature.Value = value;
2022-02-15 11:52:43 +00:00
}
2021-05-17 11:35:00 +00:00
2022-09-06 09:14:08 +00:00
private readonly TenantQuotaFeatureFlag _thirdPartyFeature;
2022-02-15 11:52:43 +00:00
public bool ThirdParty
{
2022-09-06 09:14:08 +00:00
get => _thirdPartyFeature.Value;
set => _thirdPartyFeature.Value = value;
2022-02-15 11:52:43 +00:00
}
2022-08-24 16:05:15 +00:00
public TenantQuota()
{
_featuresList = new List<string>();
2022-09-06 09:14:08 +00:00
_activeUsersFeature = new ActiveUsersFeature(this) { Order = 1 };
2022-09-09 07:27:35 +00:00
_countManagerFeature = new CountManagerFeature(this);
2022-09-13 19:35:54 +00:00
_usersInRoomFeature = new UsersInRoomFeature(this) { Order = 8 };
2022-09-06 09:14:08 +00:00
_countRoomFeature = new CountRoomFeature(this) { Order = 2 };
2022-09-08 07:23:26 +00:00
_maxTotalSizeFeature = new MaxTotalSizeFeature(this);
2022-09-09 12:13:54 +00:00
_maxFileSizeFeature = new MaxFileSizeFeature(this);
2022-09-06 09:14:08 +00:00
_nonProfitFeature = new TenantQuotaFeatureFlag(this) { Name = "non-profit", Visible = false };
_trialFeature = new TenantQuotaFeatureFlag(this) { Name = "trial", Visible = false };
_freeFeature = new TenantQuotaFeatureFlag(this) { Name = "free", Visible = false };
_updateFeature = new TenantQuotaFeatureFlag(this) { Name = "update", Visible = false };
_auditFeature = new TenantQuotaFeatureFlag(this) { Name = "audit", Order = 7 };
_docsEditionFeature = new TenantQuotaFeatureFlag(this) { Name = "docs", Visible = false };
_ldapFeature = new TenantQuotaFeatureFlag(this) { Name = "ldap", Visible = false };
_ssoFeature = new TenantQuotaFeatureFlag(this) { Name = "sso", Order = 5 };
_whiteLabelFeature = new TenantQuotaFeatureFlag(this) { Name = "whitelabel", Order = 4 };
_customizationFeature = new TenantQuotaFeatureFlag(this) { Name = "customization", Visible = false };
_customFeature = new TenantQuotaFeatureFlag(this) { Name = "custom", Visible = false };
_autoBackupRestoreFeature = new TenantQuotaFeatureFlag(this) { Name = "restore", Order = 6 };
2022-09-06 09:14:08 +00:00
_oauthFeature = new TenantQuotaFeatureFlag(this) { Name = "oauth", Visible = false };
_contentSearchFeature = new TenantQuotaFeatureFlag(this) { Name = "contentsearch", Visible = false };
_thirdPartyFeature = new TenantQuotaFeatureFlag(this) { Name = "thirdparty", Visible = false };
TenantQuotaFeatures = new List<TenantQuotaFeature>
{
_activeUsersFeature,
2022-09-09 07:27:35 +00:00
_countManagerFeature,
2022-09-13 19:35:54 +00:00
_usersInRoomFeature,
2022-09-06 09:14:08 +00:00
_countRoomFeature,
_maxTotalSizeFeature,
2022-09-09 12:13:54 +00:00
_maxFileSizeFeature,
2022-09-06 09:14:08 +00:00
_nonProfitFeature,
_trialFeature,
_freeFeature,
_updateFeature,
_auditFeature,
_docsEditionFeature,
_ldapFeature,
_ssoFeature,
_whiteLabelFeature,
_customizationFeature,
_customFeature,
_autoBackupRestoreFeature,
2022-09-06 09:14:08 +00:00
_oauthFeature,
_contentSearchFeature,
_thirdPartyFeature
};
2022-08-24 16:05:15 +00:00
}
2021-05-17 11:35:00 +00:00
2022-08-24 16:05:15 +00:00
public TenantQuota(int tenant) : this()
2022-02-15 11:52:43 +00:00
{
Tenant = tenant;
}
2022-09-06 09:14:08 +00:00
public TenantQuota(TenantQuota quota) : this()
2022-08-25 10:41:52 +00:00
{
Tenant = quota.Tenant;
Name = quota.Name;
Price = quota.Price;
ProductId = quota.ProductId;
Visible = quota.Visible;
MaxFileSize = quota.MaxFileSize;
Features = quota.Features;
}
2022-02-15 11:52:43 +00:00
public override int GetHashCode()
{
return Tenant.GetHashCode();
}
public override bool Equals(object obj)
{
return obj is TenantQuota q && q.Tenant == Tenant;
}
2022-09-09 07:27:35 +00:00
public async Task Check(IServiceProvider serviceProvider)
2022-08-24 16:05:15 +00:00
{
2022-09-06 09:14:08 +00:00
foreach (var checker in serviceProvider.GetServices<ITenantQuotaFeatureChecker>())
2022-08-24 16:05:15 +00:00
{
2022-09-15 14:44:48 +00:00
await checker.CheckUsed(this);
}
2022-08-24 16:05:15 +00:00
}
public static TenantQuota operator *(TenantQuota quota, int quantity)
2022-08-24 16:05:15 +00:00
{
2022-08-25 10:41:52 +00:00
var newQuota = new TenantQuota(quota);
newQuota.Price *= quantity;
2022-09-06 09:14:08 +00:00
for (var i = 0; i < newQuota.TenantQuotaFeatures.Count; i++)
2022-08-24 16:05:15 +00:00
{
2022-09-06 09:14:08 +00:00
newQuota.TenantQuotaFeatures[i].Multiply(quantity);
2022-08-24 16:05:15 +00:00
}
return newQuota;
2022-08-24 16:05:15 +00:00
}
public static TenantQuota operator +(TenantQuota old, TenantQuota quota)
{
if (old == null)
{
return quota;
}
2022-08-25 10:41:52 +00:00
var newQuota = new TenantQuota(quota);
newQuota.Price += quota.Price;
newQuota.Visible &= quota.Visible;
newQuota.ProductId = "";
2022-09-06 09:14:08 +00:00
foreach (var f in newQuota.TenantQuotaFeatures)
{
2022-09-09 12:13:54 +00:00
if (f is MaxFileSizeFeature fileSize)
{
fileSize.Value = Math.Max(fileSize.Value, quota.MaxFileSize);
}
2022-09-13 15:18:43 +00:00
else if (f is TenantQuotaFeatureCount count)
2022-09-06 09:14:08 +00:00
{
count.Value += quota.GetFeature<int>(f.Name).Value;
}
2022-09-13 15:18:43 +00:00
else if (f is TenantQuotaFeatureSize length)
{
2022-09-06 09:14:08 +00:00
length.Value += quota.GetFeature<long>(f.Name).Value;
}
else if (f is TenantQuotaFeatureFlag flag)
{
flag.Value |= quota.GetFeature<bool>(f.Name).Value;
}
}
2022-08-24 16:05:15 +00:00
return newQuota;
}
2022-02-15 11:52:43 +00:00
public void Mapping(Profile profile)
{
2022-09-09 12:13:54 +00:00
profile.CreateMap<DbQuota, TenantQuota>();
2022-02-15 11:52:43 +00:00
}
2022-09-06 09:14:08 +00:00
2022-09-08 07:23:26 +00:00
public TenantQuotaFeature<T> GetFeature<T>(string name)
2022-09-06 09:14:08 +00:00
{
return TenantQuotaFeatures.OfType<TenantQuotaFeature<T>>().FirstOrDefault(r => r.Name == name);
}
2022-09-09 14:39:28 +00:00
public T GetFeature<T>() where T : TenantQuotaFeature
{
return TenantQuotaFeatures.OfType<T>().FirstOrDefault();
}
2022-09-06 09:14:08 +00:00
internal string GetFeature(string name)
{
return _featuresList.FirstOrDefault(f => f.StartsWith($"{name}"));
}
internal void ReplaceFeature<T>(string name, T value)
{
var featureValue = GetFeature(name);
_featuresList.Remove(featureValue);
if (!EqualityComparer<T>.Default.Equals(value, default))
{
_featuresList.Add($"{name}:{value}");
}
}
}