2022-03-08 05:37:20 +00:00
|
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
* (c) Copyright Ascensio System Limited 2010-2021
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
namespace ASC.ActiveDirectory.Base;
|
2022-03-22 10:37:44 +00:00
|
|
|
|
[Singletone(Additional = typeof(DbHelperExtension))]
|
2022-03-17 19:44:34 +00:00
|
|
|
|
public class LdapNotifyHelper
|
2022-03-08 05:37:20 +00:00
|
|
|
|
{
|
2022-03-17 19:44:34 +00:00
|
|
|
|
private readonly Dictionary<int, Tuple<INotifyClient, LdapNotifySource>> _clients;
|
|
|
|
|
private readonly DistributedTaskQueue _ldapTasks;
|
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
|
|
|
|
|
2022-03-18 09:36:35 +00:00
|
|
|
|
public LdapNotifyHelper(
|
2022-03-17 19:44:34 +00:00
|
|
|
|
IServiceProvider serviceProvider,
|
|
|
|
|
DistributedTaskQueueOptionsManager distributedTaskQueueOptionsManager)
|
2022-03-08 05:37:20 +00:00
|
|
|
|
{
|
2022-03-17 19:44:34 +00:00
|
|
|
|
_clients = new Dictionary<int, Tuple<INotifyClient, LdapNotifySource>>();
|
|
|
|
|
_ldapTasks = distributedTaskQueueOptionsManager.Get("ldapAutoSyncOperations");
|
|
|
|
|
_serviceProvider = serviceProvider;
|
|
|
|
|
}
|
2022-03-08 05:37:20 +00:00
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
public void RegisterAll()
|
|
|
|
|
{
|
|
|
|
|
var task = new Task(() =>
|
2022-03-08 05:37:20 +00:00
|
|
|
|
{
|
2022-03-17 19:44:34 +00:00
|
|
|
|
using var scope = _serviceProvider.CreateScope();
|
2022-03-22 10:37:44 +00:00
|
|
|
|
var tenantManager = scope.ServiceProvider.GetRequiredService<TenantManager>();
|
|
|
|
|
var settingsManager = scope.ServiceProvider.GetRequiredService<SettingsManager>();
|
|
|
|
|
var dbHelper = scope.ServiceProvider.GetRequiredService<DbHelper>();
|
2022-03-08 05:37:20 +00:00
|
|
|
|
|
2022-03-22 10:37:44 +00:00
|
|
|
|
var tenants = tenantManager.GetTenants(dbHelper.GetTenants());
|
2022-03-17 19:44:34 +00:00
|
|
|
|
foreach (var t in tenants)
|
2022-03-08 05:37:20 +00:00
|
|
|
|
{
|
2022-03-17 19:44:34 +00:00
|
|
|
|
var tId = t.TenantId;
|
2022-03-08 05:37:20 +00:00
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
var ldapSettings = settingsManager.LoadForTenant<LdapSettings>(tId);
|
|
|
|
|
if (!ldapSettings.EnableLdapAuthentication) continue;
|
2022-03-08 05:37:20 +00:00
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
var cronSettings = settingsManager.LoadForTenant<LdapCronSettings>(tId);
|
|
|
|
|
if (string.IsNullOrEmpty(cronSettings.Cron)) continue;
|
2022-03-08 05:37:20 +00:00
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
RegisterAutoSync(t, cronSettings.Cron);
|
|
|
|
|
}
|
|
|
|
|
}, TaskCreationOptions.LongRunning);
|
2022-03-08 05:37:20 +00:00
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
task.Start();
|
|
|
|
|
}
|
2022-03-08 05:37:20 +00:00
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
public void RegisterAutoSync(Tenant tenant, string cron)
|
|
|
|
|
{
|
|
|
|
|
if (!_clients.ContainsKey(tenant.TenantId))
|
2022-03-08 05:37:20 +00:00
|
|
|
|
{
|
2022-03-17 19:44:34 +00:00
|
|
|
|
using var scope = _serviceProvider.CreateScope();
|
|
|
|
|
var source = new LdapNotifySource(tenant, this);
|
|
|
|
|
var client = WorkContext.NotifyContext.NotifyService.RegisterClient(source, scope);
|
|
|
|
|
WorkContext.RegisterSendMethod(source.AutoSync, cron);
|
2022-03-25 07:38:09 +00:00
|
|
|
|
_clients.Add(tenant.TenantId, new Tuple<INotifyClient, LdapNotifySource>(client, source));
|
2022-03-08 05:37:20 +00:00
|
|
|
|
}
|
2022-03-17 19:44:34 +00:00
|
|
|
|
}
|
2022-03-08 05:37:20 +00:00
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
public void UnregisterAutoSync(Tenant tenant)
|
|
|
|
|
{
|
|
|
|
|
if (_clients.ContainsKey(tenant.TenantId))
|
2022-03-08 05:37:20 +00:00
|
|
|
|
{
|
2022-03-17 19:44:34 +00:00
|
|
|
|
var client = _clients[tenant.TenantId];
|
|
|
|
|
WorkContext.UnregisterSendMethod(client.Item2.AutoSync);
|
|
|
|
|
_clients.Remove(tenant.TenantId);
|
2022-03-08 05:37:20 +00:00
|
|
|
|
}
|
2022-03-17 19:44:34 +00:00
|
|
|
|
}
|
2022-03-08 05:37:20 +00:00
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
public void AutoSync(Tenant tenant)
|
|
|
|
|
{
|
|
|
|
|
using var scope = _serviceProvider.CreateScope();
|
2022-03-25 07:38:09 +00:00
|
|
|
|
var settingsManager = scope.ServiceProvider.GetRequiredService<SettingsManager>();
|
2022-03-17 19:44:34 +00:00
|
|
|
|
var ldapSettings = settingsManager.LoadForTenant<LdapSettings>(tenant.TenantId);
|
2022-03-08 05:37:20 +00:00
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
if (!ldapSettings.EnableLdapAuthentication)
|
|
|
|
|
{
|
|
|
|
|
var cronSettings = settingsManager.LoadForTenant<LdapCronSettings>(tenant.TenantId);
|
|
|
|
|
cronSettings.Cron = "";
|
|
|
|
|
settingsManager.SaveForTenant(cronSettings, tenant.TenantId);
|
|
|
|
|
UnregisterAutoSync(tenant);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-03-08 05:37:20 +00:00
|
|
|
|
|
2022-03-25 07:38:09 +00:00
|
|
|
|
var op = scope.ServiceProvider.GetRequiredService<LdapSaveSyncOperation>();
|
2022-03-08 05:37:20 +00:00
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
op.Init(ldapSettings, tenant, LdapOperationType.Sync);
|
|
|
|
|
_ldapTasks.QueueTask(op.RunJob, op.GetDistributedTask());
|
2022-03-08 05:37:20 +00:00
|
|
|
|
}
|
2022-03-22 10:37:44 +00:00
|
|
|
|
|
|
|
|
|
public static class DbHelperExtension
|
|
|
|
|
{
|
|
|
|
|
public static void Register(DIHelper services)
|
|
|
|
|
{
|
|
|
|
|
services.TryAdd<DbHelper>();
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-08 05:37:20 +00:00
|
|
|
|
}
|