public void RunJob(DistributedTask _, CancellationToken cancellationToken) { try { CancellationToken = cancellationToken; CoreContext.TenantManager.SetCurrentTenant(CurrentTenant); SecurityContext.AuthenticateMe(ASC.Core.Configuration.Constants.CoreSystem); Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(_culture); Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(_culture); Logger = LogManager.GetLogger(typeof(LDAPOperation)); if (LDAPSettings == null) { Error = Resource.LdapSettingsErrorCantGetLdapSettings; Logger.Error("Can't save default LDAP settings."); return; } if (OperationType == LDAPOperationType.Save) { SetProgress(1, Resource.LdapSettingsStatusCheckingLdapSettings); PrepareSettings(LDAPSettings); if (!string.IsNullOrEmpty(Error)) { return; } Importer = new LDAPUserImporter(LDAPSettings); SetProgress(5, Resource.LdapSettingsStatusLoadingBaseInfo); var acceptCertificate = TaskInfo.GetProperty <bool>(CERT_ALLOW); var result = _ldapSettingsChecker.CheckSettings(Importer, acceptCertificate); if (result == LdapSettingsChecker.CERTIFICATE_REQUEST) { TaskInfo.SetProperty(FINISHED, true); TaskInfo.SetProperty(CERT_REQUEST, ((NovellLdapSettingsChecker)_ldapSettingsChecker).CertificateConfirmRequest); SetProgress(0, Resource.LdapSettingsStatusCertificateVerification); return; } var error = GetError(result); if (!string.IsNullOrEmpty(error)) { Error = error; return; } SetProgress(10, Resource.LdapSettingsStatusSavingSettings); LDAPSettings.IsDefault = LDAPSettings.Equals(LDAPSettings.GetDefault()); if (!SettingsManager.Instance.SaveSettings(LDAPSettings, CurrentTenant.TenantId)) { Logger.Error("Can't save LDAP settings."); Error = Resource.LdapSettingsErrorCantSaveLdapSettings; return; } } else if (OperationType == LDAPOperationType.Sync) { Importer = new LDAPUserImporter(LDAPSettings); } Do(); } catch (AuthorizingException authError) { Error = Resource.ErrorAccessDenied; Logger.Error(Error, new SecurityException(Error, authError)); } catch (AggregateException ae) { ae.Flatten().Handle(e => e is TaskCanceledException || e is OperationCanceledException); } catch (TenantQuotaException e) { Error = Resource.LdapSettingsTenantQuotaSettled; Logger.ErrorFormat("TenantQuotaException. {0}", e); } catch (FormatException e) { Error = Resource.LdapSettingsErrorCantCreateUsers; Logger.ErrorFormat("FormatException error. {0}", e); } catch (Exception e) { Error = Resource.LdapSettingsInternalServerError; Logger.ErrorFormat("Internal server error. {0}", e); } finally { try { TaskInfo.SetProperty(FINISHED, true); PublishTaskInfo(); } catch { /* ignore */ } } }
public void RunJob() { try { SecurityContext.AuthenticateMe(ASC.Core.Configuration.Constants.CoreSystem); var tenantId = (int)Id; CoreContext.TenantManager.SetCurrentTenant(tenantId); ResolveCulture(); SetProggressInfo(Constants.LDAP_SETTING_TASK_PERCENTAGE, 1); SetProggressInfo(Constants.LDAP_SETTING_TASK_STATUS, Resource.LdapSettingsStatusCheckingLdapSettings); SetProggressInfo(Constants.LDAP_SETTING_TASK_IS_COMPLETED, false); if (_settings == null) { _log.Error("Can't save default LDAP settings."); SetProggressInfo(Constants.LDAP_SETTING_TASK_ERROR, Resource.LdapSettingsErrorCantGetLdapSettings); return; } try { PrepareSettings(_settings); var acceptCertificate = (bool?)AscCache.Default.Get <object>(Constants.LDAP_SETTING_TASK_ACCEPT_CERTIFICATE) ?? false; if (_importer == null) { _importer = new LDAPUserImporter(_settings); } SetProggressInfo(Constants.LDAP_SETTING_TASK_PERCENTAGE, 5); SetProggressInfo(Constants.LDAP_SETTING_TASK_STATUS, Resource.LdapSettingsStatusLoadingBaseInfo); var result = _ldapSettingsChecker.CheckSettings(_importer, acceptCertificate); if (result == LdapSettingsChecker.CERTIFICATE_REQUEST) { SetProggressInfo(Constants.LDAP_SETTING_TASK_PERCENTAGE, 0); SetProggressInfo(Constants.LDAP_SETTING_TASK_STATUS, Resource.LdapSettingsStatusCertificateVerification); SetProggressInfo(Constants.NOVELL_LDAP_CERTIFICATE_CONFIRM_REQUEST, ((NovellLdapSettingsChecker)_ldapSettingsChecker).CertificateConfirmRequest); return; } var error = GetError(result); if (error != string.Empty) { SetProggressInfo(Constants.LDAP_SETTING_TASK_ERROR, error); return; } if (!_skipSaveSettings) { SetProggressInfo(Constants.LDAP_SETTING_TASK_STATUS, Resource.LdapSettingsStatusSavingSettings); SetProggressInfo(Constants.LDAP_SETTING_TASK_PERCENTAGE, 10); _settings.IsDefault = _settings.Equals(_settings.GetDefault()); if (!SettingsManager.Instance.SaveSettings(_settings, tenantId)) { _log.Error("Can't save LDAP settings."); SetProggressInfo(Constants.LDAP_SETTING_TASK_ERROR, Resource.LdapSettingsErrorCantSaveLdapSettings); return; } } RemoveOldWorkaroundOnLogoutLDAPUsers(); if (_settings.EnableLdapAuthentication) { SyncLDAP(); var taskError = AscCache.Default.Get <string>(Constants.LDAP_SETTING_TASK_ERROR); if (!string.IsNullOrEmpty(taskError)) { return; } } else { SetProggressInfo(Constants.LDAP_SETTING_TASK_STATUS, Resource.LdapSettingsModifyLdapUsers); SetProggressInfo(Constants.LDAP_SETTING_TASK_PERCENTAGE, 50); var existingLDAPUsers = CoreContext.UserManager.GetUsers().Where(u => u.Sid != null).ToList(); foreach (var existingLDAPUser in existingLDAPUsers) { existingLDAPUser.Sid = null; CoreContext.UserManager.SaveUserInfo(existingLDAPUser); } } SetProggressInfo(Constants.LDAP_SETTING_TASK_PERCENTAGE, 100); } catch (TenantQuotaException e) { _log.ErrorFormat("TenantQuotaException. {0}", e); SetProggressInfo(Constants.LDAP_SETTING_TASK_ERROR, Resource.LdapSettingsTenantQuotaSettled); } catch (FormatException e) { _log.ErrorFormat("FormatException error. {0}", e); SetProggressInfo(Constants.LDAP_SETTING_TASK_ERROR, Resource.LdapSettingsErrorCantCreateUsers); } catch (Exception e) { _log.ErrorFormat("Internal server error. {0}", e); SetProggressInfo(Constants.LDAP_SETTING_TASK_ERROR, Resource.LdapSettingsInternalServerError); } } finally { SetProggressInfo(Constants.LDAP_SETTING_TASK_IS_COMPLETED, true); AscCache.Default.Remove(Constants.LDAP_SETTING_TASK_STARTED); SecurityContext.Logout(); } }