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();
|
2022-05-18 10:36:35 +00:00
|
|
|
|
item = scope.ServiceProvider.GetRequiredService<LdapOperationJob>();
|
2022-04-26 14:03:41 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
2022-05-18 10:36:35 +00:00
|
|
|
|
var (hasStarted, operations) = HasStarterdForTenant(tenant.Id, LdapOperationType.SyncTest, LdapOperationType.SaveTest);
|
2022-05-05 13:23:05 +00:00
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2022-05-18 10:36:35 +00:00
|
|
|
|
var operations = GetOperationsForTenant(tenant.Id);
|
2022-05-05 13:23:05 +00:00
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2022-05-18 10:36:35 +00:00
|
|
|
|
var (hasStarted, operations) = HasStarterdForTenant(tenant.Id, LdapOperationType.Sync, LdapOperationType.Save);
|
2022-05-05 13:23:05 +00:00
|
|
|
|
|
|
|
|
|
if (hasStarted)
|
|
|
|
|
{
|
|
|
|
|
return ToLdapOperationStatus(tenant.Id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (operations.Any(o => o.Status <= DistributedTaskStatus.Running))
|
|
|
|
|
{
|
|
|
|
|
return GetStartProcessError();
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-18 10:36:35 +00:00
|
|
|
|
var ldapLocalization = new LdapLocalization();
|
2022-05-05 13:23:05 +00:00
|
|
|
|
ldapLocalization.Init(Resource.ResourceManager);
|
|
|
|
|
|
|
|
|
|
RunJob(ldapSettings, tenant, LdapOperationType.Sync, ldapLocalization, userId);
|
|
|
|
|
return ToLdapOperationStatus(tenant.Id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public LdapOperationStatus TestLdapSync(LdapSettings ldapSettings, Tenant tenant)
|
|
|
|
|
{
|
2022-05-18 10:36:35 +00:00
|
|
|
|
var (hasStarted, operations) = HasStarterdForTenant(tenant.Id, LdapOperationType.SyncTest, LdapOperationType.SaveTest);
|
2022-05-05 13:23:05 +00:00
|
|
|
|
|
|
|
|
|
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-13 09:20:28 +00:00
|
|
|
|
var operations = _progressQueue.GetAllTasks<LdapOperationJob>().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-06-15 12:39:37 +00:00
|
|
|
|
{
|
2022-03-17 19:44:34 +00:00
|
|
|
|
continue;
|
2022-06-15 12:39:37 +00:00
|
|
|
|
}
|
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-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-05-13 09:20:28 +00:00
|
|
|
|
CertificateConfirmRequest = operation[LdapTaskProperty.CERT_REQUEST] != "" ? operation[LdapTaskProperty.CERT_REQUEST] : null,
|
2022-05-05 13:23:05 +00:00
|
|
|
|
Source = operation[LdapTaskProperty.SOURCE],
|
2022-05-13 09:20:28 +00:00
|
|
|
|
OperationType = operation[LdapTaskProperty.OPERATION_TYPE],
|
2022-05-05 13:23:05 +00:00
|
|
|
|
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-05-18 10:36:35 +00:00
|
|
|
|
private (bool hasStarted, List<LdapOperationJob> operations) HasStarterdForTenant(int tenantId, LdapOperationType arg1, LdapOperationType arg2)
|
|
|
|
|
{
|
|
|
|
|
var operations = GetOperationsForTenant(tenantId);
|
|
|
|
|
|
|
|
|
|
var hasStarted = operations.Any(o =>
|
|
|
|
|
{
|
|
|
|
|
var opType = o[LdapTaskProperty.OPERATION_TYPE];
|
|
|
|
|
|
|
|
|
|
return o.Status <= DistributedTaskStatus.Running &&
|
2022-05-24 09:59:04 +00:00
|
|
|
|
(opType == arg1.ToString() || opType == arg2.ToString());
|
2022-05-18 10:36:35 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (hasStarted, operations);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<LdapOperationJob> GetOperationsForTenant(int tenantId)
|
|
|
|
|
{
|
|
|
|
|
return _progressQueue.GetAllTasks<LdapOperationJob>()
|
|
|
|
|
.Where(t => t[LdapTaskProperty.OWNER] == tenantId)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|