Exemple #1
0
        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);
            }
        }
        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);
            }
        }