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

218 lines
8.9 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;
2019-08-15 12:04:42 +00:00
using System.Collections.Generic;
using ASC.Common.Logging;
2019-06-21 10:42:16 +00:00
//using System.Web;
using ASC.Common.Threading.Progress;
using ASC.Core;
2019-08-15 12:04:42 +00:00
using ASC.Core.Tenants;
2019-06-21 10:42:16 +00:00
using ASC.Core.Users;
2019-09-18 12:14:15 +00:00
using ASC.Data.Storage;
2019-06-21 10:42:16 +00:00
using ASC.MessagingSystem;
2019-09-18 12:14:15 +00:00
using ASC.Web.Core;
//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;
using Microsoft.AspNetCore.Http;
2019-09-18 12:14:15 +00:00
using Microsoft.Extensions.DependencyInjection;
2019-06-21 10:42:16 +00:00
namespace ASC.Data.Reassigns
{
public class ReassignProgressItem : IProgressItem
{
private readonly HttpContext _context;
private readonly Dictionary<string, string> _httpHeaders;
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; }
public Guid ToUser { get; }
2019-09-18 12:14:15 +00:00
public IServiceProvider ServiceProvider { get; }
2019-06-21 12:42:27 +00:00
public QueueWorkerRemove QueueWorkerRemove { get; }
2019-06-21 10:42:16 +00:00
2019-09-09 12:56:33 +00:00
public ReassignProgressItem(
2019-09-18 12:14:15 +00:00
IServiceProvider serviceProvider,
2019-09-09 12:56:33 +00:00
HttpContext context,
QueueWorkerReassign queueWorkerReassign,
QueueWorkerRemove queueWorkerRemove,
int tenantId, Guid fromUserId, Guid toUserId, Guid currentUserId, bool deleteProfile)
2019-09-18 12:14:15 +00:00
{
ServiceProvider = serviceProvider;
2019-06-21 10:42:16 +00:00
_context = context;
2019-06-21 12:42:27 +00:00
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()
{
2019-09-18 12:14:15 +00:00
var logger = LogManager.GetLogger("ASC.Web");
using var scope = ServiceProvider.CreateScope();
var tenantManager = scope.ServiceProvider.GetService<TenantManager>();
var tenant = tenantManager.SetCurrentTenant(_tenantId);
2019-09-18 15:19:30 +00:00
var coreSettings = scope.ServiceProvider.GetService<CoreBaseSettings>();
2019-09-18 12:14:15 +00:00
var messageService = scope.ServiceProvider.GetService<MessageService>();
var studioNotifyService = scope.ServiceProvider.GetService<StudioNotifyService>();
var securityContext = scope.ServiceProvider.GetService<SecurityContext>();
var userManager = scope.ServiceProvider.GetService<UserManager>();
var userPhotoManager = scope.ServiceProvider.GetService<UserPhotoManager>();
2019-06-21 10:42:16 +00:00
try
{
Percentage = 0;
Status = ProgressStatus.Started;
2019-09-18 12:14:15 +00:00
securityContext.AuthenticateMe(_tenantId, _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);
2019-09-18 15:19:30 +00:00
if (!coreSettings.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);
//}
}
2019-09-18 12:14:15 +00:00
SendSuccessNotify(userManager, studioNotifyService, messageService);
2019-06-21 10:42:16 +00:00
Percentage = 100;
Status = ProgressStatus.Done;
if (_deleteProfile)
{
2019-09-18 12:14:15 +00:00
DeleteUserProfile(userManager, userPhotoManager, messageService);
2019-06-21 10:42:16 +00:00
}
}
catch (Exception ex)
{
logger.Error(ex);
Status = ProgressStatus.Failed;
Error = ex.Message;
2019-09-18 12:14:15 +00:00
SendErrorNotify(userManager, 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();
}
2019-09-18 12:14:15 +00:00
private void SendSuccessNotify(UserManager userManager, StudioNotifyService studioNotifyService, MessageService messageService)
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
2019-09-18 12:14:15 +00:00
var fromUserName = fromUser.DisplayUserName(false, userManager);
var toUserName = toUser.DisplayUserName(false, userManager);
2019-06-21 10:42:16 +00:00
if (_httpHeaders != null)
2019-09-18 12:14:15 +00:00
messageService.Send(_httpHeaders, MessageAction.UserDataReassigns, MessageTarget.Create(FromUser), new[] { fromUserName, toUserName });
2019-06-21 10:42:16 +00:00
else
2019-09-18 12:14:15 +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
}
2019-09-18 12:14:15 +00:00
private void DeleteUserProfile(UserManager userManager, UserPhotoManager userPhotoManager, MessageService messageService)
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, userManager);
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-09-18 12:14:15 +00:00
messageService.Send(_httpHeaders, MessageAction.UserDeleted, MessageTarget.Create(FromUser), new[] { userName });
2019-06-21 10:42:16 +00:00
else
2019-09-18 12:14:15 +00:00
messageService.Send(MessageAction.UserDeleted, MessageTarget.Create(FromUser), userName);
2019-06-21 10:42:16 +00:00
}
}
}