ADLib.ADUser.Save C# (CSharp) Метод

Save() публичный Метод

Save the local ADUser data to the remote server
public Save ( ) : void
Результат void
        public void Save()
        {
            if (isSaveable)
            {
                //Set password
                if (passwordChanged)
                {
                    _sourceUser.SetPassword(_password);
                    passwordChanged = false;
                }

                if (_sourceUser.Name == null || _sourceUser.DisplayName == null)
                {
                    _sourceUser.Name = GivenName + " " + Surname;
                    _sourceUser.DisplayName = _sourceUser.Name;

                }
                Boolean wasNewUser = _isNewUser;

                try
                {
                    _sourceUser.Save();

                    //Save data to AD
                    if (_isNewUser)
                    {
                        directoryEntry = _sourceUser.GetUnderlyingObject() as DirectoryEntry;
                    }
                    _isNewUser = false;
                }
                catch (PrincipalExistsException ex)
                {
                    throw new DuplicateUser(ex.Message);
                }

                Sync_Groups();

                //Have to reload the UserPrincipal to get the DirectoryEntry
                UserPrincipal newSourceUser = _connection.GetUserBySid(_sourceUser.Sid);

                //if (wasNewUser)
                //{

                    if (newSourceUser == null)
                    {
                        string msg = "Couldn't reload user: " + _sourceUser.DisplayName;

                        throw new InvalidOperationException(msg);
                    }
                    _sourceUser = newSourceUser;
                //}

                if (directoryEntry != null)
                {
                    directoryEntry.Close();
                }

                directoryEntry = _sourceUser.GetUnderlyingObject() as DirectoryEntry;

                directoryEntry.Options.SecurityMasks = SecurityMasks.Dacl;

                //Copy local attributes to directory entry
                //directoryEntry.RefreshCache(DEFields.ToArray<string>());
                mapper.Copy(this, directoryEntry);

                if ((this.Manager != null) && (!this.Manager.DistinguishedName.Equals(directoryEntry.Properties["manager"].Value)))
                {
                    if (directoryEntry.Properties.Contains("manager"))
                    {
                        directoryEntry.Properties["manager"].Value = this.Manager.DistinguishedName;
                    }
                    else
                    {
                        directoryEntry.Properties["manager"].Add(this.Manager.DistinguishedName);
                    }
                }

                if (DisplayName != directoryEntry.Properties["cn"].Value as string)
                {
                    directoryEntry.Rename("CN=" + DisplayName);
                }

                try
                {
                    directoryEntry.CommitChanges();
                }
                catch (DirectoryServicesCOMException ex)
                {
                    System.Diagnostics.Debug.Write(ex.Message + " - " + ex.StackTrace);
                    throw new ADException(String.Format("DirectoryServicesCOMException {0}", ex.Message), ex);
                }

                if (this.HideFromAddressLists)
                {
                    try
                    {
                        if (!directoryEntry.Properties.Contains("msExchHideFromAddressLists"))
                        {
                            directoryEntry.Properties["msExchHideFromAddressLists"].Add("TRUE");
                        }
                        else
                        {
                            directoryEntry.Properties["msExchHideFromAddressLists"].Value="TRUE";
                        }

                        directoryEntry.CommitChanges();
                    }
                    catch (DirectoryServicesCOMException)
                    {
                    }
                }
            }
        }