DocSpace-client/common/services/ASC.Data.Backup/Core/NotifyHelper.cs

239 lines
11 KiB
C#
Raw Normal View History

2020-05-20 15:14:44 +00:00
/*
*
* (c) Copyright Ascensio System Limited 2010-2020
*
* 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;
2021-05-17 18:27:39 +00:00
using System.Collections.Generic;
2020-06-29 09:32:43 +00:00
using System.Linq;
using ASC.Common;
2020-06-29 09:32:43 +00:00
using ASC.Core;
using ASC.Core.Tenants;
using ASC.Core.Users;
using ASC.Notify.Model;
using ASC.Notify.Patterns;
using ASC.Notify.Recipients;
using ASC.Web.Core.Users;
2021-05-17 18:27:39 +00:00
using ASC.Web.Core.WhiteLabel;
2020-06-29 09:32:43 +00:00
using ASC.Web.Studio.Core.Notify;
using ASC.Web.Studio.Utility;
using Microsoft.Extensions.DependencyInjection;
2020-05-20 15:14:44 +00:00
namespace ASC.Data.Backup
2020-10-19 15:53:15 +00:00
{
2020-10-22 17:57:18 +00:00
[Singletone(Additional = typeof(NotifyHelperExtension))]
2020-05-20 15:14:44 +00:00
public class NotifyHelper
2020-06-29 09:32:43 +00:00
{
2020-08-12 09:58:08 +00:00
private IServiceProvider ServiceProvider { get; }
2020-06-29 09:32:43 +00:00
public NotifyHelper(IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider;
}
2020-05-20 15:14:44 +00:00
2020-06-29 09:32:43 +00:00
public void SendAboutTransferStart(Tenant tenant, string targetRegion, bool notifyUsers)
{
MigrationNotify(tenant, Actions.MigrationPortalStart, targetRegion, string.Empty, notifyUsers);
2020-05-20 15:14:44 +00:00
}
2021-05-17 18:27:39 +00:00
public void SendAboutTransferComplete(Tenant tenant, string targetRegion, string targetAddress, bool notifyOnlyOwner, int toTenantId)
2020-06-29 09:32:43 +00:00
{
2021-05-17 18:27:39 +00:00
MigrationNotify(tenant, Actions.MigrationPortalSuccess, targetRegion, targetAddress, !notifyOnlyOwner, toTenantId);
2020-05-20 15:14:44 +00:00
}
2020-06-29 09:32:43 +00:00
public void SendAboutTransferError(Tenant tenant, string targetRegion, string resultAddress, bool notifyOnlyOwner)
{
MigrationNotify(tenant, !string.IsNullOrEmpty(targetRegion) ? Actions.MigrationPortalError : Actions.MigrationPortalServerFailure, targetRegion, resultAddress, !notifyOnlyOwner);
2020-05-20 15:14:44 +00:00
}
2020-06-29 09:32:43 +00:00
public void SendAboutBackupCompleted(Guid userId)
{
using var scope = ServiceProvider.CreateScope();
2020-08-24 18:41:06 +00:00
var scopeClass = scope.ServiceProvider.GetService<NotifyHelperScope>();
2021-05-17 18:27:39 +00:00
var (userManager, studioNotifyHelper, studioNotifySource, displayUserSettingsHelper, _) = scopeClass;
2020-08-31 08:18:07 +00:00
var client = WorkContext.NotifyContext.NotifyService.RegisterClient(studioNotifySource, scope);
2020-06-29 09:32:43 +00:00
client.SendNoticeToAsync(
Actions.BackupCreated,
2020-08-31 08:18:07 +00:00
new[] { studioNotifyHelper.ToRecipient(userId) },
2020-06-29 09:32:43 +00:00
new[] { StudioNotifyService.EMailSenderName },
2020-08-31 08:18:07 +00:00
new TagValue(Tags.OwnerName, userManager.GetUsers(userId).DisplayUserName(displayUserSettingsHelper)));
2020-05-20 15:14:44 +00:00
}
2020-06-29 09:32:43 +00:00
public void SendAboutRestoreStarted(Tenant tenant, bool notifyAllUsers)
{
using var scope = ServiceProvider.CreateScope();
2020-08-24 18:41:06 +00:00
var scopeClass = scope.ServiceProvider.GetService<NotifyHelperScope>();
2021-05-17 18:27:39 +00:00
var (userManager, studioNotifyHelper, studioNotifySource, displayUserSettingsHelper, _) = scopeClass;
2020-08-31 08:18:07 +00:00
var client = WorkContext.NotifyContext.NotifyService.RegisterClient(studioNotifySource, scope);
2020-06-29 09:32:43 +00:00
2020-08-31 08:18:07 +00:00
var owner = userManager.GetUsers(tenant.OwnerId);
2020-06-29 09:32:43 +00:00
var users =
notifyAllUsers
2020-08-31 08:18:07 +00:00
? studioNotifyHelper.RecipientFromEmail(userManager.GetUsers(EmployeeStatus.Active).Where(r => r.ActivationStatus == EmployeeActivationStatus.Activated).Select(u => u.Email).ToList(), false)
: owner.ActivationStatus == EmployeeActivationStatus.Activated ? studioNotifyHelper.RecipientFromEmail(owner.Email, false) : new IDirectRecipient[0];
2020-06-29 09:32:43 +00:00
client.SendNoticeToAsync(
Actions.RestoreStarted,
users,
new[] { StudioNotifyService.EMailSenderName });
2020-05-20 15:14:44 +00:00
}
2020-06-29 09:32:43 +00:00
public void SendAboutRestoreCompleted(Tenant tenant, bool notifyAllUsers)
{
using var scope = ServiceProvider.CreateScope();
2020-08-24 18:41:06 +00:00
var scopeClass = scope.ServiceProvider.GetService<NotifyHelperScope>();
2021-05-17 18:27:39 +00:00
var (userManager, studioNotifyHelper, studioNotifySource, displayUserSettingsHelper, _) = scopeClass;
2020-08-31 08:18:07 +00:00
var client = WorkContext.NotifyContext.NotifyService.RegisterClient(studioNotifySource, scope);
2020-06-29 09:32:43 +00:00
2020-08-31 08:18:07 +00:00
var owner = userManager.GetUsers(tenant.OwnerId);
2020-06-29 09:32:43 +00:00
var users =
notifyAllUsers
2020-08-31 08:18:07 +00:00
? userManager.GetUsers(EmployeeStatus.Active).Select(u => studioNotifyHelper.ToRecipient(u.ID)).ToArray()
: new[] { studioNotifyHelper.ToRecipient(owner.ID) };
2020-06-29 09:32:43 +00:00
client.SendNoticeToAsync(
Actions.RestoreCompleted,
users,
new[] { StudioNotifyService.EMailSenderName },
2020-08-31 08:18:07 +00:00
new TagValue(Tags.OwnerName, owner.DisplayUserName(displayUserSettingsHelper)));
2020-06-29 09:32:43 +00:00
}
2021-05-17 18:27:39 +00:00
private void MigrationNotify(Tenant tenant, INotifyAction action, string region, string url, bool notify, int? toTenantId = null)
2020-06-29 09:32:43 +00:00
{
using var scope = ServiceProvider.CreateScope();
2020-08-24 18:41:06 +00:00
var scopeClass = scope.ServiceProvider.GetService<NotifyHelperScope>();
2021-05-17 18:27:39 +00:00
var (userManager, studioNotifyHelper, studioNotifySource, _, authManager) = scopeClass;
2020-08-31 08:18:07 +00:00
var client = WorkContext.NotifyContext.NotifyService.RegisterClient(studioNotifySource, scope);
2021-05-17 18:27:39 +00:00
var commonLinkUtility = scope.ServiceProvider.GetService<CommonLinkUtility>();
2020-06-29 09:32:43 +00:00
2020-08-31 08:18:07 +00:00
var users = userManager.GetUsers()
2020-06-29 09:32:43 +00:00
.Where(u => notify ? u.ActivationStatus.HasFlag(EmployeeActivationStatus.Activated) : u.IsOwner(tenant))
.ToArray();
if (users.Any())
{
2021-05-17 18:27:39 +00:00
var args = CreateArgs(scope, region, url);
if (action == Actions.MigrationPortalSuccessV115)
{
foreach (var user in users)
{
var currentArgs = new List<ITagValue>(args);
var newTenantId = toTenantId.HasValue ? toTenantId.Value : tenant.TenantId;
var hash = authManager.GetUserPasswordStamp(user.ID).ToString("s");
var confirmationUrl = url + "/" + commonLinkUtility.GetConfirmationUrlRelative(newTenantId, user.Email, ConfirmType.PasswordChange, hash);
Func<string> greenButtonText = () => BackupResource.ButtonSetPassword;
currentArgs.Add(TagValues.GreenButton(greenButtonText, confirmationUrl));
client.SendNoticeToAsync(
action,
null,
new IRecipient[] { user },
new[] { StudioNotifyService.EMailSenderName },
currentArgs.ToArray());
}
}
else
{
client.SendNoticeToAsync(
action,
null,
users.Select(u => studioNotifyHelper.ToRecipient(u.ID)).ToArray(),
new[] { StudioNotifyService.EMailSenderName },
args.ToArray());
}
2020-06-29 09:32:43 +00:00
}
2021-05-17 18:27:39 +00:00
}
private List<ITagValue> CreateArgs(IServiceScope scope,string region, string url)
{
var args = new List<ITagValue>()
{
new TagValue(Tags.RegionName, TransferResourceHelper.GetRegionDescription(region)),
new TagValue(Tags.PortalUrl, url)
};
if (!string.IsNullOrEmpty(url))
{
args.Add(new TagValue(CommonTags.VirtualRootPath, url));
args.Add(new TagValue(CommonTags.ProfileUrl, url + scope.ServiceProvider.GetService<CommonLinkUtility>().GetMyStaff()));
args.Add(new TagValue(CommonTags.LetterLogo, scope.ServiceProvider.GetService<TenantLogoManager>().GetLogoDark(true)));
}
return args;
2020-08-24 18:41:06 +00:00
}
}
2020-10-26 13:38:21 +00:00
[Scope]
2020-08-24 18:41:06 +00:00
public class NotifyHelperScope
{
2021-05-17 18:27:39 +00:00
private AuthManager AuthManager { get; }
2020-08-31 08:18:07 +00:00
private UserManager UserManager { get; }
private StudioNotifyHelper StudioNotifyHelper { get; }
private StudioNotifySource StudioNotifySource { get; }
private DisplayUserSettingsHelper DisplayUserSettingsHelper { get; }
2020-07-28 07:35:17 +00:00
2021-05-17 18:27:39 +00:00
public NotifyHelperScope(
UserManager userManager,
StudioNotifyHelper studioNotifyHelper,
StudioNotifySource studioNotifySource,
DisplayUserSettingsHelper displayUserSettingsHelper,
AuthManager authManager)
2020-07-28 07:35:17 +00:00
{
2020-08-24 18:41:06 +00:00
UserManager = userManager;
StudioNotifyHelper = studioNotifyHelper;
StudioNotifySource = studioNotifySource;
DisplayUserSettingsHelper = displayUserSettingsHelper;
2021-05-17 18:27:39 +00:00
AuthManager = authManager;
2020-08-24 18:41:06 +00:00
}
2020-08-31 08:18:07 +00:00
2021-05-17 18:27:39 +00:00
public void Deconstruct(
out UserManager userManager,
out StudioNotifyHelper studioNotifyHelper,
out StudioNotifySource studioNotifySource,
out DisplayUserSettingsHelper displayUserSettingsHelper,
out AuthManager authManager
)
2020-08-31 08:18:07 +00:00
{
userManager = UserManager;
studioNotifyHelper = StudioNotifyHelper;
studioNotifySource = StudioNotifySource;
displayUserSettingsHelper = DisplayUserSettingsHelper;
2021-05-17 18:27:39 +00:00
authManager = AuthManager;
2020-08-31 08:18:07 +00:00
}
2020-05-20 15:14:44 +00:00
}
2020-08-24 18:41:06 +00:00
2020-10-22 17:57:18 +00:00
public class NotifyHelperExtension
{
2020-10-22 17:57:18 +00:00
public static void Register(DIHelper services)
2020-10-19 15:53:15 +00:00
{
2020-10-22 17:57:18 +00:00
services.TryAdd<NotifyHelperScope>();
}
}
2020-05-20 15:14:44 +00:00
}