From cfb0c957e9098e6d1fd87ac394efa10dc230194b Mon Sep 17 00:00:00 2001 From: Nikolay Rechkin Date: Thu, 15 Sep 2022 15:38:31 +0300 Subject: [PATCH] Quota: user quota storage core_user -> webstudio_settings --- .../Base/Data/LdapObjectExtension.cs | 10 ++-- common/ASC.Api.Core/Model/EmployeeFullDto.cs | 5 +- .../Context/Impl/UserManager.cs | 14 +----- common/ASC.Core.Common/Core/UserInfo.cs | 1 - common/ASC.Core.Common/EF/Model/User/User.cs | 9 ---- .../Tenants/TenantUserQuotaSettings.cs | 50 +++++++++++++++++++ .../Users/UserQuotaSettings.cs | 11 ++-- .../Core/Core/Dao/TeamlabDao/FileDao.cs | 7 +-- .../ASC.People/Server/Api/UserController.cs | 5 +- .../Api/Settings/SettingsController.cs | 4 +- 10 files changed, 73 insertions(+), 43 deletions(-) create mode 100644 common/ASC.Core.Common/Tenants/TenantUserQuotaSettings.cs diff --git a/common/ASC.ActiveDirectory/Base/Data/LdapObjectExtension.cs b/common/ASC.ActiveDirectory/Base/Data/LdapObjectExtension.cs index 5a8276d0bb..b084a0c4cc 100644 --- a/common/ASC.ActiveDirectory/Base/Data/LdapObjectExtension.cs +++ b/common/ASC.ActiveDirectory/Base/Data/LdapObjectExtension.cs @@ -148,7 +148,7 @@ public class LdapObjectExtension var emails = GetContacts(ldapUser, Mapping.AdditionalMail, settings); var skype = GetContacts(ldapUser, Mapping.Skype, settings); - var quotaSettings = _settingsManager.Load(); + var quotaSettings = _settingsManager.Load(); var quota = settings.LdapMapping.ContainsKey(Mapping.UserQuotaLimit) ? ByteConverter.ConvertSizeToBytes(GetAttribute(ldapUser, settings.LdapMapping[Mapping.UserQuotaLimit])) : quotaSettings.DefaultUserQuota; if (string.IsNullOrEmpty(userName)) @@ -173,10 +173,14 @@ public class LdapObjectExtension Title = !string.IsNullOrEmpty(title) ? title : string.Empty, Location = !string.IsNullOrEmpty(location) ? location : string.Empty, WorkFromDate = _tenantUtil.DateTimeNow(), - ContactsList = contacts, - QuotaLimit = quotaSettings.EnableUserQuota ? quota : -1 + ContactsList = contacts }; + if (quotaSettings.EnableUserQuota) + { + _settingsManager.SaveForUser(new UserQuotaSettings { UserQuota = quota }, Guid.Parse(user.Sid)); + } + if (!string.IsNullOrEmpty(firstName)) { user.FirstName = firstName.Length > MAX_NUMBER_OF_SYMBOLS diff --git a/common/ASC.Api.Core/Model/EmployeeFullDto.cs b/common/ASC.Api.Core/Model/EmployeeFullDto.cs index c1d6a21339..36cf458fda 100644 --- a/common/ASC.Api.Core/Model/EmployeeFullDto.cs +++ b/common/ASC.Api.Core/Model/EmployeeFullDto.cs @@ -199,11 +199,12 @@ public class EmployeeFullDtoHelper : EmployeeDtoHelper result.UsedSpace = Math.Max(0, _userManager.FindUserQuotaRows(_context.Tenant.Id, userInfo.Id).Where(r => !string.IsNullOrEmpty(r.Tag)).Sum(r => r.Counter)); - var quotaSettings = _settingsManager.Load(); + var quotaSettings = _settingsManager.Load(); + var userQuotaSettings = _settingsManager.LoadForUser(userInfo.IsSSO() ? Guid.Parse(userInfo.Sid) : userInfo.Id); if (quotaSettings.EnableUserQuota) { - result.QuotaLimit = userInfo.QuotaLimit; + result.QuotaLimit = userQuotaSettings != null ? userQuotaSettings.UserQuota : quotaSettings.DefaultUserQuota; } if (userInfo.Sex.HasValue) diff --git a/common/ASC.Core.Common/Context/Impl/UserManager.cs b/common/ASC.Core.Common/Context/Impl/UserManager.cs index 2e8c7c3778..2dddd03da4 100644 --- a/common/ASC.Core.Common/Context/Impl/UserManager.cs +++ b/common/ASC.Core.Common/Context/Impl/UserManager.cs @@ -53,7 +53,6 @@ public class UserManager private readonly IHttpContextAccessor _accessor; private readonly IUserService _userService; private readonly TenantManager _tenantManager; - private readonly SettingsManager _settingsManager; private readonly PermissionContext _permissionContext; private readonly UserManagerConstants _userManagerConstants; private readonly CoreBaseSettings _coreBaseSettings; @@ -77,7 +76,6 @@ public class UserManager IQuotaService quotaService, IUserService service, TenantManager tenantManager, - SettingsManager settingsManager, PermissionContext permissionContext, UserManagerConstants userManagerConstants, CoreBaseSettings coreBaseSettings, @@ -91,7 +89,6 @@ public class UserManager _quotaService = quotaService; _userService = service; _tenantManager = tenantManager; - _settingsManager = settingsManager; _permissionContext = permissionContext; _userManagerConstants = userManagerConstants; _coreBaseSettings = coreBaseSettings; @@ -108,7 +105,6 @@ public class UserManager IQuotaService quotaService, IUserService service, TenantManager tenantManager, - SettingsManager settingsManager, PermissionContext permissionContext, UserManagerConstants userManagerConstants, CoreBaseSettings coreBaseSettings, @@ -119,7 +115,7 @@ public class UserManager ILogger log, ICache cache, IHttpContextAccessor httpContextAccessor) - : this(quotaService, service, tenantManager, settingsManager, permissionContext, userManagerConstants, coreBaseSettings, coreSettings, instanceCrypto, radicaleClient, cardDavAddressbook, log, cache) + : this(quotaService, service, tenantManager, permissionContext, userManagerConstants, coreBaseSettings, coreSettings, instanceCrypto, radicaleClient, cardDavAddressbook, log, cache) { _accessor = httpContextAccessor; } @@ -362,15 +358,7 @@ public class UserManager } var oldUserData = _userService.GetUserByUserName(_tenantManager.GetCurrentTenant().Id, u.UserName); - if (oldUserData == null) //new user - { - var quotaSettings = _settingsManager.Load(); - if (quotaSettings.EnableUserQuota) - { - u.QuotaLimit = quotaSettings.DefaultUserQuota; - } - } var newUser = _userService.SaveUser(_tenantManager.GetCurrentTenant().Id, u); if (syncCardDav) diff --git a/common/ASC.Core.Common/Core/UserInfo.cs b/common/ASC.Core.Common/Core/UserInfo.cs index 622f12fefd..f5d6ed627d 100644 --- a/common/ASC.Core.Common/Core/UserInfo.cs +++ b/common/ASC.Core.Common/Core/UserInfo.cs @@ -74,7 +74,6 @@ public sealed class UserInfo : IDirectRecipient, ICloneable, IMapFrom public string SsoNameId { get; set; } // SSO SAML user identificator public string SsoSessionId { get; set; } // SSO SAML user session identificator public DateTime CreateDate { get; set; } - public long QuotaLimit { get; set; } public override string ToString() { diff --git a/common/ASC.Core.Common/EF/Model/User/User.cs b/common/ASC.Core.Common/EF/Model/User/User.cs index 256ce30f6a..4d2ec9503d 100644 --- a/common/ASC.Core.Common/EF/Model/User/User.cs +++ b/common/ASC.Core.Common/EF/Model/User/User.cs @@ -53,7 +53,6 @@ public class User : BaseEntity, IMapFrom public bool Removed { get; set; } public DateTime CreateDate { get; set; } public DateTime LastModified { get; set; } - public long QuotaLimit { get; set; } public override object[] GetKeys() { @@ -236,10 +235,6 @@ public static class DbUserExtension entity.Property(e => e.WorkFromDate) .HasColumnName("workfromdate") .HasColumnType("datetime"); - - entity.Property(e => e.QuotaLimit) - .HasColumnName("quota_limit") - .HasDefaultValueSql("'-1'"); }); } @@ -352,10 +347,6 @@ public static class DbUserExtension .HasColumnName("username") .HasMaxLength(255); - entity.Property(e => e.QuotaLimit) - .HasColumnName("quota_limit") - .HasDefaultValueSql("'-1'"); - entity.Property(e => e.WorkFromDate).HasColumnName("workfromdate"); }); } diff --git a/common/ASC.Core.Common/Tenants/TenantUserQuotaSettings.cs b/common/ASC.Core.Common/Tenants/TenantUserQuotaSettings.cs new file mode 100644 index 0000000000..eb95d268a3 --- /dev/null +++ b/common/ASC.Core.Common/Tenants/TenantUserQuotaSettings.cs @@ -0,0 +1,50 @@ +// (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.Core.Tenants; + +[Serializable] +public class TenantUserQuotaSettings : ISettings +{ + public bool EnableUserQuota { get; set; } + + public long DefaultUserQuota { get; set; } + + public TenantUserQuotaSettings GetDefault() + { + return new TenantUserQuotaSettings + { + EnableUserQuota = false, + DefaultUserQuota = -1 + }; + } + + [JsonIgnore] + public Guid ID + { + get { return new Guid("{5FE28053-BCD4-466B-8A4B-71B612F0D6FC}"); } + } +} diff --git a/common/ASC.Core.Common/Users/UserQuotaSettings.cs b/common/ASC.Core.Common/Users/UserQuotaSettings.cs index 485bf25876..9ff076bb1c 100644 --- a/common/ASC.Core.Common/Users/UserQuotaSettings.cs +++ b/common/ASC.Core.Common/Users/UserQuotaSettings.cs @@ -1,4 +1,4 @@ -// (c) Copyright Ascensio System SIA 2010-2022 +// (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 @@ -29,22 +29,19 @@ namespace ASC.Core.Users; [Serializable] public class UserQuotaSettings : ISettings { - public bool EnableUserQuota { get; set; } - - public long DefaultUserQuota { get; set; } + public long UserQuota { get; set; } public UserQuotaSettings GetDefault() { return new UserQuotaSettings { - EnableUserQuota = false, - DefaultUserQuota = -1 + UserQuota = -1 }; } [JsonIgnore] public Guid ID { - get { return new Guid("{5FE28053-BCD4-466B-8A4B-71B612F0D6FC}"); } + get { return new Guid("{5A097CFA-1A8E-45CB-A4BB-FE8D2B5E5908}"); } } } diff --git a/products/ASC.Files/Core/Core/Dao/TeamlabDao/FileDao.cs b/products/ASC.Files/Core/Core/Dao/TeamlabDao/FileDao.cs index ec0cf1c5d8..df40f1cf2c 100644 --- a/products/ASC.Files/Core/Core/Dao/TeamlabDao/FileDao.cs +++ b/products/ASC.Files/Core/Core/Dao/TeamlabDao/FileDao.cs @@ -413,11 +413,12 @@ internal class FileDao : AbstractDao, IFileDao } var user = _userManager.GetUsers(file.Id == default ? _authContext.CurrentAccount.ID : file.CreateBy); - var quotaSettings = _settingsManager.Load(); + var quotaSettings = _settingsManager.Load(); + var userQuotaSettings = _settingsManager.LoadForUser(user.IsLDAP() ? Guid.Parse(user.Sid) : user.Id); - if (quotaSettings.EnableUserQuota && user.QuotaLimit != -1) + if (quotaSettings.EnableUserQuota && userQuotaSettings.UserQuota != -1) { - var quotaLimit = user.QuotaLimit; + var quotaLimit = userQuotaSettings.UserQuota; if (quotaLimit != -1) { diff --git a/products/ASC.People/Server/Api/UserController.cs b/products/ASC.People/Server/Api/UserController.cs index 163cb10c9f..7a06c2e590 100644 --- a/products/ASC.People/Server/Api/UserController.cs +++ b/products/ASC.People/Server/Api/UserController.cs @@ -1122,8 +1122,9 @@ public class UserController : PeopleControllerBase } } - user.QuotaLimit = inDto.Quota; - _userManager.SaveUserInfo(user, syncCardDav: true); + var quotaSettings = _settingsManager.Load(); + + _settingsManager.SaveForUser(new UserQuotaSettings { UserQuota = inDto.Quota }, user.IsLDAP() ? Guid.Parse(user.Sid) : user.Id); yield return await _employeeFullDtoHelper.GetFull(user); } diff --git a/web/ASC.Web.Api/Api/Settings/SettingsController.cs b/web/ASC.Web.Api/Api/Settings/SettingsController.cs index b3f49dff82..7e0e991bb2 100644 --- a/web/ASC.Web.Api/Api/Settings/SettingsController.cs +++ b/web/ASC.Web.Api/Api/Settings/SettingsController.cs @@ -24,8 +24,6 @@ // 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.Web.Studio.Core.Quota; - using Constants = ASC.Core.Users.Constants; namespace ASC.Web.Api.Controllers.Settings; @@ -278,7 +276,7 @@ public class SettingsController : BaseSettingsController { _permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings); - _settingsManager.Save(new UserQuotaSettings { EnableUserQuota = inDto.EnableUserQuota, DefaultUserQuota = inDto.DefaultUserQuota }); + _settingsManager.Save(new TenantUserQuotaSettings { EnableUserQuota = inDto.EnableUserQuota, DefaultUserQuota = inDto.DefaultUserQuota }); return Resource.SuccessfullySaveSettingsMessage; }