private UserInfo SyncLDAPUser(UserInfo ldapUserInfo, List <UserInfo> ldapUsers, out LdapChangeCollection changes, bool onlyGetChanges = false)
        {
            UserInfo result;

            changes = new LdapChangeCollection();

            UserInfo userToUpdate;

            var userBySid = CoreContext.UserManager.GetUserBySid(ldapUserInfo.Sid);

            if (Equals(userBySid, Constants.LostUser))
            {
                var userByEmail = CoreContext.UserManager.GetUserByEmail(ldapUserInfo.Email);

                if (Equals(userByEmail, Constants.LostUser))
                {
                    if (ldapUserInfo.Status != EmployeeStatus.Active)
                    {
                        if (onlyGetChanges)
                        {
                            changes.SetSkipUserChange(ldapUserInfo);
                        }

                        _log.DebugFormat("SyncUserLDAP(SID: {0}, Username: '******') ADD failed: Status is {2}",
                                         ldapUserInfo.Sid, ldapUserInfo.UserName,
                                         Enum.GetName(typeof(EmployeeStatus), ldapUserInfo.Status));

                        return(Constants.LostUser);
                    }

                    if (!TryAddLDAPUser(ldapUserInfo, onlyGetChanges, out result))
                    {
                        if (onlyGetChanges)
                        {
                            changes.SetSkipUserChange(ldapUserInfo);
                        }

                        return(Constants.LostUser);
                    }

                    if (onlyGetChanges)
                    {
                        changes.SetAddUserChange(result, _log);
                    }

                    return(result);
                }

                if (userByEmail.IsLDAP())
                {
                    if (ldapUsers == null || ldapUsers.Any(u => u.Sid.Equals(userByEmail.Sid)))
                    {
                        if (onlyGetChanges)
                        {
                            changes.SetSkipUserChange(ldapUserInfo);
                        }

                        _log.DebugFormat(
                            "SyncUserLDAP(SID: {0}, Username: '******') ADD failed: Another ldap user with email '{2}' already exists",
                            ldapUserInfo.Sid, ldapUserInfo.UserName, ldapUserInfo.Email);

                        return(Constants.LostUser);
                    }
                }

                userToUpdate = userByEmail;
            }
            else
            {
                userToUpdate = userBySid;
            }

            UpdateLdapUserContacts(ldapUserInfo, userToUpdate.Contacts);

            if (!NeedUpdateUser(userToUpdate, ldapUserInfo))
            {
                if (onlyGetChanges)
                {
                    changes.SetNoneUserChange(ldapUserInfo);
                }

                return(userBySid);
            }

            if (!TryUpdateUserWithLDAPInfo(userToUpdate, ldapUserInfo, onlyGetChanges, out result))
            {
                if (onlyGetChanges)
                {
                    changes.SetSkipUserChange(ldapUserInfo);
                }

                return(Constants.LostUser);
            }

            if (onlyGetChanges)
            {
                changes.SetUpdateUserChange(ldapUserInfo, result, _log);
            }

            return(result);
        }
        private UserInfo SyncLDAPUser(UserInfo ldapUserInfo, List <UserInfo> ldapUsers, out LdapChangeCollection changes, bool onlyGetChanges = false)
        {
            UserInfo result;

            changes = new LdapChangeCollection();

            UserInfo userToUpdate;

            var userBySid = CoreContext.UserManager.GetUserBySid(ldapUserInfo.Sid);

            if (Equals(userBySid, Constants.LostUser))
            {
                var userByEmail = CoreContext.UserManager.GetUserByEmail(ldapUserInfo.Email);

                if (Equals(userByEmail, Constants.LostUser))
                {
                    if (ldapUserInfo.Status != EmployeeStatus.Active)
                    {
                        if (onlyGetChanges)
                        {
                            changes.SetSkipUserChange(ldapUserInfo);
                        }

                        _log.DebugFormat("SyncUserLDAP(SID: {0}, Username: '******') ADD failed: Status is {2}",
                                         ldapUserInfo.Sid, ldapUserInfo.UserName,
                                         Enum.GetName(typeof(EmployeeStatus), ldapUserInfo.Status));

                        return(Constants.LostUser);
                    }

                    if (!TryAddLDAPUser(ldapUserInfo, onlyGetChanges, out result))
                    {
                        if (onlyGetChanges)
                        {
                            changes.SetSkipUserChange(ldapUserInfo);
                        }

                        return(Constants.LostUser);
                    }

                    if (onlyGetChanges)
                    {
                        changes.SetAddUserChange(result, _log);
                    }

                    if (!onlyGetChanges && LdapSettings.Load().SendWelcomeEmail&&
                        (ldapUserInfo.ActivationStatus != EmployeeActivationStatus.AutoGenerated))
                    {
                        var client = LdapNotifyHelper.StudioNotifyClient;

                        var confirmLink = CommonLinkUtility.GetConfirmationUrl(ldapUserInfo.Email, ConfirmType.EmailActivation);

                        client.SendNoticeToAsync(
                            NotifyConstants.ActionLdapActivation,
                            null,
                            new[] { new DirectRecipient(ldapUserInfo.Email, null, new[] { ldapUserInfo.Email }, false) },
                            new[] { ASC.Core.Configuration.Constants.NotifyEMailSenderSysName },
                            null,
                            new TagValue(NotifyConstants.TagUserName, ldapUserInfo.DisplayUserName()),
                            new TagValue(NotifyConstants.TagUserEmail, ldapUserInfo.Email),
                            new TagValue(NotifyConstants.TagMyStaffLink, CommonLinkUtility.GetFullAbsolutePath(CommonLinkUtility.GetMyStaff())),
                            NotifyConstants.TagGreenButton(Resource.NotifyButtonJoin, confirmLink),
                            new TagValue(NotifyCommonTags.WithoutUnsubscribe, true));
                    }

                    return(result);
                }

                if (userByEmail.IsLDAP())
                {
                    if (ldapUsers == null || ldapUsers.Any(u => u.Sid.Equals(userByEmail.Sid)))
                    {
                        if (onlyGetChanges)
                        {
                            changes.SetSkipUserChange(ldapUserInfo);
                        }

                        _log.DebugFormat(
                            "SyncUserLDAP(SID: {0}, Username: '******') ADD failed: Another ldap user with email '{2}' already exists",
                            ldapUserInfo.Sid, ldapUserInfo.UserName, ldapUserInfo.Email);

                        return(Constants.LostUser);
                    }
                }

                userToUpdate = userByEmail;
            }
            else
            {
                userToUpdate = userBySid;
            }

            UpdateLdapUserContacts(ldapUserInfo, userToUpdate.Contacts);

            if (!NeedUpdateUser(userToUpdate, ldapUserInfo))
            {
                _log.DebugFormat("SyncUserLDAP(SID: {0}, Username: '******') No need to update, skipping", ldapUserInfo.Sid, ldapUserInfo.UserName);
                if (onlyGetChanges)
                {
                    changes.SetNoneUserChange(ldapUserInfo);
                }

                return(userBySid);
            }

            _log.DebugFormat("SyncUserLDAP(SID: {0}, Username: '******') Userinfo is outdated, updating", ldapUserInfo.Sid, ldapUserInfo.UserName);
            if (!TryUpdateUserWithLDAPInfo(userToUpdate, ldapUserInfo, onlyGetChanges, out result))
            {
                if (onlyGetChanges)
                {
                    changes.SetSkipUserChange(ldapUserInfo);
                }

                return(Constants.LostUser);
            }

            if (onlyGetChanges)
            {
                changes.SetUpdateUserChange(ldapUserInfo, result, _log);
            }

            return(result);
        }