public bool DeleteOu(Guid transactionid, AdminInfo admin, OuInfo ou, out ErrorCodeInfo error) { bool bResult = true; error = new ErrorCodeInfo(); string strError = string.Empty; string paramstr = string.Empty; paramstr += $"userID:{admin.UserID}"; paramstr += $"||UserAccount:{admin.UserAccount}"; paramstr += $"||Id:{ou.id}"; DirectoryEntry OuEntry = new DirectoryEntry(); try { do { CommonProvider commonProvider = new CommonProvider(); if (!commonProvider.GetADEntryByGuid(ou.id, out OuEntry, out strError)) { error.Code = ErrorCode.SearchADDataError; bResult = false; break; } // OuEntry.Parent.Children.Remove(OuEntry); OuEntry.DeleteTree(); OuEntry.CommitChanges(); OuEntry.Close(); } while (false); } catch (Exception ex) { LoggerHelper.Error("OuProvider调用DeleteOU异常", paramstr, ex.ToString(), transactionid); error.Code = ErrorCode.Exception; bResult = false; } finally { if (OuEntry != null) { OuEntry.Close(); } } return(bResult); }
public bool ModifyOu(Guid transactionid, AdminInfo admin, ref OuInfo ou, out ErrorCodeInfo error) { bool bResult = true; error = new ErrorCodeInfo(); string strError = string.Empty; string paramstr = string.Empty; paramstr += $"userID:{admin.UserID}"; paramstr += $"||UserAccount:{admin.UserAccount}"; paramstr += $"||Name:{ou.name}"; paramstr += $"||Description:{ou.description}"; paramstr += $"||Id:{ou.id}"; DirectoryEntry OuEntry = new DirectoryEntry(); try { do { CommonProvider commonProvider = new CommonProvider(); if (!commonProvider.GetADEntryByGuid(ou.id, out OuEntry, out strError)) { error.Code = ErrorCode.SearchADDataError; bResult = false; break; } OuEntry.Rename(string.Format("OU = {0}", ou.name)); if (string.IsNullOrEmpty(ou.description.Trim())) { OuEntry.Properties["description"].Clear(); } else { OuEntry.Properties["description"].Value = ou.description.Trim(); } OuEntry.Properties["st"].Value = ou.IsProfessionalGroups.ToString(); OuEntry.CommitChanges(); ou.distinguishedName = Convert.ToString(OuEntry.Properties["distinguishedName"].Value); OuEntry.Close(); } while (false); } catch (Exception ex) { LoggerHelper.Error("OuProvider调用ModifyOu异常", paramstr, ex.ToString(), transactionid); error.Code = ErrorCode.Exception; bResult = false; } finally { if (OuEntry != null) { OuEntry.Close(); } } return(bResult); }
public bool AddRecycleOu(Guid transactionid, AdminInfo admin, OuInfo ou, out ErrorCodeInfo error) { bool result = false; error = new ErrorCodeInfo(); string message = string.Empty; string paramstr = string.Empty; paramstr += $"userID:{admin.UserID}"; paramstr += $"||UserAccount:{admin.UserAccount}"; paramstr += $"||Id:{ou.id}"; paramstr += $"||distinguishedName:{ou.distinguishedName}"; DirectoryEntry ouParentEntry = new DirectoryEntry(); DirectoryEntry ouRecycleEntry = new DirectoryEntry(); DirectoryEntry OuEntry = new DirectoryEntry(); try { do { CommonProvider commonProvider = new CommonProvider(); if (!commonProvider.GetADEntryByGuid(ou.parentid, out ouParentEntry, out message)) { error.Code = ErrorCode.SearchADDataError; LoggerHelper.Error("OuManager调用AddRecycleOu异常", paramstr, message, transactionid); result = false; break; } string recycleoupath = ConfigADProvider.GetADRecycleOuLdapByLdap(Convert.ToString(ouParentEntry.Properties["distinguishedName"].Value)); if (!commonProvider.GetADEntryByPath(recycleoupath, out ouRecycleEntry, out message)) { result = true; break; } DirectoryEntry newOuEntry = new DirectoryEntry(); if (commonProvider.GetOneLevelSigleOuEntry(ouRecycleEntry.Path, ou.name, out newOuEntry, out message)) { result = true; break; } OuEntry = ouRecycleEntry.Children.Add(string.Format("OU = {0}", ou.name), "organizationalUnit"); OuEntry.Properties["name"].Value = ou.name; ouRecycleEntry.CommitChanges(); ouRecycleEntry.Close(); OuEntry.CommitChanges(); OuEntry.Close(); result = true; } while (false); } catch (Exception ex) { error.Code = ErrorCode.Exception; LoggerHelper.Error("OuManager调用AddRecycleOu异常", paramstr, ex.ToString(), transactionid); result = false; } finally { if (ouParentEntry != null) { ouParentEntry.Close(); } if (ouRecycleEntry != null) { ouRecycleEntry.Close(); } if (OuEntry != null) { OuEntry.Close(); } } return(result); }