public List <ADDepartment> GetDepList() { List <ADDepartment> list = new List <ADDepartment>(); SQLGetDataList bll = new SQLGetDataList(); try { var dataList = bll.GetBsonDocumentDataList(connString, "select * from OADep where [IsActive] = 1"); foreach (var item in dataList) { ADDepartment dep = new ADDepartment(); dep.DepId = item.Int("DepID"); dep.Name = item.Text("DepName"); dep.Guid = item.Text("DepID"); dep.Code = item.Text("DepCode"); dep.ParentGuid = item.Text("ParentDepID"); dep.Level = item.Int("DepLevel"); list.Add(dep); } } catch (Exception ex) { _log.Error(ex.Message); } return(list); }
public List <ADDepartment> GetDepListFilter(List <ADDepartment> depList, ADDepartment dep) { if (dep.Code.Contains("OU=用户,DC=suning,DC=com,DC=cn")) { depList.Add(dep); } return(depList); }
public List <ADDepartment> GetDepListFilter(List <ADDepartment> depList, ADDepartment dep) { if (dep.Path.ToLower().Contains("cn=org,") && !string.IsNullOrEmpty(dep.Name) && !dep.Path.ToLower().Contains(",dc=hoding,") && !dep.Path.ToLower().Contains("cn=org,dc=happyvalley")) { depList.Add(dep); } return(depList); }
/// <summary> /// 组织架构同步 /// </summary> /// <param name="adList"></param> /// <param name="tree"></param> /// <returns></returns> public InvokeResult OrganizationSave(List <ADDepartment> adList, ADDepartment tree) { InvokeResult result = new InvokeResult(); try { #region 除已经删除的组织架构 var oldOrgList = _dataOp.FindAll("Organization").Where(t => t.Int("state", 0) == 0).ToList(); var delOrgQuery = (from i in oldOrgList where !(from d in adList select d.Guid).Contains(i.Text("guid")) select i).Distinct().ToList(); //删除岗位 var orgIds = delOrgQuery.Select(t => t.Text("orgId")).Distinct().ToList(); var delOrgPost = _dataOp.FindAll("OrgPost").Where(t => orgIds.Contains(t.Text("orgId"))).Distinct().ToList(); //删除用户岗位关联 var postIds = delOrgPost.Select(t => t.Text("postId")).Distinct().ToList(); var delUserOrgPost = _dataOp.FindAll("UserOrgPost").Where(t => postIds.Contains(t.Text("postId"))).Distinct().ToList(); var userOrgPostIds = delUserOrgPost.Select(t => t.Text("relId")).Distinct().ToList(); BsonDocument doc = new BsonDocument(); doc.Add("state", 1); result = _dataOp.BatchUpdateByPrimaryKey("Organization", orgIds, doc); //逻辑删除 if (result.Status == Status.Failed) { Console.WriteLine("Organization 逻辑删除失败:" + result.Message); } result = _dataOp.BatchDeleteByPrimaryKey("OrgPost", postIds); //result = _dataOp.BatchDeleteByPrimaryKey("OrgPost", postIds); if (result.Status == Status.Failed) { Console.WriteLine("OrgPost 删除失败:" + result.Message); } result = _dataOp.BatchDeleteByPrimaryKey("UserOrgPost", userOrgPostIds); if (result.Status == Status.Failed) { Console.WriteLine("UserOrgPost 删除失败:" + result.Message); } OrgTreeInsert(tree, null); #endregion } catch (Exception ex) { Console.WriteLine(ex.Message); } return(result); }
//public int Count(ADDepartment dep) //{ // int count = 0; // count += dep.SubUsers.Count(); // foreach (var item in dep.SubDepartemnt) // { // count += Count(item); // } // return count; //} /// <summary> /// 从组织架构树中取出用户列表 /// </summary> /// <param name="dep"></param> /// <returns></returns> public List <ADUser> UserListFilter(ADDepartment dep) { List <ADUser> userList = new List <ADUser>(); foreach (var item in dep.SubDepartemnt) { userList.AddRange(UserListFilter(item)); } userList.AddRange(dep.SubUsers); return(userList); }
/// <summary> /// 华侨城同步 /// </summary> public void ImportToDBHQC() { OrganizationAD org = new OrganizationAD(); Dictionary <string, string> dicDep = new Dictionary <string, string>(); ADsyn = new ADSynchronization("", "uid=a3admin,cn=users,dc=group,dc=chinaoct,dc=com", "a3admin"); dicDep.Add("ou", "Name"); dicDep.Add("adspath", "Path"); dicDep.Add("searchguide", "Guid"); var list = ADsyn.SearchDepartment("172.16.9.25", "(objectclass=organizationalUnit)", dicDep, "Yinhe.ProcessingCenter.SynAD.PathAnalyseHQC"); ADDepartment deproot = new ADDepartment() { Name = "华侨城组织架构", Path = "LDAP://172.16.9.26/cn=chinaoct,cn=groups,dc=group,dc=chinaoct,DC=COM", Code = "LDAP://172.16.9.26/cn=华侨城组织架构,cn=groups,dc=group,dc=chinaoct,DC=COM", Level = 0, Guid = "0000000000000000000", ParentName = "" }; list.Add(deproot); Console.WriteLine(list.Count()); Yinhe.ProcessingCenter.SerializerXml <List <ADDepartment> > ser = new Yinhe.ProcessingCenter.SerializerXml <List <ADDepartment> >(list); ser.BuildXml("dep.xml"); Dictionary <string, string> dicDep1 = new Dictionary <string, string>(); dicDep1.Add("sn", "Name"); dicDep1.Add("adspath", "Path"); dicDep1.Add("uid", "LoginName"); dicDep1.Add("mail", "EmailAddr"); dicDep1.Add("seealso", "Guid"); // dicDep1.Add("o", "DepartMentID"); dicDep1.Add("o", "Code"); var list1 = ADsyn.SearchUser("172.16.9.25", "(!(objectclass=container))", dicDep1, "Yinhe.ProcessingCenter.SynAD.PathAnalyseHQC"); Console.WriteLine(list1.Count()); Yinhe.ProcessingCenter.SerializerXml <List <ADUser> > ser1 = new Yinhe.ProcessingCenter.SerializerXml <List <ADUser> >(list1); ser1.BuildXml("user.xml"); // list.Where(t => t.Name == "用户").FirstOrDefault().Name = "苏宁组织架构"; ADDepartment curChild = this.GetADTreeHQC(list, list1, deproot); _log.Info(string.Format("获取数据 部门数:{0} 用户数 :{1}", list.Count(), list1.Count())); Yinhe.ProcessingCenter.SerializerXml <ADDepartment> ser2 = new Yinhe.ProcessingCenter.SerializerXml <ADDepartment>(curChild); ser2.BuildXml("tree.xml"); if (list.Count() > 5) { _log.Info("开始同步"); org.OrganizationSave(list, curChild); org.UserInsert(this.UserListFilter(curChild), 1, 0); } _log.Info("同步结束"); }
public ADDepartment QXGetADTree(ADDepartment pNode, List <ADDepartment> AllDepList, List <ADUser> AllUserList) { ADDepartment node = pNode; node.SubDepartemnt = new List <ADDepartment>(); node.SubUsers = new List <ADUser>(); node.SubUsers.AddRange(AllUserList.Where(t => t.Code == node.Code).ToList()); foreach (var item in AllDepList.Where(t => t.ParentGuid == pNode.Code).ToList()) { node.SubDepartemnt.Add(QXGetADTree(item, AllDepList, AllUserList)); } return(node); }
/// <summary> /// 苏宁 /// </summary> public void ImportToDBSN() { OrganizationAD org = new OrganizationAD(); Dictionary <string, string> dicDep = new Dictionary <string, string>(); ADsyn = new ADSynchronization("", "*****@*****.**", "ghr9fg_(&*3Nnl0A"); dicDep.Add("name", "Name"); dicDep.Add("adspath", "Path"); var list = ADsyn.SearchDepartment("10.0.0.1", "(objectCategory=CN=Organizational-Unit,CN=Schema,CN=Configuration,DC=suning,DC=com,DC=cn)", dicDep, "Yinhe.ProcessingCenter.SynAD.PathAnalyseSN"); Console.WriteLine(list.Count()); Yinhe.ProcessingCenter.SerializerXml <List <ADDepartment> > ser = new Yinhe.ProcessingCenter.SerializerXml <List <ADDepartment> >(list); ser.BuildXml("dep.xml"); Dictionary <string, string> dicDep1 = new Dictionary <string, string>(); dicDep1.Add("name", "Name"); dicDep1.Add("adspath", "Path"); dicDep1.Add("samaccountname", "LoginName"); dicDep1.Add("mail", "EmailAddr"); dicDep1.Add("mobile", "MobieNumber"); dicDep1.Add("telephonenumber", "PhoneNumber"); var list1 = ADsyn.SearchUser("10.0.0.1", "(objectCategory=CN=Person,CN=Schema,CN=Configuration,DC=suning,DC=com,DC=cn)", dicDep1, "Yinhe.ProcessingCenter.SynAD.PathAnalyseSN"); Console.WriteLine(list1.Count()); Yinhe.ProcessingCenter.SerializerXml <List <ADUser> > ser1 = new Yinhe.ProcessingCenter.SerializerXml <List <ADUser> >(list1); ser1.BuildXml("user.xml"); // list.Where(t => t.Name == "用户").FirstOrDefault().Name = "苏宁组织架构"; ADDepartment curChild = this.GetADTree(list, list1, list.Where(t => t.Name == "用户").FirstOrDefault()); Yinhe.ProcessingCenter.SerializerXml <ADDepartment> ser2 = new Yinhe.ProcessingCenter.SerializerXml <ADDepartment>(curChild); ser2.BuildXml("tree.xml"); if (list.Count() > 5) { org.OrganizationSave(list, curChild); org.UserInsert(this.UserListFilter(curChild), 2, 0); } }
public void ImportToDBZHHY() { OrganizationAD org = new OrganizationAD(); PathAnalyseZHHY analyse = new PathAnalyseZHHY(); var depList = analyse.GetDepList(); var userList = analyse.GetUserList(); ADDepartment curChild = this.GetADTreeByGuidMultDepZHHY(depList, userList, depList.Where(t => t.ParentGuid == "0").FirstOrDefault()); //顶部 Yinhe.ProcessingCenter.SerializerXml <List <ADDepartment> > ser = new Yinhe.ProcessingCenter.SerializerXml <List <ADDepartment> >(depList); ser.BuildXml("dep.xml"); Yinhe.ProcessingCenter.SerializerXml <List <ADUser> > ser1 = new Yinhe.ProcessingCenter.SerializerXml <List <ADUser> >(userList); ser1.BuildXml("user.xml"); Yinhe.ProcessingCenter.SerializerXml <ADDepartment> ser2 = new Yinhe.ProcessingCenter.SerializerXml <ADDepartment>(curChild); ser2.BuildXml("tree.xml"); if (depList.Count() > 5) { org.OrganizationSave(depList, curChild); org.UserInsertByDepType(userList, 1, 0, 1, 0); } }
/// <summary> /// 获取AD部门,用户树 /// </summary> /// <param name="depList"></param> /// <param name="userList"></param> /// <param name="curDep"></param> /// <returns></returns> public ADDepartment GetADTreeHQC(List <ADDepartment> depList, List <ADUser> userList, ADDepartment curDep) { List <ADDepartment> childs = new List <ADDepartment>(); if (!string.IsNullOrEmpty(this.HadPathList.Where(s => s.Equals(curDep.Path) == true).FirstOrDefault())) { Console.WriteLine("已经遍历过:" + curDep.Path); } else { HadPathList.Add(curDep.Path); childs = depList.Where(d => d.ParentName.Trim() == curDep.Name && d.GrandParentName.Trim() == curDep.ParentName).ToList(); } List <ADUser> childUsers = userList.Where(u => u.Code == curDep.Guid).ToList(); if (childUsers.Count() > 0) { curDep.SubUsers.AddRange(childUsers); foreach (var item in curDep.SubUsers) { item.DepartMentGuid = curDep.Guid; } } foreach (ADDepartment child in childs) { if (!string.IsNullOrEmpty(this.HadPathList.Where(s => s.Equals(child.Path) == true).FirstOrDefault())) { Console.WriteLine("已经遍历过:" + child.Path); } else { ADDepartment curChild = this.GetADTreeHQC(depList, userList, child); curChild.ParentGuid = child.Guid; curDep.SubDepartemnt.Add(curChild); } } return(curDep); }
/// <summary> /// 中海投资 /// </summary> public void ImportToDBZHTZ() { OrganizationAD org = new OrganizationAD(); Dictionary <string, string> dicDep = new Dictionary <string, string>(); ADsyn = new ADSynchronization("", "coihl\\coihl_sysuser", "Cohl888"); //连接数据源 dicDep.Add("name", "Name"); dicDep.Add("adspath", "Path"); //dicDep.Add("objectguid", "Guid"); var list = ADsyn.SearchDepartment("192.0.0.172", "(objectCategory=CN=Organizational-Unit,CN=Schema,CN=Configuration,DC=cohl,DC=com)", dicDep, "Yinhe.ProcessingCenter.SynAD.PathAnalyseZHTZ");//根据Fileter查找树的子节点(部门) Console.WriteLine(list.Count()); Yinhe.ProcessingCenter.SerializerXml <List <ADDepartment> > ser = new Yinhe.ProcessingCenter.SerializerXml <List <ADDepartment> >(list); ser.BuildXml("dep.xml"); Dictionary <string, string> dicDep1 = new Dictionary <string, string>(); dicDep1.Add("name", "Name"); dicDep1.Add("adspath", "Path"); dicDep1.Add("samaccountname", "LoginName"); dicDep1.Add("mail", "EmailAddr"); //dicDep1.Add("objectguid", "Guid"); var list1 = ADsyn.SearchUser("192.0.0.172", "(objectCategory=CN=Person,CN=Schema,CN=Configuration,DC=cohl,DC=com)", dicDep1, "Yinhe.ProcessingCenter.SynAD.PathAnalyseZHTZ"); Console.WriteLine(list1.Count()); Yinhe.ProcessingCenter.SerializerXml <List <ADUser> > ser1 = new Yinhe.ProcessingCenter.SerializerXml <List <ADUser> >(list1); ser1.BuildXml("user.xml"); ADDepartment curChild = this.GetADTree(list, list1, list.Where(t => t.Name == "中海投資").FirstOrDefault()); //顶部 Yinhe.ProcessingCenter.SerializerXml <ADDepartment> ser2 = new Yinhe.ProcessingCenter.SerializerXml <ADDepartment>(curChild); ser2.BuildXml("tree.xml"); if (list.Count() > 5) { org.OrganizationSave(list, curChild); org.UserInsert(this.UserListFilter(curChild), 2, 0); } }
/// <summary> /// 旭辉 /// </summary> public void ImportToDBXH() { OrganizationAD org = new OrganizationAD(); Dictionary <string, string> dicDep = new Dictionary <string, string>(); ADsyn = new ADSynchronization("", "administrator", "*03ci33fiit#"); dicDep.Add("name", "Name"); dicDep.Add("adspath", "Path"); var list = ADsyn.SearchDepartment("192.168.8.33", "(objectCategory=CN=Organizational-Unit,CN=Schema,CN=Configuration,DC=cifi,DC=com,DC=cn)", dicDep, "Yinhe.ProcessingCenter.SynAD.PathAnalyseXH"); Console.WriteLine(list.Count()); Yinhe.ProcessingCenter.SerializerXml <List <ADDepartment> > ser = new Yinhe.ProcessingCenter.SerializerXml <List <ADDepartment> >(list); ser.BuildXml("dep.xml"); Dictionary <string, string> dicDep1 = new Dictionary <string, string>(); dicDep1.Add("name", "Name"); dicDep1.Add("adspath", "Path"); dicDep1.Add("samaccountname", "LoginName"); dicDep1.Add("userprincipalname", "EmailAddr"); var list1 = ADsyn.SearchUser("192.168.8.33", "(objectCategory=CN=Person,CN=Schema,CN=Configuration,DC=cifi,DC=com,DC=cn)", dicDep1, "Yinhe.ProcessingCenter.SynAD.PathAnalyseXH"); Console.WriteLine(list1.Count()); Yinhe.ProcessingCenter.SerializerXml <List <ADUser> > ser1 = new Yinhe.ProcessingCenter.SerializerXml <List <ADUser> >(list1); ser1.BuildXml("user.xml"); ADDepartment curChild = this.GetADTree(list, list1, list.Where(t => t.Name.Contains("旭辉")).FirstOrDefault()); Yinhe.ProcessingCenter.SerializerXml <ADDepartment> ser2 = new Yinhe.ProcessingCenter.SerializerXml <ADDepartment>(curChild); ser2.BuildXml("tree.xml"); if (list.Count() > 5) { org.OrganizationSave(list, curChild); org.UserInsert(list1, 2, 0); } //Console.WriteLine(Count(curChild)); }
public List <ADDepartment> GetDepListFilter(List <ADDepartment> depList, ADDepartment dep) { depList.Add(dep); return(depList); }
/// <summary> /// 组织架构同步 /// </summary> /// <param name="node"></param> /// <param name="pnode"></param> /// <returns></returns> public List <ADDepartment> OrgTreeInsert(ADDepartment node, ADDepartment pnode) { InvokeResult result = new InvokeResult(); List <ADDepartment> nodeList = new List <ADDepartment>(); ADDepartment retNode = new ADDepartment(); ADDepartment subnode = new ADDepartment(); try { subnode = node; retNode.Name = subnode.Name; retNode.Level = subnode.Level; int pid = 0; if (pnode == null) { pid = 0; } else { pid = pnode.DepId; } var org = _dataOp.FindAll("Organization").Where(t => t.Text("guid") == subnode.Guid).FirstOrDefault(); BsonDocument structModel = new BsonDocument(); structModel.Add("name", subnode.Name); structModel.Add("nodePid", pid.ToString()); structModel.Add("guid", subnode.Guid); structModel.Add("state", "0"); structModel.Add("code", subnode.Code); Console.WriteLine("组织架构:{0}——>{1}", structModel.Text("name"), structModel.Text("guid")); if (org == null) { result = _dataOp.Insert("Organization", structModel); } else { result = _dataOp.Update("Organization", "db.Organization.distinct('_id',{'guid':'" + subnode.Guid + "'})", structModel); } if (result.Status == Status.Successful) { var orgpost = _dataOp.FindAllByKeyVal("OrgPost", "orgId", result.BsonInfo.Text("orgId")); if (orgpost.Count() == 0) { BsonDocument post = new BsonDocument(); post.Add("name", "空缺岗位"); post.Add("orgId", result.BsonInfo.Text("orgId")); post.Add("type", "0"); _dataOp.Insert("OrgPost", post); } subnode.DepId = result.BsonInfo.Int("orgId"); retNode.DepId = subnode.DepId; } nodeList.Add(retNode); // Console.WriteLine(string.Format("Level:{0} Name:{1}", subnode.Level, subnode.Name)); if (subnode.SubDepartemnt != null) { foreach (var item in subnode.SubDepartemnt) { nodeList.AddRange(OrgTreeInsert(item, subnode)); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } return(nodeList); }
/// <summary> /// 搜索部门AD /// </summary> /// <param name="path"></param> /// <param name="objFilter"></param> /// <param name="dicPropertes"></param> /// <returns></returns> public List <ADDepartment> SearchDepartment(string path, string objFilter, Dictionary <string, string> dicPropertes, string pathAnalyseClass) { List <ADDepartment> depList = new List <ADDepartment>(); string[] arrPropertes = dicPropertes.Keys.ToArray(); try { path = "LDAP://" + (String.IsNullOrEmpty(this.Domain) == false ? this.Domain : "") + path; DirectoryEntry root = null; if (authorizeType == AuthorizeType.anonymous) { root = new DirectoryEntry(path, "", "", AuthenticationTypes.Anonymous); } else if (authorizeType == AuthorizeType.none) { root = new DirectoryEntry(path, this.UserName, this.PassWord, AuthenticationTypes.None); } if (root != null) { Console.WriteLine("开始遍历AD部门" + root.Path); using (DirectorySearcher searcher = new DirectorySearcher()) { searcher.SearchRoot = root; searcher.SearchScope = SearchScope.Subtree; searcher.Filter = objFilter; searcher.PropertiesToLoad.AddRange(arrPropertes); SearchResultCollection results = searcher.FindAll(); StringBuilder summary = new StringBuilder(); foreach (SearchResult result in results) { ADDepartment dep = new ADDepartment(); foreach (string propName in result.Properties.PropertyNames) { if (dicPropertes[propName] != null) { dep.SetDynamicProperty(dicPropertes[propName].ToString(), result.Properties[propName][0].ToString()); } } IPathAnalyse analyse = null; analyse = (IPathAnalyse)Activator.CreateInstance("Yinhe.ProcessingCenter", pathAnalyseClass).Unwrap();//"Yinhe.ProcessingCenter.SynAD.PathAnalyseXH" if (analyse != null) { if (String.IsNullOrEmpty(dep.Name) == false) { dep.Code = analyse.GetDepCode(dep.Path); dep.Level = analyse.GetDepLevel(dep.Path); dep.ParentName = analyse.GetDepParentName(dep.Path); dep.GrandParentName = analyse.GetGrandParentName(dep.Path); if (pathAnalyseClass != "Yinhe.ProcessingCenter.SynAD.PathAnalyseHQC") //中海投资 { dep.Guid = result.GetDirectoryEntry().Guid.ToString(); } else { if (dep.ParentName.ToLower() == "cn=org") { dep.ParentName = "华侨城组织架构"; } if (dep.GrandParentName.ToLower() == "cn=org") { dep.GrandParentName = "华侨城组织架构"; } } } depList = analyse.GetDepListFilter(depList, dep); } Console.WriteLine(dep.Name); } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } return(depList); }
public InvokeResult QXSyn() { InvokeResult result = new InvokeResult(); try { string QXOrgUrl = SysAppConfig.QXOrgUrl; string QXUserUrl = SysAppConfig.QXUserUrl; XmlDocument depDoc = new XmlDocument(); XmlDocument userDoc = new XmlDocument(); string orgXMLPath = SysAppConfig.QXOrgXMLPath; string userXMLPath = SysAppConfig.QXUserXMLPath; Console.WriteLine(orgXMLPath); Console.WriteLine(userXMLPath); depDoc.Load(orgXMLPath); userDoc.Load(userXMLPath); var depNodeList = depDoc.SelectNodes("//entry"); var userNodeList = userDoc.SelectNodes("//entry"); List <ADDepartment> depList = new List <ADDepartment>(); List <ADUser> userList = new List <ADUser>(); foreach (var item in depNodeList) { XmlNode node = (XmlNode)item; ADDepartment dep = new ADDepartment(); var subNodes = node.ChildNodes; foreach (var s in subNodes) { XmlNode snode = (XmlNode)s; if (snode.Name == "deptID") { dep.Code = snode.InnerText; } else if (snode.Name == "deptName") { dep.Name = snode.InnerText; } else if (snode.Name == "uniqueID") { dep.Guid = snode.InnerText; } } if (!string.IsNullOrEmpty(dep.Code)) { dep.Level = dep.Code.Length / 4 + 1; if (dep.Code.Length > 4) { dep.ParentGuid = dep.Code.Substring(0, dep.Code.Length - 4); } else { dep.ParentGuid = "00"; } } depList.Add(dep); } foreach (var item in userNodeList) { XmlNode node = (XmlNode)item; ADUser user = new ADUser(); var subNodes = node.ChildNodes; foreach (var s in subNodes) { XmlNode snode = (XmlNode)s; if (snode.Name == "userID") { user.LoginName = snode.InnerText; } else if (snode.Name == "userName") { user.Name = snode.InnerText; } else if (snode.Name == "jobNO") { user.Remark = snode.InnerText; } else if (snode.Name == "deptID") { user.Code = snode.InnerText; } else if (snode.Name == "mail") { user.EmailAddr = snode.InnerText; } else if (snode.Name == "mobile") { user.MobieNumber = snode.InnerText; } } userList.Add(user); } ADDepartment root = new ADDepartment { Code = "00", Name = "侨鑫集团组织架构", Guid = "00000000" }; root = QXGetADTree(root, depList, userList); OrganizationAD org = new OrganizationAD(); if (depList.Count() > 5 && userList.Count() > 4) { org.OrganizationSave(depList, root); org.UserInsertByDepType(userList, 2, 0, 0, 1); } } catch (Exception ex) { } return(result); }