private void CreateUsersAndGroups(LDAPSupportSettings settings) { Status = Resource.LdapSettingsStatusGettingGroupsFromLdap; var groups = _importer.GetDiscoveredGroupsByAttributes(settings); Percentage = 8; Status = Resource.LdapSettingsStatusGettingUsersFromLdap; var users = _importer.GetDiscoveredUsersByAttributes(settings); Percentage = 15; Status = Resource.LdapSettingsStatusSavingGroups; AddGroupsToCore(groups); Percentage = 35; Status = Resource.LdapSettingsStatusSavingUsers; double percents = 35; var step = percents / users.Count; if (users != null && users.Count != 0) { for (int i = 0; i < users.Count; i++) { if (users[i].FirstName == string.Empty) { users[i].FirstName = Resource.FirstName; } if (users[i].LastName == string.Empty) { users[i].LastName = Resource.LastName; } } users = users.SortByUserName(); for (int i = 0; i < users.Count; i++) { if (TenantStatisticsProvider.GetUsersCount() < TenantExtra.GetTenantQuota().ActiveUsers) { users[i] = UserManagerWrapper.AddUser(users[i], UserManagerWrapper.GeneratePassword(), true, false); } else { users[i] = UserManagerWrapper.AddUser(users[i], UserManagerWrapper.GeneratePassword(), true, false, true); } Percentage += step; } } Percentage = 70; var allLdapUsers = CoreContext.UserManager.GetUsers().Where(u => u.Sid != null).ToArray(); percents = 20; step = percents / allLdapUsers.Length; for (int i = 0; i < allLdapUsers.Length; i++) { _importer.AddUserIntoGroups(allLdapUsers[i], settings); Percentage += step; } Percentage = 90; AddUsersInCacheGroups(); RemoveEmptyGroups(); }
public static bool TryGetLdapUserInfo(string login, string password, out UserInfo userInfo) { userInfo = ASC.Core.Users.Constants.LostUser; if (!SetupInfo.IsVisibleSettings(ManagementType.LdapSettings.ToString()) || CoreContext.Configuration.Standalone && !CoreContext.TenantManager.GetTenantQuota(TenantProvider.CurrentTenantID).Ldap) { return(false); } var settings = SettingsManager.Instance.LoadSettings <LDAPSupportSettings>(TenantProvider.CurrentTenantID); if (!settings.EnableLdapAuthentication) { return(false); } try { var importer = new LDAPUserImporter(); try { LdapSettingsChecker ldapSettingsChecker; string currentLogin; if (!WorkContext.IsMono) { ldapSettingsChecker = new SystemLdapSettingsChecker(); currentLogin = login; } else { currentLogin = GetEntryDN(settings, login); if (currentLogin == null) { return(false); } ldapSettingsChecker = new NovellLdapSettingsChecker(); } ldapSettingsChecker.CheckCredentials(currentLogin, password, settings.Server, settings.PortNumber, settings.StartTls); } catch (Exception) { return(false); } if (login.Contains("\\")) { login = login.Split('\\')[1]; } var sid = importer.GetSidOfCurrentUser(login, settings); if (sid == null) { return(false); } List <GroupInfo> existingGroups; importer.GetDiscoveredGroupsByAttributes(settings, out existingGroups); if (importer.GetDiscoveredUser(settings, sid).Equals(ASC.Core.Users.Constants.LostUser)) { return(false); } userInfo = CoreContext.UserManager.GetUserBySid("l" + sid); if (userInfo.Equals(ASC.Core.Users.Constants.LostUser)) { userInfo = CoreContext.UserManager.GetUserBySid(sid); if (userInfo.Equals(ASC.Core.Users.Constants.LostUser)) { userInfo = importer.GetDiscoveredUser(settings, sid); if (userInfo.Equals(ASC.Core.Users.Constants.LostUser)) { return(false); } if (userInfo.FirstName == string.Empty) { userInfo.FirstName = Resource.FirstName; } if (userInfo.LastName == string.Empty) { userInfo.LastName = Resource.LastName; } try { SecurityContext.AuthenticateMe(ASC.Core.Configuration.Constants.CoreSystem); var asVisitor = TenantStatisticsProvider.GetUsersCount() >= TenantExtra.GetTenantQuota().ActiveUsers; userInfo = UserManagerWrapper.AddUser(userInfo, UserManagerWrapper.GeneratePassword(), true, false, asVisitor); importer.AddUserIntoGroups(userInfo, settings); importer.AddUserInCacheGroups(userInfo); } finally { SecurityContext.Logout(); } } } else { userInfo.Sid = sid; try { SecurityContext.AuthenticateMe(ASC.Core.Configuration.Constants.CoreSystem); var asVisitor = TenantStatisticsProvider.GetUsersCount() >= TenantExtra.GetTenantQuota().ActiveUsers; userInfo = UserManagerWrapper.AddUser(userInfo, UserManagerWrapper.GeneratePassword(), true, false, asVisitor, false, false); } finally { SecurityContext.Logout(); } } return(true); } catch (Exception e) { log.ErrorFormat("Unexpected error: {0}", e); userInfo = ASC.Core.Users.Constants.LostUser; return(false); } }
private void SyncLDAPUsersInGroups() { SetProggressInfo(Constants.LDAP_SETTING_TASK_PERCENTAGE, 15); SetProggressInfo(Constants.LDAP_SETTING_TASK_STATUS, Resource.LdapSettingsStatusGettingGroupsFromLdap); var ldapGroups = _importer.GetDiscoveredGroupsByAttributes(); if (!ldapGroups.Any()) { SetProggressInfo(Constants.LDAP_SETTING_TASK_ERROR, Resource.LdapSettingsErrorGroupsNotFound); return; } SetProggressInfo(Constants.LDAP_SETTING_TASK_PERCENTAGE, 20); SetProggressInfo(Constants.LDAP_SETTING_TASK_STATUS, Resource.LdapSettingsStatusGettingUsersFromLdap); //Get All found groups users List <UserInfo> uniqueLdapGroupUsers; var ldapGroupsUsers = GetGroupsUsers(ldapGroups, out uniqueLdapGroupUsers); uniqueLdapGroupUsers = uniqueLdapGroupUsers .ConvertAll(u => { if (u.FirstName == string.Empty) { u.FirstName = Resource.FirstName; } if (u.LastName == string.Empty) { u.LastName = Resource.LastName; } return(u); }) .SortByUserName() .ToList(); if (!uniqueLdapGroupUsers.Any()) { SetProggressInfo(Constants.LDAP_SETTING_TASK_ERROR, Resource.LdapSettingsErrorUsersNotFound); return; } SetProggressInfo(Constants.LDAP_SETTING_TASK_PERCENTAGE, 30); SetProggressInfo(Constants.LDAP_SETTING_TASK_STATUS, Resource.LdapSettingsStatusSavingUsers); SyncGroupsUsers(uniqueLdapGroupUsers); SetProggressInfo(Constants.LDAP_SETTING_TASK_PERCENTAGE, 60); SetProggressInfo(Constants.LDAP_SETTING_TASK_STATUS, Resource.LdapSettingsStatusSavingGroups); SyncDbGroups(ldapGroupsUsers); SetProggressInfo(Constants.LDAP_SETTING_TASK_PERCENTAGE, 80); SetProggressInfo(Constants.LDAP_SETTING_TASK_STATUS, Resource.LdapSettingsStatusRemovingOldGroups); RemoveOldDbGroups(ldapGroups); SetProggressInfo(Constants.LDAP_SETTING_TASK_PERCENTAGE, 90); SetProggressInfo(Constants.LDAP_SETTING_TASK_STATUS, Resource.LdapSettingsStatusRemovingOldUsers); RemoveOldDbUsers(uniqueLdapGroupUsers); }
public static bool TryLdapAuth(string login, string password) { if (!SetupInfo.IsVisibleSettings(ManagementType.LdapSettings.ToString())) { return(false); } var settings = SettingsManager.Instance.LoadSettings <LDAPSupportSettings>(TenantProvider.CurrentTenantID); if (!settings.EnableLdapAuthentication) { return(false); } try { var importer = new LDAPUserImporter(); try { ADDomain.CheckCredentials(login, password, settings.Server, settings.PortNumber); } catch (Exception) { return(false); } var sid = importer.GetSidOfCurrentUser(login, settings); if (sid == null) { return(false); } importer.GetDiscoveredGroupsByAttributes(settings); var userInfo = CoreContext.UserManager.GetUserBySid("l" + sid); if (userInfo == ASC.Core.Users.Constants.LostUser) { userInfo = CoreContext.UserManager.GetUserBySid(sid); if (userInfo == ASC.Core.Users.Constants.LostUser) { userInfo = importer.GetDiscoveredUser(settings, sid); if (userInfo == ASC.Core.Users.Constants.LostUser) { return(false); } try { SecurityContext.AuthenticateMe(ASC.Core.Configuration.Constants.CoreSystem); if (TenantStatisticsProvider.GetUsersCount() < TenantExtra.GetTenantQuota().ActiveUsers) { userInfo = UserManagerWrapper.AddUser(userInfo, UserManagerWrapper.GeneratePassword(), true, false); } else { userInfo = UserManagerWrapper.AddUser(userInfo, UserManagerWrapper.GeneratePassword(), true, false, true); } importer.AddUserIntoGroups(userInfo, settings); importer.AddUserInCacheGroups(userInfo); } finally { SecurityContext.Logout(); } } } else { if (importer.GetDiscoveredUser(settings, sid) == ASC.Core.Users.Constants.LostUser) { return(false); } userInfo.Sid = sid; try { SecurityContext.AuthenticateMe(ASC.Core.Configuration.Constants.CoreSystem); if (TenantStatisticsProvider.GetUsersCount() < TenantExtra.GetTenantQuota().ActiveUsers) { userInfo = UserManagerWrapper.AddUser(userInfo, UserManagerWrapper.GeneratePassword(), true, false); } else { userInfo = UserManagerWrapper.AddUser(userInfo, UserManagerWrapper.GeneratePassword(), true, false, true); } } finally { SecurityContext.Logout(); } } var cookiesKey = SecurityContext.AuthenticateMe(userInfo.ID); CookiesManager.SetCookies(CookiesType.AuthKey, cookiesKey); return(true); } catch (Exception e) { ADDomain.LogError(e.Message); return(false); } }