/// <summary> /// Saves the user. /// </summary> /// <returns>The user. Must have DN set</returns> /// <param name="user">User.</param> public int SaveUser(User user) { var qMgmt = LdapQueryManager.Instance; var modList = new List <LdapModification>(); var atributes = GetAttributeSet(user); //Get user from the Directory try { var duser = GetUser(user.DN); var dattrs = GetAttributeSet(duser); foreach (LdapAttribute attr in atributes) { //TODO: Threat the userAccountControl if ( attr.Name != "cn" && attr.Name != "objectclass" && attr.Name != "userAccountControl" ) { var b1 = attr.ByteValue; if (dattrs.GetAttribute(attr.Name) != null) { var b2 = dattrs.GetAttribute(attr.Name).ByteValue; var equal = ByteTools.Equality(b1, b2); if (!equal) { modList.Add(new LdapModification(LdapModification.Replace, attr)); } } else { modList.Add(new LdapModification(LdapModification.Replace, attr)); } } } try { if (modList.Count > 0) { qMgmt.SaveEntry(user.DN, modList.ToArray()); } return(0); } catch (Exception ex) { logger.Error("Error updating user"); logger.Log(LogLevel.Error, ex); return(-1); } }catch (Exception ex) { logger.Error("Error user not found"); logger.Log(LogLevel.Error, ex); return(-1); } }
/// <summary> /// Saves the group. /// </summary> /// <returns>The group. Must have DN set</returns> /// <param name="group">Group.</param> /// <param name="_listCN">If true the members will only contain the CN</param> public int SaveGroup(Group group) { var qMgmt = LdapQueryManager.Instance; var modList = new List <LdapModification>(); var atributes = GetAttributeSet(group); //Get user from the Directory try { var dgroup = GetGroup(group.DN); var dattrs = GetAttributeSet(dgroup); bool members_clean = false; foreach (LdapAttribute attr in atributes) { if ( attr.Name != "cn" && attr.Name != "objectclass" && attr.Name != "member" ) { var b1 = attr.ByteValue; var attribute = dattrs.GetAttribute(attr.Name); bool equal = true; if (attribute != null) { var b2 = attribute.ByteValue; equal = ByteTools.Equality(b1, b2); } if (!equal) { modList.Add(new LdapModification(LdapModification.Replace, attr)); } } else { if (attr.Name == "member") { if (!members_clean) { var dattr = dattrs.GetAttribute("member"); modList.Add(new LdapModification(LdapModification.Delete, dattr)); members_clean = true; } modList.Add(new LdapModification(LdapModification.Add, attr)); } } } try { qMgmt.SaveEntry(group.DN, modList.ToArray()); return(0); } catch (Exception ex) { logger.Error("Error updating group"); logger.Log(LogLevel.Error, ex); return(-1); } } catch (Exception ex) { logger.Error("Error group not found"); logger.Log(LogLevel.Error, ex); return(-1); } }
/// <summary> /// Saves the OU. /// </summary> /// <returns>The OU. Must have DN set</returns> /// <param name="ou">OU.</param> public int SaveOU(OU ou) { var qMgmt = LdapQueryManager.Instance; var modList = new List <LdapModification>(); var atributes = GetAttributeSet(ou); //Get user from the Directory try { var dou = GetOU(ou.DN); var dattrs = GetAttributeSet(dou); foreach (LdapAttribute attr in atributes) { if ( attr.Name != "ou" && attr.Name != "objectclass" ) { var b1 = attr.ByteValue; var attribute = dattrs.GetAttribute(attr.Name); bool equal = true; if (attribute != null) { var b2 = attribute.ByteValue; equal = ByteTools.Equality(b1, b2); } if (!equal) { modList.Add(new LdapModification(LdapModification.Replace, attr)); } } } try { qMgmt.SaveEntry(ou.DN, modList.ToArray()); return(0); } catch (Exception ex) { logger.Error("Error updating OU={DN}", ou.DN); logger.Log(LogLevel.Error, ex); return(-1); } } catch (Exception ex) { logger.Error("Error group not found"); logger.Log(LogLevel.Error, ex); return(-1); } }