DocSpace-client/common/ASC.Data.Reassigns/RemoveProgressItem.cs

335 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-05-20 15:24:41 +00:00
2022-03-17 15:13:38 +00:00
namespace ASC.Data.Reassigns;
2023-04-25 16:23:54 +00:00
/// <summary>
/// </summary>
2022-03-17 15:13:38 +00:00
[Transient]
public class RemoveProgressItem : DistributedTaskProgress
{
2023-04-25 16:23:54 +00:00
/// <summary>ID of the user whose data is deleted</summary>
/// <type>System.Guid, System</type>
2022-03-17 15:13:38 +00:00
public Guid FromUser { get; private set; }
2023-04-25 16:23:54 +00:00
/// <summary>The user whose data is deleted</summary>
/// <type>ASC.Core.Users.UserInfo, ASC.Core.Common</type>
2022-03-17 15:13:38 +00:00
public UserInfo User { get; private set; }
2022-03-17 15:13:38 +00:00
private readonly IDictionary<string, StringValues> _httpHeaders;
private readonly IServiceScopeFactory _serviceScopeFactory;
2022-04-14 19:23:57 +00:00
private readonly int _tenantId;
private readonly Guid _currentUserId;
private readonly bool _notify;
2022-03-17 15:13:38 +00:00
//private readonly IFileStorageService _docService;
//private readonly MailGarbageEngine _mailEraser;
public RemoveProgressItem(
IServiceScopeFactory serviceScopeFactory,
2022-03-24 11:57:39 +00:00
IDictionary<string, StringValues> httpHeaders,
int tenantId, UserInfo user, Guid currentUserId, bool notify)
{
2022-04-14 19:42:15 +00:00
_httpHeaders = httpHeaders;
2022-03-17 15:13:38 +00:00
_serviceScopeFactory = serviceScopeFactory;
//_docService = Web.Files.Classes.Global.FileStorageService;
//_mailEraser = new MailGarbageEngine();
_tenantId = tenantId;
User = user;
FromUser = user.Id;
_currentUserId = currentUserId;
_notify = notify;
Id = QueueWorkerRemove.GetProgressItemId(tenantId, FromUser);
Status = DistributedTaskStatus.Created;
Exception = null;
Percentage = 0;
IsCompleted = false;
}
2022-10-27 11:14:41 +00:00
protected override async Task DoJob()
2022-03-17 15:13:38 +00:00
{
await using var scope = _serviceScopeFactory.CreateAsyncScope();
2022-03-17 15:13:38 +00:00
var scopeClass = scope.ServiceProvider.GetService<RemoveProgressItemScope>();
var (tenantManager, coreBaseSettings, messageService, studioNotifyService, securityContext, userManager, messageTarget, webItemManagerSecurity, storageFactory, userFormatter, options) = scopeClass;
var logger = options.CreateLogger("ASC.Web");
2023-03-20 16:16:00 +00:00
await tenantManager.SetCurrentTenantAsync(_tenantId);
2022-03-17 15:13:38 +00:00
var userName = userFormatter.GetUserName(User, DisplayUserNameFormat.Default);
2022-03-17 15:13:38 +00:00
try
2020-08-07 15:42:58 +00:00
{
2022-03-17 15:13:38 +00:00
Percentage = 0;
Status = DistributedTaskStatus.Running;
2020-09-30 10:54:49 +00:00
2023-03-14 20:44:44 +00:00
await securityContext.AuthenticateMeWithoutCookieAsync(_currentUserId);
2019-06-21 10:42:16 +00:00
2022-03-17 15:13:38 +00:00
long crmSpace;
2022-10-27 10:49:42 +00:00
var wrapper = await GetUsageSpace(webItemManagerSecurity);
2019-06-21 10:42:16 +00:00
2022-04-28 09:15:33 +00:00
logger.LogInformation("deleting user data for {fromUser} ", FromUser);
2019-06-21 10:42:16 +00:00
2022-04-25 18:02:18 +00:00
logger.LogInformation("deleting of data from documents");
2019-06-21 10:42:16 +00:00
2022-03-17 15:13:38 +00:00
//_docService.DeleteStorage(_userId);
Percentage = 25;
PublishChanges();
2019-06-21 10:42:16 +00:00
2022-03-17 15:13:38 +00:00
if (!coreBaseSettings.CustomMode)
2022-02-10 08:42:03 +00:00
{
2022-04-25 18:02:18 +00:00
logger.LogInformation("deleting of data from crm");
2019-06-21 10:42:16 +00:00
2022-03-17 15:13:38 +00:00
//using (var scope = DIHelper.Resolve(_tenantId))
//{
// var crmDaoFactory = scope.Resolve<CrmDaoFactory>();
crmSpace = 0;// crmDaoFactory.ReportDao.GetFiles(_userId).Sum(file => file.ContentLength);
// crmDaoFactory.ReportDao.DeleteFiles(_userId);
//}
Percentage = 50;
2022-02-10 08:42:03 +00:00
}
2022-03-17 15:13:38 +00:00
else
{
crmSpace = 0;
}
2022-03-17 15:13:38 +00:00
PublishChanges();
2022-04-25 18:02:18 +00:00
logger.LogInformation("deleting of data from mail");
2022-03-17 15:13:38 +00:00
//_mailEraser.ClearUserMail(_userId);
Percentage = 75;
PublishChanges();
2022-04-25 18:02:18 +00:00
logger.LogInformation("deleting of data from talk");
2022-10-27 10:49:42 +00:00
await DeleteTalkStorage(storageFactory);
2022-03-17 15:13:38 +00:00
Percentage = 99;
PublishChanges();
2023-03-16 15:23:59 +00:00
await SendSuccessNotifyAsync(studioNotifyService, messageService, messageTarget, userName, wrapper.DocsSpace, crmSpace, wrapper.MailSpace, wrapper.TalkSpace);
2022-03-17 15:13:38 +00:00
Percentage = 100;
Status = DistributedTaskStatus.Completed;
}
catch (Exception ex)
{
2022-05-20 15:24:41 +00:00
logger.ErrorRemoveProgressItem(ex);
2022-03-17 15:13:38 +00:00
Status = DistributedTaskStatus.Failted;
Exception = ex;
2023-03-20 16:16:00 +00:00
await SendErrorNotifyAsync(studioNotifyService, ex.Message, userName);
2022-03-17 15:13:38 +00:00
}
finally
{
2022-04-25 18:02:18 +00:00
logger.LogInformation("data deletion is complete");
2022-03-17 15:13:38 +00:00
IsCompleted = true;
PublishChanges();
}
}
2022-03-17 15:13:38 +00:00
public object Clone()
{
return MemberwiseClone();
}
2022-10-27 10:49:42 +00:00
private async Task<UsageSpaceWrapper> GetUsageSpace(WebItemManagerSecurity webItemManagerSecurity)
2022-03-17 15:13:38 +00:00
{
2022-10-27 10:49:42 +00:00
var usageSpaceWrapper = new UsageSpaceWrapper();
2022-03-17 15:13:38 +00:00
var webItems = webItemManagerSecurity.GetItems(Web.Core.WebZones.WebZoneType.All, ItemAvailableState.All);
2022-03-17 15:13:38 +00:00
foreach (var item in webItems)
{
IUserSpaceUsage manager;
2022-03-17 15:13:38 +00:00
if (item.ID == WebItemManager.DocumentsProductID)
2022-02-10 08:42:03 +00:00
{
2022-03-17 15:13:38 +00:00
manager = item.Context.SpaceUsageStatManager as IUserSpaceUsage;
if (manager == null)
{
continue;
}
2022-10-27 10:49:42 +00:00
usageSpaceWrapper.DocsSpace = await manager.GetUserSpaceUsageAsync(FromUser);
2022-02-10 08:42:03 +00:00
}
2019-06-21 10:42:16 +00:00
2022-03-17 15:13:38 +00:00
if (item.ID == WebItemManager.MailProductID)
2022-02-10 08:42:03 +00:00
{
2022-03-17 15:13:38 +00:00
manager = item.Context.SpaceUsageStatManager as IUserSpaceUsage;
if (manager == null)
{
continue;
}
2022-10-27 10:49:42 +00:00
usageSpaceWrapper.MailSpace = await manager.GetUserSpaceUsageAsync(FromUser);
2022-02-10 08:42:03 +00:00
}
2022-03-17 15:13:38 +00:00
if (item.ID == WebItemManager.TalkProductID)
2022-02-10 08:42:03 +00:00
{
2022-03-17 15:13:38 +00:00
manager = item.Context.SpaceUsageStatManager as IUserSpaceUsage;
if (manager == null)
{
continue;
}
2022-10-27 10:49:42 +00:00
usageSpaceWrapper.TalkSpace = await manager.GetUserSpaceUsageAsync(FromUser);
2022-02-10 08:42:03 +00:00
}
2022-03-17 15:13:38 +00:00
}
2022-10-27 10:49:42 +00:00
return usageSpaceWrapper;
2022-03-17 15:13:38 +00:00
}
2019-11-06 15:03:09 +00:00
2022-10-27 10:49:42 +00:00
private async Task DeleteTalkStorage(StorageFactory storageFactory)
2022-03-17 15:13:38 +00:00
{
using var md5 = MD5.Create();
var data = md5.ComputeHash(Encoding.Default.GetBytes(FromUser.ToString()));
2022-03-17 15:13:38 +00:00
var sBuilder = new StringBuilder();
2022-03-17 15:13:38 +00:00
for (int i = 0, n = data.Length; i < n; i++)
{
sBuilder.Append(data[i].ToString("x2"));
2020-08-07 15:42:58 +00:00
}
2022-03-17 15:13:38 +00:00
var md5Hash = sBuilder.ToString();
2023-03-24 16:24:59 +00:00
var storage = await storageFactory.GetStorageAsync(_tenantId, "talk");
2022-10-27 10:49:42 +00:00
if (storage != null && await storage.IsDirectoryAsync(md5Hash))
2022-03-17 15:13:38 +00:00
{
2022-10-27 10:49:42 +00:00
await storage.DeleteDirectoryAsync(md5Hash);
2022-03-17 15:13:38 +00:00
}
}
2023-03-16 15:23:59 +00:00
private async Task SendSuccessNotifyAsync(StudioNotifyService studioNotifyService, MessageService messageService, MessageTarget messageTarget, string userName, long docsSpace, long crmSpace, long mailSpace, long talkSpace)
2020-08-24 18:41:06 +00:00
{
2022-03-17 15:13:38 +00:00
if (_notify)
2020-08-24 18:41:06 +00:00
{
2023-03-20 16:16:00 +00:00
await studioNotifyService.SendMsgRemoveUserDataCompletedAsync(_currentUserId, User, userName,
2022-03-17 15:13:38 +00:00
docsSpace, crmSpace, mailSpace, talkSpace);
2020-08-24 18:41:06 +00:00
}
2020-08-31 08:18:07 +00:00
2022-03-17 15:13:38 +00:00
if (_httpHeaders != null)
2020-08-31 08:18:07 +00:00
{
2023-03-16 15:23:59 +00:00
await messageService.SendAsync(_httpHeaders, MessageAction.UserDataRemoving, messageTarget.Create(FromUser), new[] { userName });
2020-08-31 08:18:07 +00:00
}
2022-03-17 15:13:38 +00:00
else
2020-08-07 15:42:58 +00:00
{
2023-03-16 15:23:59 +00:00
await messageService.SendAsync(MessageAction.UserDataRemoving, messageTarget.Create(FromUser), userName);
2022-03-17 15:13:38 +00:00
}
}
2019-11-06 15:03:09 +00:00
2023-03-20 16:16:00 +00:00
private async Task SendErrorNotifyAsync(StudioNotifyService studioNotifyService, string errorMessage, string userName)
2022-03-17 15:13:38 +00:00
{
if (!_notify)
{
return;
}
2023-03-20 16:16:00 +00:00
await studioNotifyService.SendMsgRemoveUserDataFailedAsync(_currentUserId, User, userName, errorMessage);
2022-03-17 15:13:38 +00:00
}
}
2022-03-17 15:13:38 +00:00
[Scope]
public class RemoveProgressItemScope
{
private readonly TenantManager _tenantManager;
private readonly CoreBaseSettings _coreBaseSettings;
private readonly MessageService _messageService;
private readonly StudioNotifyService _studioNotifyService;
private readonly SecurityContext _securityContext;
private readonly UserManager _userManager;
private readonly MessageTarget _messageTarget;
private readonly WebItemManagerSecurity _webItemManagerSecurity;
private readonly StorageFactory _storageFactory;
private readonly UserFormatter _userFormatter;
private readonly ILoggerProvider _options;
2022-03-17 15:13:38 +00:00
public RemoveProgressItemScope(TenantManager tenantManager,
CoreBaseSettings coreBaseSettings,
MessageService messageService,
StudioNotifyService studioNotifyService,
SecurityContext securityContext,
UserManager userManager,
MessageTarget messageTarget,
WebItemManagerSecurity webItemManagerSecurity,
StorageFactory storageFactory,
UserFormatter userFormatter,
ILoggerProvider options)
2022-03-17 15:13:38 +00:00
{
_tenantManager = tenantManager;
_coreBaseSettings = coreBaseSettings;
_messageService = messageService;
_studioNotifyService = studioNotifyService;
_securityContext = securityContext;
_userManager = userManager;
_messageTarget = messageTarget;
_webItemManagerSecurity = webItemManagerSecurity;
_storageFactory = storageFactory;
_userFormatter = userFormatter;
_options = options;
}
public void Deconstruct(out TenantManager tenantManager,
out CoreBaseSettings coreBaseSettings,
out MessageService messageService,
out StudioNotifyService studioNotifyService,
out SecurityContext securityContext,
out UserManager userManager,
out MessageTarget messageTarget,
out WebItemManagerSecurity webItemManagerSecurity,
out StorageFactory storageFactory,
out UserFormatter userFormatter,
out ILoggerProvider optionsMonitor)
2022-03-17 15:13:38 +00:00
{
tenantManager = _tenantManager;
coreBaseSettings = _coreBaseSettings;
messageService = _messageService;
studioNotifyService = _studioNotifyService;
securityContext = _securityContext;
userManager = _userManager;
messageTarget = _messageTarget;
webItemManagerSecurity = _webItemManagerSecurity;
storageFactory = _storageFactory;
userFormatter = _userFormatter;
optionsMonitor = _options;
}
}
2022-10-27 10:49:42 +00:00
class UsageSpaceWrapper
{
public long DocsSpace { get; set; }
public long MailSpace { get; set; }
public long TalkSpace { get; set; }
}
2022-03-17 15:13:38 +00:00
public static class RemoveProgressItemExtension
{
public static void Register(DIHelper services)
{
services.TryAdd<RemoveProgressItemScope>();
}
2019-06-21 10:42:16 +00:00
}