/// <summary>
/// Save the local ADUser data to the remote server
/// </summary>
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: "******"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)
{
}
}
}
}