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

260 lines
11 KiB
C#
Raw Normal View History

2019-06-21 10:42:16 +00:00
/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL 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 more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
using System;
2020-02-17 08:58:14 +00:00
using System.Collections.Generic;
2020-08-12 09:58:08 +00:00
using ASC.Common;
2019-10-31 11:28:30 +00:00
using ASC.Common.Logging;
2019-06-21 10:42:16 +00:00
//using System.Web;
using ASC.Common.Threading.Progress;
using ASC.Core;
using ASC.Core.Users;
using ASC.MessagingSystem;
2019-10-31 11:28:30 +00:00
//using ASC.Web.CRM.Core;
2019-06-21 10:42:16 +00:00
using ASC.Web.Core.Users;
//using ASC.Web.Files.Services.WCFService;
//using ASC.Web.Projects.Core.Engine;
using ASC.Web.Studio.Core.Notify;
//using Autofac;
//using CrmDaoFactory = ASC.CRM.Core.Dao.DaoFactory;
2019-10-31 11:28:30 +00:00
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
2020-08-12 09:58:08 +00:00
using Microsoft.Extensions.Primitives;
2019-06-21 10:42:16 +00:00
namespace ASC.Data.Reassigns
{
public class ReassignProgressItem : IProgressItem
{
private readonly HttpContext _context;
2020-06-03 15:04:14 +00:00
private readonly IDictionary<string, StringValues> _httpHeaders;
2019-06-21 10:42:16 +00:00
private readonly int _tenantId;
private readonly Guid _currentUserId;
private readonly bool _deleteProfile;
//private readonly IFileStorageService _docService;
//private readonly ProjectsReassign _projectsReassign;
public object Id { get; set; }
public object Status { get; set; }
public object Error { get; set; }
public double Percentage { get; set; }
public bool IsCompleted { get; set; }
2019-08-15 13:56:54 +00:00
public Guid FromUser { get; }
2019-10-31 11:28:30 +00:00
public Guid ToUser { get; }
2020-08-12 09:58:08 +00:00
private IServiceProvider ServiceProvider { get; }
private QueueWorkerRemove QueueWorkerRemove { get; }
2019-10-31 11:28:30 +00:00
public ReassignProgressItem(
IServiceProvider serviceProvider,
HttpContext context,
QueueWorkerReassign queueWorkerReassign,
QueueWorkerRemove queueWorkerRemove,
2019-09-09 12:56:33 +00:00
int tenantId, Guid fromUserId, Guid toUserId, Guid currentUserId, bool deleteProfile)
2019-10-31 11:28:30 +00:00
{
ServiceProvider = serviceProvider;
_context = context;
QueueWorkerRemove = queueWorkerRemove;
2019-06-21 10:42:16 +00:00
_httpHeaders = QueueWorker.GetHttpHeaders(context.Request);
_tenantId = tenantId;
2019-08-15 13:56:54 +00:00
FromUser = fromUserId;
ToUser = toUserId;
2019-06-21 10:42:16 +00:00
_currentUserId = currentUserId;
_deleteProfile = deleteProfile;
//_docService = Web.Files.Classes.Global.FileStorageService;
//_projectsReassign = new ProjectsReassign();
2019-06-21 12:42:27 +00:00
Id = queueWorkerReassign.GetProgressItemId(tenantId, fromUserId);
2019-06-21 10:42:16 +00:00
Status = ProgressStatus.Queued;
Error = null;
Percentage = 0;
IsCompleted = false;
}
public void RunJob()
2020-07-29 21:57:58 +00:00
{
using var scope = ServiceProvider.CreateScope();
2020-08-24 18:41:06 +00:00
var scopeClass = scope.ServiceProvider.GetService<ReassignProgressItemScope>();
2020-07-29 21:57:58 +00:00
var logger = scopeClass.Options.Get("ASC.Web");
var tenant = scopeClass.TenantManager.SetCurrentTenant(_tenantId);
2019-06-21 10:42:16 +00:00
try
{
Percentage = 0;
Status = ProgressStatus.Started;
2020-07-29 21:57:58 +00:00
scopeClass.SecurityContext.AuthenticateMe(_currentUserId);
2019-06-21 10:42:16 +00:00
2019-08-15 13:56:54 +00:00
logger.InfoFormat("reassignment of data from {0} to {1}", FromUser, ToUser);
2019-06-21 10:42:16 +00:00
logger.Info("reassignment of data from documents");
Percentage = 33;
//_docService.ReassignStorage(_fromUserId, _toUserId);
logger.Info("reassignment of data from projects");
Percentage = 66;
//_projectsReassign.Reassign(_fromUserId, _toUserId);
2020-07-29 21:57:58 +00:00
if (!scopeClass.CoreBaseSettings.CustomMode)
2019-06-21 10:42:16 +00:00
{
logger.Info("reassignment of data from crm");
Percentage = 99;
//using (var scope = DIHelper.Resolve(_tenantId))
//{
// var crmDaoFactory = scope.Resolve<CrmDaoFactory>();
// crmDaoFactory.ContactDao.ReassignContactsResponsible(_fromUserId, _toUserId);
// crmDaoFactory.DealDao.ReassignDealsResponsible(_fromUserId, _toUserId);
// crmDaoFactory.TaskDao.ReassignTasksResponsible(_fromUserId, _toUserId);
// crmDaoFactory.CasesDao.ReassignCasesResponsible(_fromUserId, _toUserId);
//}
}
2020-07-29 21:57:58 +00:00
SendSuccessNotify(scopeClass.UserManager, scopeClass.StudioNotifyService, scopeClass.MessageService, scopeClass.MessageTarget, scopeClass.DisplayUserSettingsHelper);
2019-06-21 10:42:16 +00:00
Percentage = 100;
Status = ProgressStatus.Done;
if (_deleteProfile)
{
2020-07-29 21:57:58 +00:00
DeleteUserProfile(scopeClass.UserManager, scopeClass.UserPhotoManager, scopeClass.MessageService, scopeClass.MessageTarget, scopeClass.DisplayUserSettingsHelper);
2019-06-21 10:42:16 +00:00
}
}
catch (Exception ex)
{
logger.Error(ex);
Status = ProgressStatus.Failed;
Error = ex.Message;
2020-07-29 21:57:58 +00:00
SendErrorNotify(scopeClass.UserManager, scopeClass.StudioNotifyService, ex.Message);
2019-06-21 10:42:16 +00:00
}
finally
{
logger.Info("data reassignment is complete");
IsCompleted = true;
}
}
public object Clone()
{
return MemberwiseClone();
}
private void SendSuccessNotify(UserManager userManager, StudioNotifyService studioNotifyService, MessageService messageService, MessageTarget messageTarget, DisplayUserSettingsHelper displayUserSettingsHelper)
2019-06-21 10:42:16 +00:00
{
2019-09-18 12:14:15 +00:00
var fromUser = userManager.GetUsers(FromUser);
var toUser = userManager.GetUsers(ToUser);
2019-06-21 10:42:16 +00:00
2019-09-18 12:14:15 +00:00
studioNotifyService.SendMsgReassignsCompleted(_currentUserId, fromUser, toUser);
2019-06-21 10:42:16 +00:00
var fromUserName = fromUser.DisplayUserName(false, displayUserSettingsHelper);
var toUserName = toUser.DisplayUserName(false, displayUserSettingsHelper);
2019-06-21 10:42:16 +00:00
if (_httpHeaders != null)
2019-10-22 11:21:44 +00:00
messageService.Send(_httpHeaders, MessageAction.UserDataReassigns, messageTarget.Create(FromUser), new[] { fromUserName, toUserName });
2019-06-21 10:42:16 +00:00
else
2019-10-22 11:21:44 +00:00
messageService.Send(MessageAction.UserDataReassigns, messageTarget.Create(FromUser), fromUserName, toUserName);
2019-06-21 10:42:16 +00:00
}
2019-09-18 12:14:15 +00:00
private void SendErrorNotify(UserManager userManager, StudioNotifyService studioNotifyService, string errorMessage)
2019-06-21 10:42:16 +00:00
{
2019-09-18 12:14:15 +00:00
var fromUser = userManager.GetUsers(FromUser);
var toUser = userManager.GetUsers(ToUser);
2019-06-21 10:42:16 +00:00
2019-09-18 12:14:15 +00:00
studioNotifyService.SendMsgReassignsFailed(_currentUserId, fromUser, toUser, errorMessage);
2019-06-21 10:42:16 +00:00
}
private void DeleteUserProfile(UserManager userManager, UserPhotoManager userPhotoManager, MessageService messageService, MessageTarget messageTarget, DisplayUserSettingsHelper displayUserSettingsHelper)
2019-06-21 10:42:16 +00:00
{
2019-09-18 12:14:15 +00:00
var user = userManager.GetUsers(FromUser);
var userName = user.DisplayUserName(false, displayUserSettingsHelper);
2019-06-21 10:42:16 +00:00
2019-09-18 12:14:15 +00:00
userPhotoManager.RemovePhoto(user.ID);
userManager.DeleteUser(user.ID);
2019-06-21 12:42:27 +00:00
QueueWorkerRemove.Start(_tenantId, user, _currentUserId, false);
2019-06-21 10:42:16 +00:00
if (_httpHeaders != null)
2019-10-22 11:21:44 +00:00
messageService.Send(_httpHeaders, MessageAction.UserDeleted, messageTarget.Create(FromUser), new[] { userName });
2019-06-21 10:42:16 +00:00
else
2019-10-22 11:21:44 +00:00
messageService.Send(MessageAction.UserDeleted, messageTarget.Create(FromUser), userName);
2020-08-24 18:41:06 +00:00
}
}
public class ReassignProgressItemScope
{
internal TenantManager TenantManager { get; }
internal CoreBaseSettings CoreBaseSettings { get; }
internal MessageService MessageService { get; }
internal StudioNotifyService StudioNotifyService { get; }
internal SecurityContext SecurityContext { get; }
internal UserManager UserManager { get; }
internal UserPhotoManager UserPhotoManager { get; }
internal DisplayUserSettingsHelper DisplayUserSettingsHelper { get; }
internal MessageTarget MessageTarget { get; }
internal IOptionsMonitor<ILog> Options { get; }
2020-07-29 21:57:58 +00:00
2020-08-24 18:41:06 +00:00
public ReassignProgressItemScope(TenantManager tenantManager,
CoreBaseSettings coreBaseSettings,
MessageService messageService,
StudioNotifyService studioNotifyService,
SecurityContext securityContext,
UserManager userManager,
UserPhotoManager userPhotoManager,
DisplayUserSettingsHelper displayUserSettingsHelper,
MessageTarget messageTarget,
IOptionsMonitor<ILog> options)
2020-07-29 21:57:58 +00:00
{
2020-08-24 18:41:06 +00:00
TenantManager = tenantManager;
CoreBaseSettings = coreBaseSettings;
MessageService = messageService;
StudioNotifyService = studioNotifyService;
SecurityContext = securityContext;
UserManager = userManager;
UserPhotoManager = userPhotoManager;
DisplayUserSettingsHelper = displayUserSettingsHelper;
MessageTarget = messageTarget;
Options = options;
}
2019-06-21 10:42:16 +00:00
}
2019-10-31 11:28:30 +00:00
public static class ReassignProgressItemExtension
2019-10-31 11:28:30 +00:00
{
2020-02-17 08:58:14 +00:00
public static DIHelper AddReassignProgressItemService(this DIHelper services)
2019-11-06 15:03:09 +00:00
{
services.TryAddSingleton<ProgressQueueOptionsManager<ReassignProgressItem>>();
2019-10-31 11:28:30 +00:00
services.TryAddSingleton<ProgressQueue<ReassignProgressItem>>();
2020-08-24 18:41:06 +00:00
services.TryAddScoped<ReassignProgressItemScope>();
2020-06-08 09:44:01 +00:00
services.AddSingleton<IPostConfigureOptions<ProgressQueue<ReassignProgressItem>>, ConfigureProgressQueue<ReassignProgressItem>>();
2019-10-31 11:28:30 +00:00
return services;
}
}
2019-06-21 10:42:16 +00:00
}