/// <summary> /// Send all pending changes to the directory service. If there is a pending rename / re-superior, /// it will fire first. /// </summary> /// <param name="ldap"></param> public void CommitChanges(LdapConnection ldap) { CheckForDeletion(); if (this.IsDnDirty) { ModifyDNRequest req = new ModifyDNRequest(); req.DistinguishedName = this.OriginalDn; req.NewName = this.RDN; logger.Info("Request new name {0}", req.NewName); req.DeleteOldRdn = true; if (this.IsSuperiorDirty) { req.NewParentDistinguishedName = this.SuperiorDn; logger.Info("Request new superior {0}", req.NewParentDistinguishedName); } ldap.SendRequest(req); this.IsDnDirty = false; this.IsSuperiorDirty = false; this.OriginalDn = this.DistinguishedName; } if (_changes.Count > 0) { if (this.IsNewEntry) { AddRequest req = new AddRequest(this.DistinguishedName); foreach (DirectoryAttributeModification dm in this.ChangesAsDAMC()) req.Attributes.Add(new DirectoryAttribute(dm.Name, dm.GetValues(typeof(string)))); ldap.SendRequest(req); } else { ModifyRequest req = new ModifyRequest(this.DistinguishedName); foreach (DirectoryAttributeModification dm in this.ChangesAsDAMC()) req.Modifications.Add(dm); ldap.SendRequest(req); } _changes.Clear(); this.IsNewEntry = false; logger.Info("Commit on {0} complete", this.DistinguishedName); } else { logger.Info("Nothing to commit on {0}", this.DistinguishedName); if (this.IsNewEntry) throw new InvalidOperationException( "Cannot commit a new directory object with no attributes"); } }
/// <summary> /// Creates a ModifyDNRequest packet. /// </summary> /// <param name="context">The user context which contains message ID.</param> /// <param name="oldDn">The original DN to be modified.</param> /// <param name="newRdn">The new relative DN.</param> /// <param name="newParentDn"> /// The new parent DN. For LDAP v3 only. Ignored when creating LDAP v2 requests. /// </param> /// <param name="delOldRdn"> /// Whether to delete old RDN. For LDAP v3 only. Ignored when creating LDAP v2 requests. /// </param> /// <returns>The packet that contains the request.</returns> internal override AdtsModifyDnRequestPacket CreateModifyDnRequest( AdtsLdapContext context, string oldDn, string newRdn, string newParentDn, bool delOldRdn) { ModifyDNRequest modifyDnRequest = new ModifyDNRequest( new LDAPDN(oldDn ?? string.Empty), new RelativeLDAPDN(newRdn ?? string.Empty), new Asn1Boolean(delOldRdn), new LDAPDN(newParentDn)); LDAPMessage_protocolOp operation = new LDAPMessage_protocolOp(); operation.SetData(LDAPMessage_protocolOp.modifyDNRequest, modifyDnRequest); LDAPMessage message = new LDAPMessage(new MessageID(context.MessageId), operation); AdtsModifyDnRequestPacket packet = new AdtsModifyDnRequestPacket(); packet.ldapMessagev2 = message; packet.messageId = context.MessageId; return packet; }
/// <summary> /// Moves and / or renames an entry in the directory. /// </summary> /// <param name="distinguishedName">The distinguished name of the entry to move or rename.</param> /// <param name="newParentDistinguishedName">The distinguished name of the entry's new parent entry in the directory (if moving), or its current parent entry (if renaming).</param> /// <param name="newCommonName">The new common name of entry.</param> /// <returns>True if moved or renamed, false otherwise.</returns> public bool MoveRenameEntry(string distinguishedName, string newParentDistinguishedName, string newCommonName) { if (!string.IsNullOrWhiteSpace(distinguishedName) && !string.IsNullOrWhiteSpace(newParentDistinguishedName) && !string.IsNullOrWhiteSpace(newCommonName)) { // Prepend the CN= if not already included. if (!newCommonName.StartsWith("CN=")) { newCommonName = "CN=" + newCommonName; } ModifyDNRequest request = new ModifyDNRequest(distinguishedName, newParentDistinguishedName, newCommonName); try { ModifyDNResponse response = (ModifyDNResponse)connection.SendRequest(request); // Check that a response was received. if (response != null) { // A response was received. if (response.ResultCode == ResultCode.Success) { return true; } } else { // A response was not received. return false; } } catch { } } return false; }
/// <summary> /// This method shows how to modify an attribute. /// </summary> /// <param name="oldUid">Old user UID</param> /// <param name="newUid">New user UID</param> public void changeUserUid(string oldUid, string newUid) { var oldDn = string.Format("uid={0},ou=users,dc=example,dc=com", oldUid); var newDn = string.Format("uid={0},ou=users,dc=example,dc=com", newUid); DirectoryRequest request = new ModifyDNRequest(oldDn, "ou=users,dc=example,dc=com", "uid=" + newUid); connection.SendRequest(request); request = new ModifyRequest(newDn, DirectoryAttributeOperation.Replace, "uid", new string[] { newUid }); connection.SendRequest(request); }