/* * * (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. * */ using ASC.ActiveDirectory.Base.Settings; using ASC.ActiveDirectory.ComplexOperations; using ASC.Common; using ASC.Common.Threading; using ASC.Core; using ASC.Core.Common.Settings; using ASC.Core.Tenants; using ASC.Notify; using Microsoft.Extensions.DependencyInjection; namespace ASC.ActiveDirectory.Base { [Singletone] public class LdapNotifyHelper { private readonly Dictionary> clients; private readonly DistributedTaskQueue ldapTasks; private IServiceProvider ServiceProvider { get; set; } LdapNotifyHelper( IServiceProvider serviceProvider, DistributedTaskQueueOptionsManager distributedTaskQueueOptionsManager) { clients = new Dictionary>(); ldapTasks = distributedTaskQueueOptionsManager.Get("ldapAutoSyncOperations"); ServiceProvider = serviceProvider; } public void RegisterAll() { var task = new Task(() => { using var scope = ServiceProvider.CreateScope(); var tenantManager = scope.ServiceProvider.GetService(); var settingsManager = scope.ServiceProvider.GetService(); var tenants = tenantManager.GetTenants(settingsManager.Load().GetTenants()); foreach (var t in tenants) { var tId = t.TenantId; var ldapSettings = settingsManager.LoadForTenant(tId); if (!ldapSettings.EnableLdapAuthentication) continue; var cronSettings = settingsManager.LoadForTenant(tId); if (string.IsNullOrEmpty(cronSettings.Cron)) continue; RegisterAutoSync(t, cronSettings.Cron); } }, TaskCreationOptions.LongRunning); task.Start(); } public void RegisterAutoSync(Tenant tenant, string cron) { if (!clients.ContainsKey(tenant.TenantId)) { using var scope = ServiceProvider.CreateScope(); var source = new LdapNotifySource(tenant, this); var client = WorkContext.NotifyContext.NotifyService.RegisterClient(source, scope); WorkContext.RegisterSendMethod(source.AutoSync, cron); clients.Add(tenant.TenantId, new Tuple(client, source)); //concurrent dict } } public void UnregisterAutoSync(Tenant tenant) { if (clients.ContainsKey(tenant.TenantId)) { var client = clients[tenant.TenantId]; WorkContext.UnregisterSendMethod(client.Item2.AutoSync); clients.Remove(tenant.TenantId); } } public void AutoSync(Tenant tenant) { using var scope = ServiceProvider.CreateScope(); var settingsManager = scope.ServiceProvider.GetService(); var ldapSettings = settingsManager.LoadForTenant(tenant.TenantId); if (!ldapSettings.EnableLdapAuthentication) { var cronSettings = settingsManager.LoadForTenant(tenant.TenantId); cronSettings.Cron = ""; settingsManager.SaveForTenant(cronSettings, tenant.TenantId); UnregisterAutoSync(tenant); return; } var op = scope.ServiceProvider.GetService(); op.Init(ldapSettings, tenant, LdapOperationType.Sync); ldapTasks.QueueTask(op.RunJob, op.GetDistributedTask()); } } }