DocSpace-buildtools/common/ASC.ActiveDirectory/ComplexOperations/LdapSaveSyncOperation.cs

259 lines
9.0 KiB
C#
Raw Normal View History

2022-05-05 13:23:05 +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-03-17 19:44:34 +00:00
namespace ASC.ActiveDirectory.ComplexOperations;
2022-05-05 13:23:05 +00:00
2022-04-26 14:03:41 +00:00
[Singletone(Additional = typeof(LdapOperationExtension))]
public class LdapSaveSyncOperation
2022-03-08 05:37:20 +00:00
{
2022-05-05 13:23:05 +00:00
public const string CUSTOM_DISTRIBUTED_TASK_QUEUE_NAME = "ldapOperation";
private readonly DistributedTaskQueue _progressQueue;
private readonly IServiceProvider _serviceProvider;
2022-03-17 19:44:34 +00:00
public LdapSaveSyncOperation(
IServiceProvider serviceProvider,
2022-05-05 13:23:05 +00:00
IDistributedTaskQueueFactory queueFactory)
2022-03-08 05:37:20 +00:00
{
2022-03-17 19:44:34 +00:00
_serviceProvider = serviceProvider;
2022-05-05 13:23:05 +00:00
_progressQueue = queueFactory.CreateQueue(CUSTOM_DISTRIBUTED_TASK_QUEUE_NAME);
2022-03-17 19:44:34 +00:00
}
2022-03-08 05:37:20 +00:00
2022-04-26 14:03:41 +00:00
public void RunJob(LdapSettings settings, Tenant tenant, LdapOperationType operationType, LdapLocalization resource = null, string userId = null)
2022-03-17 19:44:34 +00:00
{
2022-05-05 13:23:05 +00:00
var item = _progressQueue.GetAllTasks<LdapOperationJob>().FirstOrDefault(t => t.TenantId == tenant.Id);
2022-04-26 14:03:41 +00:00
if (item != null && item.IsCompleted)
2022-03-17 19:44:34 +00:00
{
2022-05-05 13:23:05 +00:00
_progressQueue.DequeueTask(item.Id);
2022-04-26 14:03:41 +00:00
item = null;
2022-03-17 19:44:34 +00:00
}
2022-04-26 14:03:41 +00:00
if (item == null)
2022-03-08 05:37:20 +00:00
{
2022-04-26 14:03:41 +00:00
using var scope = _serviceProvider.CreateScope();
item = scope.ServiceProvider.GetService<LdapOperationJob>();
item.InitJob(settings, tenant, operationType, resource, userId);
2022-05-05 13:23:05 +00:00
_progressQueue.EnqueueTask(item);
2022-03-17 19:44:34 +00:00
}
2022-03-08 05:37:20 +00:00
2022-04-26 14:03:41 +00:00
item.PublishChanges();
2022-03-17 19:44:34 +00:00
}
2022-03-08 05:37:20 +00:00
2022-05-05 13:23:05 +00:00
public LdapOperationStatus TestLdapSave(LdapSettings ldapSettings, Tenant tenant, string userId)
{
var operations = _progressQueue.GetAllTasks()
.Where(t => t[LdapTaskProperty.OWNER] == tenant.Id)
.ToList();
var hasStarted = operations.Any(o =>
{
var opType = o[LdapTaskProperty.OPERATION_TYPE];
return o.Status <= DistributedTaskStatus.Running &&
(opType == LdapOperationType.SyncTest || opType == LdapOperationType.SaveTest);
});
if (hasStarted)
{
return ToLdapOperationStatus(tenant.Id);
}
if (operations.Any(o => o.Status <= DistributedTaskStatus.Running))
{
return GetStartProcessError();
}
var ldapLocalization = new LdapLocalization();
ldapLocalization.Init(Resource.ResourceManager);
RunJob(ldapSettings, tenant, LdapOperationType.SaveTest, ldapLocalization, userId);
return ToLdapOperationStatus(tenant.Id);
}
public LdapOperationStatus SaveLdapSettings(LdapSettings ldapSettings, Tenant tenant, string userId)
{
var operations = _progressQueue.GetAllTasks()
.Where(t => t[LdapTaskProperty.OWNER] == tenant.Id).ToList();
if (operations.Any(o => o.Status <= DistributedTaskStatus.Running))
{
return GetStartProcessError();
}
//ToDo
ldapSettings.AccessRights.Clear();
if (!ldapSettings.LdapMapping.ContainsKey(LdapSettings.MappingFields.MailAttribute) || string.IsNullOrEmpty(ldapSettings.LdapMapping[LdapSettings.MappingFields.MailAttribute]))
{
ldapSettings.SendWelcomeEmail = false;
}
var ldapLocalization = new LdapLocalization();
ldapLocalization.Init(Resource.ResourceManager, WebstudioNotifyPatternResource.ResourceManager);
RunJob(ldapSettings, tenant, LdapOperationType.Save, ldapLocalization, userId);
return ToLdapOperationStatus(tenant.Id);
}
public LdapOperationStatus SyncLdap(LdapSettings ldapSettings, Tenant tenant, string userId)
{
var operations = _progressQueue.GetAllTasks()
.Where(t => t[LdapTaskProperty.OWNER] == tenant.Id)
.ToList();
var hasStarted = operations.Any(o =>
{
var opType = o[LdapTaskProperty.OPERATION_TYPE];
return o.Status <= DistributedTaskStatus.Running &&
(opType == LdapOperationType.Sync || opType == LdapOperationType.Save);
});
if (hasStarted)
{
return ToLdapOperationStatus(tenant.Id);
}
if (operations.Any(o => o.Status <= DistributedTaskStatus.Running))
{
return GetStartProcessError();
}
var ldapLocalization = new LdapLocalization();
ldapLocalization.Init(Resource.ResourceManager);
RunJob(ldapSettings, tenant, LdapOperationType.Sync, ldapLocalization, userId);
return ToLdapOperationStatus(tenant.Id);
}
public LdapOperationStatus TestLdapSync(LdapSettings ldapSettings, Tenant tenant)
{
var operations = _progressQueue.GetAllTasks()
.Where(t => t[LdapTaskProperty.OWNER] == tenant.Id)
.ToList();
var hasStarted = operations.Any(o =>
{
var opType = o[LdapTaskProperty.OPERATION_TYPE];
return o.Status <= DistributedTaskStatus.Running &&
(opType == LdapOperationType.SyncTest || opType == LdapOperationType.SaveTest);
});
if (hasStarted)
{
return ToLdapOperationStatus(tenant.Id);
}
if (operations.Any(o => o.Status <= DistributedTaskStatus.Running))
{
return GetStartProcessError();
}
var ldapLocalization = new LdapLocalization();
ldapLocalization.Init(Resource.ResourceManager);
RunJob(ldapSettings, tenant, LdapOperationType.SyncTest, ldapLocalization);
return ToLdapOperationStatus(tenant.Id);
}
2022-04-26 14:03:41 +00:00
public LdapOperationStatus ToLdapOperationStatus(int tenantId)
2022-03-17 19:44:34 +00:00
{
2022-05-05 13:23:05 +00:00
var operations = _progressQueue.GetAllTasks().ToList();
2022-03-17 19:44:34 +00:00
2022-04-26 14:03:41 +00:00
foreach (var o in operations)
2022-03-17 19:44:34 +00:00
{
2022-04-26 14:03:41 +00:00
if (Process.GetProcesses().Any(p => p.Id == o.InstanceId))
2022-03-17 19:44:34 +00:00
continue;
2022-03-08 05:37:20 +00:00
2022-05-05 13:23:05 +00:00
o[LdapTaskProperty.PROGRESS] = 100;
_progressQueue.DequeueTask(o.Id);
2022-03-17 19:44:34 +00:00
}
2022-03-08 05:37:20 +00:00
2022-04-26 14:03:41 +00:00
var operation =
operations
2022-05-05 13:23:05 +00:00
.FirstOrDefault(t => t[LdapTaskProperty.OWNER] == tenantId);
2022-03-08 05:37:20 +00:00
2022-04-26 14:03:41 +00:00
if (operation == null)
2022-03-08 05:37:20 +00:00
{
2022-04-26 14:03:41 +00:00
return null;
2022-03-08 05:37:20 +00:00
}
2022-04-26 14:03:41 +00:00
if (DistributedTaskStatus.Running < operation.Status)
2022-03-08 05:37:20 +00:00
{
2022-05-05 13:23:05 +00:00
operation[LdapTaskProperty.PROGRESS] = 100;
_progressQueue.DequeueTask(operation.Id);
2022-03-08 05:37:20 +00:00
}
2022-05-05 13:23:05 +00:00
var certificateConfirmRequest = operation[LdapTaskProperty.CERT_REQUEST];
2022-03-08 05:37:20 +00:00
2022-04-26 14:03:41 +00:00
var result = new LdapOperationStatus
2022-03-17 19:44:34 +00:00
{
2022-04-26 14:03:41 +00:00
Id = operation.Id,
2022-05-05 13:23:05 +00:00
Completed = operation[LdapTaskProperty.FINISHED],
Percents = operation[LdapTaskProperty.PROGRESS],
Status = operation[LdapTaskProperty.RESULT],
Error = operation[LdapTaskProperty.ERROR],
2022-04-26 14:03:41 +00:00
CertificateConfirmRequest = certificateConfirmRequest,
2022-05-05 13:23:05 +00:00
Source = operation[LdapTaskProperty.SOURCE],
2022-04-26 14:03:41 +00:00
OperationType = Enum.GetName(typeof(LdapOperationType),
2022-05-05 13:23:05 +00:00
(LdapOperationType)operation[LdapTaskProperty.OPERATION_TYPE]),
Warning = operation[LdapTaskProperty.WARNING]
2022-04-26 14:03:41 +00:00
};
2022-03-08 05:37:20 +00:00
2022-04-26 14:03:41 +00:00
if (!(string.IsNullOrEmpty(result.Warning)))
2022-03-08 05:37:20 +00:00
{
2022-05-05 13:23:05 +00:00
operation[LdapTaskProperty.WARNING] = ""; // "mark" as read
2022-03-17 19:44:34 +00:00
}
2022-03-08 05:37:20 +00:00
2022-04-26 14:03:41 +00:00
return result;
2022-03-17 19:44:34 +00:00
}
2022-03-08 05:37:20 +00:00
2022-05-05 13:23:05 +00:00
private static LdapOperationStatus GetStartProcessError()
{
var result = new LdapOperationStatus
{
Id = null,
Completed = true,
Percents = 0,
Status = "",
Error = Resource.LdapSettingsTooManyOperations,
CertificateConfirmRequest = null,
Source = ""
};
return result;
}
2022-04-26 14:03:41 +00:00
public static class LdapOperationExtension
2022-03-17 19:44:34 +00:00
{
2022-04-26 14:03:41 +00:00
public static void Register(DIHelper services)
2022-03-08 05:37:20 +00:00
{
2022-04-26 14:03:41 +00:00
services.TryAdd<LdapOperationJob>();
2022-03-17 19:44:34 +00:00
}
}
2022-03-08 05:37:20 +00:00
}